All About printf()

Ah, printf(). Don't we love thee, the basic output. No, not really but we have to deal with it because it's what we have to work with. It's not as picky as it's cousin, scanf(), but it still isn't a walk in the park. Let's break it down.

First, printf() gives you nothing out into your program; it returns 'null' or 'void'. Hurrah! One less thing to worry about going wrong! Next, the function takes several things. The first is the string. This string tells what the function will put on that little command prompt that you use to display results. In this string you can have commands that replace that command with a variable, supplied later. The list of these commands are as follows [with some extras that aren't very common]:

%d integer
%i integer
%f floating point number
%lf double floating point number
%c character

Another command that I use is \n, which is the 'newline' character sequence, which says to the function "Please put a new line break here, thank you". In order for the %_ commands to work, after the string you have to put the variable that you want to go there. For example:

printf("This number (%d) should be 12", 12);

would result in:

This number (12) should be 12.

In case it wasn't clear, it works as long as the %_ command has the right type of number after it, be it a varible [like x, foo or z] or a constant [like PI for float or 3 for int]. To put multiple numbers into the function, just separate with commas. Example:

printf("Count! %d %d %d", 1, 2, 3);

would be...

Count! 1 2 3

That was 'All About printf()' day on Doelltech, ask for clarification, or just go with it! See you next time on our edition of 'C Programming Unleashed'. Wow I'm tired.