Question

#include <stdio.h>
main()

printf();

What is the connection between the preprocessor and the function?

Was it helpful?

Solution

When the C preprocessor reads the line #include <stdio.h>, it literally reads the file stdio.h from a system directory and replaces this line with the contents. Then the stdio.h contains the declarations of printf and other functions, that tells the C compiler that these functions exist in another file or library.

When you then use printf() in your code, the compiler knows about this function and knows that it doesn't have to worry about it. (If you didn't include stdio.h, however, the compiler wouldn't have known what the function looked like at all, which could have been troublesome and the compiler will complain about this.)

An example stdio.h file with printf could look something like this:

/* stdio.h */

// Declaration of printf
int printf(const char *format, ...);

// And also a bunch of other function declarations...

OTHER TIPS

The preprocessor directive #include just includes the source of the header files in the source file where they are included.

More information #include

So in this case #include will include the source of the stdio.h header file in your program.

Header file stdio.h contains the declarations of the standard Input output functions which are actually defined in libc which is loaded as a shared library in your programs address space by the Dynamic Linker.

The <stdio.h> header defines the following macro names as positive integral constant expressions:

void     clearerr(FILE *);
char    *ctermid(char *);
char    *cuserid(char *);(LEGACY)
int      fclose(FILE *);
FILE    *fdopen(int, const char *);
int      feof(FILE *);
int      ferror(FILE *);
int      fflush(FILE *);
int      fgetc(FILE *);
int      fgetpos(FILE *, fpos_t *);
char    *fgets(char *, int, FILE *);
int      fileno(FILE *);
void     flockfile(FILE *);
FILE    *fopen(const char *, const char *);
int      fprintf(FILE *, const char *, ...);
int      fputc(int, FILE *);
int      fputs(const char *, FILE *);
size_t   fread(void *, size_t, size_t, FILE *);
FILE    *freopen(const char *, const char *, FILE *);
int      fscanf(FILE *, const char *, ...);
int      fseek(FILE *, long int, int);
int      fseeko(FILE *, off_t, int);
int      fsetpos(FILE *, const fpos_t *);
long int ftell(FILE *);
off_t    ftello(FILE *);
int      ftrylockfile(FILE *);
void     funlockfile(FILE *);
size_t   fwrite(const void *, size_t, size_t, FILE *);
int      getc(FILE *);
int      getchar(void);
int      getc_unlocked(FILE *);
int      getchar_unlocked(void);
int      getopt(int, char * const[], const char); (LEGACY)
char    *gets(char *);
int      getw(FILE *);
int      pclose(FILE *);
void     perror(const char *);
FILE    *popen(const char *, const char *);
int      printf(const char *, ...);
int      putc(int, FILE *);
int      putchar(int);
int      putc_unlocked(int, FILE *);
int      putchar_unlocked(int);
int      puts(const char *);
int      putw(int, FILE *);
int      remove(const char *);
int      rename(const char *, const char *);
void     rewind(FILE *);
int      scanf(const char *, ...);
void     setbuf(FILE *, char *);
int      setvbuf(FILE *, char *, int, size_t);
int      snprintf(char *, size_t, const char *, ...);
int      sprintf(char *, const char *, ...);
int      sscanf(const char *, const char *, int ...);
char    *tempnam(const char *, const char *);
FILE    *tmpfile(void);
char    *tmpnam(char *);
int      ungetc(int, FILE *);
int      vfprintf(FILE *, const char *, va_list);
int      vprintf(const char *, va_list);
int      vsnprintf(char *, size_t, const char *, va_list);
int      vsprintf(char *, const char *, va_list);

And as you see printf() is defined in the <Stdio.h>

The compiler needs to know what the arguments and their types are, for the function printf(). The programmer tells it that information using a declaration. The standard C library uses a standardized collection of header files that contain declarations for the functions in the standard C library. printf() is one of those, and it's declaration is in stdio.h. If you're on Linux, whenever you use a standard C library function, you can look up it's man page, and find the appropriate header file to use to #include the declarations for the function you're using. There are a few categories of functions which have their respective declarations aggregated into a much smaller number of more or less appropriately named header files.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top