473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Variable declaration inside a switch statement.

Hi all,

If we compile the below piece of code, it gets compiled. But gives
weird result.

switch(x)
{
int y=2;

case 1:
printf("%d", y);
}
What, if any, the C standard says about it?

Srinu.

Oct 21 '07 #1
8 3170
On Oct 21, 4:00 pm, Srinu <sinu.nayak2...@gmail.comwrote:
Hi all,

If we compile the below piece of code, it gets compiled. But gives
weird result.

switch(x)
{
int y=2;

case 1:
printf("%d", y);

}

What, if any, the C standard says about it?

Srinu.
Can u tell me what weird results it gives ?

Oct 21 '07 #2
abhy <ab************@gmail.comwrites:
On Oct 21, 4:00 pm, Srinu <sinu.nayak2...@gmail.comwrote:
>If we compile the below piece of code, it gets compiled. But gives
weird result.

switch(x)
{
int y=2;

case 1:
printf("%d", y);

}

What, if any, the C standard says about it?

Can u tell me what weird results it gives ?
Please don't use silly abbreviations like "u" for "you". This isn't a
chat room. Take the time to spell out simple words.

Yes, showing the actual questionable output is almost always a good
idea. In this particular case, though, it's unnecessary. The
variable y is uninitialized when it's printed; any output is possible.
(Actually, the behavior is undefined, so in principle *anything* is
possible, but it will most likely print some arbitrary value of type
int.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 21 '07 #3
On Oct 21, 4:00 pm, Srinu <sinu.nayak2...@gmail.comwrote:
Hi all,

If we compile the below piece of code, it gets compiled. But gives
weird result.

switch(x)
{
int y=2;

case 1:
printf("%d", y);

}

What, if any, the C standard says about it?
This is a famous question in interviews :):)

A switch statement never enters at the top.
So, your initialization never gets executed.

Karthik Balaguru

Oct 22 '07 #4
Srinu wrote:
>
If we compile the below piece of code, it gets compiled. But
gives weird result.

switch(x) {
int y=2;

case 1: printf("%d", y);
}

What, if any, the C standard says about it?
What you fail to realize is that initialization of an automatic
variable requires the generation of code. That code has to go
where the "int y = 2;" statement appears. There is no reason for
the switch statement to transfer control to that code, so y is
uninitialized (or worse) when the printf is executed.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Oct 22 '07 #5
abhy wrote:
Srinu <sinu.nayak2...@gmail.comwrote:
.... snip ...
>
>What, if any, the C standard says about it?

Can u tell me what weird results it gives ?
u hasn't posted in c.l.c for some time.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Oct 22 '07 #6
On Oct 21, 10:43 pm, CBFalconer <cbfalco...@yahoo.comwrote:
abhy wrote:
Srinu <sinu.nayak2...@gmail.comwrote:

... snip ...
What, if any, the C standard says about it?
Can u tell me what weird results it gives ?

u hasn't posted in c.l.c for some time.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account fromhttp://www.teranews.com
Hi Karthik

I didn't get the meaning of your answer " A switch statement never
enters at the top. "..?
Please explain.

Oct 22 '07 #7
abhy wrote On 10/22/07 16:45,:
[...]
I didn't get the meaning of your answer " A switch statement never
enters at the top. "..?
Please explain.
He means this:

switch (x) {
/*
* Nothing here will ever be executed,
* because `switch' proceeds directly
* to the chosen case or to the end of
* the entire block if no case is chosen.
*/

case 42:
/*
* This is the first piece of code that
* the `switch' can ever execute.
*/
...
}

--
Er*********@sun.com
Oct 22 '07 #8
Dear friends,

Thanks a lot for your valuable suggestions.

Srinu.

Oct 23 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

83
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
3
by: Anoop | last post by:
Hi guys i have a piece of code main() { switch(...) { case 1: {
5
by: Neil Zanella | last post by:
Hello, Unlike in pre-C99 versions of C where variables can only be defined at the beginning of blocks, C99 allows variables to be defined in arbitrary places inside blocks. However, gcc 3.2.2...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
7
by: gyan | last post by:
follwing code gives error: 1 #include<iostream.h> 2 int main() 3 { 4 int a=5,b; 5 switch(a){ 6 case 1: 7 {b=5; 8 break; 9 }
4
by: Ray | last post by:
Hello, I think I've had JavaScript variable scope figured out, can you please see if I've got it correctly? * Variables can be local or global * When a variable is declared outside any...
14
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; int main() { int i;
2
by: Florian Loitsch | last post by:
hi, What should be the output of the following code-snippet? === var x = "global"; function f() { var x = 0; eval("function x() { return false; }"); delete x; alert(x); }
3
by: SRoubtsov | last post by:
Dear all, Do you know whether ANSI C (or some other dialects) support the following: * a variable name coincides with a type name, * a structure/union field name coincides with a type name in...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.