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 and scanf functions are based on Variadic functions. Variadic functions can take a variable number of arguments.
To understand printf or scanf we need to first understand how Variadic functions work.
Variadic functions should have at least one argument and after that "..." should be added to indicate that this function takes variable arguments. Header file
Variadic types & macros
Variadic function example
Variadic functions output
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 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.
Let us implement our own printf function. This is only for the understanding purpose. We name it print(). It has one string argument (str) and rest are variable arguments. Variable arguments are managed by macros like va_start, va_arg and va_end. A temporary buffer (buff) is there to construct the output buffer. A while loop is needed to scan each characters in the input string. Now we iterate character by character in the loop and copy each character to output string. Same time we check for "%". "%" is not copied to output string. Once we found it, we check the next character. This is the formatting character. Formatting character says how to format the argument to visible output string. Printf supports varieties of formatting. C is for character, d for decimal integer, f for floating point, x for hexadecimal and s for strings. We match the formatting and pick the argument variable using va_arg(). Argument variable is then converted to string format and appends to the output string. character can be copied as it is and Itoa function is used for integer to string conversion.
C itoa() function has been used to convert argument integer to string. Integer to String conversion is a process to take each digits and convert those to ASCII format. Visit our topic integer to string conversionfor further understanding.
This process of coping characters and conversion of arguments repeats until the string is terminated to last NULL character. For simplicity we have implemented only c, d, x formatting cases. Now at the end we have the output string ready. This is now passed to fwrite() to stdout. Thus the output string prints in the actual console. Printf then returns the number of characters which is printed in the console and exit the function.
A 10 144 3E8 Hello from printf! printf return 32
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 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
Let us implement our own scan function. This is only for the understanding purpose. We name it scan(). It has one string argument (str) and rest are variable arguments. Variable arguments are managed by macros like va_start, va_arg and va_end. A temporary buffer (buff) is there to copy buffer from stdin. In a while loop we read characters from stdin and and copy to buff. Loop terminates when user presses return key. Now we have another while loop to scan the buffer. We iterate character by character and we check for "%". Once we found it, we check the next character. This is the formatting character. Formatting character says how to argument is formatted in the buffer. Scanf supports varieties of formatting. C is for character, d for decimal integer, f for floating point, x for hexadecimal and s for strings. We pick the argument variable using va_arg(). Argument variable is then converted from string to desired type. character can be copied as it is and strtol() function is used for string to integer conversion.
C strtol() function has been used to convert argument string to integer. String to integer conversion is a process to take each ASCII characters and convert those to integer. Visit our topic string to integer conversionfor further understanding.
This process of taking each character and convert formatted string to integers repeats until the string is terminated to last NULL character. For simplicity we have implemented only c, d, x formatting cases. Now at the end we have the output string ready. Scanf then returns the number of output argument is successfully converted and exits.
Enter char int hex oct string A 10 100 1000 Hello from scanf! C = A, I = 10, H = 256, O = 512, S = Hello from scanf! returns 5
About our authors: Team EQA
You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.
Learn on Youtube