Thread: differnces between declaration and definition of functions and global variables.

  1. #1
    Registered User
    Join Date
    Feb 2018
    Posts
    1

    differnces between declaration and definition of functions and global variables.

    hi,
    i have confusion regarding access of global variables and functions. i know that global variable can only be accessed with extern syntax but function can be accessed from anywhere declaring its prototype. why global variable can't have a prototype? what is the process behind compiler which restricts from declaring global variables like we do with functions.

    Code:
    //file1.cpp
    #include<iostream>
    printHello();
    int testInteger;
    int main(){
    printHello();
    std::cout<<testInteger<<std::endl;
    
    }
    Code:
    //file2.cpp
    #include<iostream>
    int testInteger=34;
    printHello(){
    std::cout<<testInteger<<std::endl;
    }

    in above two translation unit. i know we must use extern to use testInteger in file1 from file2. BUT why declaration of testInteger in file1 and definition in file2 restricted? why we can't do like above code? we can declare functions multiple times and define it once in any translation unit.
    what is difference? i know we will get multiple definition error but why it happens? is declaration of integer itself is the definition of that integer?
    please illustrate this explaining the process the compiler and linker works with function of this declaration and definition of functions and global variables.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Perhaps if you didn't omit so much stuff, things would be clearer.

    Code:
    //file1.cpp
    #include<iostream>
    extern void printHello();
    extern int testInteger;
    int main(){
        printHello();
        std::cout<<testInteger<<std::endl;
    }
    
    //file2.cpp
    #include<iostream>
    int testInteger=34;
    void printHello(){
        std::cout<<testInteger<<std::endl;
    }
    This is also a function prototype (without the extern)
    Code:
    void printHello();
    The ; at the end gives the compiler the clue that this isn't a function definition, which would begin with an opening brace at the position the ; occupies.
    Like so.
    Code:
    void printHello() {
      // do stuff
    }
    Variables on the other hand have no such syntactic clue, so you have to use the extern keyword to indicate that a variable is declared elsewhere.

    Symbols declared at the file scope are automatically visible to the whole program.

    To restrict visibility, use the static keyword.
    Code:
    //file1.cpp
    static int sum ( int a, int b ) {
      return a + b;
    }
    int main ( ) {
    }
    You couldn't call sum() directly from file2.cpp for example.

    The static keyword can also be applied to variables as well.
    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
    Feb 2018
    Posts
    4
    That can be a definition or a declaration, depending on what type T is:
    Code:
    typedef
    Code:
    void T();
    T t;// declaration of function "t"
    
    struct X { 
      T t;// declaration of function "t".
    };
    
    typedefint T;
    T t;// definition of object "t".
    In C++, when using templates, there is another edge case.
    Code:
    template
    Code:
    <typename T>
    struct X { 
      staticint member;// declaration
    };
    
    template<typename T>
    int X<T>::member;// definition
    
    template<>
    int X<bool>::member;// declaration!
    The last declaration was not a definition. It's the declaration of an explicit specialization of the static member of X<bool>. It tells the compiler: "If it comes to instantiating
    Code:
    X<bool>::member
    , then don't instantiate the definition of the member from the primary template, but use the definition found elsewhere". To make it a definition, you have to supply an initializer

    Code:
    template
    Code:
    <>
    int X<bool>::member =1;// definition, belongs into a .cpp file.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    jackar56: I suggest that you post your code snippets as plain text within bbcode tags, with no additional markup. The forum software will add syntax highlighting and line numbering for you. As it stands, it looks like your code snippets are getting corrupted by the forum software choking on the parsing of the additional bbcode, resulting in what appears to be missing or incorrectly formatted (to the extent of becoming incorrect) code.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 08-03-2013, 10:40 AM
  2. implementing functions without global variables
    By philgrek in forum C Programming
    Replies: 5
    Last Post: 03-05-2011, 03:08 PM
  3. Is int i; is a declaration or definition?
    By forumuser in forum C Programming
    Replies: 11
    Last Post: 05-30-2007, 04:43 AM
  4. Global variables and functions.
    By earth_angel in forum C Programming
    Replies: 6
    Last Post: 07-25-2005, 12:38 PM
  5. Declaration vs definition
    By ripper079 in forum C++ Programming
    Replies: 8
    Last Post: 03-15-2002, 11:00 PM

Tags for this Thread