惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

阮一峰的网络日志
阮一峰的网络日志
博客园 - 司徒正美
D
DataBreaches.Net
宝玉的分享
宝玉的分享
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 【当耐特】
人人都是产品经理
人人都是产品经理
博客园 - Franky
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
IT之家
IT之家
博客园 - 三生石上(FineUI控件)
J
Java Code Geeks
腾讯CDC
博客园_首页
The Cloudflare Blog
S
SegmentFault 最新的问题
C
Check Point Blog
美团技术团队
爱范儿
爱范儿
大猫的无限游戏
大猫的无限游戏
Hugging Face - Blog
Hugging Face - Blog
T
The Blog of Author Tim Ferriss
A
About on SuperTechFans
Blog — PlanetScale
Blog — PlanetScale

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
printf() and scanf() in C: Understanding Input and Output
Moksh Upadhyay · 2026-06-23 · via DEV Community

Moksh Upadhyay

One of the first things every C programmer learns is how to interact with users. A program that cannot receive input or display output is not very useful.

In C, this interaction is handled primarily by two standard library functions:

  • printf() for output
  • scanf() for input

Let's see how they work and why they are so important.

What is printf()?

printf() stands for Print Formatted.

It is used to display output on the screen.

#include <stdio.h>

int main()
{
    printf("Hello, World!");
    return 0;
}

Output:

Hello, World!

The function can also display variable values using format specifiers.

int age = 23;

printf("Age = %d", age);

Output:

Age = 23

Here, %d tells printf() to display an integer.

What is scanf()?

scanf() stands for Scan Formatted.

It reads input from the keyboard and stores it in variables.

int age;

scanf("%d", &age);

The & operator is important because scanf() needs the memory address where the value should be stored.

Complete example:

#include <stdio.h>

int main()
{
    int age;

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

    printf("You entered: %d", age);

    return 0;
}

Common Format Specifiers

Specifier Type
%d int
%f float
%lf double
%c char
%s string

Example:

float salary = 45000.50;
char grade = 'A';

printf("%f\n", salary);
printf("%c\n", grade);

A Common Beginner Mistake

Many beginners forget the & operator.

Incorrect:

scanf("%d", age);

Typical compiler warning:

warning: format '%d' expects argument of type 'int *'

Correct:

scanf("%d", &age);

How scanf() and printf() Work Internally

A useful mental model is:

Keyboard
   ↓
scanf()
   ↓
Memory
   ↓
printf()
   ↓
Monitor

When a user enters a value:

  1. scanf() reads the input.
  2. The value is stored in memory.
  3. printf() reads the value from memory.
  4. The result is displayed on the screen.

Understanding this flow makes later topics such as pointers and memory management much easier.

Key Takeaways

  • printf() displays output.
  • scanf() accepts input.
  • Format specifiers determine how data is interpreted.
  • The & operator provides a memory address.
  • These functions are fundamental to almost every beginner C program.