Is this right?

This is a discussion on Is this right? within the C Programming forums, part of the General Programming Boards category; Here's the question: Two numbers are input through the keyboard into two locations C and D. Write a program to ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    16

    Is this right?

    Here's the question: Two numbers are input through the keyboard into two locations C and D. Write a program to interchange the contents of C and D.

    Code:
    #include<stdio.h>
    
    main()
    {
       int c,d;
    
       printf( "\nEnter c value:" );
       scanf( "&#37;d", &c );
       printf( "\nEnter d value:" );
       scanf( "%d", &d );
    
       printf("\nc=%d", d);
       printf( "\nd=%d", c);
     
       getchar();
       getchar();
    }
    I think my program interchanges the values of 2 variables... Is that the same as interchanging the 'contents of 2 locations'?

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Not really. You're just printing out the value of C and calling it D. Your assignment is probably asking to actually swap the values stored in C and D. I'll give you a hint: you'll need a third variable.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    You are actually "cheating", in the sense that you are lying to the user by claiming that c is d and d is c. More likely:
    Code:
    #include<stdio.h>
    
    int main()
    {
        int c, d;
    
        printf("\nEnter c value:");
        scanf("&#37;d", &c);
        printf("\nEnter d value:");
        scanf("%d", &d);
    
        /* Insert code here to swap c and d
           ... */
    
        printf("\nc=%d", c);
        printf("\nd=%d", d);
    
        getchar();
        getchar();
        return 0;
    }
    Then when the user enters 3 followed by 4, the program will print 4 followed by 3.
    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
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    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
    Registered User
    Join Date
    May 2008
    Posts
    53
    Also,

    Code:
    int main()
    is C++. You are not supposed to leave out the "void" in C.

    --
    Computer Programming: An Introduction for the Scientifically Inclined

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    16
    Right... so is this correct?

    Code:
    #include<stdio.h>
    
    int main(void)
    {
        int c, d, x;
    
        printf("\nEnter c value:");
        scanf("%d", &c);
        printf("\nEnter d value:");
        scanf("%d", &d);
    
        x=c;
        c=d;
        d=x;
    
        printf( "The values of c and d are %d and %d", c,d );
    
        getchar();
        getchar();
        return 0;
    }
    Elysia, thanks for that heads-up. I think this book I'm using is outdated... The author's used main() everywhere...

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Yes, this is the correct "main" structure.
    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.

  8. #8
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Right... so is this correct?
    That looks correct. Just one tip: always initialise your variables and don't declare them on the same line. It's going to save you hours of head scratching at some point

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Well, I don't think it's that bad to define multiple variables on the same line, as long as they're not pointers.
    Pointers go on separate lines.
    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.

  10. #10
    Registered User
    Join Date
    May 2008
    Posts
    53
    Quote Originally Posted by Sander View Post
    Also,

    Code:
    int main()
    is C++. You are not supposed to leave out the "void" in C.
    Actually, I'll be damned. The standard says in one place that main "shall be declared" with a return type of int and with either no parameters:
    Code:
    int main(void) { /* ... */ }
    or with two parameters:
    Code:
    int main(int argc, char* argv[]) { /* ... */ }
    but it uses a simple
    Code:
    int main() { /* ... */ }
    in several examples. In 6.7.5.3, Function declarators (including prototypes) item #10 says:
    "The special case of an unnamed parameter of type void as the only item in the list specifies that the function has no parameters."

    So I can't determine whether using () instead of (void) in prototypes is valid C according to the standard... Any language lawyers in the room?

    --
    Computer Programming: An Introduction for the Scientifically Inclined

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    It seems clear to me that main shall be defined as int main(void), because it clearly says that main should take no parameters or two parameters.
    And if it does not contain void, then technically, it can take any number of arguments, which is in violation of what it says.
    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.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    So I can't determine whether using () instead of (void) in prototypes is valid C according to the standard
    It is standard C; C99 itself uses both forms. In fact, they are the same. The difference only comes with function prototypes, where an empty parameter list means that no information about the parameters is supplied.
    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

  13. #13
    Banned
    Join Date
    Nov 2007
    Posts
    678
    So this is allowed:
    Code:
    void func():
    int main() {
        func(1,2,3,4);
    }
    
    void func() {
        /* But how to acess those parameters? */
    }

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    So this is allowed:
    No, but this is allowed:
    Code:
    #include <stdio.h>
    
    void func();
    
    int main() {
        func(1,2,3,4);
        return 0;
    }
    
    void func(int w, int x, int y, int z) {
        printf("&#37;d %d %d %d\n", w, x, y, z);
    }
    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

  15. #15
    Banned
    Join Date
    Nov 2007
    Posts
    678
    That makes little magic to work. I can remove/ignore the parameters in func. I do not have to change the header file having a () prototyped function. And C uses ccall so there will be no effect on calling code also. Pretty cleaver and neat! Wow!

Page 1 of 2 12 LastLast
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