In C, you can output data to the console using the printf function, which is part of the standard input/output library (stdio.h).
Here's an example program that outputs a message to the console:
#include <stdio.h>
int main() {
printf("Hello, world!\n");
return 0;
}
When you run this program, you should see the following output:
Hello, world!
Let's break down the printf function in more detail:
- The first argument to
printfis a string of characters that specifies the message to output. In this case, the string is"Hello, world!\n". - The
\ncharacter sequence is an escape sequence that represents a newline character. When theprintffunction encounters\n, it moves the cursor to the beginning of the next line. - Note that the string is enclosed in double quotes (
"), which is how you specify a string literal in C.
You can also include variables in the output message using format specifiers. For example, here's a program that outputs the value of an integer variable:
#include <stdio.h>
int main() {
int num = 42;
printf("The answer is %d.\n", num);
return 0;
}
When you run this program, you should see the following output:
The answer is 42.
In this program, %d is a format specifier that tells printf to substitute the value of the variable num into the output message.

Comments