Just want to know why

N

Naren

Hello Grp,
I want to know why this is an error in C
a > 3 ? return 1:return 2;
when this is not.
if(a > 3)
return 1;
else
return 2;

Is it going to leave the function in an undefined state?

Thanks in advance,

regards,
Naren.
 
G

Gianni Mariani

Naren said:
Hello Grp,
I want to know why this is an error in C
a > 3 ? return 1:return 2;

"return <expression>" is a statement.

The "?" operator is of the form.

"expression ? expression : expression" - NOTE the two things after the
"?" are *expression* !


This below is:

a > 3 ? return 1:return 2;

expression ? statement : statement - which is a syntax error.

You can write:

return a > 3 ? 1 : 2;

when this is not.
if(a > 3)
return 1;
else
return 2;



Because if is:

if ( expression ) statement else statement
Is it going to leave the function in an undefined state?

No - it simply is incorrect syntax. Check out the C grammer.
 
I

Irrwahn Grausewitz

Naren said:
a > 3 ? return 1:return 2;
when this is not.
if(a > 3)
return 1;
else
return 2;

Naren,

return <whatever>;

is a statement, not an expression. You cannot use it as any
operand of the conditional operator. What you /can/ do, of
course, is something like this:

return a > 3 ? 1 : 2;

HTH
Regards
 
K

Keith Thompson

Naren said:
Hello Grp,
I want to know why this is an error in C
a > 3 ? return 1:return 2;
when this is not.
if(a > 3)
return 1;
else
return 2;

The trinary operator takes three expressions as operands:

expr ? expr : expr

"return 1" is a statement, not an expression. (You can turn an
expression into a statement by adding a semicolon; there's no way to
turn a statement into an expression).
Is it going to leave the function in an undefined state?

I'm not sure what you mean by this. If the code doesn't compile, the
function isn't in any state at all.
 
J

Jason

Naren said:
Hello Grp,
I want to know why this is an error in C
a > 3 ? return 1:return 2;
when this is not.
if(a > 3)
return 1;
else
return 2;

Is it going to leave the function in an undefined state?

Like everyone else has pointer out, return is not an expression. In other
words it doesn't evaluate to anything and the ?: operator must have an
expression becuase that is what it is for. You could ofcourse do this:

return a > 3 ? 1 : 2;
 
Y

Yakov Lerner

Naren said:
Hello Grp,
I want to know why this is an error in C
a > 3 ? return 1:return 2;
when this is not.
if(a > 3)
return 1;
else
return 2;

This is an error because 'return' is a statement not an expression.
Statements are not allowed inside expressions (expr1?expr2:expr3)

In some languages, expressions can contain blocks of statements
(with last expression within block producing the value of the block)
but not in C. I'd be not surprised to find that some extension or
dialect of C allows this. But not C.

Jacob
 
A

Avinash

Hello Naren,
Notice that the ternary operator is an expression that returns a value
based on the conditional phrase it evaluates. This is different than
the if-then-else syntax, which executes a statement block based on
it's conditional phrase.

use following syntax

return ( a > 3 ? 1:2 );

regards.
Avinash
 
S

Sriram

Hi,
a > 3 ? Value1 : Value2 ;

In this statement ? :

It should be a Value;

U cant use "return" statement because return does not result in a
value.

DONT Confuse between returning a "Value" (returns a value to the
calling function) and the expression results in a value.

Hope this helps,

Regards,
Sriram
 
A

Anupam

Naren said:
Hello Grp,
I want to know why this is an error in C
a > 3 ? return 1:return 2;
when this is not.
if(a > 3)
return 1;
else
return 2;

Is it going to leave the function in an undefined state?

Thanks in advance,

regards,
Naren.

The returns do not evaluate to expressions .. they are actually
statements and the ? : operator operates on expressions and not
statements.

Regards,
Anupam
 
M

Mark A. Odell

Hello Grp,
I want to know why this is an error in C
a > 3 ? return 1:return 2;
when this is not.
if(a > 3)
return 1;
else
return 2;

And I don't know why you don't do the simplest thing:

return a > 3 ? 1 : 2;
^^^^^ ^ ^
condition | |
| if condition false
if condition true

Not only is it more succinct, it's also correct C.
 
C

Chris Mantoulidis

Naren said:
Hello Grp,
I want to know why this is an error in C
a > 3 ? return 1:return 2;
when this is not.
if(a > 3)
return 1;
else
return 2;

Is it going to leave the function in an undefined state?

Thanks in advance,

regards,
Naren.

