3

I've been introduced to C programming a long while ago. Recently I was brushing up my C skills and stuck at one point - the very basic hello world program. I was wondering what exactly is the use of #include<stdio.h> line in the following code.

/* Hello World Program */

#include<stdio.h>

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

I read somewhere that when the pre-processor encounters #include<stdio.h> , it adds the contents of the stdio.h header file to the source program .

Another article said that

It looks for the stdio.h file and effectively copy-pastes it in the place of this #include statements. This file contains so-called function prototypes of functions such as printf(), scanf(), ... so that compiler knows what are their parameters and return values.

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

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

I created object files as well as executables for the above programs. I observed that the size of both hello1.o and hello2.o is the same. Similarly the size of executables hello1 and hello2 is also the same.

Now my doubt is that if the articles I mentioned earlier were correct, how come the sizes of both the object files is the same? In the program hello1.c ,#include<stdio.h> should be replaced with the function declarations and this should result in a different file size for the object file as compared to the one excluding the header file (hello2.o).

I again searched for a while and found another article which says,

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


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

Really appreciate your help.Thanks in advance

Community
  • 1
  • 1
code kudzu
  • 31
  • 4
  • Header files like `stdio.h` don't (usually) contain the actual functions... they contain prototypes, which are a description of how to interact with the function (what kinds of arguments it takes, what kind of value it returns, etc.). Including the header doesn't add more code to your executable/object file, it just makes sure the generated code calls the library functions (like `printf()`) the proper way, and helps the compiler find mistakes in your code. – Dmitri Jul 27 '15 at 19:45
  • Function *declarations* take up no space in the generated object code; they're only used during the translation phase to make sure that function *calls* match up with function *definitions* (right return type, right number and types of parameters). – John Bode Jul 27 '15 at 19:48
  • You say you created an executable with that code minus the #include , surely that will not compile though? It would complain that printf had not been declared. – Sam Redway Jul 27 '15 at 19:48
  • 3
    @SamRedway: `printf` returns `int`, so if he's using a C89 compiler an explicit declaration isn't required. Note that this *would* be a compile-time error under C99 and later compilers. – John Bode Jul 27 '15 at 19:50
  • function 'prototypes' NOT function declarations, do not add anything to the program size, they are directions to the compiler about the parameters and return types of functions. The stdio.h file is filled with prototypes and #defines and #ifdef types of statements and struct definitions. None of these add to the program size. Either you are using an old compiler, that assumes parameters and return types are int -or- you have a newer compiler and have not enabled the warnings (for gcc, at a minimum use: '-Wall -Wextra -pedantic') – user3629249 Jul 27 '15 at 20:10
  • @SamRedway Indeed I compiled the program and there was no error. Some warning might have popped up, which I dont remember. It's been few days since I did this. – code kudzu Jul 27 '15 at 20:23
  • @JohnBode Thnx guys. You talking about old and new compilers... That's all greek to me. I guess I need to google more about the things you've suggested. – code kudzu Jul 27 '15 at 20:25
  • Of course it will compile. *implicit function declaration* is deprecated, but not forbidden. On a side note: Why on earth is the typical *hello world* example using `printf()` instead of `puts()`? –  Jul 27 '15 at 20:27
  • @FelixPalmen Ha ha.. no specific reason dude. Except that it's been a while I worked on a C program and all I remember is printf() instead of puts(). – code kudzu Jul 27 '15 at 20:30
  • @codekudzu I didn't mean you specifically ... you will see this *`printf()`-helloworld* in a lot of text books. IMHO it's *BS* because `printf()` is for printing formatted values (hence the `f`). I wonder why this is used so much in *helloworld* examples.... –  Jul 27 '15 at 20:42
  • @FelixPalmen No offense taken dude.. That was ok. I didnt get it wrong. – code kudzu Jul 28 '15 at 11:15

4 Answers4

3

The process of translating the C program into the binary object code is done by two steps:

1) Preprocessing. This is where all of the directive denoted with initial # are interpreted. #include is among them. It is copying the header file into an intermediate file (or just to memory) for further processing.

2) Compilation. Here is the intermediate result of the previous step is actually gets translated into the machine code.

When creating an executable there is also a third step:

3) Linking. It is looking for referenced, but not implemented functions inside the generated object code, and then finding the corresponding implementations inside the other object files (if any) or linked libraries.

When looking at the final code, the declarations (which are found in the headers) are not affecting the size, since they are needed for the compiler only to "know" that there is such a function elsewhere, and it should be called with specific parameter calls, such that it can generate the calling code in the right way. If you are using a function without first declaring it (and it will happen when you are not including the stdio.h), the compiler is "guessing" about the signature and giving you a warning "implicit declaration of printf" or similar. But in the end, it is getting linked with correct printf implementation from a library, such that final code is effectively the same as with stdio.h.

Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61
2

Both are correct, depending on your point of view. What the preprocessor does on #include is to include (literally) the contents of the referred file before passing to the compiler. Your files on disk are of course not modified.

Regarding the size of your object file: a C header file typically doesn't contain any code, only declarations. If the header would contain code, the size of your object file would increase. But that's not the normal use. For a library, the header file contains declarations of function prototypes (google for that to learn why they are important) and optionally declarations of external variables as well as type definitions and preprocessor macros and defines which might help (or even be necessary to) use the library's API.

