Basics of the C Printf Function

C Tutorials from Roots - Part 22

Chrys Forcha
Introduction
This is part 22 of my series, C Tutorials from Roots. In this part of the series, we look at the basics of the C printf function. This tutorial assembles and explains all what we have seen with the printf function.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Including the Header File
For you to use the printf function, you must include the stdio.h header file.

One Argument
If your printf function will take only one argument, then it has to be a string (character) pointer. Read and try the following code:

#include

int main()
{
char *strPtr = "A test.";

printf(strPtr);

return 0;
}

You can also print a string directly without the pointer identifier, like in the following code:

#include

int main()
{
printf("A test.");

return 0;
}

The double quotes returns a pointer and the print function gets the value from the pointer. This is a property of the printf function, to obtain the value (text) pointed to by a string pointer from the pointer.

To print the other object types, you need two arguments. The first argument indicates the type of value and the second has the identifier for the value. The first argument is in double quotes and begins with %.

Printing an Int
Read and try the following code:

#include

int main()
{
int myInt = 25;

printf("%i", myInt);

return 0;
}

The i after % indicates that the next argument is an int value.

Printing a Float
Read and try the following code:

#include

int main()
{
float myFloat = 3.6;

printf("%f", myFloat);

return 0;
}

The f after % indicates that the next argument is a float value. We shall look at the problem of the extra decimal points later.

Printing a char
Read and try the following code:

#include

int main()
{
char myChar = 'B';

printf("%c", myChar);

return 0;
}

The c after % indicates that the next argument is a char value.

Printing Value of Pointed Object
Read and try the following code:

#include

int main()
{
int *intPtr;
*intPtr = 47;
printf("%i", *intPtr);

return 0;
}

The object pointed to has an int. In the printf function, the i indicates that we want an int. For the second argument we precede the pointer identifier with the dereference operator. When a pointer identifier is preceded with this operator, the value of the pointed object is returned. You print values of float type pointers in a similar way; in the printf function, replace the i with f.

There is a lot more to the printf function, but we shall not go into that in this series. Today, input and output of data is done with windows. So after this series, you need to learn what is called, Windows Programming Interface. I will have that in my blog. With that you will be able to display input and output in windows.

Time to take a break. We continue in the next part of the series.

Chrys

Published by Chrys Forcha

I have more than 10 years experience in computer programming, software, electronics and telecommunications. I have a First Degree in Electronics and a Master's Degree in Technical Education. As well a...  View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.