C programs and differances between windows c compilers and gnu gcc

This is a discussion on C programs and differances between windows c compilers and gnu gcc within the C Programming forums, part of the General Programming Boards category; Hello I was doing a home work assignment for my C programming class and I came across a problem. The ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    23

    C programs and differances between windows c compilers and gnu gcc

    Hello I was doing a home work assignment for my C programming class and I came across a problem. The assignment is a simple program that demonstrates how to use the "continue" statement.

    Code:
     
    
     
    //      Fig. 4.12  fig04_12.c
    //      Using the continue statement in a for statement
    
    #include <stdio.h>
    
    int main()
    
    {
    
            int x;
    
            for ( x = 1; x <= 10; x++ ) {
    
                    if ( x == 5 ) {
    
                             continue;
    
                    }
    
    
                    printf ( "%d ", x );
    
            }
    
    
            printf ( "\nUsed continue to skip printing the valve 5\n" );
    
            return 0;
    
    }
    I am coding on a linux laptop with gnu gcc 3.3.6, I found that when I compile it under gcc I get the wrong output, instead of skip printing the number 5 it prints it, and also never prints the last printf statement. When I compile it on a windozs box with Dev++ 4 from Bloodshed the same code runs with the correct output. Can anybody explain why this would be? I know they are two different compilers, but its a C statement shouldn't be the same for both.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    On the system that magically printed the 5, are you sure your source code is typed correctly?
    The sample you've provided will give the following:
    Code:
    1 2 3 4 6 7 8 9 10
    Used continue to skip printing the valve 5
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    23
    I am positive I used the same code. for both compilers

    this is the output I get under gcc

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    mike@laptop ~/code/C/chapter_4 $

  4. #4
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    I'm positive you didn't. In fact, why does your output above show a newline for each value, when your printf in the code has no \n?

    Not only that, your output above has no "Used continue to skip printing the valve 5", but your code sample has code to print that.

    Here's my output using gcc 3.3.3 (close enough):
    Code:
    $ gcc -v 2>&1 | tail -1
    gcc version 3.3.3 (SuSE Linux)
    $ cat > sample.c
    //      Fig. 4.12  fig04_12.c
    //      Using the continue statement in a for statement
    
    #include <stdio.h>
    
    int main()
    
    {
    
            int x;
    
            for ( x = 1; x <= 10; x++ ) {
    
                    if ( x == 5 ) {
    
                             continue;
    
                    }
    
    
                    printf ( "%d ", x );
    
            }
    
    
            printf ( "\nUsed continue to skip printing the valve 5\n" );
    
            return 0;
    
    }
    $ gcc -o sample sample.c -Wall
    $ ./sample
    1 2 3 4 6 7 8 9 10
    Used continue to skip printing the valve 5
    $
    Last edited by cwr; 10-02-2005 at 07:48 PM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It is obviously not the same code, because you have no newlines in your for loop. It won't just randomly insert newlines for the hell of it.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    23

    Ahhh

    Im sorry I don't know what I did I cut and pasted from your post and it worked fine
    i must have gotten code mixed up im going back right now to ckeck it out

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    23

    I think i figured out what was going on

    I just checked out my source files and I have a folder setup to hold all of my chapter_4
    .c files. I have two file one called pg102.c and on called pg120.c the pg120.c was the code I
    posted and every time i compiled it it did give me the wrong output. I checked my pg102.c
    file and the output I was getting with all the /n is from the pg102.c program.

    So it looks like for some reason it was running the pg102.c program instead of the pg120.c
    program. I double and tripple checked it (to make sure I wasn't typing in ./pg102 instead of ./pg120) So i renamed the pg120.c file compiled it and it ran like it should.

    cwr could you try this out on your system to verify im not a NUT !!.
    you have the first code here is the second code.

    Code:
    
    //      Fig. 4.1: fig04_01.c
    //      Counter-Controlled repetition
    
    #include <stdio.h>
    
    int main()
    
    {
    
            int counter = 1;                     // declares variable "counter" as interger and sets it value to one
    
            while ( counter <= 10 ) {            // start of my while loop
    
                    printf ( "%d\n", counter );
                    ++counter;                   // pre-incrementing varivable "counter"
            }
    
            return 0;
    
    }

    just name them like i did. this one is pg102.c and the original is pg120.c and compile them in the same folder see if you get the same thing.

  8. #8
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    The second piece of code would print out
    Code:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    After all, I don't see any continue statement.

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    23
    Quote Originally Posted by Rashakil Fol
    The second piece of code would print out
    Code:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    After all, I don't see any continue statement.

    I know what the output is going to be from the code I posted I wanted to know if he gets the same problem as I did. he is using a linux box like I am.

  10. #10
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Yeah, I don't need to run it, the output is obvious, see Rashakil Fol's result.

  11. #11
    Registered User
    Join Date
    Oct 2005
    Posts
    23
    I think your missing my point. why would it run the program ./pg102 when Im positive
    I typed in ./pg120. it some how got the two programs mixed up.

  12. #12
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Perhaps because you compiled it wrongly:
    Code:
    gcc -o pg120 pg102.c
    Make sure you specified the correct output file as well as the correct source file.

    I am certain the output is the same on my system, trust me. If you like I'll humour you and pretend to run it. Yep, the output is the same.

  13. #13
    Registered User
    Join Date
    Oct 2005
    Posts
    23
    your right im a moron I must have done just what you said. Its late i must have screwed up
    thanks for your time sorry I'm a nut job.

  14. #14
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    It's very, very rare to find a bug in gcc. And it's almost unheard of to find a bug where it won't compile really trivial programs like the one you gave above.

    If a C program has no undefined, implementation defined, or unspecified behaviour, like the ones you posted above, the output will be the same on all platforms.

Popular pages Recent additions subscribe to a feed

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