Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 1 — Input and Output in C

Lesson 5 of 32 in the free C Language notes on Siksha Sarovar, written by Rohit Jangra.

Input and Output in C

C handles I/O through the stdio.h (Standard Input/Output) library.

---

printf() — Formatted Output

printf("format string", arg1, arg2, ...);

Format specifiers:

SpecifierTypeExample
%d or %iIntegerprintf("%d", 42)
%fFloatprintf("%.2f", 3.14)
%lfDoubleprintf("%lf", 3.14)
%cCharacterprintf("%c", 'A')
%sStringprintf("%s", "hello")
%oOctalprintf("%o", 8) → 10
%xHex (lower)printf("%x", 255) → ff
%uUnsigned intprintf("%u", 42)

Escape sequences:

SequenceMeaning
\nNew line
\tTab
\Backslash
\"Double quote
\0Null character

---

scanf() — Formatted Input

scanf("format string", &var1, &var2, ...);
Note: Always use the address-of operator & with scanf (except for strings).

Example:

#include <stdio.h>

int main() {
    int age;
    float height;
    char name[50];
    
    printf("Enter age: ");
    scanf("%d", &age);
    
    printf("Enter height: ");
    scanf("%f", &height);
    
    printf("Enter name: ");
    scanf("%s", name);   /* no & for arrays */
    
    printf("Name: %s, Age: %d, Height: %.2f\n", name, age, height);
    return 0;
}

---

Character I/O

char c;
c = getchar();     /* reads one character */
putchar(c);        /* prints one character */

---

String I/O

char str[100];
gets(str);           /* reads a line (unsafe — do not use in production) */
puts(str);           /* prints string + newline */
fgets(str, 100, stdin);  /* safe alternative to gets */

---

Complete Example

#include <stdio.h>

int main() {
    int a, b, sum;
    
    printf("Enter two integers: ");
    scanf("%d %d", &a, &b);
    
    sum = a + b;
    printf("Sum = %d\n", sum);
    
    return 0;
}