-1

When after only defining any number of variables in a source code and trying to print it without initializing it.I am getting the values zero for the last two variables and one or two some other variables.The Last two variables is like blindly(at least to me) we can say like it is declared to zero.

As I am defining the variable inside the function the storage class is defaultly auto and its value is garbage value in this case why is it showing zero for the last two variables every time and for some other variables which is zero is not changing after multiple execution.

NOTE

1.If I take the last two zero values and other variables with zeroes as as garbage then why is the zeroes not changing after executions whereas other values are changing after executions.

2.Anyways the last two variable with any number of variables defined is gonna be zero.Why this is only happening with the last two variables.

3.This is not only happening with online compiler I have tried with vs code using gcc compiler in Windows 8 and Linux(pop Os) as the C program is platform dependent.

Same is in Case with CPP

First Execution Pic

Second Execution Pic

Code

#include<stdio.h>
int main(){
   int a,b,c,d,e,f;
   printf("%d\t%d\t%d\t%d\t%d\t%d\t",a,b,c,d,e,f);
   return 0;
}

Is there any logic behind the execution.Somebody please explain.Thank you.

false
  • 10,264
  • 13
  • 101
  • 209
  • 3
    And why are you posting code as images? Post it as text, please. – Paul Sanders Dec 30 '21 at 17:44
  • You're not guaranteed to get random number garbage on uninitialized variables. All you can conclude is that you can't and shouldn't assume anything about their value. They could be zero. They could be nonsense. They could be zero most of the time but some other value in rare cases. They simply use registers that may or may not have been used for something else previously, and it could very well be that the last two variables happen to use storage that had zeros previously. – nanofarad Dec 30 '21 at 17:44

1 Answers1

0

If you don't initialize a variable and use its value, it will result in undefined behavior. For example,

int i; //i has not been initialized and so holds garbage value
printf("%d\n",i);//this will lead to undefined behavior. 

Undefined behavior means anything1 can happen including but not limited to the program giving your expected output. But never rely(or make conclusions based) on the output of a program that has undefined behavior.


1For a more technically accurate definition of undefined behavior see this where it is mentioned that: there are no restrictions on the behavior of the program.

Jason
  • 36,170
  • 5
  • 26
  • 60
  • If I define 2 variables print the values without initializing and getting two zeroes.Does that mean undefined behavior ?please explain it briefly. – J C Subanesh Dec 30 '21 at 18:01
  • @JCSubanesh Yes if you use *any* *uninitialized* variable then it will lead to Undefined behavior. It doesn't matter what output you get. Never rely on the output of a program that has undefined behavior. – Jason Dec 30 '21 at 18:02
  • Lets say If an interviewer asks me to explain the auto storage class concept with its default value.Could you please tell how to explain it with example – J C Subanesh Dec 30 '21 at 18:04
  • @JCSubanesh Sure but for that you should ask a separate question so that the intent of this current question and your new question don't get mixed up. Also, can you mark my answer as correct if it helped you. – Jason Dec 30 '21 at 18:07
  • The auto storage class doesn't have a default value. The value is indeterminate until you explicitly write a value to it. There happens to be far more `0` values floating around (from the previous memory usage) than the more obviously 'garbage' values. – Weather Vane Dec 30 '21 at 18:18
  • @JCSubanesh To avoid these problems just initialize the variables for built in types irrespective of the fact that whether or not you're using `auto`. – Jason Dec 30 '21 at 18:25