C and addition, integer first and later [duplicate] - c

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.

Related

Does scanf function cause any logical problems with operators? [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 4 years ago.
I think the best way to describe the problem is to actually show the output of this simple code:
Image contains code and output
#include<stdio.h>
int main()
{
int a=5,b;
b=a-++a;
printf("%d %d",a,b);
return 0;
}
As you can see, the values returned are logically correct.
++a increases the value of a to 6. Then b=6-6=0.
However, when I take the value of 'a' as user input using scanf, the following happens:
Image contains code and output
#include<stdio.h>
int main()
{
int a,b;
scanf("%d",&a);
b=a-++a;
printf("%d %d",a,b);
return 0;
}
Shouldn't the results be identical or am I missing something simple here?
The form a - ++a has an undefined value because the order of executions of the argument of '-' is undefined.
In the first case the compiler knows the value of a so it can optimize the code and finally all is known at compile time, not in the second case.
The fact the value is not the same when computed at compile time or execution time is a consequence of the form a - ++a whose result is undefined.

Write a function receives two arguments and returns the difference [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 5 years ago.
Improve this question
Write a C-program that calls a function minus(). This function receives two arguments and returns the difference (regular subtraction, not absolute). This difference should be printed on screen.
My code is:
int minus(int a,int b)
{
int c = a - b;
return c;
}
int main()
{
int a = 4; int b = 5;
minus(a,b);
printf("%d", minus);
return 0;
}
I have two questions:
1.why a and b in
int minus(int a,int b)
are grey in Visual Studio? "int" is blue but a and b are grey.
2. I got this result but it should be -1.
Could someone help me please
a and b are grey because the editor is automatically coloring the code to help illustrate the program syntax. This may look funny at first, but, over time, your brain will become accustomed to it, and things that are the wrong colors will stand out. This will help you find mistakes in your program—when you make a mistake typing, something that should be a keyword will be colored like a parameter name, and you may notice it is the wrong color and take a closer look at what you typed.
In printf("%d", minus);, the minus is just the function. It is not the value returned by the function. To print the value returned by the function, use printf("%d", minus(a, b));.
You have undefined behavior here printing the function pointer using %d format specifier. (You have used the wrong format specifier that's why the Undefined behavior).
And the most probable way you would like is to printf("%d", minus(a,b));. You wanted to print the result of the subtraction not the function pointer itself.
You are missing an assignment
int minus(int a,int b)
{
int c = a - b;
return c;
}
int main()
{
int a = 4; int b = 5;
int d = minus(a,b);
printf("%d", d);
return 0;
}
you called the function minus() but you did not take the value returned by function in any variable so when you try to print minus then it will return the pointer value of function. so to get the correct answer hold the return value in variable and then print it int c = minus(a,b);
printf("%d", c); or you can call the minus function inside the print function like this printf("%d", minus()); as a beginner i will suggest you to implement the first suggestion it will increase the capacity to use statements in c.

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

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?

Printf Anonymous behavior in c [duplicate]

This question already has answers here:
Parameter evaluation order before a function calling in C
(7 answers)
function parameter evaluation order
(5 answers)
Closed 7 years ago.
Today I saw an anonymous behavior of the printf() function.
Can anybody please tell me why its behaving so.
Is that the execution of functions inside printf() is in reverse order?
Please explain this or share a helpful link.
MY CODE
#include <stdio.h>
int fun(){
static int c=15;c++;
return c;
}
int main()
{
printf("%d %d %d",fun(),fun(),fun());
}
Actual output : 18 17 16
Expected output : 16 17 18
EDIT 2:
I more thing I noticed that its behavior is not only with functions but also with variables
#include <stdio.h>
static int c=15;
int fun(){
c++;
return c;
}
int main()
{
printf(" %d %d %d %d %d",c,fun(),fun(),fun(),c);
}
Actual output : 18 18 17 16 15
Expected output : 15 16 17 18 18
Thanks in Advance :)
The order of evaluation of the parameters is unspecified. That means that it is dependent of the compiler implementation. The actual order from your example seems to be:
printf("%d %d %d",fun(),fun(),fun());
/* (3) (2) (1) */
but this is arbitrary and could well be any one out of the 6 possibilities.
int main()
{
int a, b, c;
a = fun();
b = fun();
c = fun();
printf("%d %d %d", a, b, c);
}
And you get what you need.
The parameter evaluation order is under compiler's control but the statement evaluation order is under your control.
This behavior seen because of the static variable. You may know A static variable inside a function keeps its value between invocations.
So the variable changed by the earlier call to the function fun() remains for the later call to the function. And the reason for the disordering of the values is -
The order that function parameters are evaluated is an unspecified behavior. It is only ensured that all parameters must be fully evaluated before the function is called.
You may have a look this link for some common undefined behavior in C and C++
A similar question was asked here:
Parameter evaluation order before a function calling in C
Essentially, the compiler is free to evaluate parameters to a function in any order.
When asked about common undefined behaviours in C, one user answered with this, which also answers your question.
The order in which the parameters to a function in not defined in the standard, and is determined by the calling convention used by the compiler.
You can refer this
I run the program, and get an output 18 17 16.
That means the different fun()'s run from the right to the left.
However, it is still an undefined behaviour. The order of them is not defined clearly in C, and it depends on the compiler.

Input Puzzler in C [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)
int main()
{
int a=5,s;
s=++a + ++a;
printf("%d",a);
printf("%d",s);
}
output is 7 and 14
BUT
int main()
{
int a, s;
printf("Enter value of a");
scanf ("%d",&a);
s=++a + ++a;
printf("%d",a);
printf("%d",s);
}
input user gives is 5
output is 7 and 13
WHY?
Undefined behaviour:
s=++a + ++a;
Anything can happen when undefined, so your behaviour is perfectly valid.
I'd suspect this is an artifact of compiler optimisation, in the first example a is known so the compiler optimises the preincrements to occur before the addition. In the second example the value is unknown and the compiler does not optimise the sequence causing it to complete left to right. This may be a function of your specific compiler and it would need to be looked at specifically.
Undefined behaviour. Change it, or you risk being attacked by raptors.
hi budy this coding working correctly in VI compiler ..

Resources