17.1 – Formatted Console I/O – Part-1

by subbu on October 6, 2014

Getting data into the program is called input and sending the information out from the program is called output. We can develop only a few applications at system level without need of input and output.

Input Output handling is not the part of original C specification developed by Dennis M. Ritchie. All the input and output related functions are later added by compiler developers.

Most of the compiler developers provide the common I/O functions with their implementations. Say for example same printf() function is used to print onto the console output device (monitor) but Turbo C has their way of printf() code to print on to the monitor and gcc has their way of printf() code to print on to the monitor.

All the I/O functions are classified into

  1. Console I/O
  2. File I/O
  3. Network I/O

Input and output in c

Console I/O

First let us discourse console I/O. Here the input device keyboard is called console input device and the output device monitor is called console output device. There are different I/O functions used to perform console I/O operations. These are basically classified into

  1. Formatted functions
  2. Unformatted functions

Formatted Console I/O functions are printf(), scanf(), sprintf() and sscanf() functions used to print and read formatted data of almost any type. Here the meaning of formatted is according to the requirement.

printf() function

printf() in C language

Here the printf() function is used to print the formatted data on to the monitor in a required format. It takes format string, list of operands as arguments and returns an integer

Note: Here in “printf()”, the meaning of “print” is to print and “f” is formatted

The format string tells the printf() that how to print the data on to the screen. The format specifiers and escape sequences help to set the format of the output. As we have already learned about format specifiers and escape sequences, we are not discoursing here. Better refer the following sessions to learn format string

4.2 – Formatting Output using printf()
4.3- Escape Sequences or Back Slash Characters

Here we need to consider the return type of printf(). The printf() returns how many bytes of data does the printf() printed on to the console. It returns -1 if any problem arises while printing.

#include<stdio.h>
int main()
{
 int x;
 x=printf("Hello world"); /* printing 11 bytes of data */
 printf("\nPrinted %d Bytes of data\n",x);
 return 0;
}

Output
Hello world
Printed 11 Bytes of data

#include<stdio.h>
int main()
{
 short a=10,x;
 x=printf("%d",a);   /* printing 2 bytes of data */
 printf("\nPrinted %d Bytes of data\n",x);
 return 0;
}

Output
10
Printed 2 Bytes of data

#include<stdio.h>
int main()
{
 short x;
 x=printf("");         /* printing nothing   */
 printf("\nPrinted %d Bytes of data\n",x);
 return 0;
}

Output
Printed 0 Bytes of data

#include<stdio.h>
int main()
{
 char s[]="igate solutions";
 short x;
 x=printf("%s",s);   /* printing 15 Bytes of data */
 printf("\nPrinted %d Bytes of data\n",x);
 return 0;
}

Output
igate solutions
Printed 15 Bytes of data

#include<stdio.h>
int main()
{
 char *s="igate solutions";
 short x;
 x=printf("%s",s); /* printing 15 bytes of data */
 printf("\nPrinted %d Bytes of data\n",x);
 return 0;
}

Output
igate solutions
Printed 15 Bytes of data

#include<stdio.h>
int main()
{
 if(printf("Hello World"))
     return 0;
}

Output
Hello World

Here printf() returns 11, if(11) would be true

scanf() function

scanf() in C language

It iss a formatted input function used to accept almost any type of data from the console input device keyboard and store into specified variable (identifier)

It takes format string, list of addresses of identifiers as arguments and returns an integer

format string has a list of format specifiers to specify the order and the types of identifiers follow to read and store. You can refer 4.1-Formatting Input through scanf() to learn more about scanf() format string

Here we need to consider the return type of scanf() function. The scanf() returns an integer that is the number of variables into which data is read and stored. It returns -1 if any thing goes wrong while reading data.

#include<stdio.h>
int main()
{
 short a,b,c;
 int x;
 printf("Enter three integers:\n");
 x=scanf("%d%d%d",&a,&b,&c); /* reading into 3 variables */
 printf("The given values are %d, %d and %d\n",a,b,c);
 printf("Data read into %d variables\n",x);
 return 0;
}

Execution
Enter three integers:
34     76     89
The given values are 34, 76 and 89
Data read into 3 variables

#include<stdio.h>
int main()
{
 char *s[20];
 int x;
 printf("Enter the string:");
 x=scanf("%s",s); /* reading into a single identifier */
 printf("String is %s\n",s);
 printf("Data read into %d identifiers\n",x);
 return 0;
}

Execution
Enter the string:hello
String is hello
Data read into 1 identifiers

#include<stdio.h>
int main()
{
 short a,b;
 int x;
 x=scanf("%2d%3d",&a,&b); /* reading into two variables */
 printf("Accepted data %d and %d\n",a,b);
 printf("Data read into %d identifiers\n",x);
 return 0;
}

Execution
12543
Accepted data 12 and 543
Data read into 2 identifiers

Though we have accepted a single number, According to the format specifier “%2d%3d” the first two digits are stored into “a” and second three digits are stored into “b”. As scanf() stored the data into two variables, it returned 2.

Previous post:

Next post: