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 Specifier | Description | Example |
|---|---|---|
%d | Signed decimal integer | 100 |
%i | Integer (input can recognize decimal, octal or hexadecimal notation) | 100 |
%u | Unsigned integer | 250 |
%f | Floating-point value | 25.75 |
%lf | Double input with scanf() | 25.75 |
%c | Single character | A |
%s | String | Hello |
%ld | Long integer | 123456 |
%lld | Long long integer | 123456789 |
%x | Hexadecimal integer (lowercase) | 2a |
%X | Hexadecimal integer (uppercase) | 2A |
%o | Octal integer | 52 |
%p | Pointer address | Memory address |
%e | Scientific notation | 2.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:
25Here, 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.68Here, %.2f displays two digits after the decimal point.
यहाँ %.2f का प्रयोग दशमलव के बाद दो अंक प्रदर्शित करने के लिए किया जाता है।
3. Width and Precision Together
float a = 25.6789f;
printf("%8.2f", a);Output:
25.68Here, 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:
%dis used to read an integer.%fis used to read a float value.%lfis used to read a double value.A leading space before
%cskips preceding whitespace characters.%19slimits the input to 19 characters, leaving space for the null character in a 20-character array. It reads only one word.
महत्वपूर्ण बातें:
Integer इनपुट करने के लिए
%dका प्रयोग किया जाता है।Float वैल्यू इनपुट करने के लिए
%fका प्रयोग किया जाता है।Double वैल्यू इनपुट करने के लिए
%lfका प्रयोग किया जाता है।%cसे पहले Space देने पर पहले से मौजूद Whitespace Characters को छोड़ दिया जाता है।%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 Sequence | Description | Purpose |
|---|---|---|
\n | Newline | Moves cursor to the next line |
\t | Horizontal Tab | Adds horizontal spacing |
\b | Backspace | Moves one position backward |
\r | Carriage Return | Moves cursor to the beginning of the current line |
\a | Alert | Produces an alert signal, if supported |
\\ | Backslash | Displays a backslash |
\' | Single Quote | Displays a single quotation mark |
\" | Double Quote | Displays a double quotation mark |
\? | Question Mark | Represents a question mark |
\0 | Null Character | Represents the null character |
\v | Vertical Tab | Produces vertical tab spacing |
\f | Form Feed | Represents 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
World2. 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 Age3. 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:
AC4. 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 Files6. 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.75Explanation:
Three variables are declared to store the roll number, name and marks.
scanf()accepts the values from the keyboard.%d,%sand%fare used to read and display the respective data types.\nmoves the cursor to a new line.\tprovides horizontal spacing.%.2fdisplays marks up to two decimal places.
व्याख्या:
रोल नंबर, नाम एवं अंक स्टोर करने के लिए तीन वेरिएबल डिक्लेयर किए गए हैं।
scanf()फंक्शन की सहायता से कीबोर्ड से वैल्यू इनपुट की जाती हैं।%d,%sएवं%fका प्रयोग संबंधित डेटा टाइप को इनपुट एवं प्रदर्शित करने के लिए किया गया है।\nCursor को नई लाइन में ले जाता है।\tHorizontal Space प्रदान करता है।%.2fअंकों को दशमलव के बाद दो स्थानों तक प्रदर्शित करता है।
Important Points to Remember
Format specifiers begin with the
%symbol.scanf()is used for formatted input.printf()is used for formatted output.%dis used for integer values.%fis used for float input and double output.%lfis used for double input withscanf().%.2fdisplays two digits after the decimal point.Escape sequences begin with a backslash (
\).\nis used for a new line, while\tis used for horizontal tab spacing.Always use appropriate format specifiers for the corresponding data types.
Format Specifiers की शुरुआत
%चिन्ह से होती है।scanf()का प्रयोग फॉर्मेटेड इनपुट के लिए किया जाता है।printf()का प्रयोग फॉर्मेटेड आउटपुट के लिए किया जाता है।Integer वैल्यू के लिए
%dका प्रयोग किया जाता है।Float इनपुट तथा Double आउटपुट के लिए
%fका प्रयोग किया जाता है।scanf()में Double इनपुट करने के लिए%lfका प्रयोग किया जाता है।%.2fदशमलव के बाद दो अंक प्रदर्शित करता है।Escape Sequences की शुरुआत Backslash (
\) से होती है।नई लाइन के लिए
\nतथा Horizontal Tab के लिए\tका प्रयोग किया जाता है।हमेशा संबंधित डेटा टाइप के अनुसार सही 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
Post a Comment