C program without title

I am writing a program "hello world" in C.

void main()
{ printf("Hello World"); }
// note that I haven't included any header file

The program compiles with a warning like

vikram@vikram-Studio-XPS-1645:~$ gcc hello.c 
hello.c: In function ‘main’:
hello.c:2:2: warning: incompatible implicit declaration of built-in function ‘printf’
vikram@vikram-Studio-XPS-1645:~$ ./a.out 
Hello Worldvikram@vikram-Studio-XPS-1645:~$

How is this possible? How does the OS link the library without including any header?

+3
source share
5 answers

The compiler creates your source file with a link to a function with a name printf(), not knowing what arguments it actually takes or what its return type is. The generated assembly contains pushline addresses "Hello World"in the static data area of ​​your program, and then callto printf.

printf C printf(). , (const char*), printf(), . , printf(), , int ( ), printf(); , printf() , undefined, , , .

: #include , , , , .

+6

printf C (libc ), ( gcc printf, ).

- , (.. " " ).

, , , .

+7

C, , , ​​. printf stdio.h.

C89 ( GNU C89, gcc), , , : foo, , :

 /* foo is a function with an unspecified number of arguments */
extern int foo();

, int , . (, printf), undefined.

C89/C90:

(C90, 6.7.1) " , , , , undefined.

, gcc C89 GNU C89: .

,

void main() { ... }

main ( , , , , ).

, :

int main(void) { ... }
+1

1 , ; .

stdio.h printf:

int printf(const char * restrict format, ...); // as of C99

printf , .

"" :

  • C89 , , int ;

  • printf int, , , printf .

, , int main(void) int main(int argc, char **argv); void main() , undefined ( , - - ).


  • ""; , , , , . , , , , .
+1

hello.c: 2: 2: : 'printf

, (stdio.h). C, 1999 .

, , , , C . , , .

0

Source: https://habr.com/ru/post/1623545/


All Articles