Printing to the Console in C programming
Printing to the Console in C
In C programming, you can print output to the console using the printf
function from the <stdio.h>
library. The function allows you to format and display text and variables.
Syntax
printf("format string", arguments);
Where format string
specifies how the output should be formatted, and arguments
are the values to be printed.
Common Format Specifiers
%d
: For integers%f
: For floating-point numbers%c
: For characters%s
: For strings
Example Code
Here’s a simple example of using printf
:
#include <stdio.h>
int main() {
int age = 25;
float height = 5.9;
char initial = 'J';
char name[] = "John Doe";
// Print an integer
printf("Age: %d\n", age);
// Print a floating-point number
printf("Height: %.1f feet\n", height);
// Print a character
printf("Initial: %c\n", initial);
// Print a string
printf("Name: %s\n", name);
return 0;
}
This code demonstrates printing different types of data using printf
:
printf("Age: %d\n", age);
prints the integer variableage
.printf("Height: %.1f feet\n", height);
prints the floating-point variableheight
with one decimal place.printf("Initial: %c\n", initial);
prints the character variableinitial
.printf("Name: %s\n", name);
prints the string variablename
.
Note:
Use format specifiers that match the type of the variable you are printing to avoid unexpected results or compiler errors.
Use format specifiers that match the type of the variable you are printing to avoid unexpected results or compiler errors.
Comments
Post a Comment