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.

melpomene
  • 84,125
  • 8
  • 85
  • 148
kber
  • 19
  • 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. – Ian MacDonald Nov 25 '18 at 14:45

4 Answers4

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.

melpomene
  • 84,125
  • 8
  • 85
  • 148
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.

perreal
  • 94,503
  • 21
  • 155
  • 181
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++;
}
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
Jose
  • 3,306
  • 1
  • 17
  • 22
  • 1
    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. – Eric Postpischil Nov 25 '18 at 13:46
  • 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. – Eric Postpischil Nov 25 '18 at 13:48
  • @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 Nov 25 '18 at 13:54
  • @Aconcagua: Clang is generally pretty good, but I have not found a constraint in the C standard that this violates. I posted [this question](https://stackoverflow.com/questions/53468223/declare-structure-within-for) to see if anybody else can resolve it. – Eric Postpischil Nov 25 '18 at 13:59
  • 2
    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`. – Eric Postpischil Nov 25 '18 at 14:42
  • FYI, I [answered my question](https://stackoverflow.com/a/53566875/298225). I have concluded the likely intent was in fact to prohibit declaring anything other than identifiers for automatic or register objects in `for` statement, although the exclusion of declaring a structure type in the course of that may have been inadvertent. – Eric Postpischil Dec 01 '18 at 01:12
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.

Aconcagua
  • 24,880
  • 4
  • 34
  • 59