printf()&scanf()

Console I/O Operations:

The printf() Function: This function is most versatile way to display output on screen. When using printf(), you
need to include the standard input/output header file, STDIO.H.

int printf(format-string [,arguments,…]);

Format string can have: Literaltext, conversionspecifier, escapesequences.
The format-string is required; however, arguments are optional.
For each argument, there must be a conversion specifier.

We can provide optional specifier as field width.
For Ex.

  • printf(“%10d”,456) .It means print the variable as decimal integer in the field of 10 columns.
  • printf(“%10.2f “,405.3405)here 10 is for columns and 2 is for digits displayed after decimal point
Specifiers Type Converted
%c char
%d int,short
%ld Long
%f float
%s char arrays
%u unsigned int, unsigned short
%lu unsigned long

scanf() Function

scanf(“format string”, list of addresses of variables);

  • The scanf() function reads data from the keyboard according to a specified format and assigns the input data to one or more program variables.
  • Like printf(), scanf() uses a format string to describe the format of the input. The format string utilizes the same conversion specifiers as the printf() function.
  • A single scanf() can input more than one value if you include multiple conversion specifiers in the format string and variable names (again, each preceded by & in the argument list).
  • The & symbol is C’s address-of operator
  • The following statement inputs an integer value and a floating-point value and assigns them to the variables x and rate, respectively:
    scanf(“%d %f”, &x, &rate);

  • When multiple variables are entered, scanf() uses white space to separate input into fields. White space can be spaces, tabs, or new lines.
  • Each conversion specifier in the scanf() format string is matched with an input field; the end of each input field is identified by white space.
  • The reading will be terminated, when scanf encounters an invalid mismatch of data or character that is not valid for value being read.
  • An unread data item in a line will be considered as part of the data input to the next scanf call.












  • null

    sandeep soni

    Founder, Trainer & CEO, Deccansoft Software Services.

    Sandeep has 21 yrs of experience working in various Microsoft Technologies/Platforms incl. VB6.0, ASP, VC++, VB.NET, C#. He is involved in managing and architecting projects at Deccansoft. He will be your liaison to Deccansoft, for any kind of communication and project updates. He knows what works and what doesn’t, and what practices are most suitable for design and programming with the ultimate goal of producing a quality system.