top of page

Basic Input/Output - Formatted and Unformatted Input/Output Statement in C

  • Writer: Siddharth Sharma
    Siddharth Sharma
  • Oct 6, 2025
  • 2 min read

Introduction / परिचय

C programming में data को input लेने और output दिखाने के लिए special functions होते हैं। ये दो तरीके से हो सकते हैं:

  • Formatted Input/Output (Format follow कर के value लेना और देना)

  • Unformatted Input/Output (Simple character-level input/output)



Formatted Input/Output / फॉर्मेटेड इनपुट/आउटपुट

printf() Function (Output) / प्रिंटफ (आउटपुट)

  • Use: किसी भी formatted data को स्क्रीन (console) पर दिखाने के लिए।

  • Syntax:

    printf("format_string", values);

  • Format string में special placeholders होते हैं, जैसे %d, %f, %c, जिन्हें variables या constants से replace किया जाता है।

Example:

int num = 10;

printf("Number = %d\n", num);  // Output: Number = 10


Common format specifiers:

  • %d or %i → Integer

  • %f → Float

  • %lf → Double

  • %c → Character

  • %s → String

scanf() Function (Input) / स्कैनफ (इनपुट)

  • Use: User से formatted data input लेने के लिए।

  • Syntax:

    scanf("format_string", &variable);

  • & address-of operator देता है ताकि scanf variable में value store कर सके।

Example:

int num;

printf("Enter a number: ");

scanf("%d", &num);

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


Multiple inputs एक साथ लेना:

int a;

float b;

scanf("%d %f", &a, &b);


Unformatted Input/Output / अनफॉर्मेटेड इनपुट/आउटपुट

getchar() / putchar()

  • getchar() एक-एक character accept करता है, usually keyboard से।

  • Syntax:

    char ch = getchar();

  • putchar() एक character output करता है।

  • Syntax:

    putchar(ch);

Example:

printf("Enter a character: ");

char ch = getchar();

printf("You entered: ");

putchar(ch);


gets() / puts()

  • gets() एक पूरा string input लेता है (including spaces)।

  • puts() एक string output करता है और newline भी add करता है।

Example:

char str[100];

gets(str);

puts(str);


Note: gets() unsafe है क्योंकि buffer overflow risk होता है इसलिए modern C में fgets() है safer विकल्प।

Summary Table / सारांश तालिका

Function

Purpose

Input/Output

Notes

printf()

Formatted Output

Output

Format specifiers use

scanf()

Formatted Input

Input

Requires & for variables

getchar()

Single character Input

Input

Reads one character

putchar()

Single character Output

Output

Prints one character

gets()

String Input (unsafe)

Input

Reads string with spaces

puts()

String Output

Output

Prints string + newline

Quick Example Program

#include <stdio.h>

int main() {
    int num;
    char ch;

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

    // Clear input buffer
    while(getchar() != '\n');

    printf("Enter a character: ");
    ch = getchar();

    printf("You entered number: %d and character: %c\n", num, ch);

    return 0;
}

Important Notes / जरूरी बातें

  • Format specifiers accurately use करें, वरना garbage output आ सकता है।

  • scanf() में address operator (&) लगाना ज़रूरी है।

  • Buffer clear करना चाहिए, खासकर character input से पहले।

  • Unformatted I/O से cursor control directly कर सकते हैं, लेकिन formatted I/O अधिक flexible और readable होता है।

अगर formatted या unformatted I/O में से किसी पर विस्तार से कोड उदाहरण या दूसरे concepts समझने हों, तो जानकारी दें।

 
 
 

Comments


bottom of page