C with more than one variable.

This is a discussion on C with more than one variable. within the C Programming forums, part of the General Programming Boards category; When I use one variable I'd do this: Code: #include <stdio.h> int main() { int a; printf("Enter: "); scanf("%d", &a); ...

  1. #1
    Novice.
    Join Date
    Oct 2005
    Posts
    88

    C with more than one variable.

    When I use one variable I'd do this:

    Code:
    #include <stdio.h>
    
    int main()
    {
     int a;
     printf("Enter: ");
     scanf("%d", &a);
     printf("%d", a);
     getchar();
     getchar();
     return 0;
    }
    The tutorial doesn't speak about more than one variable, but is this the correct way to use more than one (I've had a program crash when using more than one variable, so just to check)?

    Code:
    #include <stdio.h>
    
    int main()
    {
     int a;
     int b;
     printf("Enter: ");
     scanf("%d", &a);
     printf("%d \n", a);
     printf("Enter: ");
     scanf("%d", &b);
     printf("%d", b);
     getchar();
     getchar();
     return 0;
    }
    Thank You.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    I don't understand what is hard about multiple variables?
    But the code is correct.
    I also advise you to use a tab or at least 4 spaces for indentation.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Sure, that works just fine.

    You can actually declare more than one variable in one declaration:
    Code:
    int a, b;
    Some people prefer to do it the way you have it, with two declarations,
    Code:
    int a;
    int b;
    but you should at least be aware that you can declare two or more variables at once.

    What code were you using when using more than one variable crashed?

    [edit]
    I also advise you to use a tab or at least 4 spaces for indentation.
    Who cares -- it's indented consistently at least! [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Why don't you try it. Although if you use turbo C [like it seems that many forum users do] you may not see the output of "b = ...", because you don't have a newline on the end, which means that the output isn't printed before the end of the program, and thus you never see it - this is however a "flaw"[1] in the Turbo C runtime libraries, not in the language itself. Make it "b = %d\n" instead, and all will be fine.

    [1] I use quotes around flaw because I'm not 100% sure what the language spec says about flushing standard out before input.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    Or upgrade the compiler to a newer, better one. Turbo C from what I understand is old and bad. There are newer, free, better alternative compilers such as Dev-Cpp and Visual Studio 2008 Express.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  6. #6
    Novice.
    Join Date
    Oct 2005
    Posts
    88
    Well I use Bloodshed Dev 4 actually and the original program had something to do with arrays so I'll just thinker a bit with them untill I figure out how they work.

    Thanks for al the advice.

    PS: I do like to declare my integers one above the other.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    Well I use Bloodshed Dev 4 actually
    If you want to use Bloodshed Dev-C++, use version 4.9.9.2
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 11:07 AM
  2. Use of variable
    By alice in forum C Programming
    Replies: 8
    Last Post: 06-05-2004, 07:32 AM
  3. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 03:11 AM

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