If I remember well in my C++ compiler (sorry didn't check for C, and
I'm not home now), it doesn't work as well. What works is:

return (a>3)?1:2;

Hope that helps,
cmad
 
K

Keith Thompson

Mark A. Odell said:
And I don't know why you don't do the simplest thing:

return a > 3 ? 1 : 2;
^^^^^ ^ ^
condition | |
| if condition false
if condition true

Not only is it more succinct, it's also correct C.

This:

return a > 3 ? 1 : 2;

and this:

if (a > 3)
return 1;
else
return 2;

or, as I would write it:

if (a > 3) {
return 1;
}
else {
return 2;
}

are all equally valid, and they all do the same thing. You should use
whichever one is clearer in context.

The real problem with all three is the use of "a" as a variable name,
and the use of magic numbers 1, 2, and 3 rather than symbolic
constants that tell us what the values actually mean. I'm not
criticizing; such things are perfectly fine for short examples like
this. But it would be poor style for real-world code, and the symbols
used in the actual code can affect which form is going to be clearer.

Succinctness isn't always a virtue. Sometimes, but not always.
 
J

Jack Klein

If I remember well in my C++ compiler (sorry didn't check for C, and
I'm not home now), it doesn't work as well. What works is:

return (a>3)?1:2;

Hope that helps,
cmad

Three things:

1. What is or is not true in C++ is completely irrelevant for
comp.lang.c. C++ started with the C language and then changed some of
the parts they kept as well as adding new parts.

2. Either you misremember or your C++ compiler is non-conforming,
because this is one of the things that C++ did not change when they
adopted it from C. Parentheses are allowed where you put them in both
languages, but are not required by either language.

3. This is not a good newsgroup for answering questions based on
vague memories.
 
C

Chris Mantoulidis

Jack Klein said:
Three things:

1. What is or is not true in C++ is completely irrelevant for
comp.lang.c. C++ started with the C language and then changed some of
the parts they kept as well as adding new parts.

Who disagreed? However the code I posted is 100% valid C code.
2. Either you misremember or your C++ compiler is non-conforming,
because this is one of the things that C++ did not change when they
adopted it from C. Parentheses are allowed where you put them in both
languages, but are not required by either language.

I like using parentheses; you got a problem with that? If you don't
like my coding style, there's no need to critisize it ;)
3. This is not a good newsgroup for answering questions based on
vague memories.

I checked it in my C compiler and yeah it doesn't work. The code I
posted is not vague memories cuz i am SURE it works ;)

Don't you have anything better to do that critisize correct posts? :eek:
 
S

stelios xanthakis

Jack Klein said:
1. What is or is not true in C++ is completely irrelevant for
comp.lang.c.
*****[C++ started with the C language and then changed some of
the parts they kept as well as adding new parts.]*****

This is irrelevant to your arguments. What does C-C++ history
and programming language evolution has anything
to do with the fact that a post may or may not be offtopic???
2. Either you misremember or your C++ compiler is non-conforming,
because this is one of the things that C++ did not change when they
adopted it from C. Parentheses are allowed where you put them in both
languages, but are not required by either language.

Irrelevant. What is conforming or non-conforming for C++ is irrelevant
for comp.land.c
 
P

Peter Pichler

Keith Thompson said:
This:
return a > 3 ? 1 : 2;

and this:
if (a > 3)
return 1;
else
return 2;
are all equally valid, and they all do the same thing. You should use
whichever one is clearer in context.

Indeed. And the latter is IMO certainly clearer than:

return (short)(a > 3 ? 1 : 2);

<OT>On *some* compilers the cast is necessary, if you want to avoid silly
warnings about possible loss of data.</OT>

Peter
 
D

Dan Pop

In said:
return (short)(a > 3 ? 1 : 2);

<OT>On *some* compilers the cast is necessary, if you want to avoid silly
warnings about possible loss of data.</OT>

Hopefully, the usage of such compilers is not necessary.

Dan
 
P

Peter Pichler

Dan Pop said:
In <[email protected]> "Peter Pichler"

Hopefully, the usage of such compilers is not necessary.

One would hope so. Unfortunately, I am stuck with one of them at work.
It also complains about things like:

char a = '0';
....
a += 4; /* a = (char)(a + 4); will "fix" the warning */
/* so will turning down the warning level */
 
D

Dan Pop

In said:
One would hope so. Unfortunately, I am stuck with one of them at work.
It also complains about things like:

char a = '0';
...
a += 4; /* a = (char)(a + 4); will "fix" the warning */
/* so will turning down the warning level */

Are you sure it is not possible to disable these idiotic warnings
without turning down the warning level? These silly compilers usually
have a way of disabling individual diagnostics. Which is much better
than polluting the code with casts.

Dan
 
P

Peter Pichler

Dan Pop said:
Are you sure it is not possible to disable these idiotic warnings
without turning down the warning level? These silly compilers usually
have a way of disabling individual diagnostics. Which is much better
than polluting the code with casts.

That would involve changing the default settings for everyone in my team
plus everyone that inports our code, so we just gave in.

It now occured to me that we could use a #pragma to do the job, why oh why
did you not turn up a bit earlier? Like 5 years ago, for example? ;-)

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top