Header files (STDIO, CONIO), pre-compiler directives (#include, #define)

Header files એ બની-બનાવેલી declarations ના toolbox છે (#include <stdio.h> printf અને scanf લઈ આવે છે), અને #include તથા #define જેવા preprocessor directives compiler પહેલાં ચાલે છે, તમારા source text ને edit કરતાં.

10 min read · 10 cards · 2 checks

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


Theory

એ line જે તમે 10 વાર type કરી ચૂક્યા છો

અત્યાર સુધી દરેક program #include <stdio.h> થી શરૂ થયો, વગર વિચાર્યે ભરોસે type કરેલો.

આજે એક experiment: એ line delete કરીને compile કરો. Compiler તરત વાંધો ઉઠાવે છે: "printf undeclared". Line પાછી લાવો, શાંતિ પાછી આવે છે.

એટલે printf ની આખી તાકાત કોઈક રીતે એ એક જ line માંથી આવે છે. ક્યાંથી, આવે છે શું, અને # થી શરૂ થતી lines ને process કોણ કરે છે? મળો preprocessor ને, એ કારીગર જે compiler વાંચે એ પહેલાં તમારો code edit કરે છે.

Theory

Toolbox અને નિયમોની દીવાલ

Mechanic દરેક કામ માટે spanner ઘડતો નથી; એ કહે છે "toolbox લઈ આવો". #include <stdio.h> બરાબર એ જ વાક્ય છે: standard input/output વાળો toolbox લઈ આવો, જેથી printf અને scanf ટેબલ પર આવી જાય. અને #define એ workshop ની દીવાલ પર લખેલો નિયમ છે ("PASS_MARKS એટલે 35"): એક વાર લખ્યો, અને દરેક કારીગર numbers ગોખવાને બદલે દીવાલ વાંચી લે છે.

Theory

Header files ની ચોખ્ખી વ્યાખ્યા

એક header file (.h) બની-બનાવેલી functions ની declarations રાખે છે, compiler માટે એમનું introduction card.

  • stdio.h (STanDard Input/Output): printf, scanf અને એમનો પરિવાર. બધે standard.
  • conio.h (CONsole I/O): getch() (એક keypress ની રાહ), clrscr() (screen સાફ કરવી). એક Turbo C જમાનાનું, non-standard header: GCC અને modern compilers એને ship કરતાં નથી, અને exams અપેક્ષે છે કે એ asterisk તમને ખબર હોય.

#include ખરેખર compilation પહેલાં header નું આખું content તમારી file માં paste કરી દે છે. એ રીતે જ printf જાણીતું બને છે.

Theory

Preprocessor અને #define

Preprocessor compiler પહેલાં ચાલે છે. એના directives # થી શરૂ થાય છે અને કોઈ semicolon લેતા નથી, એ text વિશેની સૂચનાઓ છે, statements નહીં.

#define PASS_MARKS 35 એક macro constant બનાવે છે: compile પહેલાં code નું દરેક PASS_MARKS textually 35 થી બદલાઈ જાય છે.

ફાયદો શું? code માં વેરવિખેર પડેલો ઉઘાડો 35 એ magic number છે: સમજાવ્યા વગરનો અને બદલવામાં જોખમી. નામવાળો constant પોતાની જાત સમજાવે છે, અને જ્યારે university passing ને 40 કરી નાખે, ત્યારે તમે ફક્ત એક line બદલો છો.

Practical

Marks program, પોતાની નિયમ-પોથી સાથે

#include <stdio.h>
#define PASS_MARKS 35
#define MAX_MARKS 100

int main() {
    int marks = 78;
    float percentage = (marks * 100.0) / MAX_MARKS;

    char result = (marks >= PASS_MARKS) ? 'P' : 'F';
    printf("Percentage: %f, Result: %c\n", percentage, result);
    return 0;
}

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

Quiz

ઉપરના program માંથી #include <stdio.h> delete કરીને compile કરો તો શું થશે?

  1. Compiler અટકી જાય છે: printf undeclared છે
  2. બરાબર compile થાય છે પણ કંઈ print કરતું નથી
  3. ફક્ત scanf બંધ થાય છે, printf બચી જાય છે
  4. કંઈ બદલાતું નથી, stdio.h તો બસ શણગાર છે
Show the answer

Compiler અટકી જાય છે: printf undeclared છે

stdio.h printf ની declaration, એનું introduction card, લઈ આવે છે. એના વગર compiler ને printf અજાણ્યા તરીકે મળે છે અને એ "undeclared" કહીને અટકે છે (કે જૂના setups પર implicit-declaration warnings). Header શણગાર નથી; એ જ રીતે બની-બનાવેલી functions વાપરવા લાયક બને છે. એ જ દશા scanf ની પણ, બંને stdio.h માં રહે છે.

Think first

છૂપા bug ને પકડો

એક student લખે છે: #define PASS_MARKS 35; (semicolon નોંધો) અને પછી if (marks >= PASS_MARKS). Compiler if વાળી line પર ગૂંચવતો error દેખાડે છે. Preprocessor ની જેમ વિચારો: if વાળી line બની શું?

Show the answer

Preprocessor semicolon સહિત textually બદલે છે: line બની જાય છે if (marks >= 35;), અને એ જ illegal છે. Error ખરી ભૂલથી દૂર દેખાય છે, એ જ એને ક્રૂર બનાવે છે. નિયમ: directives કોઈ semicolon લેતા નથી, કારણ કે એ text-editing ની સૂચનાઓ છે, C statements નહીં. આ જ bug spot-the-error નો માનીતો exam સવાલ છે.

Watch out

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

#include કે #define માં semicolon ઉમેરવો (ઉપરનો bug). conio.h ને standard કહેવું: એ Turbo C specific છે; "non-standard" કહો અને mark વસૂલો. #include "function નો code load કરે છે" એવું લખવું: એ declarations paste કરે છે; ખરો code LINKING પર આવે છે (pipeline યાદ કરો: એટલે જ printf નું body library માં રહેતું, તમારી .obj માં નહીં).

Theory

અત્યાર સુધીનો unit, જોડાયેલો

આજના code block ને જુઓ: pipeline, structure, variables, identifiers, types, operators અને directives, આ unit નો દરેક lesson એ 10 lines માં છે. હજી પણ આ program કંઈ પૂછી શકતું નથી: marks hard-coded છે. હવે પછીના 2 lessons એને કાન અને મોં આપે છે: scanf અને printf, C નાં input અને output statements.

Summary

Key takeaways

  • Header files બની-બનાવેલી functions ની declarations રાખે છે; #include એમને paste કરે છે.
  • stdio.h: printf/scanf (standard). conio.h: getch/clrscr (Turbo C, non-standard).
  • Preprocessor compiler પહેલાં ચાલે છે; directives # થી શરૂ અને કોઈ semicolon નહીં.
  • #define NAME value compile પહેલાં text બદલે છે: નામવાળા constants, કોઈ magic numbers નહીં.
  • એક #define ફેરફાર આખા program ને update કરે છે (PASS_MARKS 35 → 40).
  • Declarations headers માંથી આવે છે; function ના bodies linking પર આવે છે.
  • યાદ રાખવાની યુક્તિ: toolbox (#include) અને નિયમોની દીવાલ (#define).

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

Header files (STDIO, CONIO), pre-compiler directives (#include, #define) · Computer Programming and Programming Methodology (CPPM) · Gri-Learn