|
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.2 with 17 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:
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:
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:
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 It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
|
Rate this tutorial
Current Comments
|
Related Tutorials
There are no related tutorials.
Related Source Code
There is no related source code.
C Job Search
|
|
|