3.3. Scanf() and printf() functions

 

The int scanf(const char *format, …) function reads the input from the standard input stream stdin and scans that input according to the format provided.

The int printf(const char *format, …) function writes the output to the standard output stream stdout and produces the output according to the format provided.

The format can be a simple constant string, but you can specify %s, %d, %c, %f, etc., to print or read strings, integer, character or float respectively. There are many other formatting options available which can be used based on requirements.

Example

#include <stdio.h>
int main( ) 
{
   char str[100];
   int i;
   printf( "Enter a value :");
   scanf("%s %d", str, &i);/* &i is a pointer to variable i like str is a pointer to array  str*/
   printf( "\nYou entered: %s %d ", str, i);
   return 0;
}

Note, that while reading a string, scanf() stops reading as soon as it encounters a space!!!

Usage of printf and scanf: %conversion character (d,i,u,o,x,…)

%d, %i int, decimal number
%u int, unsigned decimal number
%o int, octal number (unsigned), (without leading zero)
%x or %X int, hexadecimal number (unsigned), (without leading 0x or 0X)
%c int, single character
%s  char *, print characters from the string until a ‘/0’ or the number of characters given by the precision
%f  double; [-]m.dddddd, where the number of d’s is given by precision (default 6)
%e, %E  double; [-]m.dddddd e+/-xx or  [-]m.dddddd e+/-XX, where the number of d’s is given by precision (default 6)
%g, %G  double, use %e or %E, if the exponent is less than -4 or greater than or equal than the precision; otherwise use %f.
 %p  void *p; pointer
 [-]  specifies the left adjustment of the argument
 n  specifies the minimum field width
 .n  number of digits after a decimal point or maximum number of characters to be printed from a string or minimum number of digits for integer

An h if the integer is to be printed as a short and l if as a long (example %ld)

A width or precision may be specified as *, in which case value is computed by converting the next argument (must be int). For example, to print at most max characters from a string s:  printf(“%.*s”, max, s);

For example: printf(“The result is:  %-10.2f”, result); (left of the field, 10 characters with 2 decimals)

For example: scanf(“%d %s %d”, &day, monthname, &year); where monthname is an array and thus no & is needed, since an array name is already a pointer. The arguments to scanf must be pointers (like &day).

In the beginning of this lesson we copied input to output. Next we can make a program which count the characterics which the user enters.

#include <stdio.h>
/*Counting characters*/
main()
{
printf( “Enter a value and press Enter:\n);
long number =0;
while(getchar() !=  ‘\n’)
++number;
printf(“%ld charateristics\n”, number);
}

Then one useful program more, a program which counts how many words the user have entered.

#include <stdio.h>
/*Counting words and characters*/
main()
#define IN 1 /*inside a word*/
#define OUT 0 /*outside a word*/
{
printf( “Enter a value and press Enter:\n”);
int number =0, words=0, input, state;
state = OUT;
while((input =getchar()) != ‘\n’)
{
++number;
if (input == ‘ ‘ || input == ‘\t’)
state = OUT;
else if (state == OUT)
{
state = IN;
++words;
}
}
printf(“%d charateristics and %d words\n”, number, words);
}

NOTE! This chapter does not include exercises but this information will be tested in quizzes 3-4! It is very important that the student will make all examples shown in this chapter.