C uninitialized int has a value of 1 instead of 0 [duplicate] - c

This question already has answers here:
Initializing variables in C
(10 answers)
Closed 6 years ago.
#include <stdio.h>
#include <string.h>
#include "prac.h"
#define MYNAME "Butter"
int main() {
int numberOfKids;
int weight;
int shirt;
printf("If I eat a Watermelon I will weigh %d lbs \n", weight + numberOfKids+ shirt );
return 0;
}
I compiled and ran the program and the result was 1; although I expected it to be 0. When I checked the value of each variable individually, the weight variable's value was 1. Can someone explain why that specific variables result was not 0? I am new to C and want to experiment with the basics to get a deeper understanding of the nuances of C. Any help would be appreciated.

Variables inside a function in C are not guaranteed to be set to anything by default. In memory, whatever was last stored there (which might not be flushed/erased to be 0) will be what the int is initialized to.
This is answered in Initializing variables in C
EDIT: As chux has stated below, local static variables are initialized to 0 if they aren't given an initial value. Also covered Is un-initialized integer always default to 0 in c?

Related

Why does it show the output as zero and not the garbage value if the variables have not been initialized? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Here is a program in C language that simply displays variables that have not been initialized,
#include <stdio.h>
int main()
{
int a, b;
printf("%d%d",a,b);
return 0;
}
Output:
00
I have started learning c programming 20 minutes ago for the first time in my life, can you tell me why does it show the output as 00, now since we're supposed to initialize out variables with 0 in most cases to avoid seeing the garbage value in their place so I just wanted to know why does it happen ? Why don't we get a garbage value instead and not the plain '0' which has its own ASCII value?
int x = 0;
printf("%d", x); // -> 0
shows the value of x as an integer in memory. However, uninitialized variables cannot be trusted to have a value of zero every time. See What happens to a declared, uninitialized variable in C? Does it have a value?
If you want the character representation of that integer, use
int x = 99;
printf("%c", x); // -> c
instead. It will give the ASCII representation.
int x = 99; means 'c')
variable not assigned doesn't mean that it has no value, it just means that the value is not defined (or known if your prefer). But be careful, reading a variable that has never been initialised is undefined behaviour.
By default C does not give a value to uninitialized variables, you have to tell it what to store in each variable. If you do attempt to use these variables without giving them a value, the output will be unpredictable.
For example when I ran this on my pc I go the value: 41944324050944
Do not do this. If you do your program will throw unexpected results when you run it later. See this.
The proper way you would do this is to define a and b like this:
int a = 0;
int b = 0;
In the case of defining static storage the default value would be 0 (always) see this web page for more information.
This program will return 00 as expected:
#include <stdio.h>
int main()
{
static int a, b;
printf("%d%d",a,b);
return 0;
}
even-though a and b are not assigned a value the use static storage and therefore have a value 0.
Also if you want ASCII characters printed use %c instead of %d. See here. Also note that 00 in ASCII is null see here for printing null variables.

C and addition, integer first and later [duplicate]

This question already has answers here:
What happens to a declared, uninitialized variable in C? Does it have a value?
(9 answers)
Closed 6 years ago.
I'm come from Java, i want to improve my skill in coding and knowledge of how it's work in deep and i figure that the best language for this is C the mother of all. I'm very excited about how it work c but now please raise me a doubt. Why in C first code don't work and the second yes?
P.s.: I'll skip few steps to speed the code and focus on problem. I'm study C99.
int a,b,c;
int sum = a+b+c;
print scanf ecc...
printf("%d", sum);
The result it will be -1234567 ecc..
And using this code it will work wonderful, this is the mean of a imperative programming?
int a,b,c;
int sum;
print scanf ecc...
sum = a+b+c;
printf("%d", sum);
Sorry for bad english is not my first language, i will improve also that :°D
When you use the first part of the code i.e.
int a,b,c;
int sum = a+b+c;
print scanf ecc...
printf("%d", sum);
it will first add the a ,b , c
and then will produce result with garbage value
while in second case
int a,b,c;
int sum;
print scanf ecc...
sum = a+b+c;
printf("%d", sum);
it will read the values by using the scanf and then add those values so will not take the garbage value and produce a wonderful result
Local variables are not initialized in C, their values are indeterminate. Using an uninitialized local variable leads to undefined behavior.
C is also, exactly like Java, sequential in the absence of loops or gotos. Statements are executed from top to bottom so calling scanf to initialize a variable after you used it will not work. The previous operation will not be redone.

