14 Replies - 741 Views - Last Post: 24 May 2009 - 12:25 AM Rate Topic: -----

#1 pradosh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 20-May 07

C questions with fundamental Query.i want opinion from my esteemed

Posted 23 May 2009 - 07:56 AM

I tried this two question.I have some basic doubts on it.

int main(

int a=5,b=10;
if(a==b,a++)
printf("Good\n");
else
printf("Bad");
printf("%d",a);
getch();
return 0;
}


output:good
6

I know that in case of comma operator operation will be done fromleft to right.
so 1st a==b will be performed
2nd a,a=a+1
Output is right, but still i need your thoughts on this.
priority of operator
--------------------
increament operator
equality operator
comma operator

I am just little confused.

The second question:

main(){
int k=35;
printf("\n %d %d %d ",k==35,k=50,k>40);
}



How is the output is 0 50 0


Is This A Good Question/Topic? 0
  • +

Replies To: C questions with fundamental Query.i want opinion from my esteemed

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: C questions with fundamental Query.i want opinion from my esteemed

Posted 23 May 2009 - 08:26 AM

How about you offer your best guess as to why the answers are what they are and people can offer their agreement or their corrections?

The examples you are giving are clearly intended to demonstrate a point and it seems that such a demonstration would be offered after the concepts had been explored in the book or web site you are referring to.

They are almost certainly meant to reinforce what you have already been told.

Show us that you did the preliminary work and need some help with some parts of it and that we aren't just answering your homework quizzes for you.
Was This Post Helpful? 0
  • +
  • -

#3 pradosh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 20-May 07

Re: C questions with fundamental Query.i want opinion from my esteemed

Posted 23 May 2009 - 08:42 AM

View Postjanotte, on 23 May, 2009 - 07:26 AM, said:

How about you offer your best guess as to why the answers are what they are and people can offer their agreement or their corrections?

The examples you are giving are clearly intended to demonstrate a point and it seems that such a demonstration would be offered after the concepts had been explored in the book or web site you are referring to.

They are almost certainly meant to reinforce what you have already been told.

Show us that you did the preliminary work and need some help with some parts of it and that we aren't just answering your homework quizzes for you.




Hi janotte,
I seriously conside your point.I am putting my effort, i know my questions are easy and basic. Is it wrong to put my view and answer with the question? Will I ask the question only and wait for other members reply?
I would like you to be more specific if you are getting anything wrong .. the way I am posting my query. I am open to your comments.

This post has been edited by pradosh: 23 May 2009 - 08:51 AM

Was This Post Helpful? 0
  • +
  • -

#4 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: C questions with fundamental Query.i want opinion from my esteemed

Posted 23 May 2009 - 11:35 AM

Basically for the
#include <iostream>
using namespace std;
main(){
int k=35;
printf("\n %d %d %d ", k == 35, k = 50, k > 40);
}


Portion, the output is 0 50 0 because with printf it reads from right to left when placing in the values for the %d and the reason you are getting a 0 50 0 output is because reading from right to left, k is not greater than 40, than you're setting k equal to 50 which makes it print the 50, by making k equal to 50, k no longer is 35 which prints a 0. Basically it's saying
k>40 //false print a 0
k = 50 //it does equal fifty now, so print the 50
k == 35 //false print a zero.


printf("\n %d %d %d ", k == 35, k = 50, k > 40); //take this and switch around to this
printf("\n %d %d %d ", k > 40, k = 50, k == 35); //The output looks like 1 50 1


This post has been edited by IngeniousHax: 23 May 2009 - 11:36 AM

Was This Post Helpful? 1
  • +
  • -

#5 pradosh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 20-May 07

Re: C questions with fundamental Query.i want opinion from my esteemed

Posted 23 May 2009 - 11:55 AM

View PostIngeniousHax, on 23 May, 2009 - 10:35 AM, said:

Basically for the
#include <iostream>
using namespace std;
main(){
int k=35;
printf("\n %d %d %d ", k == 35, k = 50, k > 40);
}


Portion, the output is 0 50 0 because with printf it reads from right to left when placing in the values for the %d and the reason you are getting a 0 50 0 output is because reading from right to left, k is not greater than 40, than you're setting k equal to 50 which makes it print the 50, by making k equal to 50, k no longer is 35 which prints a 0. Basically it's saying
k>40 //false print a 0
k = 50 //it does equal fifty now, so print the 50
k == 35 //false print a zero.


