Thread: Ignoring the stdio.h file in a c file

  1. #1
    Registered User Raj 89's Avatar
    Join Date
    Nov 2012
    Chennai, TamilNadu

    Ignoring the stdio.h file in a c file

    I am facing a problem in the below given code.

    int main()
    printf("\nHello Programmers\n\n") ;
    return 0 ;

    In the above mentioned code i left including "#include ". And if i compile and execute this piece of code, the output is printed as expected. But "#include " being the most important thing in a C program, i have ignored it and still the compilation is done without any errors but with warning.
    Why is this happening?

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    What compiler are you using? It might have to do with linking parameters in the command line arguments that were used when building the application.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > And if i compile and execute this piece of code, the output is printed as expected.
    > Why is this happening?

    Without the include file, C will just generate a prototype for printf the first time you call it.
    Generally, this will be
    int printf();

    Now this is "close enough" to the real behaviour of printf to give you the illusion of success.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2012
    Calling a function that accpes a variable number of arguments without a prototype in scope is Undefined Behaviour.

    According to language rules, the compiler can do whatever it wants (it can emit an executable without any message, it can emit an executable that transfers money from your bank account to mine when run, it can give transfer the money itself, it can say that roses are red, ...)

    If the compiler produced an executable, running the executable can behave just like the user expects, it can behave almost like the user expects creating very very hard to track problems, it can format the hard disk, ...

    Do not write code with Undefined Behaviour!

  5. #5
    Registered User
    Join Date
    Nov 2012
    For educational purposes, you may perform the following experiment with implicit declarations:

    main(){    myfunc(1024); }
    #include <stdio.h>
    int myfunc(int a, int b, int c, int d)
        printf("This is myfunc speaking. You gave me the integers %d, %d, %d and %d.\n", a, b, c, d);
        return 123;
    Now compile myfunc.c into an object - it should compile without producing any warning

    gcc -Wall -Werror -Wextra -o myfunc.o -c myfunc.c

    Now compile main.c - turn on warnings but allow compiling even in case there are warnings:

    gcc -Wall -Wextra -o main main.c myfunc.o

    The program should run but the compiler had no way to verify you gave the correct number of arguments to myfunc(). Therefore, the values for b, c and d will be garbage values. In practice, the 1024 will be correctly printed, but I wouldn't count on this behavior. Don't use implicit declarations.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: stdio.h: No such file or directory
    By MindLess in forum C Programming
    Replies: 9
    Last Post: 12-20-2007, 08:11 AM
  2. Ignoring line in C++ when reading from file
    By falzone in forum C++ Programming
    Replies: 7
    Last Post: 11-16-2007, 12:08 AM
  3. conflict in FILE and stdio
    By George2 in forum C Programming
    Replies: 29
    Last Post: 10-12-2007, 02:23 AM
  4. stdio.h file loading/saveing
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 01-26-2002, 02:37 AM
  5. Ignoring a word from a .txt file
    By TrojanGekko in forum C++ Programming
    Replies: 1
    Last Post: 01-21-2002, 05:14 PM