Learning Sections show
Input in C
Inputting an Integer
#include<stdio.h> // Include the standard input-output library
int main() {
// Integer input
int num; // Integer variable
// Prompt the user to enter an integer
printf("Enter an integer: ");
scanf("%d", &num);
// Print the entered integer
printf("You entered: %d\n", num);
return 0;
}
Explanation: This program prompts the user to enter an integer using printf
and reads the input using scanf
. The entered integer is then printed using printf
.
Inputting a Floating-Point Number
#include<stdio.h> // Include the standard input-output library
int main() {
// Floating-point input
float fnum; // Floating-point variable
// Prompt the user to enter a floating-point number
printf("Enter a floating-point number: ");
scanf("%f", &fnum);
// Print the entered floating-point number
printf("You entered: %.2f\n", fnum);
return 0;
}
Explanation: This program prompts the user to enter a floating-point number using printf
and reads the input using scanf
. The entered number is then printed using printf
with 2 decimal places.
Inputting a Double
#include<stdio.h> // Include the standard input-output library
int main() {
// Double input
double dnum; // Double variable
// Prompt the user to enter a double
printf("Enter a double: ");
scanf("%lf", &dnum);
// Print the entered double
printf("You entered: %.10f\n", dnum);
return 0;
}
Explanation: This program prompts the user to enter a double using printf
and reads the input using scanf
. The entered double is then printed using printf
with 10 decimal places.
Inputting a Character
#include<stdio.h> // Include the standard input-output library
int main() {
// Character input
char ch; // Character variable
// Prompt the user to enter a character
printf("Enter a character: ");
scanf(" %c", &ch);
// Print the entered character
printf("You entered: %c\n", ch);
return 0;
}
Explanation: This program prompts the user to enter a character using printf
and reads the input using scanf
. The entered character is then printed using printf
.
Inputting a String
#include<stdio.h> // Include the standard input-output library
int main() {
// String input
char str[100]; // String variable (character array)
// Prompt the user to enter a string
printf("Enter a string: ");
scanf("%s", str);
// Print the entered string
printf("You entered: %s\n", str);
return 0;
}
Explanation: This program prompts the user to enter a string using printf
and reads the input using scanf
. The entered string is then printed using printf
.
Explanation:
This HTML document contains several simple C programs with syntax highlighting using CSS. Each section demonstrates a different method of input in C:
- Inputting an Integer: Uses
scanf
to read an integer input from the user. - Inputting a Floating-Point Number: Uses
scanf
to read a floating-point number input from the user. - Inputting a Double: Uses
scanf
to read a double-precision floating-point number input from the user. - Inputting a Character: Uses
scanf
to read a single character input from the user. - Inputting a String: Uses
scanf
to read a string input from the user.