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 8847641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:14:18+00:00 2026-06-14T12:14:18+00:00

Possible Duplicate: Default values of int when not initialized in c. why do i

  • 0

Possible Duplicate:
Default values of int when not initialized in c. why do i get different outputs?

Beginner so be lil soft..am compiling a simple code below, I am not assigning any value to my variables but C program generates some random values, why is it so?(Only 2nd variable generates random integers)

So where these values came from?

#include<stdio.h>

main(void) {
    int var1;
    int var2;

    printf("Var1 is %d and Var2 is %d.", var1, var2);
    return 0; //Book says I should use this for getting an output but my compiler anyways compile and return me values whether I use it or not
}

//Output 1st compiled: var1 = 19125, var2 = 8983
//Output 2nd compiled: var1 = 19125, var2 = 9207
//Output 2nd compiled: var1 = 19125, var2 = 9127
  • 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-06-14T12:14:20+00:00Added an answer on June 14, 2026 at 12:14 pm

    Your C program is compiled to some executable program. Notice that if you compile on Linux using gcc -Wall, you’ll get warnings about uninitialized variables.

    The var1 and var2 variables get compiled into using some stack slots, or some registers. These contain some apparently random number, which your program prints. (that number is not really random, it is just unpredictable garbage).

    The C language does not mandate the implicit initialization of variables (in contrast with e.g. Java).

    In practice, in C I strongly suggest to always explicitly initialize local variables (often, the compiler may be smart enough to even avoid emitting useless initialization).

    What you observe is called undefined behavior.

    You’ll probably observe a different output for var1 if you compiled with a different compiler, or with different optimization flags, or with a different environment (probably typing export SOMEVAR=something before running again your program could change the output for var1, or running your program with a lot of program arguments, etc…).

    You could (on Linux) compile with gcc -fverbose-asm -S and add various optimization flags (e.g. -O1 or -O2 …) your source code yoursource.c and look inside the generated yoursource.s assembler code with some editor.

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

Sidebar

Related Questions

Possible Duplicate: Why global and static variables are initialized to their default values? What
Possible Duplicate: Concatenate Two NSDate String values Just confused that is there any default
Possible Duplicate: Default value of a type In C#, to get the default value
Possible Duplicate: Howto change what Default(T) returns in C# print(default(int) == 0) //true Similarly
Possible Duplicate: Does Java support default parameter values? Is it possible to do something
Possible Duplicate: How can I properly escape HTML form input default values in PHP?
Possible Duplicate: Uninitialized variables and members in Java Why are local variables not initialized
Possible Duplicate: Default values for array arguments How do I give an array as
Possible Duplicate: Get back default properties after applying a global CSS reset I want
Possible Duplicate: Default textbox border-style and width I have some TextBox on my page

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.