Thread: Constant value not known

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    1

    Constant value not known

    Hi,

    When compiling any of these codes, I get the message: "constant value not known" which seems weird to me... at least for the second one I don't understand why.

    Thanks for any help.

    F.
    Code:
    ////////////////////////////////////////////////////////////// V.1
    
    const int size_input_bits = 12382;
    const int size_input_ints = ceil((float)size_input_bits/32);
    int h_input [size_input_ints];
    
    ////////////////////////////////////////////////////////////// V.2
    
    #define size_input_bits 12382;
    ...........
    const int size_input_ints = ceil((float)size_input_bits/32);
    int h_input [size_input_ints];
    Last edited by Salem; 06-12-2007 at 11:05 PM. Reason: Added [code] [/code] tags - learn to use them

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    You can't call a function (i.e. ceil()) in the initializer for a global variable, much less a const one.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Why not just assign 387 to size_input_ints?

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    136
    Find my comments Inline.

    Code:
    ////////////////////////////////////////////////////////////// V.1
    
    const int size_input_bits = 12382;
    const int size_input_ints = ceil((float)size_input_bits/32);
    int h_input [size_input_ints];
    
    ////////////////////////////////////////////////////////////// V.2
    
    #define size_input_bits 12382;
    U dont need to put ' ; ' when defining a variable.
    ...........
    const int size_input_ints = ceil((float)size_input_bits/32);
    --I think initializers of global variables cannot contain function
    calls in C. You can switch to C++, that introduces the ability to do dynamic initialization of global variables.
    
    int h_input [size_input_ints];
    S_ccess is waiting for u. Go Ahead, put u there.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > #define size_input_bits 12382;
    #defines don't normally have trailing ;

    > int h_input [size_input_ints];
    Standard (C89) C doesn't have variable length arrays of any persuasion, even if you declare the size as an int constant. As far as C is concerned, it is a dynamic run-time expression (which always yields the same value).

    The C and C++ ideas about const are very different.

    Code:
    #define size_input_bits 12382
    ...........
    int h_input [ size_input_bits / 32 ];
    This should work in C.
    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.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > The C and C++ ideas about const are very different.
    I tested and verified that in C90, using GCC with -W -Wall -ansi -pedantic, declaring the size of a static array as a const int doesn't work, but in C++, it does (of course, C99 doesn't need the const at all). Why is this?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    http://david.tribble.com/text/cdiffs...-const-linkage
    In C++, the compiler is free to substitute the known value at compile time, thus achieving what #define does in C.

    In C, const just means "warn me if I try to modify this variable". For example, this is valid C.
    const volatile int clock;
    A read-only variable which is constantly changing.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  4. Try out my new game :) !
    By Stan100 in forum Game Programming
    Replies: 10
    Last Post: 06-05-2003, 08:10 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM