What is the use of #include <stdio.h> in a traditional Hello World program?

I was familiar with C programming for a long time. Recently I was combing my C skills and got stuck at one point - the most basic hello program. I was wondering what exactly the string is using #include<stdio.h>

in the following code.

/* Hello World Program */

#include<stdio.h>

main()
{
 printf("Hello World");
}

      

I read somewhere that when the pre-processor meets #include<stdio.h>

, it adds the contents of stdio.h to the original program.

Another article says that

It looks for the stdio.h file and copies it efficiently instead of these statements #include

. This file contains the so-called prototype functions of functions such as printf (), scanf (), ... so that the compiler knows what their parameters and return values ​​are.

So, I was just playing around with this simple world program. I created two versions of the code: -

  • hello1.c (same as above code)
  • hello2.c (excluding line #include<stdio.h>

    )

I have created object files as well as executables for the above programs. I noticed that the size of both hello1.o and hello2.o is the same. Likewise, the size of the executables hello1 and hello2 are also the same.

Now I doubt that if the articles I mentioned earlier were correct, then how are the sizes of both object files the same? In the hello1.c program , #include<stdio.h>

it should be replaced with function declarations, and this should result in a different file size for the object file than the one that excludes the header file ( hello2.o ).

I searched again for a while and found another article that says:

The header file simply tells the compiler what types of external functions and variables are, defines macros, types, etc. Nothing is copied . Any functions and variables (called external symbols) that are referenced in the source file will be linked during the linker phase.


Someone please suggest me which article is correct and which one is wrong.

Really appreciate your help. thanks in advance

+3


source to share


4 answers


The process of translating a C program into the binary of an object is done in two steps:

1) Pre-processing. The entire directive indicated by the initial is interpreted here #

. #include

among them. It copies the header file to an intermediate file (or just to memory) for further processing.

2) Compilation. Here is the intermediate result of the previous step, which actually translates to machine code.



There is also a third step when creating the executable:

3) Binding. It looks for referenced but not implemented functions within the generated object code, and then finds matching implementations inside other object files (if any) or related libraries.

When looking at the final code, the declarations (which are in the headers) do not affect the size, since they are needed so that the compiler only "knows" that there is such a function elsewhere, and it must be called with specific parameter calls, so that it can generate the calling code correctly. If you use a function without any prior declaration (and it happens when you don't include it stdio.h

), the compiler "guesses" the signature and gives you an "implicit declaration printf

" warning or similar, But at the end it is linked to the correct implementation printf

from the library, so the final code actually coincides with what y stdio.h

.

+3


source


Both are correct, depending on your point of view. What the preprocessor does on #include

is to include (literally) the contents of the specified file before passing it to the compiler. Your files on disk are of course not changed.

As for the size of your object file: The C header file usually does not contain any code, only declarations. If the header contains code, your object file will grow in size. But this is not normal use. For a library, the header file contains function prototype declarations (in order to find out why they are important), and optionally external variable declarations, as well as type definitions and preprocessor macros and definitions that can help (or even need to) use the library API.



Adding to this: try to compile your hello program without #include <stdio.h>

turning on all compiler warnings (for example with gcc

:) gcc -Wall -Wextra

- you will get an implicit function declaration warning printf()

. This is because the prototype for is printf()

declared in stdio.h

.

And adding a word of warning: even if you choose to ignore the compiler warnings, the implicit function declaration assumes it returns int

(for historical reasons). printf()

does so, so you will be fine. Other functions return something different and you will crash at runtime. Better to include all compiler warnings and if you come across an implicit declaration, make sure you are in the #include

header that declares the function you want to use.

+2


source


According to C11 (N1570) §7.21.1 / p1 I / O <stdio.h>

(selection):

The header <stdio.h>

defines several macros, but declares three types and many functions for performing input and output.

Macros and type definitions, and function declarations do not increase the size of the object code unless they are actually used in your code.

For example, if you are defining a type struct

, then this is not enough, you need to instantiate it. The following code:

typedef struct car {
    int numberOfWheels; 
} Car;

int main(void)
{
    return 0;
}

      

will create the same assembly as for:

int main(void)
{
    return 0;
}

      

Note that as of the C99 standard, it is not valid not to specify any declaration for a function that is called inside a translation unit. In C89, the compiler will accept an implicit function declaration, where:

  • Returns the type int

  • actual arguments may be subject to default promotions
0


source


Let's start by looking at the actual definition of the language :

5.1.1.2 Translation phases

1 The priority among the translation syntax rules is determined by the following phases. 6)

  • The multibyte characters of the physical source file are mapped, in an implementation-defined manner, to the source character set (newline insertion for end-of-line indicators) as needed. Trigraph sequences are replaced with their corresponding single character internal representations.
  • Each instance of a backslash character ( \

    ) followed by a newline character is removed, splicing the physical source strings to form logical source strings. Only the last backslash on any physical baseline should be eligible to be part of such a splice. A source file that is not empty must end with a newline character, and must not be immediately preceded by a backslash character before any such splicing occurs.
  • The source file is split into preprocessing tokens 7) and sequences of space characters (including comments). The source file must not end with a partial preprocessing token or partial comment. Each comment is replaced with one space. Newlines are preserved. Whether a sequence of non-newline whitespace characters is retained or replaced with a single whitespace character is implementation-defined.
  • Preprocessor directives are executed, macro calls are expanded, and unary operator expressions are executed _Pragma

    . If a sequence of characters that matches the syntax of the generic symbolic name produced by the concatenation token (6.10.3.3), the behavior is undefined. A #include

    preprocessing directive causes a named header or source file to be processed from phase 1 through phase 4, recursively.
    All preprocessing directives are then removed.
  • Each element of the source character set and escape sequence in character constants and string literals are converted to the corresponding element of the execution character set; if there is no matching element, it is converted to a non-null (wide) character implementation defined member. 8)
  • Adjacent string literal tokens are concatenated.
  • The white space symbols separating tokens are no longer meaningful. each preprocessing token is converted to a token. The resulting tokens are syntactically and semantically parsed and translated as a translation unit.
  • All references to external objects and functions are allowed. Library components related to the satisfaction of external references to functions and objects not defined in the current translation. All such a translator is output to a program image that contains the information needed to run in the runtime.

6) Implementations should behave as if these separate phases were occurring, although many usually add up together in practice. Source files, translation units, and translated translation units do not need to be stored as files, and no one-to-one correspondence between these objects and any external representation is required. The description is conceptual only and does not indicate any specific implementation.

7) As described in 6.4, the process of dividing the characters of the source files into preprocessing tokens is context sensitive. For example, see Processing <

in a preprocessor directive #include

.

8) An implementation must not convert all nonconforming original characters into the same rendition of a character.

...
6.10.2 Source File Inclusion

Constraints

1 Directive A #include

shall identify a header or source file that an implementation can handle.

Semantics

2 A form preprocessing directive

# include <h-char-sequence> new-line



looks for a sequence of implementation-defined places for a header, uniquely identified by a specified sequence between delimiters <

and >

and causes the directive to be replaced throughout the header content . How places or heading are specified is defined as implementation.

Added emphasis. This means that the preprocessor will take the original file

#include<stdio.h>

main()
{
 printf("Hello World");
}

      

and then generate the text

/* preprocessed contents of stdio.h here */

main()
{
  printf("Hello World");
}

      

which is fed directly to the compiler. Here is an example of my system (SLES 10, gcc 4.1.2):

/* hello.c */
#include <stdio.h>

int main(void)
{
  printf("Hello, stupid\n");
  return 0;
}

      

The preprocessor ( gcc -E

) output , slightly reformatted, will be prepared for scrolling for a while:

# 1 "hello.c"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "hello.c"
# 1 "/usr/include/stdio.h" 1 3 4
# 28 "/usr/include/stdio.h" 3 4
# 1 "/usr/include/features.h" 1 3 4
# 323 "/usr/include/features.h" 3 4
# 1 "/usr/include/sys/cdefs.h" 1 3 4
# 313 "/usr/include/sys/cdefs.h" 3 4
# 1 "/usr/include/bits/wordsize.h" 1 3 4
# 314 "/usr/include/sys/cdefs.h" 2 3 4
# 324 "/usr/include/features.h" 2 3 4
# 346 "/usr/include/features.h" 3 4
# 1 "/usr/include/gnu/stubs.h" 1 3 4
# 1 "/usr/include/bits/wordsize.h" 1 3 4
# 5 "/usr/include/gnu/stubs.h" 2 3 4
# 1 "/usr/include/gnu/stubs-64.h" 1 3 4
# 10 "/usr/include/gnu/stubs.h" 2 3 4
# 347 "/usr/include/features.h" 2 3 4
# 29 "/usr/include/stdio.h" 2 3 4
# 1 "/usr/lib64/gcc/x86_64-suse-linux/4.1.2/include/stddef.h" 1 3 4
# 214 "/usr/lib64/gcc/x86_64-suse-linux/4.1.2/include/stddef.h" 3 4
typedef long unsigned int size_t;
# 35 "/usr/include/stdio.h" 2 3 4
# 1 "/usr/include/bits/types.h" 1 3 4
# 28 "/usr/include/bits/types.h" 3 4
# 1 "/usr/include/bits/wordsize.h" 1 3 4
# 29 "/usr/include/bits/types.h" 2 3 4
# 1 "/usr/lib64/gcc/x86_64-suse-linux/4.1.2/include/stddef.h" 1 3 4
# 32 "/usr/include/bits/types.h" 2 3 4

typedef unsigned char __u_char;
typedef unsigned short int __u_short;
typedef unsigned int __u_int;
typedef unsigned long int __u_long;
typedef signed char __int8_t;
typedef unsigned char __uint8_t;
typedef signed short int __int16_t;
typedef unsigned short int __uint16_t;
typedef signed int __int32_t;
typedef unsigned int __uint32_t;
typedef signed long int __int64_t;
typedef unsigned long int __uint64_t;
typedef long int __quad_t;
typedef unsigned long int __u_quad_t;

# 134 "/usr/include/bits/types.h" 3 4
# 1 "/usr/include/bits/typesizes.h" 1 3 4
# 135 "/usr/include/bits/types.h" 2 3 4

typedef unsigned long int __dev_t;
typedef unsigned int __uid_t;
typedef unsigned int __gid_t;
typedef unsigned long int __ino_t;
typedef unsigned long int __ino64_t;
typedef unsigned int __mode_t;
typedef unsigned long int __nlink_t;
typedef long int __off_t;
typedef long int __off64_t;
typedef int __pid_t;
typedef struct { int __val[2]; } __fsid_t;
typedef long int __clock_t;
typedef unsigned long int __rlim_t;
typedef unsigned long int __rlim64_t;
typedef unsigned int __id_t;
typedef long int __time_t;
typedef unsigned int __useconds_t;
typedef long int __suseconds_t;
typedef int __daddr_t;
typedef long int __swblk_t;
typedef int __key_t;
typedef int __clockid_t;
typedef void * __timer_t;
typedef long int __blksize_t;
typedef long int __blkcnt_t;
typedef long int __blkcnt64_t;
typedef unsigned long int __fsblkcnt_t;
typedef unsigned long int __fsblkcnt64_t;
typedef unsigned long int __fsfilcnt_t;
typedef unsigned long int __fsfilcnt64_t;
typedef long int __ssize_t;
typedef __off64_t __loff_t;
typedef __quad_t *__qaddr_t;
typedef char *__caddr_t;
typedef long int __intptr_t;
typedef unsigned int __socklen_t;

# 37 "/usr/include/stdio.h" 2 3 4
typedef struct _IO_FILE FILE;

# 62 "/usr/include/stdio.h" 3 4

typedef struct _IO_FILE __FILE;

