Thread: Declaring Variables

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    21

    Declaring Variables

    Can you only declare variables at the beginning of a C script? Also, can anyone tell me how you declare a char array and how you copy things into that char array?

    Many thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You can declare them at the start of any opening brace.

    If you're referring to your other post, there is no such thing as a variable length array in standard C.
    So
    char myArray[someComputedSize];
    is not allowed.

    The way to do this is
    char *myArray = malloc( someComputedSize );

    Don't forget to include stdlib.h to use malloc.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    7
    Quick question on this. I'm new to C, and only just recently saw people saying that you can only declare variables at the beginning of blocks. I hadn't been doing this, and never experienced a problem. I did a little test and found the following code to compile and run fine. I'm using gcc, and I don't think it's using C99 because I can't do some other C99-ish things like for(int blah;...).
    I'm sure there's an obvious explanation I'm missing; could someone please enlighten me?

    (Here is my little goofy test code where I declare variables all over the place, in case you were wondering.)
    Code:
    int main (int argc, const char * argv[]) {
        int blah;
        blah = 3 + 2;
        while (blah > 3)
            blah--;
        int poop;
        poop = 7;
        printf("%d\n", poop);
        int hi = 4;
        printf("%d\n", hi);
        return 0;
    }

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Declaring variables in the middle of blocks is an extremely common compiler extension. So is for(int x; ...), but there you have it. Single-line comments are also a common C99 and C++ feature that are commonly implemented by C89 compilers.

    If you really want to tell if something is ANSI-standard or not, enable strict ANSI compatibility.

    Here's a recent thread about this: http://cboard.cprogramming.com/showthread.php?t=95876
    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.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    7
    Thank you for clarifying

    In this day and age, as someone learning C "recreationally" (that is, not professionally using it), how "evil" would it be to write C99 code?

  6. #6
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by Nerigazh View Post
    Thank you for clarifying

    In this day and age, as someone learning C "recreationally" (that is, not professionally using it), how "evil" would it be to write C99 code?
    GCC can handle it by adding at the command line -std=c99 as a flag at compiling time.
    Depending how portable you want to make your code, you might want to remain in the C89
    realm.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    GCC 4.1.2 compiles your code without warnings or errors with no options (I added #include <stdio.h> on top), but with the -pedantic option

    gcc -pedantic foo.c

    foo.c: In function ‘main’:
    foo.c:8: warning: ISO C90 forbids mixed declarations and code
    foo.c:11: warning: ISO C90 forbids mixed declarations and code

    It's a good idea with GCC to compile with "-W -Wall -ansi -pedantic":

    gcc -W -Wall -ansi -pedantic foo.c

    foo.c: In function ‘main’:
    foo.c:8: warning: ISO C90 forbids mixed declarations and code
    foo.c:11: warning: ISO C90 forbids mixed declarations and code
    foo.c: At top level:
    foo.c:3: warning: unused parameter ‘argc’
    foo.c:3: warning: unused parameter ‘argv’

  8. #8
    lfs addicted
    Join Date
    Nov 2007
    Posts
    49
    Quote Originally Posted by Salem View Post
    You can declare them at the start of any opening brace.

    If you're referring to your other post, there is no such thing as a variable length array in standard C.
    So
    char myArray[someComputedSize];
    is not allowed.

    The way to do this is
    char *myArray = malloc( someComputedSize );

    Don't forget to include stdlib.h to use malloc.
    What? You mean that:
    Code:
    char a[3]="ab";
    is not standard? I didn't know... And what happens if you do not have a C library? (nor malloc)

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sloppy View Post
    What? You mean that:
    Code:
    char a[3]="ab";
    is not standard? I didn't know... And what happens if you do not have a C library? (nor malloc)
    Notice that 3 is a constant, so the size of a is not variable.

    And what happens if you do not have a C library? (nor malloc)
    As in the C standard library is not available? I find that rather unusual. I am no expert, but my guess is that you probably cannot code in C to begin with and have to use the assembly language for that system.

    Or... you get a better compiler.
    Last edited by laserlight; 11-18-2007 at 03:05 AM.
    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

  10. #10
    lfs addicted
    Join Date
    Nov 2007
    Posts
    49
    Quote Originally Posted by laserlight View Post
    Notice that 3 is a constant, so the size of a is not variable.
    yes, sorry, I have this bad habit to read to fast... I had understood the exact contrary.
    As in the C standard library is not available? I find that rather unusual. I am no expert, but my guess is that you probably cannot code in C to begin with and have to use the assembly language for that system.
    well you can write your own libc functions without linking to the c library, even if of course something will be written in assembly.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Free standing implementations often come with no library at all, or a very rudimentary library specific to the hardware you're running on (say driving an attached LCD display).
    Writing C for such systems is no different, except that you have to write everything yourself.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaring variables in int main()?
    By Programmer_P in forum C++ Programming
    Replies: 4
    Last Post: 05-12-2009, 02:21 AM
  2. Declaring variables
    By Marlon in forum C++ Programming
    Replies: 6
    Last Post: 06-21-2005, 04:16 AM
  3. question about declaring global variables
    By Dag_ in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2005, 06:03 AM
  4. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 04:46 PM
  5. Declaring Variables Using Variables
    By Mr. Squirrel in forum Windows Programming
    Replies: 5
    Last Post: 12-11-2001, 12:38 AM