0
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <locale.h>

int main()
{
    setlocale(LC_ALL, "Rus");

    float arr[25];
    int i;
    double x;
    int a, b, c, e, f;
    double y = a * pow(x, 2) + b * x + c;

    printf("a: ");
    scanf_s("%d", &a);

    printf("b: ");
    scanf_s("%d", &b);

    printf("c: ");
    scanf_s("%d", &c);

    printf("e: ");
    scanf_s("%d", &e);

    printf("f: ");
    scanf_s("%d", &f);

    double interval = (f - e) / 25.0 ;

    for (int i = 0, double x = e; i < 25; i++, x += interval)
    {
        printf("%f", y);
        x++;
    }

    system("pause");
}

I get [Error] expected identifier or '(' before 'double'. How can i fix it? It doesnt seem like i really need to change something in

for (int i = 0, double x = e; i < 25; i++, x += interval)

or maybe im wrong and dont know how to write multiple conditions.

1
  • Are you intentionally doing x++ and x += interval in your iteration? Also, your iteration could be rewritten as simple arithmetic.. because y doesn’t change value, you’ll get the same number 25 times. Commented Nov 25, 2018 at 14:45

4 Answers 4

2

Yeah, you can't do that.

By the way, those are declarations, not conditions. Only the middle part of a for loop is a condition.

You can declare multiple variables in the first part of a for loop, but only if they have the same base type (e.g. int):

for (int x = 1, y = 2, z = 3; ...; ...)

The workaround in your case is to declare at least one of the variables outside of the loop:

{  // this outer block limits the scope of x
    double x = e;
    for (int i = 0; i < 25; i++, x += interval) 
    {
        printf("%f", y);
        x++;
    }
}

That said, your code doesn't really make sense. Your loop doesn't use x, so there's no point in setting it. On the other hand, the value you're printing 25 times (y) doesn't change in the loop. It's set at the top of your main function, computed from a different x variable that is uninitialized.

You should move the declaration and initialization of y into the loop and delete the outer x. See also https://stackoverflow.com/a/53238897/1848654.

Sign up to request clarification or add additional context in comments.

Comments

1

You can't define variables with multiple types with the comma:

for(int i = 0, double x... 

Instead:

x = e;
for (int i = 0; i<...

and the x is already defined above.

Comments

1

You could embedded them into a struct. I do not recommend it because IMO it is not a good coding practice as it is not easy to understand (at first sight)…

typedef struct {int i; double x;} S;
for (S yourStruct = {0,e}; yourStruct.i < 25 ; yourStruct.i++, yourStruct.x += interval)
{
    printf("%f", y);
    yourStruct.x++;
}

6 Comments

Clang objects to this with “declaration of non-local variable in 'for' loop”. I am looking for something in the C standard that governs it.
You can declare a structure object in the for loop; clang does not object to for (S yourStruct = {0, e};…), where S is a previously defined type. It is only the declaration of the structure type within the for that it objects to.
@EricPostpischil Correct me if I'm wrong, but within for loop, one should be able to use any declaration/definition one could use outside as well. So if void f(void) { struct { int x; } s; } is legal, one should be able to do the same in the for loop. So I conclude clang is wrong in rejecting it...
@Aconcagua: Clang is generally pretty good, but I have not found a constraint in the C standard that this violates. I posted this question to see if anybody else can resolve it.
What is happening here is that Clang believes the declaration of the structure type within the for violates C 2018 (and same in 1999) 6.8.5 3: “The declaration part of a for statement shall only declare identifiers for objects having storage class auto or register.” Since the declaration declares a type (6.7.2.1 8 in C 2018, paragraph 7 in C 1999: “The presence of a struct-declaration-list in a struct-or-union-specifier declares a new type…”), it is not true that the declaration part of this for only declares identifiers for objects having storage class auto or register.
|
0

It is not really a matter of the for loop:

void f(void)
{
    int x, y; // legal - and you can do the same in a for loop's initialization section
    int z, double d; // can't do that either, even outside for loop...
};

All variables that you declare in a single expression need to have the same (base!) type, be it within for loop, function body or global. 'Base type': Well, because you legally can do stuff like int x, *p;, with x and p being of different type, but base/underlying type in both cases is int.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.