C programing for beginner I am fairly new in computer programming. My professor
ID: 3687581 • Letter: C
Question
C programing for beginner
I am fairly new in computer programming. My professor handed us the following code and it prints 1, 5, 4, 15
int func1(int x, int y, int *z);
int main()
{
int a = 1, b = 5, c = 4, d;
d = func1(a, b, &c);
printf("%d %d %d %d ", a, b, c, d);
return 0;
}
int func1(int x, int y, int *z)
{
int k = 2;
x += y; *z = 2 * k;
return(x + y + *z);
}
QUESTIONS:
1. I am still not clear why it prints that. I think 1 and 5 come from assigning the values to a and b, but I don't understand where the 4 and 15 come from.
2. Would somebody mind explaining me why we need int func1(int x, int y, int *z),?
3. What does d = func1(a, b, &c); mean?
4. Why we write return(x+y+*z) even though we haven't given any values to x and y?
Thank so much
Explanation / Answer
1)
I will explain you some what clearly,
He in declaration part we declared int c=4;
If we print the c variable using printf("%d",c); it prints the value contained in the variable ‘c’. i.e it prints 4.
&c means the address of the memory location whose name is c.
If we print the c variable using printf("%d",&c); it prints the address of the memory location whose name is ‘c’. like 2293312
#include <stdio.h>
int func1(int x, int y, int *z);
int main()
{
int a = 1, b = 5, c = 4, d;
//here we are calling function by passing the variable a , b and the address of memory location c(2293312)
d = func1(a, b, &c); //here the value the function returns is 15.therefore d=15.
printf("%d %d %d %d ", a, b, c, d);//here that’s y we are getting output as (a=1) (b= 5) (c= 4) (d=15)
return 0;
}
int func1(int x, int y, int *z)
{
int k = 2;
x += y; //Here the value of x =1 and y=5 . then x+=y means that x=x+y; therefore x=1+5=6;
therefore x=6
*z means the value contained in the address.
*z=2 * k;(here k=2) then k*2=4.then the value of 4 is stored in the address 2293312(which is the address of c).i.e it is storing the value 4 in the memory address 2293312.
Therefore the value contained in that address (2293312) is 4;
*z = 2 * k;
return(x + y + *z);
//Here we are adding x(6) ,y(5) and the value contained in the memory address 2293312(which is 4).therefore it return(6+5+4=15)
}
_____________________________________________________________________________________
2) Would somebody mind explaining me why we need int func1(int x, int y, int *z),?
Ans) here there is no any special purpose of calling the function int func1(int x, int y, int *z).
These are the basic programs just to understand the concepts and to make us familiar they will give this type of programs and asking us for output.
_____________________________________________________________________________________
3)What does d = func1(a, b, &c); mean?
just we are calling the function by passing some values as arguments to it.
here func1(a, b, &c); means nothing but func1(1,5,2293312) .this memory addrress is of the varibble c
and this function is returning some value which is of type int stored in a variable 'd'
____________________________________________________________________________________
4)return(x+y+*z)
x , y is getting the value from the function implementation arguments func1(1,5,2293312)
from this x=1 and y=5 and
here we are calculating the values of x
x is calulated from x+=y(i.e x=x+5) and y is 5
y is already 5
*z=2*y; means it is storing the value(2 * y=2*2=4) into the address of memory location 2293312.
there fore it is returning (6+5+4=15) to the calling function.
________________________________________________________________________________________
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.