Name of printf and scanf

Print function in C abbreviated to printf() and scan function abbreviated to scanf. Printf and scanf are most frequently used in any programs written with C language. These functions implicitly work on console stdin/stdout files. There are other variations of these functions. fprintf/fscanf operates on file buffer and sscanf/sprintf can operates on string buffer. These functions are adapted to work on different I/O buffer but they all work on the same principle.

Printf working principle

printf or print function in C takes a formatting string and couple of optional variables as input and outputs strings to console while converting input variables to strings.

Printf block diagram

Printf and scanf takes multiple arguments and these functions are called variable length arguments function or vararg function. Take printf for consideration. User supply a string and input argumnets. Printf creates an internal buffer for constructing output string. Now printf iterates through each characters of user string and copies the character to the output string. Printf only stops at "%". "%" means there is an argument to convert. Arguments are in the form of char, int, long, float, double or string. It converts it to string and appends to output buffer. If the argument is string then it does a string copy. Finally printf may reach at the end of user sting and it copies the entire buffer to the stdout file.

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
/* Note: this is a minimal printf implementation */
/* This is for building understanding only */
int print (char * str, ...)
{
10    va_list vl;
11    int i = 0, j=0;
12    char buff[100]={0}, tmp[20];
13    va_start( vl, str );
14 
15    while (str && str[i])
16    {
17      if(str[i] == '%'){
18        i++;
19        switch (str[i]) {
20          case 'c': {
21            buff[j] = (char)va_arg( vl, int );
22            j++;
23            break;
24          }
25          case 'd': {
26            itoa(va_arg( vl, int ), tmp, 10);
27            strcpy(&buff[j], tmp);
28            j += strlen(tmp);
29            break;
30          }
31          case 'x': {
32            itoa(va_arg( vl, int ), tmp, 16);
33            strcpy(&buff[j], tmp);
34            j += strlen(tmp);
35            break;
36          }
37        }
38      } else {
39        buff[j] =str[i];
40        j++;
41      }
42      i++;
43    } 
44    fwrite(buff, j, 1, stdout); 
45    va_end(vl);
46    return j;
47  }

Scanf working principle

scanf or scan function in C takes a formatting string as input and couple of optional variables as output reference arguments. It converts the scanned string to variables and copies to the output variables.

Scanf block diagram

Scanf is reverse process of printf. Scanf reads console input string. It iterates each characters of user provided string and stops at "%". Now scanf reads a line from stdin. User's input comes as a string. It converts string to char, int, long, float, double and sets the value of the pointer located at the argument. In care of string it simply copies the string to the output

/* Note: this is a minimal scanf implementation */
/* This is for building understanding only */
#include <stdio.h>
#include <stdlib.h>
#include<stdarg.h>
int scan (char * str, ...)
{
  va_list vl;
10    int i = 0, j=0, ret = 0;
11    char buff[100] = {0}, tmp[20], c;
12    char *out_loc;
13    while(!= '
14  ') {
15      if (fread(&c, 1, 1, stdin)) {
16        buff[i] = c;
17        i++;
18      }
19    }
20    va_start( vl, str );
21    i = 0;
22    while (str && str[i])
23    {
24      if (str[i] == '%') {
25        i++;
26        switch (str[i]) {
27          case 'c': {
28            *(char *)va_arg( vl, char* ) = buff[j];
29            j++;
30            ret ++;
31            break;
32          }
33          case 'd': {
34            *(int *)va_arg( vl, int* ) =strtol(&buff[j], &out_loc, 10);
35            j+=out_loc -&buff[j];
36            ret++;
37            break;
38          }
39          case 'x': {
40            *(int *)va_arg( vl, int* ) =strtol(&buff[j], &out_loc, 16);
41            j+=out_loc -&buff[j];
42            ret++;
43            break;
44          }
45        }
46      } else {
47        buff[j] =str[i];
48        j++;
49      }
50      i++;
51    }
52    va_end(vl);
53    return ret;
54 
55  }
56  int main(int argc, char *argv[])
57 
58  {
59    char c;
60    int i;
61    int h;
62    int ret = 0;
63    ret = scan("%c %d %x", &c, &i, &h);
64    print("C = %c, I = %d, H = %X, Return %d", c, i, h, ret);
65    return 0;
66  }

Output

A 10 1FF
C = A, I = 10, H = 1FF, Return 3

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

bitwise shifting, Operator precedence, operator precedence, ternary operator, fprintf, fscanf working principal, printing pointers, scanf string input, scan string with blanks, scanf string ends with newline,

# 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