Like Tree1Likes
  • 1 Post By phantomotap

expected constant expression?

This is a discussion on expected constant expression? within the C Programming forums, part of the General Programming Boards category; Hello, newbie here. Just started euler problems to get better acquainted with C. The first problem is this- If we ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    2

    expected constant expression?

    Hello, newbie here.

    Just started euler problems to get better acquainted with C. The first problem is this-

    If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.


    Find the sum of all the multiples of 3 or 5 below 1000.
    So here is my program-

    Code:
    #include <stdio.h>
    
    
    int main()
    {
    	int _MAX = 1000;
    	int nums[_MAX] = {0};
    	int checkers[] = {3,5};
    	int k = 0;
    	int len = sizeof(checkers)/sizeof(int);
    	int sum = 0;
    	int checker;
    
    
    	for(k = 0; k < len; k++)
    	{
    		checker = checkers[k];
    		sum = 0;
    		while(sum+checker <= _MAX)
    		{
    			sum += checker;
    			nums[sum] = 1;
    		}
    	}
    
    
    	sum = 0;
    	for(k = 0; k < _MAX; k++)
    	{
    		if(nums[k])
    			sum += k;
    	}
    
    
    	printf("%d", sum);
    	getchar();
    
    
    	return 0;
    }
    This compiles with an error-

    main.c(6): error C2057: expected constant expression
    main.c(6): error C2466: cannot allocate an array of constant size 0

    which is line

    Code:
    int nums[_MAX] = {0};
    If I change _MAX here to 1000 everything works and I get the right answer. What is wrong and how do I fix this? I tried google but got more confused.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,262
    o_O

    Due what it says on the label: make `_MAX' a constant value.

    Soma

    C Tutor Needed
    claudiu likes this.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    It means that you're using a C compiler that is not C99 compliant, and from the looks of it, it's some version of MSVC. MSVC is good for C++, but not so good for C.
    If you want to MSVC, use an enum or #define for the size.
    Otherwise, others will suggest better C compilers such as "Pelles C".
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    Also, you should not use a leading underscore in your identifiers:
    [quote="C99 7.1.3p1]
    All identifiers that begin with an underscore and either an uppercase letter or another
    underscore are always reserved for any use.
    [/code]
    It's not likely, but entirely possible, that later on, some implementation you use defines _MAX, and your code no longer works. And pray they don't declare _MAX as a function macro, because it may throw some nasty compiler errors that make no sense when you go back and look at the code.

    EDIT: Actually, with a name like _MAX, it's quite likely that an implementation will define it, to be used in calculating the greater of two variables:
    Code:
    #define _MAX(a,b)    a > b ? a : b  // yes, I know this is naive and error prone, it's just for demonstration
    Last edited by anduril462; 05-08-2012 at 12:20 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. expected expression before const
    By ingeniousreader in forum C Programming
    Replies: 11
    Last Post: 03-06-2012, 03:34 PM
  2. Expected compile-time constant expression
    By Elysia in forum C++ Programming
    Replies: 4
    Last Post: 09-27-2008, 01:36 AM
  3. expected primary expression
    By mju4t in forum C Programming
    Replies: 2
    Last Post: 03-27-2007, 06:59 PM
  4. Expected Constant Expression in one compiler
    By jimmyjamesii in forum C++ Programming
    Replies: 3
    Last Post: 12-07-2005, 12:47 AM
  5. expected constant expression
    By Brian in forum C Programming
    Replies: 4
    Last Post: 12-03-2003, 02:30 PM

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