Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

C Programming – Managing Input and Output Operations

By Brian Moriya | on March 30, 2006 |
C Language

Input Output operations are useful for program that interact with user, take input from the user and print messages. The standard library for input output operations used in C is stdlib. When working with input and output in C there are two important streams: standard input and standard output. Standard input or stdin is a data stream for taking input from devices such as the keyboard. Standard output or stdout is a data stream for sending output to a device such as a monitor console.

To use input and output functionality in your program, you need to include stdio header:

  1.  #include <stdio.h>

Single Character Input and Output

getc is used to accept a character from standard input and is declared as follows:

  1. int getc(FILE *stream);

You would pass stdin for the stream to get input from the command line. getchar also has the same definition as getc.  It is equivalent to getc with stdin as its argument.

  1. getc(stdin);

fgetc function is recommended for accepting a single character from standard input and is declared as follows:

  1. int fgetc(FILE *stream);

If the call to fgetc is successful it returns char, otherwise it will return EOF (End of File).

putc is used for sending a single character to standard output and is defined as follows:

  1. int putc(int c, FILE *stream);

fputc function is recommended instead of putc. fputc syntax is shown here below:

  1. int fputc(int c, FILE *stream);

Below example demonstrates fgetc and fputc functions.

When fputc is successful, it will return the value, if it fails it will return EOF.

getachar.c:

  1.  #include <stdio.h>
  2. void main() {
  3. char ip; 
  4. //prompt for character and get input. 
  5. printf("Type a character and press enter.n");
  6. ip = fgetc(stdin); 
  7. //write input value back out to standard out. 
  8. fputc(ip, stdout);
  9. printf("n");
  10. }

Here is the output I got when typing “a” for my character:


String Input and Output

You need not consider constructing for loops to get and print more than one character at a time. You can make use of similar functions for getting whole strings from standard input and printing them to standard output. They are fgets and fputs respectively.

Prototypes:

  1. int fgets(char *restrict s, int n, FILE *restrict stream); 
  2.  
  3. int fputs(char *restrict s, FILE *restrict stream);

The first argument you pass to fgets is a character array for putting the string value taken from the file stream. The n value is used as a limiter for the size (in bytes) of input accepted into the argument s. fgets will read from the file stream until it detects an EOF, a press of “Enter”, or n – 1 bytes have been read. It returns the value of s or a NULL pointer.

fputs is almost the same, besides that it does not require you to pass a number of bytes that will be accepted. It takes the character array you want to print out, and the stream you want to print it to.

Let us modify our example to read and print a string instead of just a character.

getastring.c:

  1.  #include <stdio.h> 
  2. void main() {
  3. int n = 40;
  4. char ip[n]; 
  5. //prompt for a sentence and get input. 
  6. printf("Type a sentence and press enter.n");
  7. fgets(ip, n, stdin); 
  8. //write input value back out to standard out. 
  9. fputs(ip, stdout);
  10. }

In the above program fgetc and fputc function calls are replaced with fgets and fputs. We also added an integer value for bytes to be read. Now the program will accept a sentence instead of just a single character.



Formatted Input with Scanf

The prototype of the scanf function is shown below:

  1. int scanf(const char *restrict format, ...);

scanf takes a variable number of arguments from standard input. You can get values for more than one variables with a single call to this function. All you have to do is specify the type of value you want with the conversion specifier operator (%) and the specifier and/or size.

Conversion specifiers may be of the following forms: %s, %ns, where s stands for specifier and n is the size.

For example:

%3d would accept the first 3 unsigned decimal numbers it encountered while reading the stream. The whole call to scanf could look something like this:

  1. scanf(%3d, anum);

If you type 1823 in a prompt where scanf was accepting %3d, it would assign only the 182 part to anum, the 3 would simply be discarded.

Let us do a quick example.

scanf.c:

  1.  #include <stdio.h> 
  2.  
  3. void main() { 
  4. int anum; 
  5. //prompt for character and get input. 
  6. printf("Type a series of numbers and press enter.n");
  7. scanf("%3d", &anum); 
  8. //write input value back out to standard out. 
  9. printf("%dn", anum);
  10. }

