0
#include <stdio.h>

int numero=6;
int numerodos=4;
int result=numero*numerodos;

main()
{
    while (result<200) {
        printf("The product of %d and %d is %d", numero, numerodos, result);
        result=result+1
    }
}

I know the algorithm is bad, I'm not focused on that. But at line 3 I get an "initializer element is not constant" error in Codeblocks. I was wondering why that is? I am not dealing with large objects, and that is what other stackflow's have said the error relates to. If I initialize line 3 in the main function after printf; however, it builds and runs fine albeit terrible code.

jruizaranguren
  • 12,679
  • 7
  • 55
  • 73
  • 1
    This question has been answered numerous times. Did the automatic search for the same question fail? – Jens Sep 28 '14 at 19:02

3 Answers3

0

It's because you initialize and give value of another variable outside main()

Try out this:

#include <stdio.h>

int numero=6;
int numerodos=4;
int result=0;

main()
{
result=numero*numerodos;
while (result<200) {
    printf("The product of %d and %d is %d", numero, numerodos, result);
    result=result+1; // BE CAREFUL, ADD THIS SEMICOLON
    }
}
George Eco
  • 466
  • 3
  • 16
  • I don't understand, whether I initialize with result=0 or result=numero*numerodos it is being initialized outside of main. why does your way work – Andres Meza Sep 28 '14 at 18:35
  • 1
    Look. Outside and above main() we give some directives like #include something, and then WE DECLARE a variable. We define type and initial value. We do not do math on declaration among them. ;) – George Eco Sep 28 '14 at 18:37
  • 1
    Well, this answer, right now, as it is, is wrong. That `int` behind the `result` inside `main` makes it a declaration which shadows the global one, so... I don't know. – Utkan Gezer Sep 28 '14 at 18:38
  • You are right ThoAppelsin i fixed that. – George Eco Sep 28 '14 at 18:40
  • Retracted my down-vote accordingly. – Utkan Gezer Sep 28 '14 at 18:40
  • In addition to providing a correct definition of `main()`, a better answer would move those variable definitions inside `main()` to begin with, or at least include that as an alternative, since in this particular case there is no reason at all to have them as globals. – Crowman Sep 28 '14 at 18:50
  • In any case, since you still learn C, let me rephrase what I just said so it can be clear. On Declaration, you can set a variable value as the value of a certain number or a constant. You can not set it as another variable's value though, even if you declared the other variables initial value. Hope this helped you. – George Eco Sep 28 '14 at 18:53
  • i actually think it was a compiler error, I kept the program the same and it compiled, apparently I hadn't put int result=numero*numerodos I had only put result=.... It compiled despite the reference to another variable outside of main. – Andres Meza Sep 30 '14 at 01:34
0

According to the C Standard (6.7.9 Initialization)

4 All the expressions in an initializer for an object that has static or thread storage duration shall be constant expressions or string literals.

and (6.6 Constant expressions)

2 A constant expression can be evaluated during translation rather than runtime, and accordingly may be used in any place that a constant may be

Take into account that there is no any need to declare variables in your program as global. You could write

#include <stdio.h>

int main( void )
{
   int numero=6;
   int numerodos=4;
   int result=numero*numerodos;


   while (result<200) {
      printf("The product of %d and %d is %d", numero, numerodos, result);
      result=result+1
   }
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Here, you are trying to dynamically intializing the variable but it is not possible in C. In C a variable can be assigned a constant value(any no,string) only at the time of initialization. It can be done in c++ or java.

Ashish Aggarwal
  • 334
  • 2
  • 13