-Printing a character: putchar
-Printing a string : puts
-Printing a formatted string : printf
A formatted string is a dynamical string that is built on the go. Here's an example:
#include<stdio.h>
int main(void)
{
int myInt = 42;
unsigned int myUint = 0xFFU;
char myChar = 'a';
double myDouble = 3.42;
char myString[20] = "myString";
printf("Formatted string with the values: %d, %x, %c, %lf and %s.",
myInt, myUint, myChar, myDouble, myString);
return 0;
}
/*Output
Formatted string with the values:: 42, ff, a, 3.420000 and myString.
*/
You can read more about formatted strings in this article.There is also vprintf, which allows to print arguments received as a va_list, be we shall not go into details since is not very used.
Here's a short example on how to use these three functions to print a string:
#include<stdio.h>
#include<string.h>
int main(void)
{
char myString[21] = "The bird is the word.";
//Computing the length of the string for putchar
int length = strlen(myString);
//Iterator used for putchar
int i = 0;
//Outputting the string using printf
printf("%s\n",myString);
//Outputting the string using puts
puts(myString);
//Outputting the string using putchar
for(i = 0; i<length; i++)
{
putchar(myString[i]);
}
putchar('\n');
return 0;
}
/*Output
The bird is the word.
The bird is the word.
The bird is the word.
*/
The program above prints the string myString. As you can see you can print it as a formatted string using printf, as a normal string using puts and character by character using putchar.TIP 2: To print a string using puts, you will probably need to know the length of the string. This can be achieved by using strlen from string.h.
TIP 3: printf returns the number of characters that were printed if successful. Otherwise returns a negative value.
TIP 4: puts returns a non-negative value if it was successful. Otherwise it returns EOF.
TIP 5: putchar returns the written character if it was successful. Otherwise it returns EOF.
TIP 6: You can also use putchar in the following fashion:
#include<stdio.h>
int main(void)
{
//Outputs a character specified by character
putchar('w');
//Outputs a character specified by its ASCII code in decimal
putchar(119);
//Outputs a character specified by its ASCII code in hexadecimal
putchar(0x77);
//Outputs a character specified by its ASCII code in octal
putchar(0167);
return 0;
}
/*Output
wwww
*/
A little bit about performance:
Speaking in terms of performance, printing is fastest when using puts.| Number of characters | puts (sec) | printf (sec) | putchar (sec) |
| 10 | 0.01 | 0.058 | 0.017 |
| 100 | 0.054 | 0.271 | 0.073 |
| 1000 | 0.105 | 0.782 | 0.164 |
| 10000 | 0.504 | 1.264 | 0.961 |
| 100000 | 5.126 | 5.709 | 9.428 |
If you want to see my test, click here.
Question: In which situations do you use printf, in which putchar and in which puts?
References:
http://www.songho.ca/misc/timer/timer.html
http://www.cplusplus.com/reference/cstdio/
http://pubs.opengroup.org/onlinepubs/7908799/xsh/stdio.h.html
No comments:
Post a Comment
Got a question regarding something in the article? Leave me a comment and I will get back at you as soon as I can!