Tuesday, January 8, 2013

How to Print to Console in ANSI C

The ANSI C standard I/O library (stdio.h) provides us with 3 powerful functions for outputting characters to STDOUT (the stream used for the console):
-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:

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;
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:

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
    //Outputting the string using puts
    //Outputting the string using putchar
    for(i = 0; i<length; i++)

    return 0;
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 1: puts automatically adds a newline character to the string. If you use printf or putchar you will have to specify the newline yourself.
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:

int main(void)
    //Outputs a character specified by character
    //Outputs a character specified by its ASCII code in decimal
    //Outputs a character specified by its ASCII code in hexadecimal
    //Outputs a character specified by its ASCII code in octal

    return 0;

A little bit about performance:

Speaking in terms of performance, printing is fastest when using puts.
Number of charactersputs (sec)printf (sec)putchar (sec)
The table above represents a benchmark I took using gcc on Linux. By no means, the data is 100% percent accurate since it depends on the hardware configuration and the other processes running at that time, but it can offer a little overview.

If you want to see my test, click here.

Question: In which situations do you use printf, in which putchar and in which puts?


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!

Related Posts Plugin for WordPress, Blogger...
Recommended Post Slide Out For Blogger