Thread: Test your C knowledge

  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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    28,413
    Quote Originally Posted by dxfoo
    Interesting how it still compiles without the header.
    It can compile with that header inclusion too, of course.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    That's some scary looking code.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    486
    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.

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