Strange C programming questions

This is a discussion on Strange C programming questions within the C Programming forums, part of the General Programming Boards category; consider the following question on C programming: Code: void call(int,int,int); void main(){ int a=10; call(a,a++,++a); } void call(int x,int y,int ...

  1. #1
    Registered User BlackOps's Avatar
    Join Date
    Jul 2009
    Location
    AZERBAIJAN
    Posts
    78

    Strange C programming questions

    consider the following question on C programming:

    Code:
    void call(int,int,int);
    
    void main(){
    
    int a=10;
    
    call(a,a++,++a);
    
    }
    
    void call(int x,int y,int z){
    
    printf("%d %d %d",x,y,z);
    
    }
    I thought the output is 10 10 12, but that was wrong... and in the explanation they said that the variables are evaluated from right to left, so it is 12 11 11... And before the exercises they said that the Turbo C++ compiler version 3.0 was used...

    however my tcc (Tiny C compiler) whowed result of 12 10 12...

    So i think its pretty wrong to ask such kind of questions huh? Cuz it is dependend on implementation of compiler?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    That behaviour is undefined or unspecified. Just don't do it and you'll be fine.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,406
    Quote Originally Posted by BlackOps
    So i think its pretty wrong to ask such kind of questions huh? Cuz it is dependend on implementation of compiler?
    Yes, it is a bad question, unless the expected answer is "the behaviour is undefined".
    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

  4. #4
    Registered User BlackOps's Avatar
    Join Date
    Jul 2009
    Location
    AZERBAIJAN
    Posts
    78
    or for example this one:
    Code:
           int a=5 ,b,c=7;
           printf("%d  %d   %d");
    i thought output will be 7 garbage 5, and that is what my Tiny C compiler gave.

    But the answer of question was: Output: 7 5 garbage value. Automatic variable a and c has initialized while b has not initialize. Initialize variable are more nearer than non initialize variable .They will be stored in the stack. So due to LIFO first output will be 7 then 6 (since a is more nearer than b with respect to c) then any garbage value will be output which is present in the stack.

    so?... so u also think that all those questions in internet are not good huh?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,406
    Quote Originally Posted by BlackOps
    But the answer of question was: Output: 7 5 garbage value.
    The correct answer is that the behaviour is undefined. In fact, a quick check with the MinGW port of gcc 3.4.5 shows a different output than the one cited in the answer, and that is undeniable proof that the answer is wrong. Okay, they could claim that the compiler was wrong, but the compiler vendor just needs to cite the C standard and they fail. Epic fail.

    Quote Originally Posted by BlackOps
    so?... so u also think that all those questions in internet are not good huh?
    I would not venture to say that all of them are not good, but apparently those that you have encountered are either exceptionally bad, or have exceptionally bad "model 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

  6. #6
    Registered User BlackOps's Avatar
    Join Date
    Jul 2009
    Location
    AZERBAIJAN
    Posts
    78
    Oh and here is the other one:

    Code:
    register int i,x;
    
    scanf("%d",&i);
    
    x=++i + ++i + ++i;
    
    printf("%d",x);
    their asnwer is: Compiler error. And the explanation is: In c register variable stores in CPU it doesn’t store in RAM. So register variable have not any memory address. So it is illegal to write &a.

    But my Tiny C compiler gave me result of 12 when i entered value 2!

    So maybe its because they use Turbo C++ compiler 3.0? Then why they say like... In C....etc etc... because this makes me think that in C i really cannot do that!

    But Tiny C is ANSI C... so if it does that then i CAN DO THIS IN C! so... maybe i'm wrong? Any ideas?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,406
    Quote Originally Posted by BlackOps
    their asnwer is: Compiler error.
    In this specific case, the answer is plausible. Footnote 100 of section 3.7.1 of the 1999 edition of the C standard reads:
    The implementation may treat any register declaration simply as an auto declaration. However, whether or not addressable storage is actually used, the address of any part of an object declared with storage-class specifier register cannot be computed, either explicitly (by use of the unary & operator as discussed in 6.5.3.2) or implicitly (by converting an array name to a pointer as discussed in 6.3.2.1).
    It might be more correct to say that the behaviour is undefined, but I am not so sure. The MinGW port of gcc 3.4.5 merely emits a warning even without any warning related options.

    In practice though, we usually rely on the compiler to decide what should be placed in registers, so an issue about the register keyword is generally just academic (and even if you do suggest to the compiler with the register keyword, the compiler can ignore your suggestion anyway).
    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

  8. #8
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    And by the way, void main() is _WRONG_. You should always write int main().

    EDIT: And return 0;

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Brafil View Post
    EDIT: And return 0;
    If successful, or non-zero if there was an error.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 10:21 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 08:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM

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