Test your C knowledge

This is a discussion on Test your C knowledge within the C Programming forums, part of the General Programming Boards category; Just having fun with this... What's wrong with this code while compiling on a C89 compiler? Code: #include <stdio.h> #include ...

  1. #1
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283

    Test your C knowledge

    Just having fun with this... What's wrong with this code while compiling on a C89 compiler?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    
    add(a, b)
    int a;
    int b;
    { 
       return a + b;
    } 
    
    // main entry point
    main() {
       int num;
       int array[5];
    
       num = add(5, 5);
    
       3[array] = num;
     
       printf("%d\n", array[3]);
    
       return 0;
    }

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    For one, stdbool.h is not a C90 header.
    This code also uses pre-ISO code. Not a good sign.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    hahaha. Interesting how it still compiles without the header.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Obviously it doesn't use the bool datatype.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Quote Originally Posted by dxfoo
    Interesting how it still compiles without the header.
    It can compile with that header inclusion too, of course.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    Well it's a C99 header, so it depends on the compiler. The one I'm using doesn't contain stdbool.h.

  7. #7
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    963
    That's some scary looking code.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Quote Originally Posted by dxfoo
    Well it's a C99 header, so it depends on the compiler. The one I'm using doesn't contain stdbool.h.
    All you need to do is to configure the compiler to include in its search for system headers a directory where you have a blank file named stdbool.h
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    C89 doesn't support commenting using the two slashes. Use /* main entry point */ instead.

  10. #10
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    That's another one - good!

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Quote Originally Posted by Babkockdood
    C89 doesn't support commenting using the two slashes. Use /* main entry point */ instead.
    Yes. Fascinating that I missed that, considering that it is my own practice and it was asked on this forum recently.

    dxfoo: out of curiosity, did you write this program yourself? Are you aware that this exercise is completely pointless except as a trivia question in a written test or an interview (in which case it would probably be a bad interview question for most job scopes), as compilers can be used to more or less find the answers?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
       3[array] = num;
    This is very out-dated...
    Every(almost all) C programmers know that after reading c-faq or IOCC code.

  13. #13
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    Code:
    add(a, b)
    int a;
    int b;
    { 
       return a + b;
    }
    That's an odd way to declaring this function. Should be:
    Code:
    add(int a,int b)
    { 
       return a + b;
    }

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That's a deprecated style, even in Turbo C/C++ ver. 1.01, for crying out loud. That's how we did it in my C class, before the last ice age.

  15. #15
    Registered User
    Join Date
    Jul 2007
    Posts
    131
    Quote Originally Posted by Bayint Naung View Post
    This is very out-dated...
    Every(almost all) C programmers know that after reading c-faq or IOCC code.
    What do you mean with out-dated? That is correct both syntatically and semantically though not usual style.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Online Test Your Knowledge..
    By darren78 in forum C++ Programming
    Replies: 2
    Last Post: 08-13-2009, 09:02 AM
  2. Easy to use random number test suite?
    By Sebastiani in forum General Discussions
    Replies: 3
    Last Post: 07-12-2009, 08:17 AM
  3. sustring test puzzle
    By WDT in forum C# Programming
    Replies: 3
    Last Post: 06-29-2009, 07:19 AM
  4. Why is my program freezing?
    By ShadowMetis in forum Windows Programming
    Replies: 8
    Last Post: 08-20-2004, 03:20 PM
  5. Question About Linker Errors In Dev-C++
    By ShadowMetis in forum C++ Programming
    Replies: 9
    Last Post: 08-18-2004, 08:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21