 |
13-12-2007, 10:54 PM
|
#1 (permalink)
|
|
Wise Old Owl
Join Date: Aug 2006
Location: Indore
Posts: 1,687
|
how does increment & for loops work in c?
a=2
printf(++a * a++ * a++ * ++a)
answer is 216
how ??
and how is for loop used ?
__________________
| Athlon II X4 635 @ 2.9Ghz | | Gigabyte GA-MA785GMT-US2H | | Kingston 2x2 Gb 1333Mhz DDR3 | | WDC 500Gb Green | | Palit GTS 250 512mb | | Tagan 500W | | Samsung B2030 | | Lg DVD Writer |
|
|
|
|
Advertisements. Register and be a member of the community to get rid of them.
|
|
Advertisement
|
|
13-12-2007, 11:02 PM
|
#2 (permalink)
|
|
Burning Bright
Join Date: May 2006
Location: NIT, Bhopal
Posts: 266
|
Re: how does increment & for loops work in c?
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);
__________________
..::Fedora ::.. Freedom + Infinity + Speech
Registered Linux User #447318
GNUger was here.... Grrr....
Maah! Blog
http://brightedges.blogspot.com/
|
|
|
13-12-2007, 11:23 PM
|
#3 (permalink)
|
|
Wise Old Owl
Join Date: Aug 2006
Location: Indore
Posts: 1,687
|
Re: how does increment & for loops work in c?
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
__________________
| Athlon II X4 635 @ 2.9Ghz | | Gigabyte GA-MA785GMT-US2H | | Kingston 2x2 Gb 1333Mhz DDR3 | | WDC 500Gb Green | | Palit GTS 250 512mb | | Tagan 500W | | Samsung B2030 | | Lg DVD Writer |
Last edited by clmlbx; 13-12-2007 at 11:23 PM.
Reason: Automerged Doublepost
|
|
|
14-12-2007, 12:24 AM
|
#4 (permalink)
|
|
Boom Bhadam Dhishkyao
Join Date: Sep 2007
Location: Bhopal
Posts: 44
|
Re: how does increment & for loops work in c?
Pre increment : Increment then operate
Post increment : Operate then increment
__________________
In my life, there are only two rules....
Rule No 1: I am always right...
Rule No 2: If i am wrong, please see rule no 1 again
|
|
|
14-12-2007, 12:58 AM
|
#5 (permalink)
|
|
The Lord of Death
Join Date: May 2005
Location: यमलोक
Posts: 253
|
Re: how does increment & for loops work in c?
Guess you people skipped sequence points and their importance while reading your books. And BTW, that code is non-standard.
|
|
|
14-12-2007, 12:56 PM
|
#6 (permalink)
|
|
Burning Bright
Join Date: May 2006
Location: NIT, Bhopal
Posts: 266
|
Re: how does increment & for loops work in c?
@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
__________________
..::Fedora ::.. Freedom + Infinity + Speech
Registered Linux User #447318
GNUger was here.... Grrr....
Maah! Blog
http://brightedges.blogspot.com/
|
|
|
14-12-2007, 01:44 PM
|
#7 (permalink)
|
|
Google Bot
Join Date: Aug 2005
Posts: 9,772
|
Re: how does increment & for loops work in c?
Quote:
|
Originally Posted by clmlbx
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
Quote:
|
Originally Posted by clmlbx
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.
|
|
|
14-12-2007, 03:27 PM
|
#8 (permalink)
|
|
die blizzard die! D3?
Join Date: Aug 2007
Location: Event horizon
Posts: 2,361
|
Re: how does increment & for loops work in c?
^^exactly guys listen up!!different compiler interprets these increments inside printf statements differently afaik,so its like try before you use kind of thing.
__________________
Stealing your women and horses since 1843.
|
|
|
15-12-2007, 12:12 AM
|
#9 (permalink)
|
|
Wise Old Owl
Join Date: Aug 2006
Location: Indore
Posts: 1,687
|
Re: how does increment & for loops work in c?
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
*
**
***
****
*****
__________________
| Athlon II X4 635 @ 2.9Ghz | | Gigabyte GA-MA785GMT-US2H | | Kingston 2x2 Gb 1333Mhz DDR3 | | WDC 500Gb Green | | Palit GTS 250 512mb | | Tagan 500W | | Samsung B2030 | | Lg DVD Writer |
Last edited by clmlbx; 15-12-2007 at 12:26 AM.
Reason: Automerged Doublepost
|
|
|
15-12-2007, 01:04 AM
|
#10 (permalink)
|
|
Pawned!... Beyond GODLIKE
Join Date: May 2006
Location: World Of Warcraft -DOTA
Posts: 1,051
|
Re: how does increment & for loops work in c?
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.
Quote:
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
|
__________________
If God has indeed created Himself in His own image, then I submit to you that God is a cockroach :mrgreen:!!!!!!!!
|
|
|
15-12-2007, 10:17 AM
|
#11 (permalink)
|
|
Wise Old Owl
Join Date: Aug 2006
Location: Indore
Posts: 1,687
|
Re: how does increment & for loops work in c?
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
__________________
| Athlon II X4 635 @ 2.9Ghz | | Gigabyte GA-MA785GMT-US2H | | Kingston 2x2 Gb 1333Mhz DDR3 | | WDC 500Gb Green | | Palit GTS 250 512mb | | Tagan 500W | | Samsung B2030 | | Lg DVD Writer |
|
|
|
15-12-2007, 10:51 AM
|
#12 (permalink)
|
|
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
|
Re: how does increment & for loops work in c?
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
__________________
Harsh J
www.harshj.com
|
|
|
15-12-2007, 09:44 PM
|
#13 (permalink)
|
|
Google Bot
Join Date: Aug 2005
Posts: 9,772
|
Re: how does increment & for loops work in c?
#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.
|
|
|
15-12-2007, 09:52 PM
|
#14 (permalink)
|
|
Wise Old Owl
Join Date: Aug 2006
Location: Indore
Posts: 1,687
|
Re: how does increment & for loops work in c?
hi
this all are from left
how can I give output from right ?
means output to right side of screen
__________________
| Athlon II X4 635 @ 2.9Ghz | | Gigabyte GA-MA785GMT-US2H | | Kingston 2x2 Gb 1333Mhz DDR3 | | WDC 500Gb Green | | Palit GTS 250 512mb | | Tagan 500W | | Samsung B2030 | | Lg DVD Writer |
|
|
|
15-12-2007, 09:53 PM
|
#15 (permalink)
|
|
Google Bot
Join Date: Aug 2005
Posts: 9,772
|
Re: how does increment & for loops work in c?
Do u need output starting from the right side of the screen or aligned to the right??
|
|
|
15-12-2007, 10:02 PM
|
#16 (permalink)
|
|
Wise Old Owl
Join Date: Aug 2006
Location: Indore
Posts: 1,687
|
Re: how does increment & for loops work in c?
STARTING FROM THE RIGHT SIDE AND ALSO ALIGNED TO RIGHT
AGAIN CAPS.......
__________________
| Athlon II X4 635 @ 2.9Ghz | | Gigabyte GA-MA785GMT-US2H | | Kingston 2x2 Gb 1333Mhz DDR3 | | WDC 500Gb Green | | Palit GTS 250 512mb | | Tagan 500W | | Samsung B2030 | | Lg DVD Writer |
|
|
|
15-12-2007, 11:21 PM
|
#17 (permalink)
|
|
Wahahaha~!
Join Date: Dec 2006
Location: Pune/there
Posts: 7,675
|
Re: how does increment & for loops work in c?
Quote:
|
Originally Posted by clmlbx
STARTING FROM THE RIGHT SIDE AND ALSO ALIGNED TO RIGHT
AGAIN CAPS....... 
|
Use ur intuition to solve this
I know u can do it  , ,and stop forgetting,probably u will suffer from Alzhemeir later. Make ur barin sharp
|
|
|
16-12-2007, 11:12 AM
|
#18 (permalink)
|
|
Apprentice
Join Date: Dec 2007
Posts: 54
|
Re: how does increment & for loops work in c?
guys, what solutions u are giving is wrt TC only
C++ doesn't enforce this issue, as said by Bjarne Stroustrup
Quote:
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] = i++;
Related example:
f(v[i],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.
|
|
|
|
16-12-2007, 11:31 AM
|
#19 (permalink)
|
|
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
|
Re: how does increment & for loops work in c?
Quote:
|
Originally Posted by clmlbx
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!
__________________
Harsh J
www.harshj.com
|
|
|
16-12-2007, 02:16 PM
|
#20 (permalink)
|
|
Wise Old Owl
Join Date: Aug 2006
Location: Indore
Posts: 1,687
|
Re: how does increment & for loops work in c?
Quote:
|
Originally Posted by QwertyManiac
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  .( my prevoious was not good  )
nothing more, and here guys are to help so I asked.....
__________________
| Athlon II X4 635 @ 2.9Ghz | | Gigabyte GA-MA785GMT-US2H | | Kingston 2x2 Gb 1333Mhz DDR3 | | WDC 500Gb Green | | Palit GTS 250 512mb | | Tagan 500W | | Samsung B2030 | | Lg DVD Writer |
|
|
|
16-12-2007, 02:25 PM
|
#21 (permalink)
|
|
Wahahaha~!
Join Date: Dec 2006
Location: Pune/there
Posts: 7,675
|
Re: how does increment & for loops work in c?
Quote:
|
Originally Posted by clmlbx
I missed one class and then to I want to complete this assignment to make a good impression  .( 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
|
|
|
16-12-2007, 02:59 PM
|
#22 (permalink)
|
|
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
|
Re: how does increment & for loops work in c?
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:
http://www.cplusplus.com/reference/c...io/printf.html
__________________
Harsh J
www.harshj.com
|
|
|
16-12-2007, 05:12 PM
|
#23 (permalink)
|
|
The Smaller Bang
Join Date: Sep 2007
Location: Gautham City
Posts: 7,489
|
Re: how does increment & for loops work in c?
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
__________________
http://TheSmallerBang.wordpress.com
eMachines E725 - T4400 2.2GHz, 1GB, 160GB
Nokia 5130XM * T-Sonic 610 2GB
Nokia 2323C * Samsung Galaxy Y
Apple iPad 2 16GB WiFi
|
|
|
17-12-2007, 09:55 PM
|
#24 (permalink)
|
|
Wise Old Owl
Join Date: Aug 2006
Location: Indore
Posts: 1,687
|
Re: how does increment & for loops work in c?
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
__________________
| Athlon II X4 635 @ 2.9Ghz | | Gigabyte GA-MA785GMT-US2H | | Kingston 2x2 Gb 1333Mhz DDR3 | | WDC 500Gb Green | | Palit GTS 250 512mb | | Tagan 500W | | Samsung B2030 | | Lg DVD Writer |
|
|
|
17-12-2007, 10:14 PM
|
#25 (permalink)
|
|
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
|
Re: how does increment & for loops work in c?
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:
__________________
Harsh J
www.harshj.com
|
|
|
17-12-2007, 11:10 PM
|
#26 (permalink)
|
|
Wise Old Owl
Join Date: Aug 2006
Location: Indore
Posts: 1,687
|
Re: how does increment & for loops work in c?
thank you very much
thanxx a lotttttttt 
__________________
| Athlon II X4 635 @ 2.9Ghz | | Gigabyte GA-MA785GMT-US2H | | Kingston 2x2 Gb 1333Mhz DDR3 | | WDC 500Gb Green | | Palit GTS 250 512mb | | Tagan 500W | | Samsung B2030 | | Lg DVD Writer |
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|