Formatted Mode console

C library has two library functions scanf() and printf() for doing all formatted console input output works. scanf is to read formatted input and printf is to write formatted output.

Formatted output printf

printf takes one fixed argument and can take variable additional arguments. First argument is the format string. Additional arguments depends of formatting string. User has to provide the elements in the same sequence as the format string has been given. printf scans the formatting string and replaces all the %<element> with the text equivalent of the arguments given. This buffer then goes for writing to the output console device. It returns the number of bytes written to the console.

Syntax

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

Formatted inputs scanf

scanf takes one fixed argument and can take variable additional arguments. First argument is the format string. Additional arguments depends of formatting string. User has to provide the elements in the same sequence as the format string has been given. scanf reads console input file and scans input buffer as per formatting string and whenever it finds %<element> it converts the element and stores in the address given in the additional arguments. User has to provide the reference of the variables in the arguments in order to receive type objects from file. It returns the number of successful arguments converted successfully.

Syntax

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

Formatted Mode example

int main(int argc, char *argv[])
{
  char first_name[50];
  char last_name[50];
  int std;
  int roll;
  char grade;
  float score;
  
10    printf ("Entering data for student database\n");
11    /* Taking all formatted inputs */
12    printf ("First name : ");
13    scanf ("%s", first_name);
14    printf ("Last name : ");
15    scanf ("%s", last_name);
16    printf ("Class/Std [1-12]: ");
17    scanf ("%d", &std);
18    printf ("Enrollment number : ");
19    scanf ("%d", &roll);
20    printf ("Score : ");
21    scanf ("%f", &score);
22    printf ("Grade [A-D] : ");
23    fflush(stdin);
24    scanf ("%c", &grade);
25 
26    /* Displaying all formatted outputs */  
27    
28    printf ("First name : %s\n", first_name);
29    printf ("Last name : %s\n", first_name);
30    printf ("Class/Std : %d\n", std);
31    printf ("Roll number : %d\n", roll);
32    printf ("Score : %.2f %c\n", score, '%');
33    printf ("Grade : %c\n", grade);
34  }

Output

Entering data for student database
First name : John
Last name : Carter
Class/Std [1-12]: 1
Enrollment number : 2
Score : 85.6
Grade [A-D] : A
First name  : John
Last name   : John
Class/Std   : 1
Roll number : 2
Score       : 85.60
Grade       : A

You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

fprintf, fscanf, fflush, poll, select, fcntl, fputs,fgets,fgetc,fputc, printf, scanf, ioctl, mmap, DOS low level IO access,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan