how does increment & for loops work in c?

Status
Not open for further replies.

anantkhaitan

Burning Bright
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);
 
OP
clmlbx

clmlbx

Technomancer
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
 
Last edited:

Yamaraj

The Lord of Death
Guess you people skipped sequence points and their importance while reading your books. And BTW, that code is non-standard.
 

anantkhaitan

Burning Bright
@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

Google Bot
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

die blizzard die! D3?
^^exactly guys listen up!!different compiler interprets these increments inside printf statements differently afaik,so its like try before you use kind of thing.
 
OP
clmlbx

clmlbx

Technomancer
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



*
**
***
****
*****
 
Last edited:

fun2sh

Pawned!... Beyond GODLIKE
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
 
OP
clmlbx

clmlbx

Technomancer
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

Commander in Chief
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

Google Bot
#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.
 
OP
clmlbx

clmlbx

Technomancer
hi

this all are from left

how can I give output from right ?

means output to right side of screen
 
OP
clmlbx

clmlbx

Technomancer
STARTING FROM THE RIGHT SIDE AND ALSO ALIGNED TO RIGHT

AGAIN CAPS.......*gigasmilies.googlepages.com/actions1.gif
 

Faun

Wahahaha~!
Staff member
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

Broken In
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

Commander in Chief
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!
 
OP
clmlbx

clmlbx

Technomancer
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.....:(
 
Status
Not open for further replies.
Top Bottom