Type specifiers (formatting strings): %d, %ld, %f, %c, %s, %lf

Type specifiers એ printf/scanf ના placeholders છે, દરેક data type માટે એક: %d int, %ld long, %f float, %c char, %s string, %lf double, અને ખોટી જોડી તમારી value ને બદલે garbage print કરે છે.

10 min read · 10 cards · 3 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

પોશાક વાળી party

તમે 3 lessons થી %d અને %f પર ભરોસો કરતા આવ્યા છો. હવે એમને decode કરવાનો સમય છે.

આ experiment મનમાં ચલાવો: printf("%d", 78.5);, એક float ને int ના પોશાકમાં print કરવો. Output ન 78 છે, ન 79, પણ garbage: કદાચ -1717986918.

printf એ ચકાસતું નથી કે કોણ શું પહેર્યું છે. Type specifier એને કહે છે કે કાચા bytes કેવી રીતે વાંચવા, અને ખોટા પોશાકમાં વંચાયેલા bytes બેહૂદા બની જાય છે. મહેમાનો સાથે પોશાક મેળવવો એ જ આજની આખી કળા છે.

Theory

આકારવાળી ખાલી જગ્યાઓથી fill-in-the-blanks

printf ની format string ખાલી જગ્યાવાળું વાક્ય છે: "Marks: __, Grade: __". પણ દરેક ખાલી જગ્યાનો એક આકાર છે: %d વાળી ખાલી જગ્યામાં int બેસે, %c વાળીમાં એક character, %f વાળીમાં એક decimal. int-આકારની ખાલી જગ્યામાં float ઠાલવો અને એ બગડીને નીકળે છે, જાણે ગોળ dabba ને ચોરસ ખાનામાં ઠાલવવો. ખાલી જગ્યાઓ એ specifiers છે; આકાર મેળ ખાવા જોઈએ.

At a glance

Specifier પરિવાર

SpecifierTypeઉદાહરણ value
%dint78
%ldlong8930012345
%ffloat (printf: double પણ)78.500000
%lfdouble (scanf માં જરૂરી)78.53
%cchar (એક character)A
%sstring (char array)Riya

Practical

એક marksheet, દરેક specifier

#include <stdio.h>

int main() {
    char name[20] = "Riya";
    int marks = 78;
    float percentage = 78.53;
    char grade = 'A';

    printf("Name : %s\n", name);
    printf("Marks: %d\n", marks);
    printf("Perc : %.2f\n", percentage);
    printf("Grade: %c (ASCII %d)\n", grade, grade);
    return 0;
}

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

એક char, 2 પોશાક

છેલ્લું printf grade ને 2 વાર print કરે છે: એક વાર %c થી અને એક વાર %d થી. char ખરેખર શું છે એ વાપરીને એ line નું બરાબર output કહો.

Show the answer

Grade: A (ASCII 65)

એ જ variable, એ જ byte: %c એને character (A) તરીકે પહેરાવે છે, %d એના number (65) તરીકે. કંઈ convert થતું નથી; ફક્ત વાંચવાનો પોશાક બદલાય છે. આ એક જ line સૌથી ચોખ્ખો પુરાવો છે કે char એ ભેખ બદલેલો ASCII number છે, અને predict-the-output નું માનીતું item.

Quiz

printf("%d", 78.5); ખરેખર શું print કરે છે?

  1. અનિશ્ચિત garbage: bytes ખોટા format માં વંચાય છે
  2. 78, C decimal ભાગ કાઢી નાખે છે
  3. 79, C એને round કરે છે
  4. 78.5, printf આપોઆપ adjust કરી લે છે
Show the answer

અનિશ્ચિત garbage: bytes ખોટા format માં વંચાય છે

Float ના bytes int ના lens થી વંચાય તો અર્થહીન છે: output garbage છે અને formally undefined behaviour. 78 સુધી કાપવાનો જવાબ એને ASSIGNMENT સાથે ગૂંચવે છે (int x = 78.5 ખરેખર કાપે છે); printf કંઈ convert કરતું નથી, એ ફક્ત bytes ને કહ્યા મુજબ વાંચે છે. આ 2 સ્થિતિ વચ્ચે ફરક કરવો એ જ examiners ટટોળે છે.

Quiz

તમે scanf થી એક double વાંચી રહ્યા છો. કયો specifier જરૂરી (REQUIRED) છે?

  1. %lf
  2. %f
  3. %d
  4. %s
Show the answer

%lf

scanf ને બરાબર ખબર હોવી જોઈએ કે એ કેટલા મોટા ડબ્બાને ભરી રહ્યું છે: %f 4 float bytes ભરે, %lf 8 double bytes, એટલે double ને %lf જોઈએ જ. આ અસમતુલા બધાને ફસાવે છે: printf માં %f ખુશીથી float અને double બંને print કરી દે છે. answer sheet ના હાંસિયા માટે નાનો નિયમ: scanf કડક છે, printf ઉદાર છે.

Watch out

Marks ક્યાં કપાય છે

ખોટી જોડીઓ: floats સાથે %d, ints સાથે %f, બંને બાજુ garbage. char પર %c વિરુદ્ધ %d બદલી નાખે છે કે શું દેખાશે (A વિરુદ્ધ 65), જુઓ સવાલ કોને માગે છે. scanf માં એ ભૂલવું કે double માટે %lf જરૂરી છે. અને marks અપાવતી ચોખ્ખી ટેવ: %.2f 2 decimals print કરે છે (78.530000 ને બદલે 78.53), display round થાય છે, store કરેલી value અકબંધ રહે છે.

Theory

Unit 1 પૂરો થયો

જુઓ marks program હવે શું કરી શકે છે: typed ડબ્બા declare કરવા, input લેવું, operators થી ગણતરી, અને formatted marksheet print કરવી, Unit 1 નો દરેક topic હવે theory નહીં, અભ્યાસ છે. આજના code માં એક નાની ઝલક છૂપાયેલી હતી: %s માટે scanf ને & જોઈતું નથી, કારણ કે array નું નામ પહેલેથી એક સરનામું છે; એ રહસ્ય Unit 3 (arrays અને pointers) માં ખૂલે છે. હવે પછી: Unit 2, જ્યાં program નિર્ણય લેવાનું શરૂ કરે છે.

Summary

Key takeaways

  • Specifiers printf/scanf ને bytes વાંચવાની રીત કહે છે: %d int, %ld long, %f float, %lf double, %c char, %s string.
  • Mismatch = garbage: printf કંઈ convert કરતું નથી (%d સાથે 78.5 undefined છે).
  • scanf કડક છે (double માટે %lf જરૂરી); printf ઉદાર છે (%f બંને print કરે).
  • એ જ char પર %c વિરુદ્ધ %d: અક્ષર વિરુદ્ધ ASCII number.
  • %.2f DISPLAY ને 2 decimals સુધી છાંટે છે; store કરેલી value એ જ રહે છે.
  • યાદ રાખવાની યુક્તિ: આકારવાળી ખાલી જગ્યાઓ, પોશાક મહેમાન સાથે મેળ ખાવો જોઈએ.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Introduction to C language

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Type specifiers (formatting strings): %d, %ld, %f, %c, %s, %lf · Computer Programming and Programming Methodology (CPPM) · Gri-Learn