Like Tree5Likes

Strange!!

This is a discussion on Strange!! within the C Programming forums, part of the General Programming Boards category; The following code won't compile with MSVC 2010, but when compiled and executed with Turbo C, it reports the value ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    385

    Strange!!

    The following code won't compile with MSVC 2010, but when compiled and executed with Turbo C, it reports the value of x as 20, even when there is no return statement in fun1.....

    Code:
    #include<stdio.h>
    
    int x;
    
    int fun1();
    
    
    int main()
    {
    	x=10;
    	
    	printf("x = %d\n",fun1());
    }
    
    int fun1(void)
    {
    	x=x+10;
    }

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    MSVC is a C++ compiler, which are more strict than C compilers (all other things being equal). You're only getting 20 back from the function by accident - that value happened to be floating around and landed in the correct register when the function returned, so it accidentally worked in that one case. Definitely don't rely on it.

    If I was you, I'd either stick with using MSVC (can't believe I just suggested that...) or play with Turbo C's warning options until it literally will not allow you to compile that code into an executable. You'll thank yourself in the long run, trust me.
    Last edited by JohnGraham; 12-05-2011 at 09:07 AM.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Quote Originally Posted by JohnGraham View Post
    You're only getting 20 back from the function by accident - that value happened to be floating around and landed in the correct register when the function returned, so it accidentally worked in that one case.
    Well... no matter how many times I compile it, it gives the same result, and am quiet sure that if you try it at ur place with a turbo C compiler, it would produce the same results.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by juice View Post
    The following code won't compile with MSVC 2010, but when compiled and executed with Turbo C, it reports the value of x as 20, even when there is no return statement in fun1.....

    Code:
    #include<stdio.h>
    
    int x;
    
    int fun1();
    
    
    int main()
    {
    	x=10;
    	
    	printf("x = %d\n",fun1());
    }
    
    int fun1(void)
    {
    	x=x+10;
    }

    It works because x is a global variable and you are probably ignoring the warning about your function missing a return value...
    Move your int x; line into main and see what happens...

    It won't compile in MSVC++ not because there's a problem with global variables but because your function is missing a return value.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Quote Originally Posted by CommonTater View Post
    It works because x is a global variable and you are probably ignoring the warning about your function missing a return value...
    No warnings... no nothing. It is as if the call to fun1 through printf is working as a macro expansion and getting replaced with x+10....

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,112
    Quote Originally Posted by juice
    No warnings... no nothing.
    You could try increasing your compiler's warning level. As for MSVC 2010: make sure you are compiling as C, not C++.

    Quote Originally Posted by juice
    It is as if the call to fun1 through printf is working as a macro expansion and getting replaced with x+10
    The behaviour is undefined since you attempt to use the return value despite the lack of a return statement.
    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

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by juice View Post
    No warnings... no nothing. It is as if the call to fun1 through printf is working as a macro expansion and getting replaced with x+10....
    Most likely it's because the result of x = x + 10 is still sitting in the cpu register when you call printf() in main() and turbo C isn't smart enough to catch the missing return value error.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by laserlight View Post
    You could try increasing your compiler's warning level. As for MSVC 2010: make sure you are compiling as C, not C++.


    The behaviour is undefined since you attempt to use the return value despite the lack of a return statement.
    I disagree. There is no return value, and there is no effort to catch or work with a return value. The return value is simply an integer with a NULL value, that is never used by the program.

    The function merely changed a global variable - and when the execution returned to main(), it printed the global variable.

    Yes, Turbo C will do this ALL the time, since it is correct C, according to the AT&T standards it was written to work with, at that time. It will give you a warning about the lack of a return from the function - at least, my early version of Turbo C, did.
    Last edited by Adak; 12-05-2011 at 09:55 AM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,112
    Quote Originally Posted by Adak
    I disagree. There is no return value, and there is no effort to catch or work with a return value. The return value is simply an integer with a NULL value, that is never used by the program.
    The return value is the second argument to printf, thus the C standard disagrees with you:
    Quote Originally Posted by C99 Clause 6.9.1 Paragraph 12
    If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.
    Quote Originally Posted by C89 Clause 3.6.6.4
    If a return statement without an expression is executed, and the value of the function call is used by the caller, the behavior is undefined. Reaching the } that terminates a function is equivalent to executing a return statement without an expression.
    MK27 likes this.
    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

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    I disagree. There is no return value, and there is no effort to catch or work with a return value. The return value is simply an integer with a NULL value, that is never used by the program.
    Yes it is... look at line 12 ... the return value (if it really existed) would be used by printf().

    The function merely changed a global variable - and when the execution returned to main(), it printed the global variable.

    Yes, Turbo C will do this ALL the time, since it is correct C, according to the AT&T standards it was written to work with, at that time. It will give you a warning about the lack of a return from the function - at least, my early version of Turbo C, did.
    Wow... scabby standard. Thank goodness we've moved on.

    For what it's worth... Pelles C says x = 1 and if I switch to __STDCALL it produces a redefinition error because the prototype doesn't have void in it's parameter list. Adding the return x; line in the function correctly reports 20.
    Last edited by CommonTater; 12-05-2011 at 10:15 AM.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by Adak View Post
    There is no return value, and there is no effort to catch or work with a return value.
    Except in this statement:

    Quote Originally Posted by juice View Post
    Code:
    printf("x = %d\n",fun1());

  12. #12
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    can anyone suggest me where can I download proper C99 standards....

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,112
    Quote Originally Posted by juice
    can anyone suggest me where can I download proper C99 standards....
    We have a sticky concerning that
    C Draft Standards

    If you are really dying for the published document, try obtaining a PDF version from the ANSI store.
    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

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by juice View Post
    can anyone suggest me where can I download proper C99 standards....
    You might also want to download a C-99 compiler, because MSVC isn't ... Pelles C
    laserlight likes this.

  15. #15
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    Did you go to the link???????????

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

Similar Threads

  1. Strange bug....
    By beene in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2006, 12:55 PM
  2. Strange !!!
    By Moni in forum C++ Programming
    Replies: 19
    Last Post: 01-05-2004, 01:48 PM
  3. Very strange bug...
    By JaWiB in forum Tech Board
    Replies: 6
    Last Post: 04-27-2003, 02:56 PM
  4. strange strange functions
    By threahdead in forum C Programming
    Replies: 4
    Last Post: 10-13-2002, 06:31 PM
  5. strange
    By pode in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2002, 01:07 PM

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