How can we be sure that value of 'b' is 0 at beginning in this code [duplicate]

This question already has answers here:
Are global variables always initialized to zero in C? [duplicate]
(6 answers)
Closed 6 years ago.
This is a program which I came across on the net that calculates the value of 𝝅.
#include <stdlib.h>
#include <stdio.h>
long a=10000,b,c=2800,d,e,f[2801],g;//--------HERE the value of b---------//
int main()
{
printf("\nValue of b: %ld", b);
getch();
for(;b-c;)
f[b++]=a/5;
for(;d=0,g=c*2;c-=14,printf("%.4d",e+d/a),e=d%a)
for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b);
}
The calculated value is correct. But how could the programmer be sure that the initial value of b would be 0?
It does not seem to be initialized at all!
Is there some specialty about initial value of global variables?
As pointed out in this question, global variables are initialized to 0 by default.
In your code, b is declared as a global variable.

Confused with GCC output [duplicate]

This question already has answers here:
Uninitialized variable in C [duplicate]
(6 answers)
Closed 7 years ago.
Recently I have installed Ubuntu and obviously I am compiling my C code in gcc. I came across the following code :
#include <stdio.h>
main()
{
int i = 10,j = 20, k;
printf("i=%d j=%d k=%d\n", i, j, k);
}
The output is coming as ::
i=10 j=20 k=0
But as far as I know that the output for the value of k should be a Garbage value since it has not been initialized.
Is there something that I am missing here?
You cannot expect anything from the program you posted, 0 is a perfectly possible garbage value.
Also, main() must have a return value and it must be int.
It's undefined behavior, so anything is possible, including 0 to be printed.
How is 0 not a garbage value?

C Programming Math Functions #include <stdlib.h> abs() [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I start learing C programming from "Beginning Programming with C For Dummies" by Dan Gookin.
I have a problem with understanding "C Math Functions" - my question is how to use #include <stdlib.h> and abs() function. Only explanation in the book is this:
Here is a simple example:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i = -42;
int j = abs(i);
printf("i = %d, j = %d\n", i, j);
return 0;
}
LIVE DEMO
Besides the examples given above,functions work just like a mathematical function
in that y = f(x) you put in x and it returns a y(of course some dont return anything and some dont take in anything)
now what you need to do is to catch that value when it returns it by storing it into some memory location which is called a variable that you declare
its also important that you know what the return type is so that you dont truncate or lose some part of the result
for example if the result is a floating point number then you need to return the value into a float/double variable and if it is a integer you can store it in either int or double/float
also if it is a char you would probably want it to be returned to a char variable and so on and so forth
So any function that you write or that you use from somebody elses library/header is going to work like that
It takes in an argument(sometimes it can even take no arguments and you just use it by calling it with parentheses, parentheses must always be typed because otherwise it will look like a variable or something else and convention says so) and it returns some result(if it does return a result) or does something else unrelated to the calling functions variables/values
So hopefully that explains what functions are, and how you can use them
Now all of this described also applies to the functions you posted about, they have a set algorithm that they do which somebody else wrote, and you just give it the argument, and catch the return type and it will do what it says it intended to do i.e abs() gives you the absolute value, pow() returns the square of some base and so on
The line #include <stdlib.h> gives your code access to certain functions and abilities that are within that library. One of which is the abs() function.
The abs() function return the absolute value of an integer, by that we mean it will always convert it to a positive number.
Example:
#include <stdio.h> /* printf */
#include <stdlib.h> /* abs */
int main ()
{
int n,m;
n=abs(23);
m=abs(-11);
printf ("n=%d\n",n);
printf ("m=%d\n",m);
return 0;
}
Edit & Run
Output:
n=23
m=11

Resources