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;








10















According to the code given below and the answer for it:




Question: Which of the following structure declarations will throw an error?



  1. struct temp char c; s;
    int main(void)

  2. struct temp char c; ;
    struct temp s;
    int main(void)

  3. struct temp s; 
    struct temp char c; ;
    int main(void)

  4. None of the above.


Answer: 4




Is this correct? Can we declare a structure object first and only then the structure definition?










share|improve this question









New contributor




DSW is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 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 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





    @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





    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

















10















According to the code given below and the answer for it:




Question: Which of the following structure declarations will throw an error?



  1. struct temp char c; s;
    int main(void)

  2. struct temp char c; ;
    struct temp s;
    int main(void)

  3. struct temp s; 
    struct temp char c; ;
    int main(void)

  4. None of the above.


Answer: 4




Is this correct? Can we declare a structure object first and only then the structure definition?










share|improve this question









New contributor




DSW is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 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 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





    @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





    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













10












10








10


1






According to the code given below and the answer for it:




Question: Which of the following structure declarations will throw an error?



  1. struct temp char c; s;
    int main(void)

  2. struct temp char c; ;
    struct temp s;
    int main(void)

  3. struct temp s; 
    struct temp char c; ;
    int main(void)

  4. None of the above.


Answer: 4




Is this correct? Can we declare a structure object first and only then the structure definition?










share|improve this question









New contributor




DSW is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












According to the code given below and the answer for it:




Question: Which of the following structure declarations will throw an error?



  1. struct temp char c; s;
    int main(void)

  2. struct temp char c; ;
    struct temp s;
    int main(void)

  3. struct temp s; 
    struct temp char c; ;
    int main(void)

  4. None of the above.


Answer: 4




Is this correct? Can we declare a structure object first and only then the structure definition?







c






share|improve this question









New contributor




DSW is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




DSW is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 days ago









StoryTeller

106k13223286




106k13223286






New contributor




DSW is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 days ago









DSWDSW

564




564




New contributor




DSW is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





DSW is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






DSW is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 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 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





    @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





    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





    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 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





    @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





    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












1 Answer
1






active

oldest

votes


















14














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.






share|improve this answer

























    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.









    draft saved

    draft discarded


















    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









    14














    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.






    share|improve this answer





























      14














      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.






      share|improve this answer



























        14












        14








        14







        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.






        share|improve this answer















        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 2 days ago

























        answered 2 days ago









        StoryTellerStoryTeller

        106k13223286




        106k13223286






















            DSW is a new contributor. Be nice, and check out our Code of Conduct.









            draft saved

            draft discarded


















            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

            Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

            Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form