Write a C program thathas the following statements int ab a

Write a C program thathas the following statements:

int a,b;

a = 10;

b = a + fun();

printf(\"With the funcion call on the right, b is: %d \ \",b);

a = 10;

b = fun() + a;

printf(\"With the funcion call on the left, b is: %d \ \",b);

and define fun to add 10 to a. Explain the results.

This is confusing me because I am not sure if it is asking for me to just return 10 from fun() or add 10 to a in fun() then return to main.

Solution


/*
C program that demonstrates the fun function
*/
//header files
#include<stdio.h>
#include<conio.h>
//global variables
int a,b;
int fun();
int main()
{
  

   a = 10;
   //fun function is called . This add value 10 to a, a=a+10=10+10=20
   //since the a variable is global variable, a is simentaneously updated
   //in main function as a=20.
   //20+20=40 stored in b
   b = a + fun();
   //print b value
   printf(\"With the funcion call on the right, b is: %d \ \",b);
   //set a=10
   a = 10;
   //fun function is called . value of a is updated to 20
   //since the a variable is global variable, a is simentaneously updated
   //in main function as 20.
   //20+20=40 stored in b
   b = fun() + a;
   //print b value
   printf(\"With the funcion call on the left, b is: %d \ \",b);  
   getch();

   return 0;
}
/**
The function fun that add value 10 to a and
return a value
*/
int fun()
{
   a=a+ 10;
   return a;
}

------------------------------------------------------------------------------------------------------------

Sample output:

With the funcion call on the right, b is: 40
With the funcion call on the left, b is: 40

Write a C program thathas the following statements: int a,b; a = 10; b = a + fun(); printf(\
Write a C program thathas the following statements: int a,b; a = 10; b = a + fun(); printf(\

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site