3

I am using the atoi() function which I know is a part of stdlib.h. Then why is the following code working correctly when I have not included the required header file?

#define _CRT_SECURE_NO_wARNINGS
#include <stdio.h>
int main()
{

    char y[10] = "1234";
    int z = atoi(y);
    printf("%d\n", z);
    return 0;
}
Sled
  • 18,541
  • 27
  • 119
  • 168
user2684198
  • 812
  • 1
  • 10
  • 18
  • 1
    I think you will find answer http://stackoverflow.com/questions/4800102/not-including-stdlib-h-does-not-produce-any-compiler-error – Jimilian Feb 19 '14 at 18:31

1 Answers1

3

If the compiler detects a function in use, not being prototyped, it assumes int as return value. Lucky atoi() returns ìnt, so as the Standard Library is linked by default the symbol atoi() is resolved by the linker successfully.

If you'd had made your compiler log warnings (options -Wall -Wextras -pedantic for gcc) it would have notified you about the missing prototype for atoi().


Btw: It should be

int main(void)

at least.

alk
  • 69,737
  • 10
  • 105
  • 255