C - Printf and Scanf


Printf :

The printf function is used to output(display) any combination of numeric values, single characters and strings. It is similar to the input function scanf, except that its purpose is to display data rather than to read data.

Example:

						
#include <stdio.h>

void main(){
	int a,b,sum;
	a=5;
	b=10;
	sum= a + b ;
	printf("sum of a and b is = %d ", sum); // sum is 15
}

									

The output of the given program will be:


sum of a and b is = 15								

									

Scanf :

In general, the printf function is written as,
printf(control string, arg1, arg2,...,argn)

where control string refers to a string containing certain required formatting information and arg1, arg2, …,argn are arguments that represent the individual input data items.

The arguments can be written as constants, single variables or array names or more complex expressions. Function calls may also be included. The arguments need not be preceded by ampersands(&).

Note : The putchar function is used to output(display) single characters on the monitor while the printf function is used to output(display) any combination of numerical values, single characters and strings.

Control String within the printf function: :

  • The control string is used to convey certain formatting information which is required as part of printf function.
  • The control string consists of characters groups , one group for each output data items.
  • Each character's group must begin with a percent sign (%) . In its simplest form, a single characters group will consists of the percent sign, followed by a conversion characters also known as format specifier which indicates the type of the corresponding data item. Consider the example “%d %c %f” where d,c,f are conversion characters and each is preceded by % sign.
  • Within the control string, multiple characters groups can be contiguous or they can be separated by other characters including white spaces characters(i.e. blank spaces, tabs or newline characters). If other characters are used to separate multiple characters groups in the control string, then these “other” characters are directly transferred to the output device where they are displayed. This Feature allows us to include messages within the output, if we with.
  • Thus the use of “Other” characters within the control string of printf does not introduce any complications as it does in case of scanf.

Scanf :

The Scanf function can be used to read anything, that is, a numeric value or a single character or even a string.

Example :

						
#include <stdio.h>

main(){
	int a,b,sum;
	printf("Enter value of a and b\n");  
	scanf("%d%d\n",&a,&b); // This will read the user input
	sum=a+b;
	printf("sum is = %d",sum);
}

									

The output of the given program will be:


Enter value of a and b
5 
10
sum is = 15							

									

In general, the scanf function is written as
scanf(control string, arg1, arg2, …,argn)
where control string refers to a string containing certain required formatting information and arg1, arg2,...,argn are arguments that present the individual input data items. Actually the arguments represent the addresses of the data items within the computer’s memory.

The arguments are written as variables or arrays, whose types match the corresponding characters groups by an ampersand(&). However, array names should not begin with an ampersand(&).

Note : The getchar function is used to read ONLY single characters while the scanf function can even read numerical values and strings.

Control string in a scanf function :

  • The control string is used to convey certain formatting information which is required as part of scanf function. The control string consists of character groups, one group for each input data item.
  • Each character group must begin with a percent sign(%). In its simplest form, a single character group will consists of the percent sign, followed by a conversion character also known as format specifier which indicated the type of the corresponding data item.
    Consider the example “ %d %c %f ” where d,c,f are format specifiers and each is preceded by % sign.
  • Within the control string, multiple characters groups can be contiguous or they can be separated by whitespace characters (i.e. blank spaces, tabs or newline characters). If whitespaces characters are used to separate multiple characters groups in the control string, then all consecutive whitespace characters in the input data will be read but ignored.

  • Print Page