Geekpedia Programming Tutorials






Using the printf() function

A guide to the famous printf() function in C. Shows you how to output data in several ways. The tutorial is not yet complete but will be continued soon.

On Saturday, April 23rd 2005 at 10:48 AM
By Andrew Pociu (View Profile)
*****   (Rated 4.5 with 15 votes)
Contextual Ads
More C Resources
Advertisement
One of the most popular functions in C is the printf() function.

If you don't know what printf() does, compile the following code in a C file:





#include




void
main(void)

{

   printf("Hello World");

}



...and you will get:





Hello World



It's the easiest way to ouput information to the console. But printf() doesn't know just that, let's see what else printf() is capable of doing.

Outputting the value of a variable


We have a variable named myNum that stores the number 69. We want to output the value of this variable using the printf() function:





#include



void
main(void)

{

   int MyNum = 69;

   printf("Outputting number %d", MyNum);

}



Because our variable is of type int we use %d to output its value where we want in the string. The result is:





Outputting number 69



With printf() you can also output the number in a hexadecimal form:





#include



void
main(void)

{

   int MyNum = 169;

   printf("Number %d in hexadecimal format is %x", MyNum, MyNum);

}



The result being:





Number 169 in hexadecimal format is a9



Instead of %x you can use %X to get the result in uppercase (A9).



To get the number in octal format use %o as in the following example:





#include



void
main(void)

{

   int MyNum = 169;

   printf("Number %d in octal format is %o", MyNum, MyNum);

}



And with the result:





Number 169 in octal format is 251




Moving on, let's see how we can output the character represented by an ASCII value. For example the ASCII value 65 outputted as a character gives the letter A. 66 is B, 67 is C and so on.





#include



void
main(void)

{

   int MyChar = 65;

   printf("Outputting ASCII value 65: %c", MyChar);

}







Outputting ASCII value 65: A



However, using %c you can at any time pass the character itself, not just the ASCII equivalent. You simply need to enclose it in single quotes ( ' ) and store it in a char type of variable:





#include



void
main(void)

{

   char MyChar = 'A';

   printf("Outputting character A: %c", MyChar);

}



The output being:





Outputting character A: A



If you're trying to output more than one character you will need to enclose it in double quotes ( " ) and use %s (string) instead of %c (char):





#include



void
main(void)

{

   printf("Geek%s", "pedia");

}



Or if you store the string pedia in a char variable:





#include



void
main(void)

{

   char MyString[50] = "pedia";

   printf("Geek%s", MyString);

}



No matter if you use a variable or pass the string directly, the result is the same:





Geekpedia



printf() is even capable of showing the address of a variable, thus showing the pointer of that variable. This can be seen in the following example:





#include



void
main(void)

{

   int MyVar = 69;

   printf("The address of MyVar is %p", MyVar);

}



In my case, the result is:





The address of MyVar is 0045
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by Salah on Thursday, May 12th 2005 at 03:10 PM

Thank you for your effort

by Newton on Sunday, August 6th 2006 at 08:45 AM

wow thanks ...Wanted the ASCII one for a long time

by quynt on Monday, April 30th 2007 at 06:52 AM

su dung ham printf

by jagaveer on Friday, June 29th 2007 at 03:13 AM

hi

its very useful for beginners.plz send more than like this

Thanks

by nraju on Thursday, April 3rd 2008 at 02:38 AM

I am a beginner. I found it was very useful.

by ioriliao on Friday, June 13th 2008 at 08:42 PM

Thank you for your effort

by ioriliao on Friday, June 13th 2008 at 08:43 PM

Thank you for your effort


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs C Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Latest Tech Bargains

Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons