A 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.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This 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.
Creating 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.
Using the printf() functionA 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) |
|||||||||||||||||
|
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:
...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 variableWe have a variable named myNum that stores the number 69. We want to output the value of this variable using the printf() function:
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:
The result being:
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:
And with the result:
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.
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:
The output being:
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):
Or if you store the string pedia in a char variable:
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:
In my case, the result is:
|
||||||||||||||||||
Digg It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
||||||||||||||||||
|
||||||||||||||||||
Current CommentsThank you for your effort
wow thanks ...Wanted the ASCII one for a long time
su dung ham printf
hi
its very useful for beginners.plz send more than like this
Thanks
I am a beginner. I found it was very useful.
Thank you for your effort
Thank you for your effort
how to remove aline by using printf function
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
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
Related Tutorials
Related Source Code
C Job Search