cheaderdev-c++

Why my function in my program still running without including my library header file


My project have 3 file, which are main.c , and 2 files for my library (increase.h and increase.c). In my main.c these is a function I have writen in increase.c, when i delete #include "increase.h" in main, I expected the program to crash but somehow it still gave my correct answer.

Here is my program

main.c

#include <stdio.h>
#include <stdlib.h>

int main()
{
   int a =9,b= 5;
   printf("%d",inc(a));
  
}

increas.h

#ifndef INCREAS_H_INCLUDED
#define INCREAS_H_INCLUDED

int inc(int a);

#endif

increas.c

int inc(int a)
{
    return a+1;
}

Solution

  • I expected the program to crash but somehow it still gave my correct answer.

    because you do not have the function prototype the compiler is using the implict return and parameters type which is int. As your function returns int and takes int parameter - it works fine.

    Try to pass and return the pointer to int or double and your program will invoke undefined behaviour and most likely to stop working correctly.

    double inc(double a)
    {
        return a+1.0;
    }
    
    int main()
    {
       double a =9,b= 5;
       printf("%d",inc(a));
      
    }