printf
Introduction
printf is used to display information on screen, that is, standard output. printf is a function that returns the number of characters printed by the printf function.
Program
#includemain() { int i = 0; i=printf("abcde"); // A printf("total characters printed %d\n",i); //B }
Explanation
-
Here, five characters are printed by statement A. So, i will get the value 5.
-
Statement B prints the value of i as 5.
-
The general format for the printf statement has a first string argument followed by any additional arguments.
-
In statement B, "total characters printed %d\n" is the first string argument.
-
i is the second argument. You may have multiple arguments, but that depends on what value you have to print. For each additional argument you will have to include a placeholder. Each placeholder begins with %. In statement B, %d is the placeholder.
-
For the second argument i, the placeholder is %d. So when you need an integer value, you have to use %d. The placeholders are given for each data type.
-
For example, if you want to print i and j, you may have to use two placeholders. Any material in the first string argument, other than the placeholder and characters, represents the escape sequence. In this example, the escape sequence character is \n, which is not printed but acts as a directive. For example, the \n directive indicates that the next printing should be done on a new line.
Points to Remember
-
printf is used to direct output to standard output format.
-
printf is a function that returns the number of characters printed.
No comments:
Post a Comment