Having an issue with pointers and functions.
#include <stdio.h>
int f(int *a, int *b);
int main()
{
int a = 2, b = 7;
b = f(&b, &a);
printf("a = %d,\n", a);
printf("b = %d\n", b);
return 0;
}
int f(int* a, int* b) {
(*a) = (*a) + 3;
(*b) = 2*(*a) - (*b)+5;
printf("a = %d b = %d\n", *a, *b);
return(*a)+(*b);
}
can someone explain to me why the output is
a = 10 b =23
a = 23
b = 33
I understand that (&b, &a) flips the numbers in the function but why does b = 23 and not 13 ?
Ans :
int main()
{
int a = 2, b = 7;
b = f(&b, &a);
printf("a = %d,\n", a);
printf("b = %d\n", b);
return 0;
}
in that function initailly value of a=2 and b=7
and when we calling the function f(&b, &a); we are passing the reference of the variable. It is call by reference type function. inside that function we are passing the address of the variable a and b.
so in the first arguement we are passing reference(address) of b and in the second arguement, address of a is passed.
Inside the function f
int f(int* a, int* b) {
(*a) = (*a) + 3;
(*b) = 2*(*a) - (*b)+5;
printf("a = %d b = %d\n", *a, *b);
return(*a)+(*b);
}
we are getting the address of b into a and address of a into b.
So here address of variable in the memory are flipped.
In that function currently a is holding value 7 and b is holding value 2.
in the expression (*a) = (*a) + 3; will be evaluated as (*a)=7+3=10;
so value of (*a) is 10.
In the next expession (*b) = 2*(*a) - (*b)+5; here (*b)=2
=> (*b) = 2 * 10 - 2 +5 =23
so printf("a = %d b = %d\n", *a, *b); will print a =10 b =23. inside the function these are local variable for the function.
This statement return(*a)+(*b) will return 10 +23=33
So return(*a)+(*b); will return 33
and that value is hold by b in the main function b = f(&b, &a); so here value of b will became 33.
and value of a will became 33.
Handwritten detailed solution :
Having an issue with pointers and functions. #include <stdio.h> int f(int *a, int *b); int main()...
#include<stdio.h> int f(int a,int b); int main(){ int a=8, b=4,c=2; b = f(c,a); printf("a=%d,b=%d\n,a,b,c); c = f(a+3,b-2); printf("a=%d,b=%d,c=%d\n",a,b,c); return 0; } int f(int a, int b){ a=b+6 b=a-2 printf("a=%d,b=%d\n",a,b); return a+b; } what is the first line of output produced? what is the second line of output? what is the third line if output? what is the forth line of output? if there is problems ill post the choices thank you
Solve using C programming 3. lf you are given this code. #include <stdio.h> int main() int var1, var2; int sum; printf("Enter number 1:\n "); scanf("%d",&var1); printf("Enter number 2:In ); scanf("%d",&var2); sum-var1+var2; printf ("Vnsum of two entered numbers : %d", //printf ("Output: %d", res); sum); return e; Modify this code by creating a function called "addition". Make the arguments of the functions numberl and number 2. Add the two values in the function. Return a value called "result". Print "result" in...
#include <stdio.h> int main(int argc, char *argv[]) { int i; for (i = argc - 1; i > 0; i--) printf("%s ", argv[i]); printf("\n"); return 0; } can you explain this code in c and why use this function
what is output #include<stdio.h> pint main() { int i, j, total; for ( 2 { printf("\n"); for ( 2 { total = i + j; printf("%d", total); بہا } getchar(); return 0; } Find the for loop content | Output of progrm 5 15 1 11 21 31 41 51 61 2 12 22 32 25 3 13 23 33 43 53 63 4 14 24 34 44 54 64 35 45 6 16 26 36 46 56 66 7...
24. What will be the output of the following code #include<stdio.h> int main() { char *p; p="%d\n"; p++; p++; printf(p-2, 100); return 0; } a. 100 b. 00 c. 10 d. compiler error e. none of the above 26. What will be the output of the following code #define TRIPPLE(x) (3*x) int x = 4; printf(“triple(%d) = %d \n”,x+1, TRIPPLE(x+1)); a. triple(5) = 12 b. triple(5) = 13 c. triple(4) = 14 d. triple(4) = 15 e. none of the...
Convert the below C code to basic MIPS. Please leave comments for explanation #include <stdio.h> int main(void) { printf("Insert two numbers\n"); int a,b; scanf("%d",&a); scanf("%d",&b); a=a<<2; b=b<<2; printf("%d&%d\n",a,b); return 0; }
Convert the below C code to basic MIPS. Leave comments for explanation please #include <stdio.h> int main(void) { printf("Insert two numbers\n"); int a,b,c; scanf("%d",&a); scanf("%d",&b); c=a|b; printf("%d|%d=%d\n",a,b,c); return 0; }
What is going to be the output of the following program: # include <stdio.h> int main(void) {struct dob {int month; int day; int year;}; struct student {int sid; int yearOfAdmission; float currentGPA; struct dob bday; struct student stdnt = {123, 2015, 8.3, 11, 26, 1993}; printf(" Id is: %d \n", stdnt.sid); printf("Year of admission is: %d \n", stdnt.year OfAdmission); printf("Current GPA is: %.2f\n\n", stdnt.currentGPA); printf("Date of Birth is: %d/%d/%d", stdnt. bday. month, stdnt. bday.day, stdnt. bday.} return 0;}
All in C please~ #1 Consider this program: #include <stdio.h> int main () { /* main */ constint constant1 = 10, constant2 = 20, constant3 = 14; int input_value; char current_truth; printf("What is the input value?\n"); scanf("%d", &input_value); current_truth = input_value > constant1; printf("current_truth = %d\n", current_truth); current_truth = current_truth && (input_value < constant2); printf("current_truth = %d\n", current_truth); current_truth = current_truth && (input_value == constant3); printf("current_truth = %d\n", current_truth); } /* main */ What is the output of this program...
i need flowchart for the following c code #include <stdio.h> int main() { int n, i, sum = 0; printf("Enter an integer: "); scanf("%d",&n); i = 1; while ( i <=n ) { sum += i; ++i; } printf("Sum = %d",sum); return 0; }