Input and Output in C

Every program interacts with the outside world through input and output. In C, input means reading data from the user (via keyboard), and output means displaying data on the screen. The standard library functions printf() and scanf() are the primary tools for output and input in C.

Both functions are available through the header file <stdio.h>, which must be included at the beginning of the program.

The printf() Function — Output

printf() is used to display data on the screen. It can display text, values of variables, and formatted output.

Syntax


printf("format string", variable1, variable2, ...);

Basic Examples


#include <stdio.h>

int main()
{
    printf("Hello, World!\n");           // printing text
    printf("Number: %d\n", 42);         // printing an integer
    printf("Float: %.2f\n", 3.14159);   // printing a float with 2 decimals
    printf("Char: %c\n", 'A');          // printing a character
    printf("String: %s\n", "C Language"); // printing a string
    return 0;
}

Output


Hello, World!
Number: 42
Float: 3.14
Char: A
String: C Language

Format Specifiers in printf()

SpecifierData TypeExample
%dint (decimal)printf("%d", 10)
%iint (same as %d)printf("%i", 10)
%ffloatprintf("%f", 3.14)
%lfdoubleprintf("%lf", 3.14)
%ccharprintf("%c", 'A')
%sstring (char array)printf("%s", "Hi")
%uunsigned intprintf("%u", 300)
%ldlong intprintf("%ld", 100000L)
%ooctalprintf("%o", 8) → 10
%xhexadecimal (lowercase)printf("%x", 255) → ff
%Xhexadecimal (uppercase)printf("%X", 255) → FF
%%Literal % signprintf("%%") → %

Formatting Output Width and Precision


#include <stdio.h>

int main()
{
    printf("%10d\n", 42);       // right-aligned in width of 10
    printf("%-10d|\n", 42);     // left-aligned in width of 10
    printf("%05d\n", 42);       // zero-padded to width 5
    printf("%.3f\n", 3.14159);  // float with 3 decimal places
    printf("%8.2f\n", 3.14159); // width 8, 2 decimal places

    return 0;
}

Output


        42
42        |
00042
3.142
    3.14

Printing Multiple Values


#include <stdio.h>

int main()
{
    int age = 22;
    float gpa = 8.75;
    char grade = 'A';

    printf("Age: %d, GPA: %.2f, Grade: %c\n", age, gpa, grade);

    return 0;
}

Output


Age: 22, GPA: 8.75, Grade: A

The scanf() Function — Input

scanf() is used to read input from the user via the keyboard. The user types a value and presses Enter — that value is stored in the specified variable.

Syntax


scanf("format string", &variable1, &variable2, ...);

Important: The & (address-of) operator is used before the variable name in scanf(). This gives the address of the variable so the function knows where to store the input. (Exception: strings/arrays do not need &.)

Reading a Single Integer


#include <stdio.h>

int main()
{
    int num;

    printf("Enter a number: ");
    scanf("%d", &num);

    printf("You entered: %d\n", num);

    return 0;
}

Sample Interaction


Enter a number: 25
You entered: 25

Reading Multiple Values


#include <stdio.h>

int main()
{
    int age;
    float height;
    char initial;

    printf("Enter age, height, and initial: ");
    scanf("%d %f %c", &age, &height, &initial);

    printf("Age: %d\n", age);
    printf("Height: %.1f\n", height);
    printf("Initial: %c\n", initial);

    return 0;
}

Sample Interaction


Enter age, height, and initial: 21 5.8 R
Age: 21
Height: 5.8
Initial: R

Reading a String


#include <stdio.h>

int main()
{
    char name[50];  // array to hold string

    printf("Enter your name: ");
    scanf("%s", name);  // no & needed for arrays

    printf("Hello, %s!\n", name);

    return 0;
}

Note: scanf("%s") reads only one word (stops at whitespace). To read a full line with spaces, use fgets().

The getchar() and putchar() Functions

These functions read and write a single character.

getchar() — Read one character


#include <stdio.h>

int main()
{
    char ch;
    printf("Enter a character: ");
    ch = getchar();
    printf("You entered: ");
    putchar(ch);
    printf("\n");
    return 0;
}

The gets() and puts() Functions

gets() — Read a full line (including spaces)

Note: gets() is considered unsafe and deprecated in modern C standards. It is better to use fgets() instead.

puts() — Print a string followed by a newline


#include <stdio.h>

int main()
{
    char city[50];

    printf("Enter city name: ");
    fgets(city, 50, stdin);  // safer alternative to gets()

    printf("City: ");
    puts(city);  // prints string with automatic newline

    return 0;
}

Reading Full Line with fgets()

fgets() reads a full line of text, including spaces, making it a safer and better choice than scanf("%s") or gets().

Syntax


fgets(variable_name, size, stdin);

#include <stdio.h>

int main()
{
    char fullName[100];

    printf("Enter full name: ");
    fgets(fullName, 100, stdin);

    printf("Full Name: %s", fullName);

    return 0;
}

Sample Interaction


Enter full name: Alice Johnson
Full Name: Alice Johnson

Common Program — Calculate Area of a Rectangle


#include <stdio.h>

int main()
{
    float length, width, area;

    printf("Enter length: ");
    scanf("%f", &length);

    printf("Enter width: ");
    scanf("%f", &width);

    area = length * width;

    printf("Area of Rectangle = %.2f sq. units\n", area);

    return 0;
}

Sample Interaction


Enter length: 8
Enter width: 5
Area of Rectangle = 40.00 sq. units

Common Mistakes with scanf()

MistakeProblemFix
scanf("%d", num);Missing & — program may crashscanf("%d", &num);
scanf("%f", &num) for doubleWrong specifier for doublescanf("%lf", &num);
Using gets() for stringsUnsafe, can overflow bufferUse fgets() instead

Summary

printf() and scanf() are the primary I/O functions in C. printf() uses format specifiers to display data in a specific format. scanf() reads data from the user and stores it in variables using the address-of operator &. For reading strings with spaces, fgets() is the safest option. Mastering input and output functions is essential for building interactive C programs.

Leave a Comment

Your email address will not be published. Required fields are marked *