Printing Output in C (printf)
To display text on the screen, we use the printf() function from <stdio.h>.
Example: Using printf()
#include <stdio.h>
int main() {
printf(“C programming is fun!\n”);
printf(“I am learning C.\n”);
return 0;
}
📌 Output:
C programming is fun!
I am learning C.
🔹 \n (newline character) moves text to the next line.
Adding Comments in C
Comments help explain the code but are ignored by the compiler.
Types of Comments in C
Type | Syntax | Example |
Single-line comment | // | // This is a single-line comment |
Multi-line comment | /* … */ | /* This is a multi-line comment */ |
Example: Using Comments in C
#include <stdio.h>
int main() {
// This is a single-line comment
printf(“Hello, World!\n”);
/* This is a multi-line comment
It explains multiple lines of code */
return 0;
}
Taking User Input (scanf)
To read user input, we use scanf(). It scans the input based on format specifiers.
Data Type | Format Specifier |
int | %d |
float | %f |
char | %c |
string | %s |
Example: Taking Input from the User
#include <stdio.h>
int main() {
int age;
printf(“Enter your age: “);
scanf(“%d”, &age); // Reads an integer input
printf(“You are %d years old.\n”, age);
return 0;
}
📌 Output (Example Run)
Enter your age: 25
You are 25 years old. 🔹 Note: &age is used to store the input in the variable age