# 72 "/usr/include/stdio.h" 3 4
# 1 "/usr/include/libio.h" 1 3 4
# 32 "/usr/include/libio.h" 3 4
# 1 "/usr/include/_G_config.h" 1 3 4
# 14 "/usr/include/_G_config.h" 3 4
# 1 "/usr/lib64/gcc/x86_64-suse-linux/4.1.2/include/stddef.h" 1 3 4
# 326 "/usr/lib64/gcc/x86_64-suse-linux/4.1.2/include/stddef.h" 3 4

typedef int wchar_t;

# 355 "/usr/lib64/gcc/x86_64-suse-linux/4.1.2/include/stddef.h" 3 4

typedef unsigned int wint_t;

# 15 "/usr/include/_G_config.h" 2 3 4
# 24 "/usr/include/_G_config.h" 3 4
# 1 "/usr/include/wchar.h" 1 3 4
# 48 "/usr/include/wchar.h" 3 4
# 1 "/usr/lib64/gcc/x86_64-suse-linux/4.1.2/include/stddef.h" 1 3 4
# 49 "/usr/include/wchar.h" 2 3 4
# 1 "/usr/include/bits/wchar.h" 1 3 4
# 51 "/usr/include/wchar.h" 2 3 4
# 76 "/usr/include/wchar.h" 3 4

typedef struct
{
  int __count;
  union
  {
    wint_t __wch;
    char __wchb[4];
  } __value;
} __mbstate_t;

# 25 "/usr/include/_G_config.h" 2 3 4

typedef struct
{
  __off_t __pos;
  __mbstate_t __state;
} _G_fpos_t;

typedef struct
{
  __off64_t __pos;
  __mbstate_t __state;
} _G_fpos64_t;

# 44 "/usr/include/_G_config.h" 3 4
# 1 "/usr/include/gconv.h" 1 3 4
# 28 "/usr/include/gconv.h" 3 4
# 1 "/usr/include/wchar.h" 1 3 4
# 48 "/usr/include/wchar.h" 3 4
# 1 "/usr/lib64/gcc/x86_64-suse-linux/4.1.2/include/stddef.h" 1 3 4
# 49 "/usr/include/wchar.h" 2 3 4
# 29 "/usr/include/gconv.h" 2 3 4
# 1 "/usr/lib64/gcc/x86_64-suse-linux/4.1.2/include/stddef.h" 1 3 4
# 32 "/usr/include/gconv.h" 2 3 4

enum
{
  __GCONV_OK = 0,
  __GCONV_NOCONV,
  __GCONV_NODB,
  __GCONV_NOMEM,
  __GCONV_EMPTY_INPUT,
  __GCONV_FULL_OUTPUT,
  __GCONV_ILLEGAL_INPUT,
  __GCONV_INCOMPLETE_INPUT,
  __GCONV_ILLEGAL_DESCRIPTOR,
  __GCONV_INTERNAL_ERROR
};

enum
{
  __GCONV_IS_LAST = 0x0001,
  __GCONV_IGNORE_ERRORS = 0x0002
};

struct __gconv_step;
struct __gconv_step_data;
struct __gconv_loaded_object;
struct __gconv_trans_data;
typedef int (*__gconv_fct) (struct __gconv_step *, struct __gconv_step_data *,
       __const unsigned char **, __const unsigned char *,
       unsigned char **, size_t *, int, int);
typedef wint_t (*__gconv_btowc_fct) (struct __gconv_step *, unsigned char);
typedef int (*__gconv_init_fct) (struct __gconv_step *);
typedef void (*__gconv_end_fct) (struct __gconv_step *);
typedef int (*__gconv_trans_fct) (struct __gconv_step *,
      struct __gconv_step_data *, void *,
      __const unsigned char *,
      __const unsigned char **,
      __const unsigned char *, unsigned char **,
      size_t *);
typedef int (*__gconv_trans_context_fct) (void *, __const unsigned char *,
       __const unsigned char *,
       unsigned char *, unsigned char *);
typedef int (*__gconv_trans_query_fct) (__const char *, __const char ***,
     size_t *);
