Search code examples
c

return type defaults to 'int' [-Wimplicit-int]


This should be a simple program as I am new to programming but I can't get through this error, any help will be appreciated.

Here's my code:

#include <stdio.h>

main()
{
    int num;
    printf("Please provide a number\n");
    scanf(" %d", num);

    printf("The given num is %s", (num % 2 == 0) ? ("even") : ("odd"));

    return 0;
}

I am using CodeBlocks. The program runs fine until scanf as it prompts to enter a number. But, after I enter the number it gives error "untitled5.exe" has stopped working and when I look into the built message it says:

return type defaults to 'int' [-Wimplicit-int]


Solution

  • This warning/error is because this function main() does not specify its return value type. Add int before main() so first line should be int main().

    In addition, your crash is because you don't pass the right arguments to scanf(). If you want to have the user input go into num you should pass its address to scanf(), not the value of num itself. so the call should be: scanf("%d", &num);.

    The fixed version is this:

    #include <stdio.h>
    
    int main()
    {
        int num;
        printf("Please provide a number\n");
        scanf("%d", &num);
    
        printf("The given num is %s\n",(num%2==0)?("even"):("odd"));
    
        return 0;
    }
    

    Your error/warning and crash should be gone now.