Input and output statement

 In C, we mainly use these standard library functions (from ) for input and output:

TaskFunctionUsed forNotes / 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", &section); // ← 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 readBest choice todayAlternative (still okay)Avoid completely
One numberscanf("%d", &x)
One word (no space)scanf("%s", name)
Full name/sentencefgets()gets()
Single characterscanf(" %c", &ch)getchar()
Just print one lineputs("text")printf("text\n")
Formatted outputprintf()

Mini Practice Program

#include <stdio.h>

int main()
{
    char name[50];
    int age;
    float height;

    printf("Enter your name     : ");
    fgets(name, sizeof(name), stdin);
    name[strcspn(name, "\n")] = '\0';

    printf("Enter your age      : ");
    scanf("%d", &age);

    printf("Enter your height (cm): ");
    scanf("%f", &height);

    printf("\n--- Your Details ---\n");
    printf("Name   : %s\n", name);
    printf("Age    : %d years\n", age);
    printf("Height : %.1f cm\n", height);

    return 0;
}


Common format specifiers:








Previous Post Next Post

Contact Form