Can we declare structure object at file scope before the structure definition? Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar Manara Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!What is your favorite C programming trick?Declaring variables inside a switch statementWhat is the difference between a definition and a declaration?Function Declarations and Structure declarations in Cerror: `itoa` was not declared in this scopeWhy does GCC generate 15-20% faster code if I optimize for size instead of speed?What is wrong with this structure/union declaration?Using a structure definition in cIn C, if objects declared at block scope have no linkage, why does function declaration inside main() without “extern” work?when is an object declaration not a definition?
Was there ever a LEGO store in Miami International Airport?
What is the definining line between a helicopter and a drone a person can ride in?
Is it OK if I do not take the receipt in Germany?
Protagonist's race is hidden - should I reveal it?
In search of the origins of term censor, I hit a dead end stuck with the greek term, to censor, λογοκρίνω
Why doesn't the university give past final exams' answers?
How to translate "red flag" into Spanish?
Is Bran literally the world's memory?
Preserving file and folder permissions with rsync
Is there a verb for listening stealthily?
Putting Ant-Man on house arrest
Why does the Cisco show run command not show the full version, while the show version command does?
Why did Israel vote against lifting the American embargo on Cuba?
What is /etc/mtab in Linux?
Why does Java have support for time zone offsets with seconds precision?
What's parked in Mil Moscow helicopter plant?
"Working on a knee"
Processing ADC conversion result: DMA vs Processor Registers
Israeli soda type drink
/bin/ls sorts differently than just ls
When I export an AI 300x60 art board it saves with bigger dimensions
Why isPrototypeOf() returns false?
TV series episode where humans nuke aliens before decrypting their message that states they come in peace
How do I deal with an erroneously large refund?
Can we declare structure object at file scope before the structure definition?
Unicorn Meta Zoo #1: Why another podcast?
Announcing the arrival of Valued Associate #679: Cesar Manara
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!What is your favorite C programming trick?Declaring variables inside a switch statementWhat is the difference between a definition and a declaration?Function Declarations and Structure declarations in Cerror: `itoa` was not declared in this scopeWhy does GCC generate 15-20% faster code if I optimize for size instead of speed?What is wrong with this structure/union declaration?Using a structure definition in cIn C, if objects declared at block scope have no linkage, why does function declaration inside main() without “extern” work?when is an object declaration not a definition?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
According to the code given below and the answer for it:
Question: Which of the following structure declarations will throw an error?
struct temp char c; s;
int main(void)
struct temp char c; ;
struct temp s;
int main(void)
struct temp s;
struct temp char c; ;
int main(void)
None of the above.
Answer: 4
Is this correct? Can we declare a structure object first and only then the structure definition?
c
New contributor
|
show 4 more comments
According to the code given below and the answer for it:
Question: Which of the following structure declarations will throw an error?
struct temp char c; s;
int main(void)
struct temp char c; ;
struct temp s;
int main(void)
struct temp s;
struct temp char c; ;
int main(void)
None of the above.
Answer: 4
Is this correct? Can we declare a structure object first and only then the structure definition?
c
New contributor
3
All the functions shown are non-standard for the whole of the current millennium. All the structures are malformed. None of the code should compile.
– Jonathan Leffler
2 days ago
1
You’re not supposed to be able to compile an empty structure body. In (c) there is no typestruct temp
whens
is nominally defined, so the variable shouldn’t be definable. There might be a get-out-of-jail-free card somewhere; I don’t have a C compiler on my iPhone.
– Jonathan Leffler
2 days ago
2
@JonathanLeffler The question is about the order of declarations, the structure members are irrelevant. I addedint main
and a structure member, and it still compiles with no warnings. I also added a body tomain
that accesses the structure member, no complaints. I'm as surprised as the OP.
– Barmar
2 days ago
1
I’m asleep. My robot is responding now. If there’s still a controversy in the morning, I’ll look. If you ever needed evidence of why multichoice questions are abominable, this illustrates the point. You can’t present reasoning in an exam.
– Jonathan Leffler
2 days ago
2
@jonathan: Empty structures are a GCC extension to C.
– rici
2 days ago
|
show 4 more comments
According to the code given below and the answer for it:
Question: Which of the following structure declarations will throw an error?
struct temp char c; s;
int main(void)
struct temp char c; ;
struct temp s;
int main(void)
struct temp s;
struct temp char c; ;
int main(void)
None of the above.
Answer: 4
Is this correct? Can we declare a structure object first and only then the structure definition?
c
New contributor
According to the code given below and the answer for it:
Question: Which of the following structure declarations will throw an error?
struct temp char c; s;
int main(void)
struct temp char c; ;
struct temp s;
int main(void)
struct temp s;
struct temp char c; ;
int main(void)
None of the above.
Answer: 4
Is this correct? Can we declare a structure object first and only then the structure definition?
c
c
New contributor
New contributor
edited 2 days ago
StoryTeller
106k13223286
106k13223286
New contributor
asked 2 days ago
DSWDSW
564
564
New contributor
New contributor
3
All the functions shown are non-standard for the whole of the current millennium. All the structures are malformed. None of the code should compile.
– Jonathan Leffler
2 days ago
1
You’re not supposed to be able to compile an empty structure body. In (c) there is no typestruct temp
whens
is nominally defined, so the variable shouldn’t be definable. There might be a get-out-of-jail-free card somewhere; I don’t have a C compiler on my iPhone.
– Jonathan Leffler
2 days ago
2
@JonathanLeffler The question is about the order of declarations, the structure members are irrelevant. I addedint main
and a structure member, and it still compiles with no warnings. I also added a body tomain
that accesses the structure member, no complaints. I'm as surprised as the OP.
– Barmar
2 days ago
1
I’m asleep. My robot is responding now. If there’s still a controversy in the morning, I’ll look. If you ever needed evidence of why multichoice questions are abominable, this illustrates the point. You can’t present reasoning in an exam.
– Jonathan Leffler
2 days ago
2
@jonathan: Empty structures are a GCC extension to C.
– rici
2 days ago
|
show 4 more comments
3
All the functions shown are non-standard for the whole of the current millennium. All the structures are malformed. None of the code should compile.
– Jonathan Leffler
2 days ago
1
You’re not supposed to be able to compile an empty structure body. In (c) there is no typestruct temp
whens
is nominally defined, so the variable shouldn’t be definable. There might be a get-out-of-jail-free card somewhere; I don’t have a C compiler on my iPhone.
– Jonathan Leffler
2 days ago
2
@JonathanLeffler The question is about the order of declarations, the structure members are irrelevant. I addedint main
and a structure member, and it still compiles with no warnings. I also added a body tomain
that accesses the structure member, no complaints. I'm as surprised as the OP.
– Barmar
2 days ago
1
I’m asleep. My robot is responding now. If there’s still a controversy in the morning, I’ll look. If you ever needed evidence of why multichoice questions are abominable, this illustrates the point. You can’t present reasoning in an exam.
– Jonathan Leffler
2 days ago
2
@jonathan: Empty structures are a GCC extension to C.
– rici
2 days ago
3
3
All the functions shown are non-standard for the whole of the current millennium. All the structures are malformed. None of the code should compile.
– Jonathan Leffler
2 days ago
All the functions shown are non-standard for the whole of the current millennium. All the structures are malformed. None of the code should compile.
– Jonathan Leffler
2 days ago
1
1
You’re not supposed to be able to compile an empty structure body. In (c) there is no type
struct temp
when s
is nominally defined, so the variable shouldn’t be definable. There might be a get-out-of-jail-free card somewhere; I don’t have a C compiler on my iPhone.– Jonathan Leffler
2 days ago
You’re not supposed to be able to compile an empty structure body. In (c) there is no type
struct temp
when s
is nominally defined, so the variable shouldn’t be definable. There might be a get-out-of-jail-free card somewhere; I don’t have a C compiler on my iPhone.– Jonathan Leffler
2 days ago
2
2
@JonathanLeffler The question is about the order of declarations, the structure members are irrelevant. I added
int main
and a structure member, and it still compiles with no warnings. I also added a body to main
that accesses the structure member, no complaints. I'm as surprised as the OP.– Barmar
2 days ago
@JonathanLeffler The question is about the order of declarations, the structure members are irrelevant. I added
int main
and a structure member, and it still compiles with no warnings. I also added a body to main
that accesses the structure member, no complaints. I'm as surprised as the OP.– Barmar
2 days ago
1
1
I’m asleep. My robot is responding now. If there’s still a controversy in the morning, I’ll look. If you ever needed evidence of why multichoice questions are abominable, this illustrates the point. You can’t present reasoning in an exam.
– Jonathan Leffler
2 days ago
I’m asleep. My robot is responding now. If there’s still a controversy in the morning, I’ll look. If you ever needed evidence of why multichoice questions are abominable, this illustrates the point. You can’t present reasoning in an exam.
– Jonathan Leffler
2 days ago
2
2
@jonathan: Empty structures are a GCC extension to C.
– rici
2 days ago
@jonathan: Empty structures are a GCC extension to C.
– rici
2 days ago
|
show 4 more comments
1 Answer
1
active
oldest
votes
Yeah, C is weird sometimes. Because that variable is at file scope and has no initializer or storage class specifier, it constitutes a tentative defintion. The C standard defines it as follows:
6.9.2 External object definitions
A declaration of an identifier for an object that has file scope
without an initializer, and without a storage-class specifier or with
the storage-class specifier static, constitutes a tentative
definition. If a translation unit contains one or more tentative
definitions for an identifier, and the translation unit contains no
external definition for that identifier, then the behavior is exactly
as if the translation unit contains a file scope declaration of that
identifier, with the composite type as of the end of the translation
unit, with an initializer equal to 0.
I emphasized the relevant part. Because there is no initializer on your variable, it's as though you'd written it at the very end of the file and initialized to zero. The physical layout of the file is immaterial, because logically, the definition of the structure type is available at the end of the file.
So the answer is indeed (4). I wouldn't write code like that in real life however, this is terribly confusing in the C eco-system where near everything must be pre-declared to be used.
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
);
);
DSW is a new contributor. Be nice, and check out our Code of Conduct.
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%2f55780564%2fcan-we-declare-structure-object-at-file-scope-before-the-structure-definition%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Yeah, C is weird sometimes. Because that variable is at file scope and has no initializer or storage class specifier, it constitutes a tentative defintion. The C standard defines it as follows:
6.9.2 External object definitions
A declaration of an identifier for an object that has file scope
without an initializer, and without a storage-class specifier or with
the storage-class specifier static, constitutes a tentative
definition. If a translation unit contains one or more tentative
definitions for an identifier, and the translation unit contains no
external definition for that identifier, then the behavior is exactly
as if the translation unit contains a file scope declaration of that
identifier, with the composite type as of the end of the translation
unit, with an initializer equal to 0.
I emphasized the relevant part. Because there is no initializer on your variable, it's as though you'd written it at the very end of the file and initialized to zero. The physical layout of the file is immaterial, because logically, the definition of the structure type is available at the end of the file.
So the answer is indeed (4). I wouldn't write code like that in real life however, this is terribly confusing in the C eco-system where near everything must be pre-declared to be used.
add a comment |
Yeah, C is weird sometimes. Because that variable is at file scope and has no initializer or storage class specifier, it constitutes a tentative defintion. The C standard defines it as follows:
6.9.2 External object definitions
A declaration of an identifier for an object that has file scope
without an initializer, and without a storage-class specifier or with
the storage-class specifier static, constitutes a tentative
definition. If a translation unit contains one or more tentative
definitions for an identifier, and the translation unit contains no
external definition for that identifier, then the behavior is exactly
as if the translation unit contains a file scope declaration of that
identifier, with the composite type as of the end of the translation
unit, with an initializer equal to 0.
I emphasized the relevant part. Because there is no initializer on your variable, it's as though you'd written it at the very end of the file and initialized to zero. The physical layout of the file is immaterial, because logically, the definition of the structure type is available at the end of the file.
So the answer is indeed (4). I wouldn't write code like that in real life however, this is terribly confusing in the C eco-system where near everything must be pre-declared to be used.
add a comment |
Yeah, C is weird sometimes. Because that variable is at file scope and has no initializer or storage class specifier, it constitutes a tentative defintion. The C standard defines it as follows:
6.9.2 External object definitions
A declaration of an identifier for an object that has file scope
without an initializer, and without a storage-class specifier or with
the storage-class specifier static, constitutes a tentative
definition. If a translation unit contains one or more tentative
definitions for an identifier, and the translation unit contains no
external definition for that identifier, then the behavior is exactly
as if the translation unit contains a file scope declaration of that
identifier, with the composite type as of the end of the translation
unit, with an initializer equal to 0.
I emphasized the relevant part. Because there is no initializer on your variable, it's as though you'd written it at the very end of the file and initialized to zero. The physical layout of the file is immaterial, because logically, the definition of the structure type is available at the end of the file.
So the answer is indeed (4). I wouldn't write code like that in real life however, this is terribly confusing in the C eco-system where near everything must be pre-declared to be used.
Yeah, C is weird sometimes. Because that variable is at file scope and has no initializer or storage class specifier, it constitutes a tentative defintion. The C standard defines it as follows:
6.9.2 External object definitions
A declaration of an identifier for an object that has file scope
without an initializer, and without a storage-class specifier or with
the storage-class specifier static, constitutes a tentative
definition. If a translation unit contains one or more tentative
definitions for an identifier, and the translation unit contains no
external definition for that identifier, then the behavior is exactly
as if the translation unit contains a file scope declaration of that
identifier, with the composite type as of the end of the translation
unit, with an initializer equal to 0.
I emphasized the relevant part. Because there is no initializer on your variable, it's as though you'd written it at the very end of the file and initialized to zero. The physical layout of the file is immaterial, because logically, the definition of the structure type is available at the end of the file.
So the answer is indeed (4). I wouldn't write code like that in real life however, this is terribly confusing in the C eco-system where near everything must be pre-declared to be used.
edited 2 days ago
answered 2 days ago
StoryTellerStoryTeller
106k13223286
106k13223286
add a comment |
add a comment |
DSW is a new contributor. Be nice, and check out our Code of Conduct.
DSW is a new contributor. Be nice, and check out our Code of Conduct.
DSW is a new contributor. Be nice, and check out our Code of Conduct.
DSW is a new contributor. Be nice, and check out our Code of Conduct.
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%2f55780564%2fcan-we-declare-structure-object-at-file-scope-before-the-structure-definition%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
3
All the functions shown are non-standard for the whole of the current millennium. All the structures are malformed. None of the code should compile.
– Jonathan Leffler
2 days ago
1
You’re not supposed to be able to compile an empty structure body. In (c) there is no type
struct temp
whens
is nominally defined, so the variable shouldn’t be definable. There might be a get-out-of-jail-free card somewhere; I don’t have a C compiler on my iPhone.– Jonathan Leffler
2 days ago
2
@JonathanLeffler The question is about the order of declarations, the structure members are irrelevant. I added
int main
and a structure member, and it still compiles with no warnings. I also added a body tomain
that accesses the structure member, no complaints. I'm as surprised as the OP.– Barmar
2 days ago
1
I’m asleep. My robot is responding now. If there’s still a controversy in the morning, I’ll look. If you ever needed evidence of why multichoice questions are abominable, this illustrates the point. You can’t present reasoning in an exam.
– Jonathan Leffler
2 days ago
2
@jonathan: Empty structures are a GCC extension to C.
– rici
2 days ago