In C, we mainly use these standard library functions (from
| Task | Function | Used for | Notes / Common mistakes |
|---|---|---|---|
| Output (screen) | printf() | formatted output | -most commonly used |
| Output (screen) | puts() | simple string + newline | -faster than printf for plain text |
| Output (screen) | putchar() | single character | -rarely used directly |
| Input (keyboard) | scanf() | formatted input | -needs & for most types (very important!) |
| Input (keyboard) | getchar() | single character | -returns int (EOF or character) |
| Input (keyboard) | gets() | whole line (string) | -dangerous – never use (buffer overflow) |
| Input (safer) | fgets() | whole line (recommended)- | -modern & safe choice |
1. Most Important Output Function: printf()
#include <stdio.h> int main() { int age = 21; float marks = 89.75; char grade = 'A'; char name[] = "Pentas"; // Basic printing printf("Hello World\n"); // Printing with variables (format specifiers) printf("My name is %s\n", name); // string printf("I am %d years old\n", age); // integer printf("Marks = %.2f\n", marks); // float → 2 decimal places printf("Grade = %c\n", grade); // single character // Multiple values in one line printf("Name: %s, Age: %d, Marks: %.1f, Grade: %c\n", name, age, marks, grade); return 0; }
2. Most Important Input Function: scanf()
#include <stdio.h> int main() { int roll; float percentage; char section; char firstName[30]; printf("Enter your roll number: "); scanf("%d", &roll); // ← note the & printf("Enter your percentage: "); scanf("%f", &percentage); printf("Enter your section (A/B/C): "); scanf(" %c", §ion); // ← space before %c is very important! printf("Enter your first name: "); scanf("%s", firstName); // ← NO & for arrays/strings printf("\nYou entered:\n"); printf("Roll : %d\n", roll); printf("Percentage: %.2f%%\n", percentage); printf("Section : %c\n", section); printf("Name : %s\n", firstName); return 0; }
Very important rules for scanf():
- Use & before variable name for all basic types (int, float, char, double, etc.)
- Do NOT use & for arrays and strings (char name[50])
- Add a space before %c → scanf(" %c", &ch); (otherwise it often reads leftover Enter key \n)
3. Safer Way to Read a Line
scanf("%s") stops at a space—so "Pentas Hub" becomes only "Pentas". Better to use fgets():
#include <stdio.h> #include <string.h> int main() { char fullName[100]; printf("Enter your full name: "); fgets(fullName, sizeof(fullName), stdin); // Remove trailing newline (very common step) fullName[strcspn(fullName, "\n")] = '\0'; printf("Hello, %s!\n", fullName); return 0; }
| You want to read | Best choice today | Alternative (still okay) | Avoid completely |
|---|---|---|---|
| One number | scanf("%d", &x) | — | — |
| One word (no space) | scanf("%s", name) | — | — |
| Full name/sentence | fgets() | — | gets() |
| Single character | scanf(" %c", &ch) | getchar() | — |
| Just print one line | puts("text") | printf("text\n") | — |
| Formatted output | printf() | — | — |
Mini Practice Program
Common format specifiers: