C Program To Shift Variable Values Circularly To Right

Given three variables x, y, z write a function to circularly shift their values to right. In other words if x = 5, y = 8, z = 10, after circular shift y = 5, z = 8, x = 10. Call the function with variables a, b, c to circularly shift values.

Analyze The Above Problem Statement

1. We need to write a function which receives 3 numbers.
2. Inside the function we need to swap the values of 3 variables circularly to the right.

i.e., value of x to y, value of y to z, and value of z to a.

Related Read:
Basics of Pointers In C Programming Language
Swap 2 Numbers Using a Temporary Variable: C

Very Important Note:

Any address preceded by a * (Indirection operator) will fetch the value present at that address.

Video Tutorial: C Program To Shift Variable Values Circularly To Right


[youtube https://www.youtube.com/watch?v=VWh8WKUT5Fc]

YouTube Link: https://www.youtube.com/watch?v=VWh8WKUT5Fc [Watch the Video In Full Screen.]


Source Code: C Program To Shift Variable Values Circularly To Right

#include<stdio.h>

void shift_right(int*, int*, int*);

int main()
{
    int x, y, z;

    printf("Enter 3 numbers\n");
    scanf("%d%d%d", &x, &y, &z);

    printf("Before shifting right: x = %d, y = %d and z = %d\n", x, y, z);

    shift_right(&x, &y, &z);

    printf("After shifting right: x = %d, y = %d and z = %d\n", x, y, z);

    return 0;
}

void shift_right(int *a, int *b, int *c)
{
    int temp;

    temp = *c;
    *c   = *b;
    *b   = *a;
    *a   = temp;
}

Output 1:
Enter 3 numbers
5
8
10
Before shifting right: x = 5, y = 8 and z = 10
After shifting right: x = 10, y = 5 and z = 8

Output 2:
Enter 3 numbers
2
3
1
Before shifting right: x = 2, y = 3 and z = 1
After shifting right: x = 1, y = 2 and z = 3

Logic To Shift Variable Values Circularly To Right

We ask the user to enter 3 numbers and store it inside the address of variables x, y and z. Now we pass these 3 address to the function shift_right();

Inside shift_right() function
Inside shift_right() function we shift the value present at *c to a temp variable. Then, we shift the value of *b to *c, then the value of *a to *b, and then we shift the value present in temp to *a. This shifts the values of variables x, y and z circularly to the right.

*a has the value present at the address &x;
*b has the value present at the address &y;
*c has the value present at the address &z;


int temp;

temp = *c;
*c = *b;
*b = *a;
*a = temp;

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Basics of Pointers In C Programming Language

In today’s video tutorial lets learn basics of pointers in C programming language. Think of it as base of a building. If you can hold these basic concepts strong your building will be safer.

Topics Covered

1. What are pointers?
2. How to declare pointers.
3. How to access the value present at the address stored in pointer variable.
4. Using Address of operator.
5. Using Indirection operator.

What are pointers?

A pointer is a variable which refers to or points to an address in your computers memory.

Definition: A pointer variable is a variable which holds the address of another variable.

And since pointer variable is also a variable, it also has a unique address associated with it.

Operators

& is called “address of” operator.
* is called Indirection or “value at address” operator.

Video Tutorial: Basics of Pointers In C Programming Language


[youtube https://www.youtube.com/watch?v=1R6dBm3LN68]

YouTube Link: https://www.youtube.com/watch?v=1R6dBm3LN68 [Watch the Video In Full Screen.]


Source Code: Basics of Pointers In C Programming Language

#include<stdio.h>

int main()
{
    int num = 5;
    int *ptr; // pointer variable

    ptr = &num; // both hold address of num

    printf("Value of num = %d\n", num);
    printf("Address of num = %d\n", &num);
    printf("Address of ptr = %d\n", &ptr);
    printf("Value of ptr = %d\n", ptr);
    printf("Access value of num from ptr = %d\n", *ptr);
    printf("Access value of num from num = %d\n", *(&num));

    return 0;
}

Output:
Value of num = 5
Address of num = 6356732
Address of ptr = 6356728
Value of ptr = 6356732
Access value of num from ptr = 5
Access value of num from num = 5

Note: Whenever you declare and initialize a variable, for example, int num = 5; Your program does the following tasks:
1. Reserves space in your computers memory depending upon the data type.
2. Associates the name num with this memory location.
3. Stores the number 5 in this memory location.

Declaration of pointer variable

General Syntax
data_type* pointer_variable_name;

OR

data_type *pointer_variable_name;

Example:

int *ptr1;

int* ptr2;

Both these declaration are valid.

Rules for constructing pointer variable are same as that of regular variables: Rules for Constructing Variable Names: C

Assigning value to pointer variable

General Syntax
pointer_variable_name = &another_variable;

Example:

ptr = &num;

This stores the address of variable num as value of pointer variable ptr.

Declaring and Assigning value to pointer variable

Example:

int num = 5;
int *ptr;  // Declaration of pointer variable

ptr = &num;  // Assigning value to pointer variable

Note: While assigning address to pointer variable do not include * in front of pointer variable.

Values and addresses

num will have 5.
&num will have address of num.
&ptr will have address of ptr.
ptr will have address of num.
*ptr will have value of num.

Call by Value and Call by reference

Call by Value Example: C Program To Swap Two Numbers using Function
Call by Reference Example: C Program To Swap Two Numbers using Pointers

Note: Address can’t be negative values. So we can use %u as format specifier, which means unsigned integer.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert