Learning Sections show
Output in C
Outputting an Integer
#include<stdio.h> // Include the standard input-output library
int main() {
// Integer output
int num = 10; // Integer variable
// Print the integer
printf("The integer is: %d\n", num);
return 0;
}
Explanation: This program assigns the integer value 10
to the variable num
and prints it using printf
.
Outputting a Floating-Point Number
#include<stdio.h> // Include the standard input-output library
int main() {
// Floating-point output
float fnum = 3.14; // Floating-point variable
// Print the floating-point number
printf("The floating-point number is: %.2f\n", fnum);
return 0;
}
Explanation: This program assigns the floating-point value 3.14
to the variable fnum
and prints it using printf
with 2 decimal places.
Outputting a Double
#include<stdio.h> // Include the standard input-output library
int main() {
// Double output
double dnum = 3.1415926535; // Double variable
// Print the double
printf("The double is: %.10f\n", dnum);
return 0;
}
Explanation: This program assigns the double value 3.1415926535
to the variable dnum
and prints it using printf
with 10 decimal places.
Outputting a Character
#include<stdio.h> // Include the standard input-output library
int main() {
// Character output
char ch = 'A'; // Character variable
// Print the character
printf("The character is: %c\n", ch);
return 0;
}
Explanation: This program assigns the character 'A'
to the variable ch
and prints it using printf
.
Outputting a String
#include<stdio.h> // Include the standard input-output library
int main() {
// String output
char str[] = "Hello, World!"; // String variable
// Print the string
printf("The string is: %s\n", str);
return 0;
}
Explanation: This program assigns the string "Hello, World!"
to the variable str
and prints it using printf
.
Formatted Output
#include<stdio.h> // Include the standard input-output library
int main() {
// Formatted output
int i = 10;
float f = 3.14;
char c = 'A';
char str[] = "Hello";
// Print formatted output
printf("Integer: %d, Float: %.2f, Char: %c, String: %s\n", i, f, c, str);
return 0;
}
Explanation: This program demonstrates formatted output by printing an integer, a floating-point number, a character, and a string all in one printf
statement.
Explanation:
This HTML document contains several simple C programs with syntax highlighting using CSS. Each section demonstrates a different method of output in C:
- Outputting an Integer: Uses
printf
to print an integer. - Outputting a Floating-Point Number: Uses
printf
to print a floating-point number with 2 decimal places. - Outputting a Double: Uses
printf
to print a double with 10 decimal places. - Outputting a Character: Uses
printf
to print a character. - Outputting a String: Uses
printf
to print a string. - Formatted Output: Demonstrates how to print multiple variables of different types in a single
printf
statement.