typedef int (*__gconv_trans_init_fct) (void **, const char *);
typedef void (*__gconv_trans_end_fct) (void *);
struct __gconv_trans_data
{
  __gconv_trans_fct __trans_fct;
  __gconv_trans_context_fct __trans_context_fct;
  __gconv_trans_end_fct __trans_end_fct;
  void *__data;
  struct __gconv_trans_data *__next;
};

struct __gconv_step
{
  struct __gconv_loaded_object *__shlib_handle;
  __const char *__modname;
  int __counter;
  char *__from_name;
  char *__to_name;
  __gconv_fct __fct;
  __gconv_btowc_fct __btowc_fct;
  __gconv_init_fct __init_fct;
  __gconv_end_fct __end_fct;
  int __min_needed_from;
  int __max_needed_from;
  int __min_needed_to;
  int __max_needed_to;
  int __stateful;
  void *__data;
};

struct __gconv_step_data
{
  unsigned char *__outbuf;
  unsigned char *__outbufend;
  int __flags;
  int __invocation_counter;
  int __internal_use;
  __mbstate_t *__statep;
  __mbstate_t __state;
  struct __gconv_trans_data *__trans;
};

typedef struct __gconv_info
{
  size_t __nsteps;
  struct __gconv_step *__steps;
  __extension__ struct __gconv_step_data __data [];
} *__gconv_t;

# 45 "/usr/include/_G_config.h" 2 3 4

typedef union
{
  struct __gconv_info __cd;
  struct
  {
    struct __gconv_info __cd;
    struct __gconv_step_data __data;
  } __combined;
} _G_iconv_t;

typedef int _G_int16_t __attribute__ ((__mode__ (__HI__)));
typedef int _G_int32_t __attribute__ ((__mode__ (__SI__)));
typedef unsigned int _G_uint16_t __attribute__ ((__mode__ (__HI__)));
typedef unsigned int _G_uint32_t __attribute__ ((__mode__ (__SI__)));

# 33 "/usr/include/libio.h" 2 3 4
# 53 "/usr/include/libio.h" 3 4
# 1 "/usr/lib64/gcc/x86_64-suse-linux/4.1.2/include/stdarg.h" 1 3 4
# 43 "/usr/lib64/gcc/x86_64-suse-linux/4.1.2/include/stdarg.h" 3 4
typedef __builtin_va_list __gnuc_va_list;
# 54 "/usr/include/libio.h" 2 3 4
# 167 "/usr/include/libio.h" 3 4

struct _IO_jump_t; struct _IO_FILE;

# 177 "/usr/include/libio.h" 3 4

typedef void _IO_lock_t;
struct _IO_marker {
  struct _IO_marker *_next;
  struct _IO_FILE *_sbuf;
  int _pos;

# 200 "/usr/include/libio.h" 3 4

};


enum __codecvt_result
{
  __codecvt_ok,
  __codecvt_partial,
  __codecvt_error,
  __codecvt_noconv
};

# 268 "/usr/include/libio.h" 3 4

struct _IO_FILE {
  int _flags;
  char* _IO_read_ptr;
  char* _IO_read_end;
  char* _IO_read_base;
  char* _IO_write_base;
  char* _IO_write_ptr;
  char* _IO_write_end;
  char* _IO_buf_base;
  char* _IO_buf_end;
  char *_IO_save_base;
  char *_IO_backup_base;
  char *_IO_save_end;
  struct _IO_marker *_markers;
  struct _IO_FILE *_chain;
  int _fileno;
  int _flags2;
  __off_t _old_offset;
  unsigned short _cur_column;
  signed char _vtable_offset;
  char _shortbuf[1];
  _IO_lock_t *_lock;

# 316 "/usr/include/libio.h" 3 4

  __off64_t _offset;

# 325 "/usr/include/libio.h" 3 4
  void *__pad1;
  void *__pad2;
  void *__pad3;
  void *__pad4;
  size_t __pad5;
  int _mode;
  char _unused2[15 * sizeof (int) - 4 * sizeof (void *) - sizeof (size_t)];
};

