What does ? in C mean?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
What does a question mark (?) in C mean?
c
|
show 1 more comment
What does a question mark (?) in C mean?
c
1
Could you give us a contextual example of where you're seeing the '?'?
– Rich Turner
Feb 3 '11 at 10:45
@bitcrazed: Just curious: what other use than the ternary operator are you thinking of?
– chris
Feb 3 '11 at 10:48
@chris it's also (in seriously old code) part of a trigraph, as Benoit beat me to answering.
– Rup
Feb 3 '11 at 10:50
@bitcrazed: I read about those, never seen them in actual code though. Not even in really old code ;)
– chris
Feb 3 '11 at 10:58
1
@chris You're more likely to accidentally enter one and get compiler warnings about them rather than find real examples, yes.
– Rup
Feb 3 '11 at 10:59
|
show 1 more comment
What does a question mark (?) in C mean?
c
What does a question mark (?) in C mean?
c
c
asked Feb 3 '11 at 10:43
Eric BrottoEric Brotto
36.4k27108168
36.4k27108168
1
Could you give us a contextual example of where you're seeing the '?'?
– Rich Turner
Feb 3 '11 at 10:45
@bitcrazed: Just curious: what other use than the ternary operator are you thinking of?
– chris
Feb 3 '11 at 10:48
@chris it's also (in seriously old code) part of a trigraph, as Benoit beat me to answering.
– Rup
Feb 3 '11 at 10:50
@bitcrazed: I read about those, never seen them in actual code though. Not even in really old code ;)
– chris
Feb 3 '11 at 10:58
1
@chris You're more likely to accidentally enter one and get compiler warnings about them rather than find real examples, yes.
– Rup
Feb 3 '11 at 10:59
|
show 1 more comment
1
Could you give us a contextual example of where you're seeing the '?'?
– Rich Turner
Feb 3 '11 at 10:45
@bitcrazed: Just curious: what other use than the ternary operator are you thinking of?
– chris
Feb 3 '11 at 10:48
@chris it's also (in seriously old code) part of a trigraph, as Benoit beat me to answering.
– Rup
Feb 3 '11 at 10:50
@bitcrazed: I read about those, never seen them in actual code though. Not even in really old code ;)
– chris
Feb 3 '11 at 10:58
1
@chris You're more likely to accidentally enter one and get compiler warnings about them rather than find real examples, yes.
– Rup
Feb 3 '11 at 10:59
1
1
Could you give us a contextual example of where you're seeing the '?'?
– Rich Turner
Feb 3 '11 at 10:45
Could you give us a contextual example of where you're seeing the '?'?
– Rich Turner
Feb 3 '11 at 10:45
@bitcrazed: Just curious: what other use than the ternary operator are you thinking of?
– chris
Feb 3 '11 at 10:48
@bitcrazed: Just curious: what other use than the ternary operator are you thinking of?
– chris
Feb 3 '11 at 10:48
@chris it's also (in seriously old code) part of a trigraph, as Benoit beat me to answering.
– Rup
Feb 3 '11 at 10:50
@chris it's also (in seriously old code) part of a trigraph, as Benoit beat me to answering.
– Rup
Feb 3 '11 at 10:50
@bitcrazed: I read about those, never seen them in actual code though. Not even in really old code ;)
– chris
Feb 3 '11 at 10:58
@bitcrazed: I read about those, never seen them in actual code though. Not even in really old code ;)
– chris
Feb 3 '11 at 10:58
1
1
@chris You're more likely to accidentally enter one and get compiler warnings about them rather than find real examples, yes.
– Rup
Feb 3 '11 at 10:59
@chris You're more likely to accidentally enter one and get compiler warnings about them rather than find real examples, yes.
– Rup
Feb 3 '11 at 10:59
|
show 1 more comment
8 Answers
8
active
oldest
votes
?
is the first symbol of the ?:
ternary operator.
a = (b==0) ? 1 : 0;
a
will have the value 1 if b
is equal to 0
, and 0 otherwise.
add a comment |
Additionally to other answers, ?
can be part of a trigraph.
It can also be part of a string or character in general without being a trigraph:char c = '?'; char const * s = "?"
.
– Thomas Eding
Dec 8 '11 at 22:33
add a comment |
This is a ternary Operator which is conditional operator uses like if-else
example
int i=1;
int j=2;
int k;
k= i > j ? i : j;
//which is same as
if(i>j)
k=i;
else
k=j;
Usage:
Syntax of ?: is
assignment_Variable = Condition ? value_if_true : value_if_false;
add a comment |
That’s probably a part of the ternary operator:
const int numApples = …;
printf("I have %i apple%s.n", numApples == 1 ? "" : "s");
Just to save future generations on any confusion here. It is the "conditional operator". It just happens to be a ternary operator, of which there is only one in C and C++. There are lots of unary (~, !, -) and binary (+, -, <<) operators in C/C++ as well. Neato!
– Thomas Eding
Dec 8 '11 at 22:17
add a comment |
This is a so called conditional operator. You can shorten your if else statement with this operator.
The following link should explain everything
http://www.crasseux.com/books/ctutorial/The-question-mark-operator.html
add a comment |
It is a conditional operator. For example refer the below link
http://en.wikipedia.org/wiki/Conditional_operator
add a comment |
Its the ternary
operator, see http://en.wikipedia.org/wiki/Ternary_operation#C.2C_C.2B.2B.2C_C.23.2C_Objective-C.2C_Java.2C_JavaScript.2C_ActionScript
add a comment |
Most likely the '?' is the ternary operator. Its grammar is:
RESULT = (COND) ? (STATEMEN IF TRUE) : (STATEMENT IF FALSE)
It is a nice shorthand for the typical if-else statement:
if (COND) {
RESULT = (STATEMENT IF TRUE);
} else {
RESULT = (STATEMENT IF FALSE);
as it can usually fit on one line and can improve readability.
Some answers here refer to a trigraph, which is relevant to the C preprocessor. Take a look at this really dumb program, trigraphs.c
:
# /* preprocessor will remove single hash symbols and this comment */
int main()
{
char *t = "??=";
char *p = "??/"";
char *s = "??'";
??(, ??), ??! ??<, ??>, ??-
return 0;
}
invoking only the c preprocessor by running gcc -E -trigraphs trigraph.c
the output is
int main()
{
char *t = "#"
char *p = """;
char *s = "^";
[, ], | {, }, ~
return 0;
}
Hopefully that clarifies a little bit what a trigraphs are, and what a '?' "means" in C.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4885143%2fwhat-does-in-c-mean%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
8 Answers
8
active
oldest
votes
8 Answers
8
active
oldest
votes
active
oldest
votes
active
oldest
votes
?
is the first symbol of the ?:
ternary operator.
a = (b==0) ? 1 : 0;
a
will have the value 1 if b
is equal to 0
, and 0 otherwise.
add a comment |
?
is the first symbol of the ?:
ternary operator.
a = (b==0) ? 1 : 0;
a
will have the value 1 if b
is equal to 0
, and 0 otherwise.
add a comment |
?
is the first symbol of the ?:
ternary operator.
a = (b==0) ? 1 : 0;
a
will have the value 1 if b
is equal to 0
, and 0 otherwise.
?
is the first symbol of the ?:
ternary operator.
a = (b==0) ? 1 : 0;
a
will have the value 1 if b
is equal to 0
, and 0 otherwise.
answered Feb 3 '11 at 10:45
Didier TrossetDidier Trosset
27.6k1364102
27.6k1364102
add a comment |
add a comment |
Additionally to other answers, ?
can be part of a trigraph.
It can also be part of a string or character in general without being a trigraph:char c = '?'; char const * s = "?"
.
– Thomas Eding
Dec 8 '11 at 22:33
add a comment |
Additionally to other answers, ?
can be part of a trigraph.
It can also be part of a string or character in general without being a trigraph:char c = '?'; char const * s = "?"
.
– Thomas Eding
Dec 8 '11 at 22:33
add a comment |
Additionally to other answers, ?
can be part of a trigraph.
Additionally to other answers, ?
can be part of a trigraph.
edited Mar 24 '16 at 14:55
tripleee
96.6k14135191
96.6k14135191
answered Feb 3 '11 at 10:47
BenoitBenoit
60.1k15170213
60.1k15170213
It can also be part of a string or character in general without being a trigraph:char c = '?'; char const * s = "?"
.
– Thomas Eding
Dec 8 '11 at 22:33
add a comment |
It can also be part of a string or character in general without being a trigraph:char c = '?'; char const * s = "?"
.
– Thomas Eding
Dec 8 '11 at 22:33
It can also be part of a string or character in general without being a trigraph:
char c = '?'; char const * s = "?"
.– Thomas Eding
Dec 8 '11 at 22:33
It can also be part of a string or character in general without being a trigraph:
char c = '?'; char const * s = "?"
.– Thomas Eding
Dec 8 '11 at 22:33
add a comment |
This is a ternary Operator which is conditional operator uses like if-else
example
int i=1;
int j=2;
int k;
k= i > j ? i : j;
//which is same as
if(i>j)
k=i;
else
k=j;
Usage:
Syntax of ?: is
assignment_Variable = Condition ? value_if_true : value_if_false;
add a comment |
This is a ternary Operator which is conditional operator uses like if-else
example
int i=1;
int j=2;
int k;
k= i > j ? i : j;
//which is same as
if(i>j)
k=i;
else
k=j;
Usage:
Syntax of ?: is
assignment_Variable = Condition ? value_if_true : value_if_false;
add a comment |
This is a ternary Operator which is conditional operator uses like if-else
example
int i=1;
int j=2;
int k;
k= i > j ? i : j;
//which is same as
if(i>j)
k=i;
else
k=j;
Usage:
Syntax of ?: is
assignment_Variable = Condition ? value_if_true : value_if_false;
This is a ternary Operator which is conditional operator uses like if-else
example
int i=1;
int j=2;
int k;
k= i > j ? i : j;
//which is same as
if(i>j)
k=i;
else
k=j;
Usage:
Syntax of ?: is
assignment_Variable = Condition ? value_if_true : value_if_false;
edited Feb 3 '11 at 10:54
answered Feb 3 '11 at 10:48
Javed AkramJaved Akram
9,7372270109
9,7372270109
add a comment |
add a comment |
That’s probably a part of the ternary operator:
const int numApples = …;
printf("I have %i apple%s.n", numApples == 1 ? "" : "s");
Just to save future generations on any confusion here. It is the "conditional operator". It just happens to be a ternary operator, of which there is only one in C and C++. There are lots of unary (~, !, -) and binary (+, -, <<) operators in C/C++ as well. Neato!
– Thomas Eding
Dec 8 '11 at 22:17
add a comment |
That’s probably a part of the ternary operator:
const int numApples = …;
printf("I have %i apple%s.n", numApples == 1 ? "" : "s");
Just to save future generations on any confusion here. It is the "conditional operator". It just happens to be a ternary operator, of which there is only one in C and C++. There are lots of unary (~, !, -) and binary (+, -, <<) operators in C/C++ as well. Neato!
– Thomas Eding
Dec 8 '11 at 22:17
add a comment |
That’s probably a part of the ternary operator:
const int numApples = …;
printf("I have %i apple%s.n", numApples == 1 ? "" : "s");
That’s probably a part of the ternary operator:
const int numApples = …;
printf("I have %i apple%s.n", numApples == 1 ? "" : "s");
answered Feb 3 '11 at 10:45
zoulzoul
79.4k37224324
79.4k37224324
Just to save future generations on any confusion here. It is the "conditional operator". It just happens to be a ternary operator, of which there is only one in C and C++. There are lots of unary (~, !, -) and binary (+, -, <<) operators in C/C++ as well. Neato!
– Thomas Eding
Dec 8 '11 at 22:17
add a comment |
Just to save future generations on any confusion here. It is the "conditional operator". It just happens to be a ternary operator, of which there is only one in C and C++. There are lots of unary (~, !, -) and binary (+, -, <<) operators in C/C++ as well. Neato!
– Thomas Eding
Dec 8 '11 at 22:17
Just to save future generations on any confusion here. It is the "conditional operator". It just happens to be a ternary operator, of which there is only one in C and C++. There are lots of unary (~, !, -) and binary (+, -, <<) operators in C/C++ as well. Neato!
– Thomas Eding
Dec 8 '11 at 22:17
Just to save future generations on any confusion here. It is the "conditional operator". It just happens to be a ternary operator, of which there is only one in C and C++. There are lots of unary (~, !, -) and binary (+, -, <<) operators in C/C++ as well. Neato!
– Thomas Eding
Dec 8 '11 at 22:17
add a comment |
This is a so called conditional operator. You can shorten your if else statement with this operator.
The following link should explain everything
http://www.crasseux.com/books/ctutorial/The-question-mark-operator.html
add a comment |
This is a so called conditional operator. You can shorten your if else statement with this operator.
The following link should explain everything
http://www.crasseux.com/books/ctutorial/The-question-mark-operator.html
add a comment |
This is a so called conditional operator. You can shorten your if else statement with this operator.
The following link should explain everything
http://www.crasseux.com/books/ctutorial/The-question-mark-operator.html
This is a so called conditional operator. You can shorten your if else statement with this operator.
The following link should explain everything
http://www.crasseux.com/books/ctutorial/The-question-mark-operator.html
answered Feb 3 '11 at 10:46
Stefan PappStefan Papp
1,38111335
1,38111335
add a comment |
add a comment |
It is a conditional operator. For example refer the below link
http://en.wikipedia.org/wiki/Conditional_operator
add a comment |
It is a conditional operator. For example refer the below link
http://en.wikipedia.org/wiki/Conditional_operator
add a comment |
It is a conditional operator. For example refer the below link
http://en.wikipedia.org/wiki/Conditional_operator
It is a conditional operator. For example refer the below link
http://en.wikipedia.org/wiki/Conditional_operator
answered Feb 3 '11 at 10:45
ckvckv
5,1781568126
5,1781568126
add a comment |
add a comment |
Its the ternary
operator, see http://en.wikipedia.org/wiki/Ternary_operation#C.2C_C.2B.2B.2C_C.23.2C_Objective-C.2C_Java.2C_JavaScript.2C_ActionScript
add a comment |
Its the ternary
operator, see http://en.wikipedia.org/wiki/Ternary_operation#C.2C_C.2B.2B.2C_C.23.2C_Objective-C.2C_Java.2C_JavaScript.2C_ActionScript
add a comment |
Its the ternary
operator, see http://en.wikipedia.org/wiki/Ternary_operation#C.2C_C.2B.2B.2C_C.23.2C_Objective-C.2C_Java.2C_JavaScript.2C_ActionScript
Its the ternary
operator, see http://en.wikipedia.org/wiki/Ternary_operation#C.2C_C.2B.2B.2C_C.23.2C_Objective-C.2C_Java.2C_JavaScript.2C_ActionScript
answered Feb 3 '11 at 10:45
ismailismail
34.9k87186
34.9k87186
add a comment |
add a comment |
Most likely the '?' is the ternary operator. Its grammar is:
RESULT = (COND) ? (STATEMEN IF TRUE) : (STATEMENT IF FALSE)
It is a nice shorthand for the typical if-else statement:
if (COND) {
RESULT = (STATEMENT IF TRUE);
} else {
RESULT = (STATEMENT IF FALSE);
as it can usually fit on one line and can improve readability.
Some answers here refer to a trigraph, which is relevant to the C preprocessor. Take a look at this really dumb program, trigraphs.c
:
# /* preprocessor will remove single hash symbols and this comment */
int main()
{
char *t = "??=";
char *p = "??/"";
char *s = "??'";
??(, ??), ??! ??<, ??>, ??-
return 0;
}
invoking only the c preprocessor by running gcc -E -trigraphs trigraph.c
the output is
int main()
{
char *t = "#"
char *p = """;
char *s = "^";
[, ], | {, }, ~
return 0;
}
Hopefully that clarifies a little bit what a trigraphs are, and what a '?' "means" in C.
add a comment |
Most likely the '?' is the ternary operator. Its grammar is:
RESULT = (COND) ? (STATEMEN IF TRUE) : (STATEMENT IF FALSE)
It is a nice shorthand for the typical if-else statement:
if (COND) {
RESULT = (STATEMENT IF TRUE);
} else {
RESULT = (STATEMENT IF FALSE);
as it can usually fit on one line and can improve readability.
Some answers here refer to a trigraph, which is relevant to the C preprocessor. Take a look at this really dumb program, trigraphs.c
:
# /* preprocessor will remove single hash symbols and this comment */
int main()
{
char *t = "??=";
char *p = "??/"";
char *s = "??'";
??(, ??), ??! ??<, ??>, ??-
return 0;
}
invoking only the c preprocessor by running gcc -E -trigraphs trigraph.c
the output is
int main()
{
char *t = "#"
char *p = """;
char *s = "^";
[, ], | {, }, ~
return 0;
}
Hopefully that clarifies a little bit what a trigraphs are, and what a '?' "means" in C.
add a comment |
Most likely the '?' is the ternary operator. Its grammar is:
RESULT = (COND) ? (STATEMEN IF TRUE) : (STATEMENT IF FALSE)
It is a nice shorthand for the typical if-else statement:
if (COND) {
RESULT = (STATEMENT IF TRUE);
} else {
RESULT = (STATEMENT IF FALSE);
as it can usually fit on one line and can improve readability.
Some answers here refer to a trigraph, which is relevant to the C preprocessor. Take a look at this really dumb program, trigraphs.c
:
# /* preprocessor will remove single hash symbols and this comment */
int main()
{
char *t = "??=";
char *p = "??/"";
char *s = "??'";
??(, ??), ??! ??<, ??>, ??-
return 0;
}
invoking only the c preprocessor by running gcc -E -trigraphs trigraph.c
the output is
int main()
{
char *t = "#"
char *p = """;
char *s = "^";
[, ], | {, }, ~
return 0;
}
Hopefully that clarifies a little bit what a trigraphs are, and what a '?' "means" in C.
Most likely the '?' is the ternary operator. Its grammar is:
RESULT = (COND) ? (STATEMEN IF TRUE) : (STATEMENT IF FALSE)
It is a nice shorthand for the typical if-else statement:
if (COND) {
RESULT = (STATEMENT IF TRUE);
} else {
RESULT = (STATEMENT IF FALSE);
as it can usually fit on one line and can improve readability.
Some answers here refer to a trigraph, which is relevant to the C preprocessor. Take a look at this really dumb program, trigraphs.c
:
# /* preprocessor will remove single hash symbols and this comment */
int main()
{
char *t = "??=";
char *p = "??/"";
char *s = "??'";
??(, ??), ??! ??<, ??>, ??-
return 0;
}
invoking only the c preprocessor by running gcc -E -trigraphs trigraph.c
the output is
int main()
{
char *t = "#"
char *p = """;
char *s = "^";
[, ], | {, }, ~
return 0;
}
Hopefully that clarifies a little bit what a trigraphs are, and what a '?' "means" in C.
answered Jan 23 '16 at 19:41
bpmbpm
874
874
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4885143%2fwhat-does-in-c-mean%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Could you give us a contextual example of where you're seeing the '?'?
– Rich Turner
Feb 3 '11 at 10:45
@bitcrazed: Just curious: what other use than the ternary operator are you thinking of?
– chris
Feb 3 '11 at 10:48
@chris it's also (in seriously old code) part of a trigraph, as Benoit beat me to answering.
– Rup
Feb 3 '11 at 10:50
@bitcrazed: I read about those, never seen them in actual code though. Not even in really old code ;)
– chris
Feb 3 '11 at 10:58
1
@chris You're more likely to accidentally enter one and get compiler warnings about them rather than find real examples, yes.
– Rup
Feb 3 '11 at 10:59