C Program to Illustrate Pass by Reference


This is a C Program to illustrate pass by reference.

Problem Description

This program illustrates pass by reference.

Problem Solution

1. Pass the addresses of the variables as parameters to the function.
2. In function definition receive the parameters through pointers.
3. Print the output and exit.

Program/Source Code

Here is source code of the C Program to illustrate pass by reference. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Illustrate Pass by Reference
  3.  */
  4. #include <stdio.h>
  6. void cube( int *x);
  8. int main()
  9. {
  10.     int num = 10;
  12.     cube(&num);
  13.     printf("the cube of the given number is %d", num);
  14.     return 0;
  15. }
  17. void  cube(int *x)
  18. {
  19.     *x = (*x) * (*x) * (*x);
  20. }
Program Explanation

1. Initialize the variable num to 10.
2. Call the function cube and pass the address of variable num as parameter.
3. In the function definition, receive the parameter through pointer x and compute the cube of the variable through pointer x.
4. Print the variable num as output in the main function and exit.

Advertisement: Join [email protected]
Runtime Test Cases
the cube of the given number is 1000

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Simple C Programs, go to Simple C Programs. If you wish to look at programming examples on all topics, go to C Programming Examples.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter