The standard functions for I/O with stdin and stdout are as follows:
The rule for output is always use the simplest method. For example, use putchar('a') instead of puts("a"), and puts("Hi, Bob") instead of printf("Hi, Bob\n"). Using printf() to print a simple string that has no place-holders wastes CPU time, since printf() combs the string for place-holders as it prints. This may or may not be an issue for a particular program, but it's better to form good habits. If you can eliminate complex library functions such as printf() from your code altogether, you'll end up with a smaller executable file.
putchar() - send the character ch to stdout.
putchar('a');
The argument to putchar can be a char or int variable (discussed
later) or a character constant, which is
any visible character or escape sequence
between SINGLE quotes. The most common escape sequences are
'\n' (newline) and '\t' (tab).
puts(str) - sends a simple string (array of characters) str to stdout. The puts() function simply calls putchar() for each character in the string. The argument to puts() can be a character array (discussed later) or a string constant, which is any set of characters and/or escape sequences between DOUBLE quotes.
puts("Hi, Bob.");
Note: Puts outputs a newline ('\n') after the string, so you do not need to place on in the string. If you don't want a newline printed after the string, use fputs(str, stdout). The following is equivalent to the above:
fputs("Hi, Bob\n", stdout);
printf(format-string, additional arguments) - prints any type of data to stdout.
The format string provides an overview of what the entire output will look like. It contains constant character data as would be sent to puts(), plus place-holders for the additional arguments that follow.
The value of the first argument after the format string is displayed where the first place-holder appears, and so on.
printf("%d squared is %d\n", c, c*c);
Place-holders begin with a '%'. If you actually want a '%' to be printed by a printf() statement, use "%%". There is a different place-holders for each type in the C language. (Data types are explained in the next section.) Below is a list of the most useful ones in Vex programming:
Table 9.1. printf() place-holders
| Place-holder | Data type |
|---|---|
| %d | int |
| %ld | long |
| %p | memory address |
| %f | float |
| %c | character |