typedef struct _IO_FILE _IO_FILE;
struct _IO_FILE_plus;
extern struct _IO_FILE_plus _IO_2_1_stdin_;
extern struct _IO_FILE_plus _IO_2_1_stdout_;
extern struct _IO_FILE_plus _IO_2_1_stderr_;
# 361 "/usr/include/libio.h" 3 4
typedef __ssize_t __io_read_fn (void *__cookie, char *__buf, size_t __nbytes);
typedef __ssize_t __io_write_fn (void *__cookie, __const char *__buf,
     size_t __n);
typedef int __io_seek_fn (void *__cookie, __off64_t *__pos, int __w);
typedef int __io_close_fn (void *__cookie);
# 413 "/usr/include/libio.h" 3 4
extern int __underflow (_IO_FILE *) __attribute__ ((__nothrow__));
extern int __uflow (_IO_FILE *) __attribute__ ((__nothrow__));
extern int __overflow (_IO_FILE *, int) __attribute__ ((__nothrow__));
extern wint_t __wunderflow (_IO_FILE *) __attribute__ ((__nothrow__));
extern wint_t __wuflow (_IO_FILE *) __attribute__ ((__nothrow__));
extern wint_t __woverflow (_IO_FILE *, wint_t) __attribute__ ((__nothrow__));
# 451 "/usr/include/libio.h" 3 4
extern int _IO_getc (_IO_FILE *__fp) __attribute__ ((__nothrow__));
extern int _IO_putc (int __c, _IO_FILE *__fp) __attribute__ ((__nothrow__));
extern int _IO_feof (_IO_FILE *__fp) __attribute__ ((__nothrow__));
extern int _IO_ferror (_IO_FILE *__fp) __attribute__ ((__nothrow__));
extern int _IO_peekc_locked (_IO_FILE *__fp) __attribute__ ((__nothrow__));
extern void _IO_flockfile (_IO_FILE *) __attribute__ ((__nothrow__));
extern void _IO_funlockfile (_IO_FILE *) __attribute__ ((__nothrow__));
extern int _IO_ftrylockfile (_IO_FILE *) __attribute__ ((__nothrow__));
# 481 "/usr/include/libio.h" 3 4
extern int _IO_vfscanf (_IO_FILE * __restrict, const char * __restrict,
   __gnuc_va_list, int *__restrict);
extern int _IO_vfprintf (_IO_FILE *__restrict, const char *__restrict,
    __gnuc_va_list);
extern __ssize_t _IO_padn (_IO_FILE *, int, __ssize_t) __attribute__ ((__nothrow__));
extern size_t _IO_sgetn (_IO_FILE *, void *, size_t) __attribute__ ((__nothrow__));
extern __off64_t _IO_seekoff (_IO_FILE *, __off64_t, int, int) __attribute__ ((__nothrow__));
extern __off64_t _IO_seekpos (_IO_FILE *, __off64_t, int) __attribute__ ((__nothrow__));
extern void _IO_free_backup_area (_IO_FILE *) __attribute__ ((__nothrow__));
# 73 "/usr/include/stdio.h" 2 3 4
# 86 "/usr/include/stdio.h" 3 4
typedef _G_fpos_t fpos_t;
# 138 "/usr/include/stdio.h" 3 4
# 1 "/usr/include/bits/stdio_lim.h" 1 3 4
# 139 "/usr/include/stdio.h" 2 3 4
extern struct _IO_FILE *stdin;
extern struct _IO_FILE *stdout;
extern struct _IO_FILE *stderr;
extern int remove (__const char *__filename) __attribute__ ((__nothrow__));
extern int rename (__const char *__old, __const char *__new) __attribute__ ((__nothrow__));
extern FILE *tmpfile (void);
# 185 "/usr/include/stdio.h" 3 4
extern char *tmpnam (char *__s) __attribute__ ((__nothrow__));
extern char *tmpnam_r (char *__s) __attribute__ ((__nothrow__));
# 203 "/usr/include/stdio.h" 3 4
extern char *tempnam (__const char *__dir, __const char *__pfx)
     __attribute__ ((__nothrow__)) __attribute__ ((__malloc__));
