Using the printf() function

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.

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:

Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top