只想知道原因 [英] Just want to know why
问题描述
Hello Grp,
我想知道为什么这是C中的错误
a> 3?返回1:返回2;
如果不是这样的话。
if(a> 3)
返回1;
其他
返回2;
是否会使函数处于未定义状态?
提前致谢,
问候,
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.
推荐答案
Naren写道:
Hello Grp,
我想知道为什么这是一个错误在C
a> 3?返回1:返回2;
" return< expression>"是一个声明。
?运营商的形式。
"表达式?表达式:表达式 - 请注意
"后面的两件事。是*表达*!
以下是:
a> 3?返回1:返回2;
表达式?声明:声明 - 这是一个语法错误。
你可以写:
返回一个> 3? 1:2;
如果不是这样的话。
if(a> 3)
返回1;
返回2 ;
因为如果是:
if(表达式)语句其他声明
它是否会离开函数处于未定义状态?
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?
否 - 这只是不正确的语法。查看C语法。
No - it simply is incorrect syntax. Check out the C grammer.
" Naren" < RB ********* @ in.bosch.com>写道:
"Naren" <RB*********@in.bosch.com> wrote:
a> 3?返回1:返回2;
如果不是。
if(a> 3)
返回1;
返回2;
a > 3 ? return 1:return 2;
when this is not.
if(a > 3)
return 1;
else
return 2;
Naren,
返回< whatever> ;;
是一个语句,而不是表达式。您不能将它用作条件运算符的任何
操作数。您/可以/做什么,
当然是这样的:
返回> 3? 1:2;
HTH
问候
-
Irrwahn Grausewitz(ir ** *****@freenet.de)
欢迎来到clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list: http://www.faqs.org/faqs/C-faq/faq/
acllc-c ++ faq: http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
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
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
acllc-c++ faq : http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
" Naren" < RB ********* @ in.bosch.com>写道:
"Naren" <RB*********@in.bosch.com> writes:
Hello Grp,
我想知道为什么这是一个错误在C
a> 3?返回1:返回2;
当这不是。
if(a> 3)
返回1;
返回2;
三元运算符将三个表达式作为操作数:
expr? expr:expr
" return 1"是一种陈述,而不是一种表达。 (您可以通过添加分号将
表达式转换为语句;无法将
语句转换为表达式。
它是否会使函数处于未定义状态?
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?
我不确定你的意思。如果代码没有编译,则
函数根本不处于任何状态。
-
Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst>
圣地亚哥超级计算机中心< *> < http://www.sdsc.edu/~kst>
Schroedinger做莎士比亚:要*和*不要
I''m not sure what you mean by this. If the code doesn''t compile, the
function isn''t in any state at all.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
这篇关于只想知道原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!