extern int fclose (FILE *__stream);
extern int fflush (FILE *__stream);
# 228 "/usr/include/stdio.h" 3 4
extern int fflush_unlocked (FILE *__stream);
# 228 "/usr/include/stdio.h" 3 4
extern int fflush_unlocked (FILE *__stream);
# 242 "/usr/include/stdio.h" 3 4
extern FILE *fopen (__const char *__restrict __filename,
      __const char *__restrict __modes);
extern FILE *freopen (__const char *__restrict __filename,
        __const char *__restrict __modes,
        FILE *__restrict __stream);
# 269 "/usr/include/stdio.h" 3 4
# 280 "/usr/include/stdio.h" 3 4
extern FILE *fdopen (int __fd, __const char *__modes) __attribute__ ((__nothrow__));
# 306 "/usr/include/stdio.h" 3 4
extern void setbuf (FILE *__restrict __stream, char *__restrict __buf) __attribute__ ((__nothrow__));
extern int setvbuf (FILE *__restrict __stream, char *__restrict __buf,
      int __modes, size_t __n) __attribute__ ((__nothrow__));
extern void setbuffer (FILE *__restrict __stream, char *__restrict __buf,
         size_t __size) __attribute__ ((__nothrow__));
extern void setlinebuf (FILE *__stream) __attribute__ ((__nothrow__));
extern int fprintf (FILE *__restrict __stream,
      __const char *__restrict __format, ...);
extern int printf (__const char *__restrict __format, ...);
extern int sprintf (char *__restrict __s,
      __const char *__restrict __format, ...) __attribute__ ((__nothrow__));
extern int vfprintf (FILE *__restrict __s, __const char *__restrict __format,
       __gnuc_va_list __arg);
extern int vprintf (__const char *__restrict __format, __gnuc_va_list __arg);
extern int vsprintf (char *__restrict __s, __const char *__restrict __format,
       __gnuc_va_list __arg) __attribute__ ((__nothrow__));
extern int snprintf (char *__restrict __s, size_t __maxlen,
       __const char *__restrict __format, ...)
     __attribute__ ((__nothrow__)) __attribute__ ((__format__ (__printf__, 3, 4)));
extern int vsnprintf (char *__restrict __s, size_t __maxlen,
        __const char *__restrict __format, __gnuc_va_list __arg)
     __attribute__ ((__nothrow__)) __attribute__ ((__format__ (__printf__, 3, 0)));
# 400 "/usr/include/stdio.h" 3 4
extern int fscanf (FILE *__restrict __stream,
     __const char *__restrict __format, ...) ;
extern int scanf (__const char *__restrict __format, ...) ;
extern int sscanf (__const char *__restrict __s,
     __const char *__restrict __format, ...) __attribute__ ((__nothrow__));
# 442 "/usr/include/stdio.h" 3 4
extern int fgetc (FILE *__stream);
extern int getc (FILE *__stream);
extern int getchar (void);
# 466 "/usr/include/stdio.h" 3 4
extern int getc_unlocked (FILE *__stream);
extern int getchar_unlocked (void);
# 477 "/usr/include/stdio.h" 3 4
extern int fgetc_unlocked (FILE *__stream);
extern int fputc (int __c, FILE *__stream);
extern int putc (int __c, FILE *__stream);
extern int putchar (int __c);
# 510 "/usr/include/stdio.h" 3 4
extern int fputc_unlocked (int __c, FILE *__stream);
extern int putc_unlocked (int __c, FILE *__stream);
extern int putchar_unlocked (int __c);
extern int getw (FILE *__stream);
extern int putw (int __w, FILE *__stream);
extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
     ;
extern char *gets (char *__s) ;
# 591 "/usr/include/stdio.h" 3 4
extern int fputs (__const char *__restrict __s, FILE *__restrict __stream);
extern int puts (__const char *__s);
extern int ungetc (int __c, FILE *__stream);
extern size_t fread (void *__restrict __ptr, size_t __size,
       size_t __n, FILE *__restrict __stream) ;
