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:
#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 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:
#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 charactersputs (sec)printf (sec)putchar (sec)
100.010.0580.017
1000.0540.2710.073
10000.1050.7820.164
100000.5041.2640.961
1000005.1265.7099.428
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?

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!

Post a Comment

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