Are structure variable initialized by default? [duplicate] - c

This question already has answers here:
Are the members of a global structure initialized to zero by default in C?
(5 answers)
Closed 2 years ago.
I didn't find anything online about what happens when objects are created in C: like their value are initialized or they take garbage value.
#include <stdio.h>
struct temp
{
int a;
} s;
int main()
{
printf("%d", s.a);
}
OUTPUT is : 0.
So is 0 a garbage value?? Or is it an undefined behavior?

Since globals and static structures have static storage duration, the answer is yes - they are zero initialized (pointers in the structure will be set to the NULL pointer value, which is usually zero bits, but strictly speaking doesn't need to be).
You have used a global structure variable. Therefore initialised to default value, i.e., 0.

It depends on the place where the variable is declared. The structure variable s is declared as global. So it initialized to zero by default.
More in this link

Related

C are pointers erased after function call ends? [duplicate]

This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
How to access a local variable from a different function using pointers?
(10 answers)
Closed 2 years ago.
I have learned that whenever a function ends, every local variable declared inside of it is erased. That's why we must use malloc when declaring an array.
But when I think about that again when a function ends what is really erased (in case I defined an array) is the pointer to that array and not the array itself, So we could simply solve the former problem by returning the pointer.
Here is a look at my code, why did it work while my professor said "we must use malloc"?
#include <stdio.h>
int * test ()
{
int arr[3]={4,5,6};
return arr;
}
int main() {
printf("Hello, World!\n");
int *arr=test();
printf("%d",arr[1]);
return 0;
}
Edit: I'm working according to the C99 standard if that does make a difference.
Option 1:
Change it to static
static int arr[3]={4,5,6};
Lifetime of a static variable is throughout the program. So we can always create a local static array and return it.
Option 2:
Create a struct
struct wrapper {
int arr[3];
};
The reason this is, array members of structures are deeply copied.

Need explanation on short code snippet [duplicate]

This question already has answers here:
What happens to a declared, uninitialized variable in C? Does it have a value?
(9 answers)
Closed 5 years ago.
I just made this short program. Can someone please explain why I am getting 2 as a result here?
Here is the code
#include <stdio.h>
#include <stdlib.h>
int main()
{
int variable;
int a;
a=variable;
a=200;
printf("%d",variable);
return 0;
}
Because you print the value of an uninitialized variable. It will have an indeterminate (and seemingly random) value.
The assignment you make to a just copies the value of variable and then of 200 into a. The value of variable remains unmodified and indeterminate.
I recommend you find a good beginners book or two to read.
At the beginning, you define two variables, a and variable. Note, that those variables at this point are not initialized. Right now, the compiler only knows that there are two variables of type int and their names, nothing more.
You then try to initialize variable a with the not initialized variable variable, the result should be clear: The two variables remain uninitialized.
Then you proceed and initialize a with 200, variable is still only defined, not initialized.
After that, you print the still uninitialized variable variable, which hasn't recieved any "real" value as of yet, only what already was "lying around" in memory when the compiler assigned that memory location to the variable. In your case, that was "2" (or at least, that's what printf could extract from there).
Further reading: C Variables. This explains how variables are defined, declared and initialized.
Assigment operator (a = variable;) does not link the two variables, just assignes whatever value there is in the righthandside expression to the left handside.
You can visualize local variables as boxes where you can put values into.
In c if you don't assign a value to the variable then it stores the garbage value which can be anything.In your code you did not assign the value to variable so it print 2 (which can be anything)

Is there any fixed assignment for int and char if not assigned? [duplicate]

This question already has answers here:
What happens to a declared, uninitialized variable in C? Does it have a value?
(9 answers)
Closed 5 years ago.
#include<stdio.h>
void main(void)
{ char m,n;
printf("%d\n",m);//value of m
printf("%d",n);//value of n
}
in the above snippet value of m is always printed as 0 why?why it doesn't change even after multiple times compilation is it automatically assigned if we do not assign while the value of n always changes so why not both change randomly every time i compile?
Am i missing any concept?
Local variables like yours are automatic variables. They are allocated on stack memory and their value is garbage.
Global variables are implicitly have static storage class and have value 0 by default.
Since m is a local variable, its value needn't always be the same. It is indeterminate. This memory location can be given to other processes.

C struct array default values [duplicate]

This question already has answers here:
What happens to a declared, uninitialized variable in C? Does it have a value?
(9 answers)
Closed 7 years ago.
I have this struct:
typedef struct {
GPIO_TypeDef* GPIO_Reg;
uint16_t GPIO_Pin;
uint16_t status;
} PinType;
Then if I declare this array:
PinType array[10];
The PinType elements in the array are initialized with some default values?
For example, if I write this:
printf("%d", array[1].status);
Should I see 0 as output? Or the initial value depends on the content of the memory before I declared the array?
This answer depends on the scope of the variable.
if array is global, then it will be auto-initialized.
if array is static, all the elements will be auto-initialized to 0.
if array is of automatic storage, it won't be initialized automatically.
The structure members are not initialised, unless the variable is static or global.
In fact, using an uninitialised member is undefined behaviour in C.
memsetting the array with zeros is idiomatic.

Local variables Vs Global variable [duplicate]

This question already has answers here:
What happens to a declared, uninitialized variable in C? Does it have a value?
(9 answers)
Closed 7 years ago.
int main()
{
int a;
printf("the value is %d", a+'a');
return 0;
}
In the above code a is local variable, And local variable are initialize to garbage value if we don't explicitly give them value . So the output should be some garbage value . But why am I getting output as 97?
In your code,
printf("the value is %d", a+'a');
produces undefined behaviour. The output of UB, is, well, undefined.
You cannot rely upon (or justify) the outcome (if any) for a statement which invokes UB.
Local variables are stack variables. They are not initialized (unlike static variables). So better initialize yourself.

Resources