DIGIT-ARC read-only archive · snapshot: July 23, 2026

how does increment & for loops work in c?

26 posts · originally from https://geek.digit.in/community/threads/how-does-increment-for-loops-work-in-c.75656/

clmlbx
clmlbx Original post #1
Dec 13, 2007 at 10:54 PM
a=2

printf(++a * a++ * a++ * ++a)

answer is 216

how ??

and how is for loop used ?
anantkhaitan
Dec 13, 2007 at 11:02 PM
Code:
printf(++a * a++ * a++ * ++a)
bcoz we start count from right side.. now look
++a -> 3
a++ -> 4
a++ -> 5
++a -> 6
and ultimately 6*6*6*6 gives 216

Code:
how is for loop used ?
as per the syntax is
Code:
for(initialization;condition;value modification){loop body}
Example:
Code:
for(int i=0;i<10;i++)cprintf("%d",i);
clmlbx
clmlbx #3
Dec 13, 2007 at 11:23 PM
what about this

printf(++a*a++*++a)

ans 45

^^^ you mean to say left to right

why did u took

6*6*6*6

what is the difference between pre & post increment
timemachine
Dec 14, 2007 at 12:24 AM
Pre increment : Increment then operate
Post increment : Operate then increment
Yamaraj
Yamaraj #5
Dec 14, 2007 at 12:58 AM
Guess you people skipped sequence points and their importance while reading your books. And BTW, that code is non-standard.
anantkhaitan
Dec 14, 2007 at 12:56 PM
@clmlbx
You are correct brother..What I told you was my CBSE based bookish knowledge.. I am sorry for that..
It should have been : 4*3*3*3 thats what logic says ..Well today I tried the program (GCC) and output was 108 equals to 4*3*3*3
Code:
#include<stdio.h>
int main()
{
int a=2;
printf("%d",++a*a++*a++*++a);
}

For your second problem the answer should be 36 i.e. 4*3*3 and that is what I got after compiling
Pathik
Pathik #7
Dec 14, 2007 at 1:44 PM
clmlbx said:
a=2

printf(++a * a++ * a++ * ++a)

answer is 216

how ??

and how is for loop used ?
See
++a means a is first incremented then used and a++ means a is first used then incremented.
So this would mean (3*3*4*6) = 216
clmlbx said:
what about this

printf(++a*a++*++a)

ans 45

^^^ you mean to say left to right

why did u took

6*6*6*6

what is the difference between pre & post increment
And this would mean (3*3*5) = 45..
Though the ansewrs may vary with the compiler used.
The_Devil_Himself
Dec 14, 2007 at 3:27 PM
^^exactly guys listen up!!different compiler interprets these increments inside printf statements differently afaik,so its like try before you use kind of thing.
clmlbx
clmlbx #9
Dec 15, 2007 at 12:12 AM
can it be more clear ?

add one before or after it is the same thing ..............

how will this program work?

1
12
123
1234
12345

I know using "for" loops and using "\t,\n" but how ............

*
**
***
****
*****,


1
2 4
3 6 9
4 8 12
5 10 15 20



*
**
***
****
*****
fun2sh
fun2sh #10
Dec 15, 2007 at 1:04 AM
its simple. wen priority is same then unirary exp are evaluated from right to left.
so here is wat happens for
1. a++ -----> means the current value of 'a' is used in exp then it is incremented
2. ++a------> means the current value is incremented by 1 first then that new incremented value is used in the code.

