Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 13-12-2007, 10:54 PM   #1 (permalink)
Wise Old Owl
 
clmlbx's Avatar
 
Join Date: Aug 2006
Location: Indore
Posts: 1,687
Default 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
clmlbx is online now  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 13-12-2007, 11:02 PM   #2 (permalink)
Burning Bright
 
anantkhaitan's Avatar
 
Join Date: May 2006
Location: NIT, Bhopal
Posts: 266
Default 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/
anantkhaitan is offline  
Old 13-12-2007, 11:23 PM   #3 (permalink)
Wise Old Owl
 
clmlbx's Avatar
 
Join Date: Aug 2006
Location: Indore
Posts: 1,687
Default 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
clmlbx is online now  
Old 14-12-2007, 12:24 AM   #4 (permalink)
Boom Bhadam Dhishkyao
 
timemachine's Avatar
 
Join Date: Sep 2007
Location: Bhopal
Posts: 44
Default 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
timemachine is offline  
Old 14-12-2007, 12:58 AM   #5 (permalink)
The Lord of Death
 
Yamaraj's Avatar
 
Join Date: May 2005
Location: यमलोक
Posts: 253
Default 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.
Yamaraj is offline  
Old 14-12-2007, 12:56 PM   #6 (permalink)
Burning Bright
 
anantkhaitan's Avatar
 
Join Date: May 2006
Location: NIT, Bhopal
Posts: 266
Default 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/
anantkhaitan is offline  
Old 14-12-2007, 01:44 PM   #7 (permalink)
Google Bot
 
Pathik's Avatar
 
Join Date: Aug 2005
Posts: 9,772
Default 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.
Pathik is offline  
Old 14-12-2007, 03:27 PM   #8 (permalink)
die blizzard die! D3?
 
The_Devil_Himself's Avatar
 
Join Date: Aug 2007
Location: Event horizon
Posts: 2,361
Default 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.
The_Devil_Himself is offline  
Old 15-12-2007, 12:12 AM   #9 (permalink)
Wise Old Owl
 
clmlbx's Avatar
 
Join Date: Aug 2006
Location: Indore
Posts: 1,687
Default 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
clmlbx is online now  
Old 15-12-2007, 01:04 AM   #10 (permalink)
Pawned!... Beyond GODLIKE
 
fun2sh's Avatar
 
Join Date: May 2006
Location: World Of Warcraft -DOTA
Posts: 1,051
Default 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:!!!!!!!!
fun2sh is offline  
Old 15-12-2007, 10:17 AM   #11 (permalink)
Wise Old Owl
 
clmlbx's Avatar
 
Join Date: Aug 2006
Location: Indore
Posts: 1,687
Default 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
clmlbx is online now  
Old 15-12-2007, 10:51 AM   #12 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default 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
QwertyManiac is offline  
Old 15-12-2007, 09:44 PM   #13 (permalink)
Google Bot
 
Pathik's Avatar
 
Join Date: Aug 2005
Posts: 9,772
Default 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.
Pathik is offline  
Old 15-12-2007, 09:52 PM   #14 (permalink)
Wise Old Owl
 
clmlbx's Avatar
 
Join Date: Aug 2006
Location: Indore
Posts: 1,687
Default 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
clmlbx is online now  
Old 15-12-2007, 09:53 PM   #15 (permalink)
Google Bot
 
Pathik's Avatar
 
Join Date: Aug 2005
Posts: 9,772
Default 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??
Pathik is offline  
Old 15-12-2007, 10:02 PM   #16 (permalink)
Wise Old Owl
 
clmlbx's Avatar
 
Join Date: Aug 2006
Location: Indore
Posts: 1,687
Default 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
clmlbx is online now  
Old 15-12-2007, 11:21 PM   #17 (permalink)
Wahahaha~!
 
Faun's Avatar
 
Join Date: Dec 2006
Location: Pune/there
Posts: 7,675
Default 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
__________________
Blog | Flickr | Battlelog
Spoiler:
Asus Z68 V-Pro|i5 2500k|TRUE Black|Ripjaws X|U2311H|N560GTX|D7000|XONAR STX|RE272|RE0|CC51|XE200PRO Walnut| TD II V2| Ultraphile|N5800

Mono
Faun is online now  
Old 16-12-2007, 11:12 AM   #18 (permalink)
Apprentice
 
aditya_v's Avatar
 
Join Date: Dec 2007
Posts: 54
Default 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.
aditya_v is offline  
Old 16-12-2007, 11:31 AM   #19 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default 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
QwertyManiac is offline  
Old 16-12-2007, 02:16 PM   #20 (permalink)
Wise Old Owl
 
clmlbx's Avatar
 
Join Date: Aug 2006
Location: Indore
Posts: 1,687
Smile 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
clmlbx is online now  
Old 16-12-2007, 02:25 PM   #21 (permalink)
Wahahaha~!
 
Faun's Avatar
 
Join Date: Dec 2006
Location: Pune/there
Posts: 7,675
Default 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
__________________
Blog | Flickr | Battlelog
Spoiler:
Asus Z68 V-Pro|i5 2500k|TRUE Black|Ripjaws X|U2311H|N560GTX|D7000|XONAR STX|RE272|RE0|CC51|XE200PRO Walnut| TD II V2| Ultraphile|N5800

Mono
Faun is online now  
Old 16-12-2007, 02:59 PM   #22 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default 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
QwertyManiac is offline  
Old 16-12-2007, 05:12 PM   #23 (permalink)
The Smaller Bang
 
MetalheadGautham's Avatar
 
Join Date: Sep 2007
Location: Gautham City
Posts: 7,489
Default 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
MetalheadGautham is offline  
Old 17-12-2007, 09:55 PM   #24 (permalink)
Wise Old Owl
 
clmlbx's Avatar
 
Join Date: Aug 2006
Location: Indore
Posts: 1,687
Default 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
clmlbx is online now  
Old 17-12-2007, 10:14 PM   #25 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default 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:
Code:
As you expect.
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 17-12-2007, 11:10 PM   #26 (permalink)
Wise Old Owl
 
clmlbx's Avatar
 
Join Date: Aug 2006
Location: Indore
Posts: 1,687
Default 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
clmlbx is online now  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Work, Work & Work.... u get no raise ! vish786 Chit-Chat 5 14-09-2007 03:49 PM
Will it work? Cool G5 Mobiles and Tablets 5 24-12-2006 02:24 PM
fruity loops tutorials pranavrahul QnA (read only) 7 01-11-2006 11:24 PM
Fruity Loops Studio 5 rajkumar_personal QnA (read only) 7 08-06-2005 06:35 PM
FRoooTY loops Help vijaythefool QnA (read only) 4 09-01-2005 09:42 PM

 
Latest Threads
- by Charan
- by Sarath
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 12:45 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2