printf("\n %d %d %d ", k == 35, k = 50, k > 40); //take this and switch around to this
printf("\n %d %d %d ", k > 40, k = 50, k == 35); //The output looks like 1 50 1






It's been a lucid explanation by you.But I am still having this doubt that ...When there's comma operator the assciativity order will be from left to right .
In the case of the first question the two expression inside the if clause are being separated by the comma operator what's the
order there?

This post has been edited by pradosh: 23 May 2009 - 11:56 AM

Was This Post Helpful? 0
  • +
  • -

#6 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: C questions with fundamental Query.i want opinion from my esteemed

Posted 23 May 2009 - 12:06 PM

If you switch around the two statements in the if to
if(a++, a==b)


It will go to the else statement and print bad, because if you add 1 to a, does a==b? No, so it runs to the else statement and prints bad 6. IDK why it prints good 6 when they are in the original order though.

EDIT::
This code produces the correct results, but im not sure why either
#include <iostream>
#include <cstdlib>
using namespace std;
int main(){
int a = 8;
int b = 10;
if(a++, a == b) //I imagine at this point, youre incrementing a, and than checking it against b, where as previously you were checking it against
					  //b and than incrementing a, but IDK why it would produce a false positive.
printf("Good-> %d\n", a);
else
printf("Bad -> a = %d b = %d\n", a, b);
return 0;
}


This post has been edited by IngeniousHax: 23 May 2009 - 12:13 PM

Was This Post Helpful? 0
  • +
  • -

#7 pradosh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 20-May 07

Re: C questions with fundamental Query.i want opinion from my esteemed

Posted 23 May 2009 - 12:32 PM

View PostIngeniousHax, on 23 May, 2009 - 11:06 AM, said:

If you switch around the two statements in the if to
if(a++, a==b)


It will go to the else statement and print bad, because if you add 1 to a, does a==b? No, so it runs to the else statement and prints bad 6. IDK why it prints good 6 when they are in the original order though.

EDIT::
This code produces the correct results, but im not sure why either
#include <iostream>
#include <cstdlib>
using namespace std;
int main(){
int a = 8;
int b = 10;
if(a++, a == b) //I imagine at this point, youre incrementing a, and than checking it against b, where as previously you were checking it against
					  //b and than incrementing a, but IDK why it would produce a false positive.
printf("Good-> %d\n", a);
else
printf("Bad -> a = %d b = %d\n", a, b);
return 0;
}





Hi IngeniousHax,
What's your thought on this. Inside the if clause we are having this post increament expression ,how come the evaluation is being done in that same step.

EDIT:

#include <iostream>
#include <cstdlib>
using namespace std;
int main(){
int a = 9;
int b = 10;
if(a++, a == b) //I imagine at this point, youre incrementing a, and than checking it against b, where as previously you were checking it against
					  //b and than incrementing a, but IDK why it would produce a false positive.
printf("Good-> %d\n", a);
else
printf("Bad -> a = %d b = %d\n", a, b);
return 0;
}



It's is giving me the result of 'If' part.
Was This Post Helpful? 0
  • +
  • -

#8 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: C questions with fundamental Query.i want opinion from my esteemed

Posted 23 May 2009 - 12:43 PM

Well basically the increment is being done with-in the if statement because you're telling it to, basically what it's saying here is--->
if ( a + 1 is equal to B)
print this stuff ------
else print other stuff
Was This Post Helpful? 1
  • +
  • -

#9 pradosh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 20-May 07

Re: C questions with fundamental Query.i want opinion from my esteemed

Posted 23 May 2009 - 11:41 PM

In this question I am really bit confused about the order of operators here.

int x,y=2,a=5;
x=y-2 && ++a;
cout<<x<<"\n"<<a;




I went through the code. The precedence of operators

1. ++
2.-
3.&&


But as the output of the question suggests, it's not executing the ++a part as the y-2 part is getting executed first and giving result 0 and the value of a remains 5 after that statement.
Why it's happening like this.?I would like to know the right explanation

This post has been edited by pradosh: 23 May 2009 - 11:43 PM

Was This Post Helpful? 0
  • +
  • -

#10 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: C questions with fundamental Query.i want opinion from my esteemed

Posted 23 May 2009 - 11:50 PM

I'm not sure, so I wrote my CSIS professor and asked him, and this is the answer he gave me..."Unless a starts out as -1, a++ _must_ be nonzero.
In fact, this isn't C++, it's compiler-dependent and
you shouldn't rely on any order within comma-separated
operations."


