Thread: Initialize a variable with a function call

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    8

    Initialize a variable with a function call

    I have used a function call to iniatialize a static const variable and a co-worker says it is not legit.

    My sparring partner says it would not even be legit if I remove the const because he claims the rh value must be known at compile time and he cites this link: Static Variable Must Be Constant?.

    I am not an ANSI standard expert but somewhere in my readings I am sure I have read that this is allowable. Here is a simplified example of what I have done.
    Code:
    #include"stdafx.h"
    
    int f1(void)
    {
      printf("\nEnter a character: ");
      return getchar();
    }
    
    void main(void)
    {
      int i = 1;
      static const char c = f1();
      printf("\nf1() returned: %c", c);
      /* Next line would gen compile error. 
         c = 'a';
       */
      getchar();                    // Pauses output window.
    }

    The first question that someone will ask is do I really need a function call if it is a constant. Technically no, but the function returns a constant pointer from power loss memory and the code to do so is the better part of a pageful and it is already nicely packaged in a function so the pragmatic answer is yes.

    I would appreciate your comments and maybe the answer will surprise more folks than just me.

    Thanks,
    jvh
    Last edited by Salem; 08-22-2012 at 02:28 PM. Reason: demunged the horrible font/size abuse

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    A few things first:

    1. It's int main(void) and return an int at the end (usually 0). Read this link.
    2. Please copy/paste your code as plain text. Our website will use an appropriate font size and syntax highlighting.
    3. You're missing spaces on the declaration of c and you're missing an open parenthesis on the printf call in main. Maybe this is just a copy/paste error or you typed it off the top of your head.


    EDIT: On closer reading, it appears this may be some sort of embedded system, in which case void main may be acceptable. Also, please post the actual code you have, it will help us determine if you really need/want to use static or const qualifiers on the variable.

    EDIT 2: If the function does nothing other that return a value that is known at compile time and never changes during run time, why make it a function at all? Alternatively, if it must be a function, especially if the function is small, simply call it every time you need the value. It will always return the right value, and the call overhead will be quite small, especially with an optimizing compiler that may simply in-line the call for you.

    As for your question: your coworker is correct.

    static, when applied to local variables*, means the variable does not exist on the stack. Rather, there exists one instance of the variable in the data portion of the memory map (where the global variables live). That means it exists in the executable** file, before the program is ever run. Thus it's initial value must be able to be determined at compile time (when the executable is made). You could declare it static in one line and assign it in another statement, but then you can't have it be const. Of course, if you expect the data to be set by something at run time (e.g. getchar()), it shouldn't be const to begin with. const is for, well, constant data, that never changes.

    *static, when used on functions and file-scope variables has a different meaning, a bit like the opposite of extern. For clarification on that topic, read FAQ > static and extern? - Cprogramming.com.

    ** This doesn't just apply to executables, it applies to any C code.
    Last edited by anduril462; 08-22-2012 at 01:36 PM.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    Here's the exact reference from the standard (C99) if you want to check it later:
    "[6.7.8.]4 All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals."
    and
    "[6.6.]3 Constant expressions shall not contain assignment, increment, decrement, function-call, or comma operators, except when they are contained within a subexpression that is not evaluated."
    and concerning the "subexpression that is not evaluated":
    "98) The operand of a sizeof operator is usually not evaluated (6.5.3.4)"

  4. #4
    Registered User
    Join Date
    Aug 2012
    Posts
    8
    Quote Originally Posted by root4 View Post
    Here's the exact reference from the standard (C99) if you want to check it later:
    "[6.7.8.]4 All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals."
    and
    "[6.6.]3 Constant expressions shall not contain assignment, increment, decrement, function-call, or comma operators, except when they are contained within a subexpression that is not evaluated."
    and concerning the "subexpression that is not evaluated":
    "98) The operand of a sizeof operator is usually not evaluated (6.5.3.4)"
    thanks for the reference
    jvh

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    8
    [QUOTE=anduril462;1119715]A few things first:

    1. It's int main(void) and return an int at the end (usually 0). Read this link.
      Embedded, nowhere to return to.
    2. Please copy/paste your code as plain text. Our website will use an appropriate font size and syntax highlighting.
      Shucks, thought I was doing good following webmaster's directive for code tags, my apologies for offending the list.
    3. You're missing spaces on the declaration of c and you're missing an open parenthesis on the printf call in main. Maybe this is just a copy/paste error or you typed it off the top of your head.
      Copy/past conversion going into code tags I suppose. It is a working program.


    EDIT 2: If the function does nothing other that return a value that is known at compile time and never changes during run time, why make it a function at all?

    Please see pp below my code in orginal post.


    Thanks for the reply,

    jvh
    Last edited by jvh24521; 08-22-2012 at 02:54 PM.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Quote Originally Posted by jvh24521 View Post
    Please see pp below my code in orginal post.
    Yeah, my bad. I actually did read your entire post but the words just never sank in.

  7. #7
    Registered User
    Join Date
    Aug 2012
    Posts
    8
    Quote Originally Posted by anduril462 View Post
    Yeah, my bad. I actually did read your entire post but the words just never sank in.
    's OK. 'Twas a point that need to be made more often IMO.
    jh

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable shows NAN after return from function call
    By CodeKate in forum C Programming
    Replies: 21
    Last Post: 11-18-2010, 11:58 AM
  2. Replies: 8
    Last Post: 02-14-2010, 04:14 PM
  3. variable can auto initialize itself?
    By thinhare in forum C Programming
    Replies: 6
    Last Post: 09-13-2005, 06:07 AM
  4. Replies: 1
    Last Post: 02-03-2005, 03:33 AM
  5. call function from variable
    By Mithoric in forum Windows Programming
    Replies: 2
    Last Post: 11-29-2003, 05:40 AM