Thread: variable declatration

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    1

    variable declatration

    in c programming a variable declare in between program or some place in program what keyword should we used ?

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    See the section "Using variables" in the tutorial.

    You don't need an extra keyword for declaring a variable, just <type of variable> <name of variable>.

    Depending on the location of the declaration the variable will have a specific storage class.

    Beginning with C99 selection- and iteration-statements (if, switch, for, while) are considered as blocks and thus the following is also valid:
    Code:
    for (int i = 0; i < 10; i++)
    {
        /* body of loop */
    }
    Bye, Andreas

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I think that in C we declare variables at the start of the program.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    Quote Originally Posted by std10093 View Post
    I think that in C we declare variables at the start of the program.
    no not really. it depends on what you want to do with them.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You mean that we can declare a variable at the middle of the main for example?Like C++??

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by std10093
    You mean that we can declare a variable at the middle of the main for example?Like C++??
    Yes, when compiling with respect to the 1999 edition of the C standard, or later. Besides, even if you are compiling with respect to C89/C90, you would still be declaring variables at the start of blocks like the start of a function, not just "the start of the program" (as in at global scope or at the start of the main function).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I did not know that.Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-21-2012, 04:51 PM
  2. Replies: 7
    Last Post: 09-19-2011, 01:37 PM
  3. Replies: 7
    Last Post: 04-16-2011, 12:28 PM
  4. Replies: 8
    Last Post: 02-14-2010, 04:14 PM
  5. Static Local Variable vs. Global Variable
    By arpsmack in forum C Programming
    Replies: 7
    Last Post: 08-21-2008, 03:35 AM