I have following C program. I am not understanding the output of this program:
#include <stdio.h>
int find (int a, int b, int c)
{
int temp;
c=a+b;
temp=a;
a=b;
b=2*temp;
printf("%d %d %d\n", a,b,c);
return b;
}
int main()
{
int x, y, z;
x=15;
y=25;
z=30;
printf("%d %d %d %d\n", x, y, z, find(x,y,z));
return 0;
}
According to me, the output value should be:
15 25 30
25 30 40 30
But why is it:
25 30 40
15 25 30 30
So, let's walk the steps of this program:
We create and set initial values for x, y, z.
We call printf(..., find(x,y,z));
To do the work for that printf() call, we will need to fully evaluate the find() call.
find() does some work changing local values of some variables named a, b, c.
find() calls a printf() inside it, printing 25 30 40\n.
find() returns with a value of 30.
Now, we actually have the parameters for the original printf() call, we can execute it, and it prints 15 25 30 30\n.
Note that, we can't call the first printf() until we have all of the parameters to it. To do that, we need to call find() and let it fully evaluate.
First, you modify the parameters a, b, and c inside the the find function, which accounts for the modified values in the first line of output. However, those changes are only visible inside the body of find; arguments are passed by value in C. The values of x, y, and z in main are not affected by the call to find, which explains the second line of output.
Put another way, a, b, and c are local variables to the find function which are initialized by, but in no other way related to, the values of x, y, and z in main.
Before the printf in main can print its string, it must evaluate all 4 of its remaining arguments, which includes a call to find. During the course of calling find, printf is called again, which accounts for the 3-number output line appearing first.
Related
The values which are output from inside the called function and from inside the calling function are not identical.
Why are values of y and value of m not the same?
screenshot of code and output, showing "Value of m 6" and "value of y 13"
#include<stdio.h>
int pass(a);
int main()
{
int x,y;
printf("Hello world\n");
printf("Enter the x ");
scanf("%d",&x);
y = pass(x);
printf("value of y %d",y);
return 9;
}
pass(m)
{
m = m + 5;
printf("Value of m %d\n",m);
// return 5;
}
Output:
Hello World
Enter the x 1
Value of m 6
value of y 13
Strictly, this should not even attempted to be explained, because of undefined behaviour, compare Reaching end of function without return statement
but ...
Assuming that my guess on the pattern of returned values (a constant plus the number of digits in "input + 5") is correct:
By chance, the default assumptions of the compiler when seeing the incomplete prototype int pass(a); (should be int pass(int a);), allow it to associate the later provided implementation of pass(m) (should be int pass(int m);, or more consistently int pass(int a)).
So when you call y = pass(x);, y gets the value returned by that implementation.
The implementation then lacks a clean return statement (it has one, but inactive by being a comment).
So at the end of executing that function, the most recently determined result is returned, another default of compilers which you should better not rely on for clarity and readability of code.
The most recent result is the return value of the call to printf().
That return value is the number of successfully printed characters.
You might want to read up on printf() and its return value in its specification, and about the concept of return values, prototypes and data types of parameters and return values in general.
For the output you show in your picture of text,
Value of m 6\n (thanks for making me type that...) that is, let me count
^ ^ ^ ^
1 5 10 13,
13, including the newline at the end of the output from inside the function.
Obviously, this is completely unrelated to the value of the local variable m, which is seen in the picture of text.
For more details on how to achieve what you might try to do see the comment David C. Rankin.
I simply want a program that has the user input a value for a and b, and will ask the user to repeat this process if the value a is less than b.
Here is my program:
#include <stdio.h>
#include <math.h>
int main(void)
{
int a, b ,c;
while (a<=b)
{
printf("Please enter a value for a:\n");
scanf("%d", &a);
printf("Please enter a value for b:\n");
scanf("%d", &b);
if (a<=b)
printf("a must be greater than b:\n");
}
c=a+b;
printf("The answer of c is: %d\n", c);
return 0;
}
As soon as i run the program, it prints: "The answer of c is: 1829030"
(Please note that the last number is always random)
Please help me run this program.
The value of an uninitialized non static local variable is indeterminate. This means that the value can be anything at run time. In this case, the values of a, b, c are random and the loop may or may not be entered based on the random values of a, b, c.
try -
int a = 0, b = 0, c;
You declare variables a, b, c but you do not give them an initial value. That means that they have an indeterminate value.
Right after declaring a and b with no value given, you go on and compare them in your while (a <= b) condition. C does not know how to compare these value-less variables apparently, so it skips your loop altogether.
You could avoid this problem by giving int a = 0, b = 1 values initially.
This way you make sure that the loop will at least run once.
What happens with uninitialized variables is explained in this other SO answer
currently I have a question at hand that I don't understand due to a few things. Here is the question:
Que. 1. Given the following program, show the values of the variables a, b, c, x, y, z in the main function after each function call to FindSum. Also, show the values of a, b, c in FindSum immediately after executing each function call to FindSum.
And here is the code:
#include <stdio.h>
/* function prototype declaration for FindSum */
void FindSum(int, int, int *);
int main(void)
{
int a=2, b=5, c=1, x=3, y=4, z=7;
FindSum (a, b, &c); /* a first call to FindSum */
printf(“first call in main %d %d %d %d %d %d \n”, a, b, c, x, y, z);
FindSum (x, y, &z); /* a second call to FindSum */
printf(“second call in main %d %d %d %d %d %d \n”, a, b, c, x, y, z);
return 0;
}
/* definition of FindSum */
void FindSum (int a, int b, int *c)
{
a += (b * 2);
b += (b * 2);
*c += (b * 2);
printf(“in FindSum: %d %d %d \n”, a, b, *c);
My questions:
1) Which one is the main function?
2) Is the problem asking me to finish up the code so that it produces the desired result?
3) Also, show the values of a, b, c in FindSum immediately after executing each function call to FindSum" Where is the function call to findsum
I'm currently in a C programming class where the professor doesn't explain well/english is subpar. Due to this, I am struggling slightly on these issues. Was hoping someone could shed me light
1) Which one is the main function?
The one named main.
2) Is the problem asking me to finish up the code so that it produces the desired result?
No, it's asking you to act like the computer and "run the program" on paper and say what it would output.
3) Also, "show the values of a, b, c in FindSum immediately after executing each function call to FindSum" Where is the function call to findsum
They're marked with comments in the code. Search for "call to FindSum".
It's important to understand the basic structure of a program. Including how functions work and the difference between pass by value and pass by reference. Understanding this is key towards answering these questions. The "int main(void)" line is the main function declaration. The left and right brackets define it's scope. Note the values of the variables a,b,c,x,y,&z at the beginning. Write them on a piece of paper. The FindSum function takes in two parameters and outputs one parameter. Perform the calculation and write down your results. This answers the first part of the question. Then step through line of code and write down the results after each call to FindSum. This is not asking for you to modify the code, but simply execute what the computer would compute.
What I'm trying to achieve is this:
X+Y X - Y X/Y X*Y
a b c d
I have searched google and I didn't find exactly what I was looking for, tried multiple methods,it still shows messed up.
This is my code so far:
int main()
{
int x,y,a,b,c,d;
printf("Introdu X si Y");
scanf("%d%d",&x,&y);
a=x+y;
b=x*y;
c=x-y;
d=x/y;
printf("X+Y\t","X*Y\t","X-Y\t","X/y\t");
printf("3%d,3%d,3%d,3%d",a,b,c,d);
return 0;
}
EDIT:Removing the commas worked fine,and the 3's in the code, I thought you could allign to the left with 3 spaces using 3%d.Thank you.
First of all, there's no need for the commas of the first print().
Secondly, the format you used in the second printf() is "3%d". It means that the number 3 will be printed before any number, and this is unnecessary.
Look at this code as an option to fix your problem (added some alignments and newlines)
#include <stdio.h>
int main() {
int x,y,a,b,c,d;
printf("Introdu X si Y \n");
scanf("%d%d",&x,&y);
a=x+y;
b=x*y;
c=x-y;
d=x/y;
printf("X+Y\t X*Y\t X-Y\t X/y\t\n");
printf("%d\t,%d\t,%d\t,%d\t",a,b,c,d);
return 0;
}
Some possible output is:
Introdu X si Y
6 3
X+Y X*Y X-Y X/y
9 ,18 ,3 ,2
Note that the use of int variable for the division will always give you the result as integer. Hence, in your code: 1/2 = 0.
The problem is in your printf statements.
printf("X+Y X*Y X-Y X/y\n");
printf("%d %d %d %d", a, b, c, d);
I think you just want this.
#include<stdio.h>
float func (float t, float y){
return y ;
}
int main (){
float t0,y0,t,y;
printf ("the value of t: ");
scanf ("%f",&t0);
printf ("the value of y: ");
scanf ("%f",&y0);
t=t0;
y=y0;
static int n=0;
// t[0]=t0;
// y[0]=y0;
for (n=0;n<=3;n++){
y[1]=y[0];
printf ("value of y %f %f \n",t,y);
}
return 0;
}
The error is:
Building prog.obj.
D:\master\c language\ch3\prog.c(166): warning #2117: Old-style function definition for 'main'.
D:\master\c language\ch3\prog.c(182): error #2144: Type error: pointer expected.
D:\master\c language\ch3\prog.c(182): error #2144: Type error: pointer expected.
*** Error code: 1 ***
You cannot array index something that is not an array, or a pointer into an array.
Your y and t floats are not pointers into arrays in your program.
You should make them float *y, *t into pointers so you can point them into array.
Change float t0,y0,t,y; to float t0,y0,*t,*y;
and
t=&t0; //assign address of t0 to t
y=&y0;
Change printf ("value of y %f %f \n",t,y); to
printf ("value of y %f %f \n",*t,*y); //note to dereference t and y here, to get their values
Here's a example of your program I fixed to work
The 'Old-style function definition for main()' message means that you've not given a prototype definition. The correct forms are:
int main(void) { ... }
int main(int argc, char **argv) { ... }
The version int main() is fine in C++, but not strictly a prototype in C, and hence gets the 'old-style' tag.
The other messages are more inscrutable; the line numbers do not correspond to the code you show. However, as Tony The Lion notes in his answer, the line
y[1] = y[0];
is erroneous since y is not an array. There is room to think that should be:
y = y0;
and you'd need a companion:
t = t0;
in order to have defined values printed in the printf() statement.
Even with these changes, the code does not make a lot of sense. However, given that you removed 150-odd lines, we can suppose that the missing code would make more sense.
There is no need to make n into a static variable; it is better not to do so.
Please make sure, in future, that your error messages correspond to the source code you post, not to some variant version of the code you post. The line numbers should not be as large as 166 or 182; they should be single digit numbers or small double digit numbers. But even more importantly, they should match the code!