Note that you must pass your variable/s by reference because it needs to be able to modify them.

Expected output for the above program is showing below:


Input Specifications for Real Number

Use the %f specifier to accept floating point values. Ironically enough, the %d specifier will not accept decimal places. In order to accept a real number you would need to make a scanf call as follows:

  1. scanf("%f", &anum);

Assuming that anum has been declared as a float.

You could now take 2.98 from standard input and it would be stored as 2.98, rather than 2 if you had used the %d specifier.

Input Specifications for a Character

Accepting character input from standard input to the scanf function works just like it did with integers. You can take a single character, or a specified number of them.

Let us modify our example program scanf.c to accept characters instead of integers. If we change scanf call for accepting 3 integers to 3 characters all we have to do is change the specifier. Of course we also have to change anum to a character array, but you get the idea.

  1. scanf(%3c, anum);

Printing One Line

You can print a single line to the standard output as shown below: 

  1. printf("Type a series of numbers and press enter.n");

Note the newline added at the end of the string. That is so the cursor will move down to the next line. Still technically one line though.

Conversion Strings and Specifiers

The usage of printf is almost identical to the usage of scanf we have just talked about. Take a look at the prototype:

  1. int printf(const char *restrict format, ...);

Printf takes specifiers and a variable number of arguments. You can enter a whole list of arguments and specifiers, just make sure they line up so you do not put the wrong value in the wrong variable.

You can also insert newline, as you may have seen throughout the tutorials thus far.

Let us make a call to printf that will print that same value:

  1. scanf("%f", &anum);
  2. printf("%f", anum);

The above program is similar to the call to scanf to read a real number, the difference being that we did not have to pass anum by reference to printf.

Specifier Meaning

There are more specifiers than we have shown up until now.

  • %d, %i: signed integer values
  • %o: unsigned octal
  • %s: character array
  • %c: unsigned character
  • %f, %F: float
  • %ld: long
  • %x: lowercase hexadecimal
  • %X: uppercase hexadecimal
  • %e: exponential form for a double
  • %p: pointer
  • %a: unsigned integer
  • « « C Programming – Expressions
    Working with CSS Units, Colors and References » »

Author Description

Avatar

Free Training

RSSSubscribe 394 Followers
  • Popular
  • Recent
  • C Programming – Functions (Part-II)

    May 22, 2006 - 0 Comment
  • C Programming – Data Types : Part 2

    August 21, 2011 - 0 Comment
  • C Programming – An Overview

    March 2, 2006 - 0 Comment
  • C Programming – Functions (Part-I)

    May 23, 2006 - 0 Comment
  • C Doubly Linked Lists

    June 26, 2011 - 0 Comment
  • C Programming – Constants and Identifiers

    March 2, 2006 - 0 Comment
  • C Programming – Structures and Unions

    May 25, 2006 - 0 Comment
  • C Programming – Data Types : Part 1

    March 7, 2006 - 0 Comment
  • C Programming – Pointers

    May 25, 2006 - 0 Comment
  • C Circular Linked Lists

    June 26, 2011 - 0 Comment
  • C Programming – Data Types : Part 2

    August 21, 2011 - 0 Comment
  • C Circular Linked Lists

    June 26, 2011 - 0 Comment
  • C Doubly Linked Lists

    June 26, 2011 - 0 Comment
  • TSR in C – An Introduction

    July 11, 2006 - 0 Comment
  • Concept of Pixel in C Graphics

    July 11, 2006 - 0 Comment
  • Call by Value and Call by Reference

    July 6, 2006 - 0 Comment
  • C Language – The Preprocessor

    May 31, 2006 - 0 Comment
  • C Programming – File management in C

    May 31, 2006 - 0 Comment
  • C Programming – Linked Lists

    May 29, 2006 - 0 Comment
  • C Programming – Dynamic Memory allocation

    May 29, 2006 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • C Programming – Data Types : Part 2
  • C Circular Linked Lists
  • C Doubly Linked Lists
  • TSR in C – An Introduction
  • Concept of Pixel in C Graphics

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2023. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.AcceptReject Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT