Thread: Unexpected behaviour

  1. #1
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275

    Unexpected behaviour

    Hi
    The following code must give error but it does not! I use gcc 3.3.4 under Slackware Linux.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    if (int x = getchar())
    cout << x;
    
    x = 50; // must be an error
    
    }
    But the code below gives error message...
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    if (int x = getchar())
    cout << x;
    
    cout << x;   // error
    }
    What is the reason of this?

  2. #2
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Declare x before comparing it to something in an if statement. You can't declare a variable inside a conditional expression. Also, if you're trying to compare it, you need to use == instead of =. And shouldn't x be a char variable?

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    That's not the issue he was asking about. Perhaps assigning a variable a value is more sensitive than using it. Just don't do that, that's all.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    Hi
    Actually it is not the thing I am asking about Krak (as CodeMonkey said). Just try to cpoy and paste the codes, then compile them.In the first code, there is no error. Although, an error message is expected. In the second code, you will get the error message as expected.
    In the first code, the variable "x" is assigned an integer value after the statemnt it is declared first. This shouldn't be happen!

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    With VC6, I get the same error and warning for both your programs, and both errors refer to the last line:

    error C2065: 'x' : undeclared identifier
    warning C4508: 'main' : function should return a value; 'void' return type assumed
    Error executing cl.exe.

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by fnoyan
    Hi
    The following code must give error but it does not! I use gcc 3.3.4 under Slackware Linux.

    What is the reason of this?
    There should be a compiler error in both cases. I ran your program through g++3.4.2 in my Linux box and got both errors.

    I ran the program through g++ 3.3.3 (cygwin) on my Windows XP machine and got the same results as you did: no error for the first, but a properly reported error on the second. (Borland and Microsoft Windows compilers also properly reported errors for both cases.)

    For g++ 3.3.3, assembly code indicated that the value outside the loop is stored in the location of the loop-local x. Apparently the compiler didn't mind.

    (Of course, the example code does nothing, and when you add program statements that access the variable outside its scope, the compiler properly reports the error. However, if this is really a compiler bug, then it is cause for concern, since it may affect other things that you would actually use in a program.)

    I vaguely remember that, once upon a time, there was a gcc bug report something like this: If a variable was declared extern and another variable with the name had local scope, then after the local variable went out of scope, attempts to access the extern variable caused segfault (in some cases). Perhaps when (if) the compiler writers fixed the one problem they introduced the bug that you have seen. (Then maybe they fixed both with gcc 3.4. Maybe you can upgrade gcc to 3.4.?)

    Regards,

    Dave
    Last edited by Dave Evans; 03-05-2005 at 09:47 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unexpected variable behaviour
    By Dondrei in forum C++ Programming
    Replies: 5
    Last Post: 02-24-2009, 12:11 AM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Unexpected behaviour with float
    By j.sreejit in forum C Programming
    Replies: 6
    Last Post: 09-14-2006, 09:53 PM
  4. unexpected 'class Window (' ??? i dont understand the error
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 10:12 AM
  5. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 06:04 AM