Hello everyone. I am back after some time with tutorials again and hopefully would continue with momentum.
printf is something every one encounters as soon as they see their first “hello world” program in c
It is a very common and fundamental, as well as a flexible and powerful function.
So I thought of assembling together all info i.e. starting from the basics to the less known and advanced territories of this function.
We will try to assume minimum basic knowledge such as you know what data types are i.e. int, char, float, double, string (string is not a data type itself but represented by a null terminated character array) etc. but nothing beyond that.
Also, check out our tutorials on scanf and sscanf!
So printf is mainly used when you want to print anything to screen / console. The infamous hello world program is below for your reference.
The most primitive things are that it is a function whose code is located under header file stdio.h. However, this is often the library that compilers include by default. This is the reason that you often are able to use it even without including stdio.h.
The first aspect is “whatever you pass to printf() in double quotes is printed as it is on screen” with some exceptions of course such as ‘\n’ which simply tells us to print a new line. The character ‘\’ is called an escape sequence and is NOT treated as any normal character to print. We will get back to it in more detail shortly.
A few quick examples:
printf(“how \t are you? \n A backslash \\”);
\t prints a tab (4 spaces generally), \\ prints a single normal back slash because the second \ tells that it is nothing but an actual slash itself.
char *s = “string constant\n”; printf(s); // prints ‘string constant’ because s points to the same string
Now as a format string, one can embedded any type of variables in a printf string i.e. characters, integers, floats, doubles, or other strings themselves. The key to do this is ‘%’ character. Anywhere % is seen in a printf string, it means this is no ordinary character, but it is followed by a variable of some data type. The data type is indicated by c for char, d for signed integer, s for string etc. Note that this is still present inside the double quotes i.e. “” (e.g. %d, %c, %s). However, logically speaking we also need to specify which variable to print whose value will be replacing the %<type>. So the double quotes are followed by a comma, and then contain the name of variable. If there are more, then each of these names is separate by commas. Note that there should be as many variables after the double quotes as there are %<type> in the quotes themselves. If this is not the case, you may not get an error but the result is undefined. Let’s see more examples
char a = ‘k’ printf(“Hi character %c\n”, a); // prints Hi character k int integer = 10; printf(“%d\n”, integer); // prints 10 followed by a newline. A string can only contain a %<type> too double db = 12.003; printf(“%f\n”, db); // prints 12.003000 – Double and float variables both use %f and by default print six places after the decimal.
However, if you like to specify how many places to print after the decimal, you can do that as follows
printf(“%[b].2[/b]f\n”, db); // prints 12.00
Inserting a .<number of digits you want to print> between % and f will do the job. Note that this does a rounding off too i.e. if number was 12.005 (or greater) it would print 12.01 but 12.00 if less as in above example.
long lg=100; unsigned int un = 20; printf(“%l\n”, lg); // prints 100 printf(“%u\n”, un) // prints 20
These prefixes can be combined where it makes sense such as
unsigned long var = 100000; printf(“%lu\n”, var); // prints 100000 which is an unsigned long variable
In case of strings
char s = “I am string”; char *t = “another type of string”; printf(“%s %s\n”, str, t);
The above prints both strings (first followed by the second) on a single line.
Similarly, hexadecimal integer
int a = 10; // 0xA in hex printf(“%x %X\n”, a, a); // prints a and A – Note that small %x prints small case hex and vice versa.
To print a single % on screen, just use two %% together.
Also, %e is used for scientific notation of a float or double. Moreover using %g would mean use either of %f or %e which ever appears shorter in printing. %o is used for octal.
You can also specify the width of particular field
printf(“M\n”, a); // prints 10 but with two spaces in the beginning to complete field width of 4.
If ‘a’ contains a 3 digit value then there would only be a single space in the beginning. However, this does not truncate values. So if a=10000, it would still print the 5 digit value. This can be helpful when you are printing a table of values and you set a maximum width so all numeric values are pretty printed right aligned.
This also applies to strings.
printf(“0s\n”, s); // prints the string s in a field 30 characters wide right aligned.
If for some reason you need to override the right justification (which is default) you can precede with a ‘-’ sign
printf(“%-30s\n”, s); // prints the string s in a field 30 characters wide but left aligned padded by spaces.
That is so far with the basics and most common uses.
Click here for Part 2 of this tutorial.
Edited by Roger, 19 February 2013 - 02:37 PM.