C Programming Tutorial 0/65 lessons ~6 min read Lesson 8
Input and Output
Console I/O lives in stdio.h: formatted output with printf and formatted input with scanf.
Course progress0%
Focus
10 guided sections
Practice signal
Examples included
Career prep
Foundation builder
Introduction
Console I/O lives in stdio.h: formatted output with printf and formatted input with scanf. For line-based text, fgets is often safer than unbounded scanf.
Understanding the topic
printf Formatted output — printf("format", args);
scanf Formatted input — always pass address with & for variables.
Safer input Prefer fgets for strings to avoid buffer overflow.
- printf — Formatted output — printf("format", args);.
- scanf — Formatted input — always pass address with & for variables.
- Safer input — Prefer fgets for strings to avoid buffer overflow.
Step-by-step explanation
- printf — Formatted output — printf("format", args);.
- scanf — Formatted input — always pass address with & for variables.
- Safer input — Prefer fgets for strings to avoid buffer overflow.
Syntax reference
Syntax reference:
c
printf("format", args);scanf("format", &var);
Informative example
Example program:
c
#include <stdio.h>int main(void) {int n;printf("Enter a number: ");scanf("%d", &n);printf("You entered: %d\n", n);return 0;}
Execution workflow
1Input and Output — step by step
1 / 3printf
Formatted output — printf("format", args);.
Best practices
- Enable warnings: gcc -Wall -Wextra -std=c11 source.c -o app
- Give every variable a defined value before it is read.
- Stay inside array bounds — C will not stop you from over-running a buffer.
Common mistakes
- Never use scanf("%s") without width limit on fixed buffers
Hands-on exercise
Practice problems:
- Read and print an integer
- Read a full line with fgets
Summary
Input and Output in C — Formatted console I/O with stdio and safer line-reading patterns.
Ready to mark this lesson complete?Track your journey across the entire course.