That was for the if(//stuff) question with the a++, a==b.

However for your other question above...What? In that you are setting x equal to y minus 2 and incrementing a by one...?? So the output should be
0
6
????

This post has been edited by IngeniousHax: 23 May 2009 - 11:54 PM

Was This Post Helpful? 0
  • +
  • -

#11 pradosh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 20-May 07

Re: C questions with fundamental Query.i want opinion from my esteemed

Posted 24 May 2009 - 12:00 AM

View PostIngeniousHax, on 23 May, 2009 - 10:50 PM, said:

I'm not sure, so I wrote my CSIS professor and asked him, and this is the answer he gave me..."Unless a starts out as -1, a++ _must_ be nonzero.
In fact, this isn't C++, it's compiler-dependent and
you shouldn't rely on any order within comma-separated
operations."


That was for the if(//stuff) question with the a++, a==b.

However for your other question above...What? In that you are setting x equal to y minus 2 and incrementing a by one...?? So the output should be
0
6
????



Hi Ingenioushax,

The output is
:
0
5



Tell me the order of execution over here. It's not executing the ++a part as the output suggests. it is surely executing the y-2 part first!!!
Was This Post Helpful? 0
  • +
  • -

#12 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: C questions with fundamental Query.i want opinion from my esteemed

Posted 24 May 2009 - 12:00 AM

View Postpradosh, on 23 May, 2009 - 10:41 PM, said:

I would like to know the right explanation


Why do you want to know?

You are presenting all these strange brain teasers to us. Why?

If anyone who knew what they were doing saw code written in that awful manner the first thing they would do is "fix" it.

I am at a loss as to why you are investing time in struggling with these weird bits of code.
I'm not saying you shouldn't spend your time however you choose to but if these puzzles are beyond you why do you persist in torturing yourself with them?
They will never occur in the 'real world' so why do you care?
I certainly don't.

It is a big free world and you are free to do whatever you want but I might have more interest in these if I could see there was some reason for investing time in them.
Was This Post Helpful? 0
  • +
  • -

#13 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: C questions with fundamental Query.i want opinion from my esteemed

Posted 24 May 2009 - 12:08 AM

Yeah, i'm kind of curious on this whole matter myself. I mean it seems as though you're kind of coming here to just have someone flip these codes around to make them work crroectly. Basically the order of operations is like the PEMDAS in mathematics. When it comes to yours it's kind of hard to say as you are setting x equal to y - 2 AND 1 + a, basically, that's terrible practice, if you want to add one to a, put it on another line such as a++.

This post has been edited by IngeniousHax: 24 May 2009 - 12:09 AM

Was This Post Helpful? 0
  • +
  • -

#14 pradosh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 20-May 07

Re: C questions with fundamental Query.i want opinion from my esteemed

Posted 24 May 2009 - 12:12 AM

View Postjanotte, on 23 May, 2009 - 11:00 PM, said:

View Postpradosh, on 23 May, 2009 - 10:41 PM, said:

I would like to know the right explanation


Why do you want to know?

You are presenting all these strange brain teasers to us. Why?

If anyone who knew what they were doing saw code written in that awful manner the first thing they would do is "fix" it.

I am at a loss as to why you are investing time in struggling with these weird bits of code.
I'm not saying you shouldn't spend your time however you choose to but if these puzzles are beyond you why do you persist in torturing yourself with them?
They will never occur in the 'real world' so why do you care?
I certainly don't.

It is a big free world and you are free to do whatever you want but I might have more interest in these if I could see there was some reason for investing time in them.



Hi Janotte,
i completely echo your point .These strange type of code won't occur in real world. Unfortunately I am joining as a fresher to a new company. There ,i will have two months of traning and test on C and C++.What I heard from my seniors about the tests and all..they are really putting all such codes and bugs in tests. Well,I told my compulsion.If you would feel like replying to my queries it will be nice..otherwise i will search some other way :-(.
Was This Post Helpful? 0
  • +
  • -

#15 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: C questions with fundamental Query.i want opinion from my esteemed

Posted 24 May 2009 - 12:25 AM

Don't let anything I say affect your plans.

I speak for me and me alone.

I'm sure there are plenty of people here who will be happy to help you work these out, especially now that it makes some sense as to why you are interested.

Good luck with the job application!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1