Thread: Initialisation of static variables

  1. #1
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99

    Initialisation of static variables

    Hello all,

    Another trivial question. Is this valid C code?

    Code:
        #include <stdio.h>
    
        int a = 2, b = 2, c = a + b;
    
        void main(void)
        {
        printf("%d", c);
        return;
        }
    The program compiles and runs fine on my C++ compiler. Unfortunately I haven't got a C compiler to test it on, but I am assuming a C compiler will handle it the same way (or as that assumption wrong?).

    According to everything I've read, static variables must be initialised with constant expressions. "c" above is a static variable; however, "a + b" is not what I would consider to be a constant expression, even if its value clearly is constant here. So I would have expected a compilation error. Why don't I get one?

    Suggestions would be most appreciated, etc.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There are differences between C and C++ - this is one of them. If you compile the above code with gcc, you get an error. In g++, which is the same compiler but for C++, then it compiles fine.

    What compiler have you got that isn't able to compile C when it can compile C++? I'm not aware of a single compiler manufacturer that does ONLY a C++ compiler.

    --
    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.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Once I fix your main function by changing its return type to int and returning 0, gcc 3.4.5 reports the error that an initializer element is not constant. Frankly, I am not sure why a C++ compiler would allow this to pass.
    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

  4. #4
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99
    Thanks for the quick response.

    Actually I am using Borland C++ Version 4.52, which is probably totally unsuitable for Windows XP anyway (which is what I have on my PC). It came free with a magazine my dad bought about 10 years ago so I haven't any sort of manual for it.

    Can you recommend anything more suitable?

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by DL1 View Post
    Thanks for the quick response.

    Actually I am using Borland C++ Version 4.52, which is probably totally unsuitable for Windows XP anyway (which is what I have on my PC). It came free with a magazine my dad bought about 10 years ago so I haven't any sort of manual for it.

    Can you recommend anything more suitable?
    VC++ 2008 express

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by DL1 View Post
    Thanks for the quick response.

    Actually I am using Borland C++ Version 4.52, which is probably totally unsuitable for Windows XP anyway (which is what I have on my PC). It came free with a magazine my dad bought about 10 years ago so I haven't any sort of manual for it.

    Can you recommend anything more suitable?
    I'm pretty sure that Borland C++ has a "C only" option as well, but it's quite an old compiler, and I expect that it's not up to date with the current C++ standard, since the standard was only ratified about 10 years ago, and it usually takes compiler vendors a few years to "get everything right" once the standard is settled down.

    Sure, gcc-mingw is completely free and full-function, standards compliant compiler.
    You can also get an IDE (That is the editor, debugger etc) such as Code::Blocks which brings you the compiler in the same package - or you can download gcc-mingw on it's own and get the IDE on it's own.

    Microsoft Visual Studio Express Edition is a "stripped down" version of the professional level Visual Studio compiler at no cost - it is the same COMPILER as the "pro" edition, but the package is somewhat reduced with respect to other functions, such as no MFC (Windows Framework code to make it easier to develop Windows applications).

    If you are a student, you can also get a "student version" of the MSVS at a reduced cost compared to the "pro" edition. Not sure how much it costs.

    --
    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.

  7. #7
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99
    Thanks. I'll look into those. Unfortunately it's nearly 20 years since I was a student though, so no discounts for me!

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by DL1 View Post
    Thanks. I'll look into those. Unfortunately it's nearly 20 years since I was a student though, so no discounts for me!
    Well, in that case you could use VS Express, Code::Block, Dev-C++. Lots of option for you. I use Dev-C++ which i think, this project has been withdrawn i guess, since i dint get any updates. Perhaps you should go for Code::Block that a very nice one. Which myself i switching over to it!

    ssharish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Replies: 23
    Last Post: 07-09-2007, 04:49 AM
  3. Static variables + Initialisation
    By kris.c in forum C Programming
    Replies: 2
    Last Post: 07-08-2007, 02:16 AM
  4. static variables
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 08-28-2006, 06:35 AM
  5. Replies: 5
    Last Post: 11-19-2002, 07:49 PM