tags:

views:

95

answers:

4

While programming I have come to an unusual error. When I initialize an integer in a loop, sometimes it says that the expression is not valid, but at times it accepts it. This is my code which gives error:

int pow(int x,int n);
int main()
{
    int x,n,result;
    printf("Enter a number:\n");
    scanf("%d",&x);
    printf("Enter its power:\n");
    scanf("%d",&n);
    result=pow(x,n);
    printf("Result is %d\n",result);
    getch();
    return 0;
}
int pow(int x,int n)
{   
    for(int i=1;i<n;i++)   //<-- here it says that declaration syntax error
    x=x*i;
    return x;
}

While when i change it like this :

int pow(int x,int n)
{   
    int i;
    for(i=1;i<n;i++)  
    x=x*i;
    return x;
}
+7  A: 

In C, before C99, you need to declare your variables before your loop. In C++, and now in C99, you can declare them within the loop as you try here. The different results you are getting may be because you are sometimes compiling as C++, and sometimes compiling as C.

You could try to make sure you are always compiling your code as C++, or if you're using GCC or Clang, compile with the --std=c99 flag. C99 is unsupported by MSVC, so you will either need to use C++ or move the declaration outside of the loop if you're using MSVC.

Brian Campbell
I use a turbo c compiler
fahad
@fahad Sorry, I can't help you with the specifics of Turbo C. As I said, if you want the first example to work, make sure you set it to C++ or C99 mode (probably C++, I don't think Turbo C supports C99).
Brian Campbell
Thanks a lot :) I will find Ming GW for that .
fahad
+2  A: 

What C compiler are you using?

Older versions of C prior to C99 require all variable declarations be made at the top of a code block.

Justin Ethier
the turbo c compiler
fahad
which version of turbo c? If I remember anything from college in India, it's that the turbo C and borland C compilers we had were older than the professors. chances are that you're facing the same issue.
bluesmoon
The last release of Borland Turbo C was in 1989 (twenty-one years ago!). That would explain why it doesn't support features instituted a decade after it was created. I would strongly suggest you get a newer compiler, for many reasons. MinGW is an excellent, free, modern C99 compiler for windows based on gcc. Microsoft VC++ Express Edition is also free and modern, but is only a C89 compiler when it is working with C code.
Tyler McHenry
@bluesmoon:yes it its turbo c 3.0 compiler(borland)
fahad
turbo C 3.0 may not even be C89 compliant, but IIRC it was closer than any MS compiler in the same era as Borland actually participated or at least paid attention to the standards process. It took longer for MS to join that party...
RBerteig
+4  A: 

It sounds like you have a C89 compiler (rather than C99 compiler).

In C89, you are only allowed to declare variables at the beginning of a function. You are simply not allowed to declare variables elsewhere in a function, including in the initialization part of a for statement. That's why the second syntax works and the first fails.

The second syntax is valid for C99 and C++ but not for C89.

Tyler McHenry
but at times it works .Why does that happens?
fahad
Can you add to your question a code snippet where you declare the variable inside the `for` statement and it works? Without that to compare the non-working version to, there's no way to tell you what the difference is.
Tyler McHenry
If you see the code i have done that
fahad
You posted one piece of code where you declare it *inside* the `for` statement, and it doesn't work. Then you have another piece of code where you declare it *before* the `for` statement and it does work. The reason that they produce different results is because they declare the variable in different places. One of them declares the variable in a legal place, and the other declares the variable in an illegal place. Much like anything else in programming, the times when it works are the times when you write it the correct, legal way.
Tyler McHenry
+2  A: 

C89 and earlier versions only support declaration statements at the head of a block (IOW, the only thing that can appear between an opening { and a declaration is another declaration):

/* C89 and earlier */
int main(void)
{
  int x;                      /* this is legal */
  ...
  for (x = 0; x < 10; x++)
  {
    int i;                    /* so is this */
    printf("x = %d\n", x);
    int j = 2*x;              /* ILLEGAL */
  }
  int y;                      /* ILLEGAL */
  ...
}

With C99, declaration statements can appear pretty much anywhere, including control expressions (with the caveat that something must be declared before it is used):

// C99 and later, C++
int main(void)
{
  int x;                       // same as before
  ...
  for (int i = 0; i < 10; i++) // supported as of C99
  {
    printf("i = %d\n", i);
    int j = 2 * i;             // supported as of C99
  }
  int y;                       // supported as of C99
}

Turbo C predates the C99 standard, so if you want to write code like in the second example, you will need to use a more up-to-date compiler.

John Bode
Thanx.Really helped
fahad