Formatted Input and Output in C Language – Format Specifiers and Escape Sequences

Formatted input and output are important features of C programming. The scanf() function is used to accept formatted input from the keyboard, while the printf() function is used to display formatted output on the monitor. C language uses format specifiers to identify the type of data being read or displayed. It also provides escape sequences to represent special characters such as newline, tab and quotation marks.

फॉर्मेटेड इनपुट एवं आउटपुट C प्रोग्रामिंग की महत्वपूर्ण विशेषताएँ हैं। scanf() फंक्शन का प्रयोग कीबोर्ड से फॉर्मेटेड इनपुट प्राप्त करने के लिए किया जाता है, जबकि printf() फंक्शन का प्रयोग मॉनिटर पर फॉर्मेटेड आउटपुट प्रदर्शित करने के लिए किया जाता है। C भाषा में डेटा के प्रकार को पहचानने के लिए Format Specifiers का प्रयोग किया जाता है। इसके अतिरिक्त, Newline, Tab और Quotation Marks जैसे विशेष कैरेक्टर्स को प्रदर्शित करने के लिए Escape Sequences का प्रयोग किया जाता है।

1. Format Specifiers in C

A format specifier is a special symbol beginning with % that specifies the type of data to be read or displayed by input/output functions.

Format specifiers are mainly used with scanf() and printf() functions.

Format Specifier एक विशेष चिन्ह होता है जो % से शुरू होता है। इसका प्रयोग इनपुट या आउटपुट फंक्शन को यह बताने के लिए किया जाता है कि किस प्रकार के डेटा को पढ़ना या प्रदर्शित करना है।

Format Specifiers का प्रयोग मुख्य रूप से scanf() और printf() फंक्शन्स के साथ किया जाता है।

Common Format Specifiers

Format SpecifierDescriptionExample
%dSigned decimal integer100
%iInteger (input can recognize decimal, octal or hexadecimal notation)100
%uUnsigned integer250
%fFloating-point value25.75
%lfDouble input with scanf()25.75
%cSingle characterA
%sStringHello
%ldLong integer123456
%lldLong long integer123456789
%xHexadecimal integer (lowercase)2a
%XHexadecimal integer (uppercase)2A
%oOctal integer52
%pPointer addressMemory address
%eScientific notation2.5e+01
%%Displays the percentage symbol%

Important Note: In printf(), %f is used to display a double value. In scanf(), %f is used for float and %lf is used for double.

महत्वपूर्ण नोट: printf() में double वैल्यू प्रदर्शित करने के लिए %f का प्रयोग किया जाता है। जबकि scanf() में float के लिए %f तथा double के लिए %lf का प्रयोग किया जाता है।

2. Width and Precision in printf()

The printf() function allows us to control the width and precision of displayed values.

Width specifies the minimum number of character positions used for displaying a value, while precision controls the number of digits after the decimal point for floating-point values.

printf() फंक्शन की सहायता से आउटपुट की Width और Precision को नियंत्रित किया जा सकता है।

Width यह निर्धारित करता है कि किसी वैल्यू को प्रदर्शित करने के लिए कम से कम कितने कैरेक्टर स्थानों का उपयोग होगा, जबकि Precision यह निर्धारित करता है कि दशमलव के बाद कितने अंक प्रदर्शित किए जाएँगे।

Examples

1. Integer Width

printf("%5d", 25);

Output:

   25

Here, the integer is displayed using a minimum width of five character positions.

यहाँ Integer को कम से कम पाँच कैरेक्टर स्थानों में प्रदर्शित किया जाता है।

2. Floating-Point Precision

float a = 25.6789f;
printf("%.2f", a);

Output:

25.68

Here, %.2f displays two digits after the decimal point.

यहाँ %.2f का प्रयोग दशमलव के बाद दो अंक प्रदर्शित करने के लिए किया जाता है।

3. Width and Precision Together

float a = 25.6789f;
printf("%8.2f", a);

Output:

   25.68

Here, the output occupies a minimum width of eight character positions, with two digits after the decimal point.

यहाँ आउटपुट के लिए कम से कम आठ कैरेक्टर स्थानों का उपयोग होता है तथा दशमलव के बाद दो अंक प्रदर्शित होते हैं।

3. Format Specifiers in scanf()

The scanf() function uses format specifiers to read different types of data from the keyboard.

The address operator & is generally used before variable names to provide their memory addresses. However, when reading a string into a character array using %s, the array name is used without &.

scanf() फंक्शन में अलग-अलग प्रकार के डेटा को कीबोर्ड से पढ़ने के लिए Format Specifiers का प्रयोग किया जाता है।

सामान्यतः वेरिएबल का मेमोरी एड्रेस देने के लिए उसके नाम से पहले Address Operator & लगाया जाता है। लेकिन %s की सहायता से Character Array में स्ट्रिंग इनपुट करते समय Array के नाम के साथ & लगाने की आवश्यकता नहीं होती।

Examples

int a;
float b;
double c;
char ch;
char name[20];

scanf("%d", &a);
scanf("%f", &b);
scanf("%lf", &c);
scanf(" %c", &ch);
scanf("%19s", name);

Important Points:

  1. %d is used to read an integer.

  2. %f is used to read a float value.

  3. %lf is used to read a double value.

  4. A leading space before %c skips preceding whitespace characters.

  5. %19s limits the input to 19 characters, leaving space for the null character in a 20-character array. It reads only one word.

महत्वपूर्ण बातें:

  1. Integer इनपुट करने के लिए %d का प्रयोग किया जाता है।

  2. Float वैल्यू इनपुट करने के लिए %f का प्रयोग किया जाता है।

  3. Double वैल्यू इनपुट करने के लिए %lf का प्रयोग किया जाता है।

  4. %c से पहले Space देने पर पहले से मौजूद Whitespace Characters को छोड़ दिया जाता है।

  5. %19s अधिकतम 19 कैरेक्टर इनपुट करता है, जिससे 20 कैरेक्टर की Array में Null Character के लिए स्थान बचा रहता है। यह केवल एक शब्द पढ़ता है।

4. Escape Sequences in C

Escape sequences are special character combinations beginning with a backslash (\). They are used to represent special characters that cannot be easily written or displayed directly in a string.

Escape sequences are commonly used with the printf() function.

Escape Sequences विशेष कैरेक्टर कॉम्बिनेशन होते हैं जो Backslash (\) से शुरू होते हैं। इनका प्रयोग ऐसे विशेष कैरेक्टर्स को प्रदर्शित करने के लिए किया जाता है जिन्हें सामान्य रूप से सीधे लिखना या प्रदर्शित करना आसान नहीं होता।

Escape Sequences का प्रयोग सामान्यतः printf() फंक्शन के साथ किया जाता है।

Common Escape Sequences

Escape SequenceDescriptionPurpose
\nNewlineMoves cursor to the next line
\tHorizontal TabAdds horizontal spacing
\bBackspaceMoves one position backward
\rCarriage ReturnMoves cursor to the beginning of the current line
\aAlertProduces an alert signal, if supported
\\BackslashDisplays a backslash
\'Single QuoteDisplays a single quotation mark
\"Double QuoteDisplays a double quotation mark
\?Question MarkRepresents a question mark
\0Null CharacterRepresents the null character
\vVertical TabProduces vertical tab spacing
\fForm FeedRepresents a form-feed character

Examples of Escape Sequences

1. Newline (\n)

The \n escape sequence is used to move the cursor to the next line.

\n Escape Sequence का प्रयोग Cursor को अगली लाइन पर ले जाने के लिए किया जाता है।

printf("Hello\nWorld");

Output:

Hello
World

2. Horizontal Tab (\t)

The \t escape sequence is used to provide horizontal spacing between text.

\t Escape Sequence का प्रयोग टेक्स्ट के बीच Horizontal Space देने के लिए किया जाता है।

printf("Name\tAge");

Output:

Name    Age

3. Backspace (\b)

The \b escape sequence moves the output position one character backward. Its visible effect depends on the output device.

\b Escape Sequence आउटपुट पोजीशन को एक कैरेक्टर पीछे ले जाता है। इसका दिखाई देने वाला प्रभाव Output Device पर निर्भर करता है।

printf("AB\bC");

Typical Output:

AC

4. Carriage Return (\r)

The \r escape sequence moves the cursor to the beginning of the current line.

\r Escape Sequence Cursor को वर्तमान लाइन के प्रारंभ में ले जाता है।

printf("Hello\rWorld");

The visible result depends on the output environment because the new characters may overwrite characters already displayed.

इसका दिखाई देने वाला परिणाम Output Environment पर निर्भर करता है, क्योंकि नए कैरेक्टर्स पहले से प्रदर्शित कैरेक्टर्स को Overwrite कर सकते हैं।

5. Backslash (\\)

The \\ escape sequence is used to display a backslash character.

\\ Escape Sequence का प्रयोग Backslash कैरेक्टर प्रदर्शित करने के लिए किया जाता है।

printf("C:\\Program Files");

Output:

C:\Program Files