so for ur exp
++a..................*...............a++................... *........a++ ....................* ++a
3 (first incre ..................3 (use then ............. 4(use curren ......................6(coz last a=5)
then use,a=3 now)..........incre,a=4 now)..........then incre,a=5now
clmlbx
clmlbx #11
Dec 15, 2007 at 10:17 AM
guys I know this is not correct but can someone write that programme for me ..
these are like examples, I have many of them ,so if I got to know how u did this then I will try others

post no. 9
QwertyManiac
Dec 15, 2007 at 10:51 AM
Here you go. 3 simple loop constructs. The i-loop is used for the number of lines to print, and the j-loop is used to display the content needed.

Code:
#include<stdio.h>

int main(void)
{
	int i,j;
	
	for(i=1;i<6;i++)
	{
		for(j=1;j<i+1;j++)
		{
			printf("%d ",j);
		}
		printf("\n");
	}
	
	printf("\n");
	
	for(i=1;i<6;i++)
	{
		for(j=1;j<i+1;j++)
		{
			printf("* ");
		}
		printf("\n");
	}
	
	printf("\n");
	
	for(i=1;i<6;i++)
	{
		for(j=1;j<i+1;j++)
		{
			printf("%d ",i*j);
		}
		printf("\n");
	}
	
	printf("\n");
	return 0;
}
Outputs as:
Code:
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 

* 
* * 
* * * 
* * * * 
* * * * * 

1 
2 4 
3 6 9 
4 8 12 16 
5 10 15 20 25
Pathik
Pathik #13
Dec 15, 2007 at 9:44 PM
#include<stdio.h>

int main(void)
{
int i,j;

for(i=1;i<6;i++)
{
for(j=1;j<i+1;j++)
{
printf("*");
}
printf("\n");
}

printf("\n");
return 0;
}
Arey just remove the SPACE after the * in the printf.
clmlbx
clmlbx #14
Dec 15, 2007 at 9:52 PM
hi

this all are from left

how can I give output from right ?

means output to right side of screen
Pathik
Pathik #15
Dec 15, 2007 at 9:53 PM
Do u need output starting from the right side of the screen or aligned to the right??
clmlbx
clmlbx #16
Dec 15, 2007 at 10:02 PM
STARTING FROM THE RIGHT SIDE AND ALSO ALIGNED TO RIGHT

AGAIN CAPS.......*gigasmilies.googlepages.com/actions1.gif
Faun
Faun #17
Dec 15, 2007 at 11:21 PM
clmlbx said:
STARTING FROM THE RIGHT SIDE AND ALSO ALIGNED TO RIGHT

AGAIN CAPS.......*gigasmilies.googlepages.com/actions1.gif
Use ur intuition to solve this:D

I know u can do it*farm3.static.flickr.com/2074/2106526281_d24c2bda14_o.png, ,and stop forgetting,probably u will suffer from Alzhemeir later. Make ur barin sharp*farm3.static.flickr.com/2398/2107306002_6240bfbc60_o.png
aditya_v
aditya_v #18
Dec 16, 2007 at 11:12 AM
guys, what solutions u are giving is wrt TC only
C++ doesn't enforce this issue, as said by Bjarne Stroustrup

What's the value of i++ + i++?

It's undefined. Basically, in C and C++, if you read a variable twice in an expression where you also write it, the result is undefined. Don't do that. Another example is:

v = i++;

Related example:

f(v,i++);

Here, the result is undefined because the order of evaluation of function arguments are undefined.

Having the order of evaluation undefined is claimed to yield better performing code. Compilers could warn about such examples, which are typically subtle bugs (or potential subtle bugs). I'm disappointed that after decades, most compilers still don't warn, leaving that job to specialized, separate, and underused tools.
QwertyManiac
Dec 16, 2007 at 11:31 AM
clmlbx said:
hi

this all are from left

how can I give output from right ?

means output to right side of screen
Look for Output flags (printf flags) in your text book. Do the guys at Ape-tech not teach you ANYTHING at all? These questions are very trivial even for a new programmer!
clmlbx
clmlbx #20
Dec 16, 2007 at 2:16 PM
QwertyManiac said:
Look for Output flags (printf flags) in your text book. Do the guys at Ape-tech not teach you ANYTHING at all? These questions are very trivial even for a new programmer!

I missed one class and then to I want to complete this assignment to make a good impression :D :D .( my prevoious was not good :( )


nothing more, and here guys are to help so I asked.....:(
Faun
Faun #21
Dec 16, 2007 at 2:25 PM
clmlbx said:
I missed one class and then to I want to complete this assignment to make a good impression :D :D .( my prevoious was not good :( )


nothing more, and here guys are to help so I asked.....:(
put ur brain into the subject, logic is all u need.

These things are the simplest ones, the real nightmare is advanced data structures. So save ur questions for that time.

U posted now without CAPS on :D
QwertyManiac
Dec 16, 2007 at 2:59 PM
All you have to do is search.

What you are asking for is left justification I think, so read up on the printf function at CPlusPlus, read the flags part:
*www.cplusplus.com/reference/clibrary/cstdio/printf.html
MetalheadGautham
Dec 16, 2007 at 5:12 PM
I think the only reason this thread has these many replies is because this being a simple question, everyone can "prove" that he/she is a programmer :lol:
clmlbx
clmlbx #24
Dec 17, 2007 at 9:55 PM
ok guys whatever you say ?

I did not try and tomorrow morning is my class so can anybody give e the code ?

please......please

*****
-****
--***
---**
----*

and

----*
---**
--***
-****
*****

"-" = spaces

please some one give me code
QwertyManiac
Dec 17, 2007 at 10:14 PM
Don't ask code again, its time you set into dusted off your brain-cobwebs and put it to some use!

Code:
#include<stdio.h>

int main(void)
{
    int i,j;
    char a='*';
    for(i=6;i>0;i--)
    {
        for(j=6;j>i;j--)
        {
            printf("  ");
        }
        
        for(j=1;j<i;j++)
        {
            printf("%c ",a);
        }
        printf("\n");
    }
    
    printf("\n");
    
    for(i=1;i<6;i++)
    {
        for(j=5;j>i;j--)
        {
            printf("  ");
        }
    
        for(j=1;j<i+1;j++)
        {
            printf("%c ",a);
        }
        printf("\n");
    }
    
    printf("\n");
    return 0;
}
Outputs as:
Code:
As you expect.
clmlbx
clmlbx #26
Dec 17, 2007 at 11:10 PM
thank you very much

thanxx a lotttttttt *gigasmilies.googlepages.com/1.gif*gigasmilies.googlepages.com/36.gif