Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

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.3 with 19 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

by sameh on Monday, January 5th 2009 at 02:46 AM

how to remove aline by using printf function

by dev on Saturday, April 24th 2010 at 03:56 AM

thanks it was very helpful for me to understand the topic. kindly send me some details on c program. i am a student of AMIETE

by dev on Saturday, April 24th 2010 at 03:56 AM

thanks it was very helpful for me to understand the topic. kindly send me some details on c program. i am a student of AMIETE


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 >>
Sponsors
Discover Geekpedia

Other Resources