extern size_t fwrite (__const void *__restrict __ptr, size_t __size,
        size_t __n, FILE *__restrict __s) ;
# 644 "/usr/include/stdio.h" 3 4
extern size_t fread_unlocked (void *__restrict __ptr, size_t __size,
         size_t __n, FILE *__restrict __stream) ;
extern size_t fwrite_unlocked (__const void *__restrict __ptr, size_t __size,
          size_t __n, FILE *__restrict __stream) ;
extern int fseek (FILE *__stream, long int __off, int __whence);
extern long int ftell (FILE *__stream) ;
extern void rewind (FILE *__stream);
# 680 "/usr/include/stdio.h" 3 4
extern int fseeko (FILE *__stream, __off_t __off, int __whence);
extern __off_t ftello (FILE *__stream) ;
# 699 "/usr/include/stdio.h" 3 4
extern int fgetpos (FILE *__restrict __stream, fpos_t *__restrict __pos);
extern int fsetpos (FILE *__stream, __const fpos_t *__pos);
# 722 "/usr/include/stdio.h" 3 4
# 731 "/usr/include/stdio.h" 3 4
extern void clearerr (FILE *__stream) __attribute__ ((__nothrow__));
extern int feof (FILE *__stream) __attribute__ ((__nothrow__)) ;
extern int ferror (FILE *__stream) __attribute__ ((__nothrow__)) ;
extern void clearerr_unlocked (FILE *__stream) __attribute__ ((__nothrow__));
extern int feof_unlocked (FILE *__stream) __attribute__ ((__nothrow__)) ;
extern int ferror_unlocked (FILE *__stream) __attribute__ ((__nothrow__)) ;
extern void perror (__const char *__s);
# 1 "/usr/include/bits/sys_errlist.h" 1 3 4
# 27 "/usr/include/bits/sys_errlist.h" 3 4
extern int sys_nerr;
extern __const char *__const sys_errlist[];
# 761 "/usr/include/stdio.h" 2 3 4
extern int fileno (FILE *__stream) __attribute__ ((__nothrow__)) ;
extern int fileno_unlocked (FILE *__stream) __attribute__ ((__nothrow__)) ;
# 780 "/usr/include/stdio.h" 3 4
extern FILE *popen (__const char *__command, __const char *__modes) ;
extern int pclose (FILE *__stream);
extern char *ctermid (char *__s) __attribute__ ((__nothrow__));
# 820 "/usr/include/stdio.h" 3 4
extern void flockfile (FILE *__stream) __attribute__ ((__nothrow__));
extern int ftrylockfile (FILE *__stream) __attribute__ ((__nothrow__)) ;
extern void funlockfile (FILE *__stream) __attribute__ ((__nothrow__));
# 850 "/usr/include/stdio.h" 3 4
# 2 "hello.c" 2
int main(void)
{
  printf("Hello, stupid\n");
  return 0;
}

      

Then the output at the preprocessor stage is fed to the parser (stage 7); whether the preprocessor output will be stored in a temporary file or not before being passed to the parser prior to implementation.

Please note that stdio.h

contains other header files ( features.h

, types.h

, libio.h

etc.), which may include other header files in turn. The resulting result is a sediment of all type definitions and external (non-defining) functions and object declarations specified in those headers.

None of this precipitation lingers in the actual object file; the only things that take up space in the object file are function and object definitions such as function body main

, string literal and implementation printf

(along with any other objects and machine code required by the OS to make this running program).

Given that a simple one main()

compiled with no problem, you are obviously using a C89 or earlier compiler that allowed implicit declarations int

; IOW, if the compiler saw a function call for which there was no corresponding function declaration or definition without a type specifier, it assumed that the function was returned int

. Since it printf

returns int

, your version is stdio.h

uncompiled and linked without issue. If you used a C99 or later compiler, or called a library function that did not return int

, you should get a compile-time diagnostic.

0


source







All Articles