Adding to that: try to compile your hello program without #include <stdio.h> enabling all compiler warnings (e.g. with gcc: gcc -Wall -Wextra) -- you will get a warning about an implicit function declaration of printf(). That's because the prototype for printf() is declared in stdio.h.

And adding a word of warning: even if you chose to ignore compiler warnings, implicitly declaring a function assumes it returns int (for historical reasons). printf() does that, so you'll be fine in that case. Other functions return something different and you will experience a runtime crash. Better enable all compiler warnings and if you encounter an implicit declaration warning, make sure you #include the header declaring the function you want to use.

  • Thanks for your valuable time and suggestions. I guess I need to look more into headers, data and other sections of object and executable files. – code kudzu Jul 27 '15 at 20:18
0

According to C11 (N1570) §7.21.1/p1 Input/output <stdio.h> (emphasis mine):

The header <stdio.h> defines several macros, and declares three types and many functions for performing input and output.

Macros and type definitions as well as function declaration don't increase size of object code, unless they are actually used in your code.

For example, if you define a struct type, then it is not enough, you need to create an instance of it. The following code:

typedef struct car {
    int numberOfWheels; 
} Car;

int main(void)
{
    return 0;
}

would generate the same assembly as for:

int main(void)
{
    return 0;
}

Note, that since C99 standard it is invalid not to supply any declaration for function, that is called within single translation unit. In C89, compiler would assume implicit function declaration, where:

  • return type is int
  • actual arguments are subject to default argument promotions
Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
0

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

5.1.1.2 Translation phases

1 The precedence among the syntax rules of translation is specified by the following phases.6)

  1. Physical source file multibyte characters are mapped, in an implementation- defined manner, to the source character set (introducing new-line characters for end-of-line indicators) if necessary. Trigraph sequences are replaced by corresponding single-character internal representations.
  2. Each instance of a backslash character (\) immediately followed by a new-line character is deleted, splicing physical source lines to form logical source lines. Only the last backslash on any physical source line shall be eligible for being part of such a splice. A source file that is not empty shall end in a new-line character, which shall not be immediately preceded by a backslash character before any such splicing takes place.
  3. The source file is decomposed into preprocessing tokens7) and sequences of white-space characters (including comments). A source file shall not end in a partial preprocessing token or in a partial comment. Each comment is replaced by one space character. New-line characters are retained. Whether each nonempty sequence of white-space characters other than new-line is retained or replaced by one space character is implementation-defined.
  4. Preprocessing directives are executed, macro invocations are expanded, and _Pragma unary operator expressions are executed. If a character sequence that matches the syntax of a universal character name is produced by token concatenation (6.10.3.3), the behavior is undefined. A #include preprocessing directive causes the named header or source file to be processed from phase 1 through phase 4, recursively. All preprocessing directives are then deleted.
  5. Each source character set member and escape sequence in character constants and string literals is converted to the corresponding member of the execution character set; if there is no corresponding member, it is converted to an implementationdefined member other than the null (wide) character.8)
  6. Adjacent string literal tokens are concatenated.
  7. White-space characters separating tokens are no longer significant. Each preprocessing token is converted into a token. The resulting tokens are syntactically and semantically analyzed and translated as a translation unit.
  8. All external object and function references are resolved. Library components are linked to satisfy external references to functions and objects not defined in the current translation. All such translator output is collected into a program image which contains information needed for execution in its execution environment.

6) Implementations shall behave as if these separate phases occur, even though many are typically folded together in practice. Source files, translation units, and translated translation units need not necessarily be stored as files, nor need there be any one-to-one correspondence between these entities and any external representation. The description is conceptual only, and does not specify any particular implementation.

7) As described in 6.4, the process of dividing a source file’s characters into preprocessing tokens is context-dependent. For example, see the handling of < within a #include preprocessing directive.

8) An implementation need not convert all non-corresponding source characters to the same execution character.

...
6.10.2 Source file inclusion

Constraints

1 A #include directive shall identify a header or source file that can be processed by the implementation.

Semantics

2 A preprocessing directive of the form

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

searches a sequence of implementation-defined places for a header identified uniquely by the specified sequence between the < and > delimiters, and causes the replacement of that directive by the entire contents of the header. How the places are specified or the header identified is implementation-defined.

Emphasis added. What this means is that the preprocessor will take the source 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's an example on my system (SLES 10, gcc 4.1.2):

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

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

Preprocessor output (gcc -E), slightly reformatted, be prepared to scroll 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;
}

The output of the preprocessor stages is then fed to the parser (stage 7); whether the preprocessor output is saved to a temporary file or not before being fed to the parser is up to the implementation.

Note that stdio.h includes other header files (features.h, types.h, libio.h, etc.), which may include other header files in turn. The resulting output is a sludge of all the type definitions and external (non-defining) function and object declarations specified in those headers.

None of that sludge winds up in the actual object file; the only things that take up space in the object file are the function and object definitions, such as your main function body, the string literal, and the implementation of printf (along with whatever other objects and machine code are required by the OS to make this a runnable program).

Given that a simple main() compiled with no issues, you're obviously using a C89 or earlier compiler, which allowed implicit int declarations; IOW, if the compiler saw a function call for which there was no corresponding declaration, or a function definition with no type specifier, it assumed the function returned int. Since printf returns an int, your version without stdio.h compiled and linked with no issues. If you were using a C99 or later compiler, or calling a library function that did not return int, you would get a diagnostic at compile time.

John Bode
  • 119,563
  • 19
  • 122
  • 198