Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6754229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:15:18+00:00 2026-05-26T13:15:18+00:00

I had a question regarding a C code. #include <stdio.h> void foo(void){ int a;

  • 0

I had a question regarding a C code.

#include <stdio.h>

void foo(void){
        int a;
        printf("%d\n",a);
}

void bar(void){
        int a = 42;
}

int main(void){
        bar();foo();
}

Apparently I am supposed to get a 42 as a result at the end of compilation. http://www.slideshare.net/olvemaudal/deep-c , slide #126. But when I compile it on my machine I get garbage values (gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5))

Do I need to turn off some optimization? Or is the author of the slide wrong about this? Can someone explain what should be the result of the code? If it is 42, could you also explain how/why? Also, would the result be any different on a C++ compiler (g++).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:15:19+00:00Added an answer on May 26, 2026 at 1:15 pm

    Accessing uninitialized variables is simply undefined behavior.
    Undefined behavior means that the standard doesn’t specify how to behave in some situations, including this one. The compiler might even “make demons fly out of your nose” (popular joke about UB 🙂 )

    In that article the program works because bar‘s a and foo‘s a are placed in the same memory location, but this is completely uninsured and you should never rely on such behaviors!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've a question regarding the following code: #include all_needed.h static uint8_t array[2] = {0};
I am going to paste a code here and had a question regarding that
i had a question in my program. When I pass the 3D int array
I had a quick question regarding the datacontractserializer. Maybe it's more of a stream
I had a question regarding the split function used in Visual Basic. As of
I'm bit of a newbie and had a basic question regarding adhering to the
I'm building a TCP server app and had a question regarding the efficiency of
I had earlier posted a question, regarding same, but over here i want guidance
I have a question regarding as3 listeners, and instances of a class. The main
I have recently started with XML and had a question regarding XML schemas. As

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.