extern variable assignment

This is a discussion on extern variable assignment within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, Statement like this, extern int i = 3; 1. Is it correct? 2. Is it definition or declaration? ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    extern variable assignment

    Hello everyone,


    Statement like this,

    extern int i = 3;

    1. Is it correct?

    2. Is it definition or declaration? Any special function or benefits we could get compared with

    extern int i; or
    int i = 3


    thanks in advance,
    George

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    1) No.

    And I thought I had made it quite clear in my other post that it was an example of what could happen if such code was legal.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    This was discussed yesterday - or was it the day before - and no, you should not be allowed to do that.

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

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    A definition does not have an extern keyword.
    Using the extern keyword, it can only be a declaration and thus it cannot be initialized.
    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 2006
    Posts
    1,579
    Thanks CornedBee and others,


    I agree we have discussed before. I just saw some code written by others, so I feel confused and come here again. It is not correct code even if it can compile. :-)

    My question is answered.

    Quote Originally Posted by CornedBee View Post
    1) No.

    And I thought I had made it quite clear in my other post that it was an example of what could happen if such code was legal.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using extern to have a global variable
    By steve1_rm in forum C Programming
    Replies: 3
    Last Post: 01-29-2009, 05:44 AM
  2. extern const for memory allocation?
    By donglee in forum C++ Programming
    Replies: 1
    Last Post: 11-03-2008, 08:38 AM
  3. Global Variable Usage -- Failure in assignment
    By jake123 in forum C Programming
    Replies: 7
    Last Post: 02-15-2008, 01:30 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. float/double variable storage and precision
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2003, 06:23 PM

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