Can we declare structure object at file scope before the structure 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
    Apr 21 at 7:00






  • 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
    Apr 21 at 7:07






  • 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
    Apr 21 at 7:10






  • 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
    Apr 21 at 7:13






  • 2





    @jonathan: Empty structures are a GCC extension to C.

    – rici
    Apr 21 at 7:15


















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
    Apr 21 at 7:00






  • 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
    Apr 21 at 7:07






  • 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
    Apr 21 at 7:10






  • 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
    Apr 21 at 7:13






  • 2





    @jonathan: Empty structures are a GCC extension to C.

    – rici
    Apr 21 at 7:15














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 Apr 21 at 7:30









StoryTeller

107k14224288




107k14224288






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 Apr 21 at 6:49









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
    Apr 21 at 7:00






  • 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
    Apr 21 at 7:07






  • 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
    Apr 21 at 7:10






  • 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
    Apr 21 at 7:13






  • 2





    @jonathan: Empty structures are a GCC extension to C.

    – rici
    Apr 21 at 7:15














  • 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
    Apr 21 at 7:00






  • 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
    Apr 21 at 7:07






  • 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
    Apr 21 at 7:10






  • 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
    Apr 21 at 7:13






  • 2





    @jonathan: Empty structures are a GCC extension to C.

    – rici
    Apr 21 at 7:15








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
Apr 21 at 7:00





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
Apr 21 at 7:00




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
Apr 21 at 7:07





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
Apr 21 at 7:07




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
Apr 21 at 7:10





@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
Apr 21 at 7:10




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
Apr 21 at 7:13





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
Apr 21 at 7:13




2




2





@jonathan: Empty structures are a GCC extension to C.

– rici
Apr 21 at 7:15





@jonathan: Empty structures are a GCC extension to C.

– rici
Apr 21 at 7:15












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 Apr 21 at 7:33

























        answered Apr 21 at 7:16









        StoryTellerStoryTeller

        107k14224288




        107k14224288
























            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

            Plaza Victoria

            Brian Clough

            Cáceres