CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2003
    Posts
    27

    about Declaration and Definition

    1. in C, we define a global variable in *.c file, e.g.:
    int myval;
    and then use "extern" to declare it as a global one in *.h:
    extern int myval;

    But what if I define it direct in *h as:
    int myval;
    and use it in my *c file?
    What is the difference?


    2. What is more, "static" variable in a *c file scope means it is not visible outside this file. e.g.:
    static int myval;
    But what happens if I declare it in *.h file as "extern":
    extern int myval;


    3. What is the difference in the following situations(definition in *.c file and never declared in *.h file):
    int myval;
    and
    static int myval;


    Thank you in advance!!

  2. #2
    Join Date
    Nov 2003
    Posts
    27
    Could someone help me?

  3. #3
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    1) See what happens when you include the .h in two .c files [note there should never be a reason to create a .h file unless it is going to be included in at least two .c files

    2) That "should" be a compile error.

    3) See what happens if you declare the same variable in another .c file (both with and without the static.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured