Hi

I have started learning C and my first problem is very simple. I can't get output from a program! What am i doing wrong?

#include <stdio.h>
#include <stdlib.h>
int main()
{
        int a;
        int c;
        c = 0;
        int i;
        a = 0;
        for (i = 0;c = 0;i++) {
                printf("%i",a+i,"\n");
        }
        return 0;
}
~                                                                                                                              
~                                                                                                                              
~                                                                                                                              
~                                                                                                                              
~                                                                                                                              
~                              

Recommended Answers

All 5 Replies

One = is for assignment ONLY. Two ='s are needed for comparison: c==0. Since this is already true, the loop never starts.

Use

for(i=0;i<10;i++)
   printf("%d\n",i);

a is zero, so adding it to i will make no difference.

line 10 is incorrect. The = is an assignment, and the == is comparison (boolean). The mikddle part of that loop statement tells how many times the loop is to run, so if you say c == 0 then you are saying the loop continues until c becomes 0. Since the value of c is already 0 (at line 7) the loop will do nothing. You need to put something else there so that the loop will print something the desired number of times. If you want the loop to continue 10 times than do this

for(i = 0; i < 10; i++)

[edit]What ^^^ said :)

Thank you! I'm feeling so stupid...

We've all been there, Tadas. That's half the motivation to get better with our programming, to be honest. ;)

Welcome to the forum!

Thank you! I'm feeling so stupid...

17 years programming C and I still make that particular mistake (ie. using = instead of ==). No worries, it's a common and easy one to make. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.