1 Replies - 654 Views - Last Post: 12 April 2014 - 06:46 AM Rate Topic: -----

#1 gtrp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-January 14

can anyone explain me the output of this code?

Posted 12 April 2014 - 06:40 AM

I have executed the following code in C language:
#include<stdio.h>
#include<conio.h>
#define L 10
void main()
{
	auto money=10;
	switch(money,money*2)
	{
		case L:printf("William\n");
		break;
		case L*2:printf("Super\n");
		break;
		case L*3:printf("hello!\n");
		break;
		default:printf("Wrong answer\n");
		break;
		case L*4:printf("hi!");
		break;
	}
	getch();
}




I thought it wont compile but it is compiling and the output is
Super
Super

can anyone explain me its working??
how can Switch statement take two arguments??

Is This A Good Question/Topic? 0
  • +

Replies To: can anyone explain me the output of this code?

#2 pythonuser007   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 25-January 14

Re: can anyone explain me the output of this code?

Posted 12 April 2014 - 06:46 AM

You have separated 2 statements with a comma. The comma is an operator is C and is evaluated from left to right. So you have a comma separated statement as an arguement to the switch statement. This is evaluated as switch is equal to money then immediately switch is equal to 2 times money. Hence switch has a final value of 2 * money. The output then resembles the input and outputs super twice.

Google "comma operator is C"

Regards
PythonUser007


*** MOD EDIT ***
No need to quote previous post.

This post has been edited by GunnerInc: 12 April 2014 - 09:19 AM
Reason for edit:: Quote go POOF!

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1