About for-loop's first argument | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

About for-loop's first argument

It works well when I put i=0 and printf in the for-loops first argument. https://code.sololearn.com/cOmt1bCE5Q3m/?ref=app But if I move the definition of i int for-loop, It just sent me an error. https://code.sololearn.com/cqAg5xoO4BOY/?ref=app Can anyone explain why? thx.

7th Oct 2018, 12:01 PM
左其右
左其右 - avatar
3 Answers
+ 1
左其右 I believe the problem is the same as if you do this: int i =1, accumulator = 0, printf('hello'); The compiler sees the printf as a declaration of an int so you get an error.
7th Oct 2018, 12:23 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 2
Ulisses Cruz I knew. But how can I declare a variable and run a function simultaneously at the start of the for-loop.
7th Oct 2018, 12:43 PM
左其右
左其右 - avatar
+ 1
左其右 The fist part of the for loop is for variables initialization, If you need to run a function at the start of a for loop, run it before the for loop, like this: printf('Hello'); // print function for(int i = 0; i < 10; i++){ // do something }
7th Oct 2018, 2:32 PM
Ulisses Cruz
Ulisses Cruz - avatar