6. Double Quote (\")

The \" escape sequence is used to display double quotation marks inside a string.

\" Escape Sequence का प्रयोग किसी String के अंदर Double Quotation Marks प्रदर्शित करने के लिए किया जाता है।

printf("Welcome to \"C Programming\"");

Output:

Welcome to "C Programming"

7. Single Quote (\')

The \' escape sequence represents a single quotation mark.

\' Escape Sequence का प्रयोग Single Quotation Mark प्रदर्शित करने के लिए किया जाता है।

printf("It\'s easy to learn C.");

Output:

It's easy to learn C.

5. Complete C Program Using Format Specifiers and Escape Sequences

The following program accepts a student's roll number, name and marks, and displays the information using formatted output and escape sequences.

निम्नलिखित प्रोग्राम विद्यार्थी का रोल नंबर, नाम एवं अंक इनपुट करता है तथा Format Specifiers और Escape Sequences की सहायता से जानकारी प्रदर्शित करता है।

Program:

#include <stdio.h>

int main(void)
{
    int roll;
    char name[30];
    float marks;

    printf("Enter Roll Number: ");
    scanf("%d", &roll);

    printf("Enter Name: ");
    scanf("%29s", name);

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

    printf("\n----- Student Details -----\n");

    printf("Roll Number\t: %d\n", roll);
    printf("Name\t\t: %s\n", name);
    printf("Marks\t\t: %.2f\n", marks);

    return 0;
}

Output:

Enter Roll Number: 101
Enter Name: Ritesh
Enter Marks: 85.75

----- Student Details -----
Roll Number     : 101
Name            : Ritesh
Marks           : 85.75

Explanation:

  1. Three variables are declared to store the roll number, name and marks.

  2. scanf() accepts the values from the keyboard.

  3. %d, %s and %f are used to read and display the respective data types.

  4. \n moves the cursor to a new line.

  5. \t provides horizontal spacing.

  6. %.2f displays marks up to two decimal places.

व्याख्या:

  1. रोल नंबर, नाम एवं अंक स्टोर करने के लिए तीन वेरिएबल डिक्लेयर किए गए हैं।

  2. scanf() फंक्शन की सहायता से कीबोर्ड से वैल्यू इनपुट की जाती हैं।

  3. %d, %s एवं %f का प्रयोग संबंधित डेटा टाइप को इनपुट एवं प्रदर्शित करने के लिए किया गया है।

  4. \n Cursor को नई लाइन में ले जाता है।

  5. \t Horizontal Space प्रदान करता है।

  6. %.2f अंकों को दशमलव के बाद दो स्थानों तक प्रदर्शित करता है।

Important Points to Remember

  1. Format specifiers begin with the % symbol.

  2. scanf() is used for formatted input.

  3. printf() is used for formatted output.

  4. %d is used for integer values.

  5. %f is used for float input and double output.

  6. %lf is used for double input with scanf().

  7. %.2f displays two digits after the decimal point.

  8. Escape sequences begin with a backslash (\).

  9. \n is used for a new line, while \t is used for horizontal tab spacing.

  10. Always use appropriate format specifiers for the corresponding data types.

  11. Format Specifiers की शुरुआत % चिन्ह से होती है।

  12. scanf() का प्रयोग फॉर्मेटेड इनपुट के लिए किया जाता है।

  13. printf() का प्रयोग फॉर्मेटेड आउटपुट के लिए किया जाता है।

  14. Integer वैल्यू के लिए %d का प्रयोग किया जाता है।

  15. Float इनपुट तथा Double आउटपुट के लिए %f का प्रयोग किया जाता है।

  16. scanf() में Double इनपुट करने के लिए %lf का प्रयोग किया जाता है।

  17. %.2f दशमलव के बाद दो अंक प्रदर्शित करता है।

  18. Escape Sequences की शुरुआत Backslash (\) से होती है।

  19. नई लाइन के लिए \n तथा Horizontal Tab के लिए \t का प्रयोग किया जाता है।

  20. हमेशा संबंधित डेटा टाइप के अनुसार सही Format Specifier का प्रयोग करना चाहिए।

Format specifiers and escape sequences make input and output operations more flexible and meaningful in C programming. Understanding their correct usage is essential for writing clear and effective C programs.

Format Specifiers एवं Escape Sequences C Programming में इनपुट और आउटपुट ऑपरेशन को अधिक उपयोगी एवं व्यवस्थित बनाते हैं। C प्रोग्राम को सही और प्रभावी तरीके से लिखने के लिए इनका सही उपयोग समझना आवश्यक है।

Comments

Popular posts from this blog

C Language Topics in Hindi and English

Complete C Programming Syllabus | Advanced C Roadmap for Students

Top High-Paying Tech Skills to Learn in 2025