0% found this document useful (0 votes)
15 views22 pages

Functions

The document contains code snippets demonstrating the use of pointers in C programs. It shows how to declare and call functions, pass arguments by reference, access the address of variables using the & operator and dereference pointers to access the value at a memory address using the * operator. Examples include adding two numbers with and without pointers, swapping values, checking if a number is even or odd, and demonstrating basic pointer declarations and handling.

Uploaded by

LEESHMA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views22 pages

Functions

The document contains code snippets demonstrating the use of pointers in C programs. It shows how to declare and call functions, pass arguments by reference, access the address of variables using the & operator and dereference pointers to access the value at a memory address using the * operator. Examples include adding two numbers with and without pointers, swapping values, checking if a number is even or odd, and demonstrating basic pointer declarations and handling.

Uploaded by

LEESHMA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

#include <stdio.

h>

int sum (int, int);//function declaration


int main (void)
{
int total;
printf("\n\n Function : a simple structure of function :\n");
total = sum (5, 6);//function call
printf ("The total is : %d\n", total);
return 0;
}

int sum (int a, int b) //function definition


{
int s;
s=a+b;
return s; //function returning a value
}
Write a program in C to swap two numbers using a
function.
#include<stdio.h>
void swap(int *,int *);
int main()
{
int n1,n2;
printf("Input 1st number : ");
scanf("%d",&n1);
printf("Input 2nd number : ");
scanf("%d",&n2);

printf("Before swapping: n1 = %d, n2 = %d ",n1,n2);


//pass the address of both variables to the function.
swap(&n1,&n2);
printf("\nAfter swapping: n1 = %d, n2 = %d \n\n",n1,n2);
return 0;
}
void swap(int *p,int *q)
{ //p=&n1 so p store the address of n1, so *p store the value of n1
//q=&n2 so q store the address of n2, so *q store the value of n2
int tmp;
tmp = *p; // tmp store the value of n1
*p=*q; // *p store the value of *q that is value of n2
*q=tmp; // *q store the value of tmp that is the value of n1
}
Write a program in C to check if a given number is even or odd using the
function.
#include <stdio.h>
int checkOddEven(int n1)
{
return (n1 & 1);
}
int main()
{
int n1;
printf("\n\n Function : check the number is even or odd:\n");
printf("Input any number : ");
scanf("%d", &n1);
if(checkOddEven(n1))
{
printf("The entered number is odd.\n\n");
}
else
{
printf("The entered number is even.\n\n");
}
return 0;
}
Write a program in C to find the sum of the series
1!/1+2!/2+3!/3+4!/4+5!/5 using the function.
Write a program in C to show the basic declaration of a pointer.
Expected Output :

Pointer : Show the basic declaration of pointer :


-------------------------------------------------------
Here is m=10, n and o are two integer variable and *z is an integer

z stores the address of m = 0x7ffd40630d44

*z stores the value of m = 10

&m is the address of m = 0x7ffd40630d44

&n stores the address of n = 0x7ffd40630d48

&o stores the address of o = 0x7ffd40630d4c

&z stores the address of z = 0x7ffd40630d50


#include <stdio.h>
void main(void)
{
int m=10,n,o;
int *z=&m ;

printf("\n\n Pointer : Show the basic declaration of pointer :\n");


printf("-------------------------------------------------------\n");
printf(" Here is m=10, n and o are two integer variable and *z is an integer");
printf("\n\n z stores the address of m = %p\n", z); // z is a pointer so %p would print
the address
printf("\n *z stores the value of m = %i\n", *z);
printf("\n &m is the address of m = %p\n", &m); // &m gives the address of the
integer variable m
// so %p is the specifier for that address
printf("\n &n stores the address of n = %p\n", &n);
printf("\n &o stores the address of o = %p\n", &o);
printf("\n &z stores the address of z = %p\n\n", &z); // &z gives the address, where
the pointer z is
// stored -> still an address -> %p is the right
// specifier
}
2. Write a program in C to demonstrate how to handle pointers in a program.
Expected Output :

Address of m : 0x7ffcc3ad291c
Value of m : 29

Now ab is assigned with the address of m.


Address of pointer ab : 0x7ffcc3ad291c
Content of pointer ab : 29

The value of m assigned to 34 now.


Address of pointer ab : 0x7ffcc3ad291c
Content of pointer ab : 34

The pointer variable ab is assigned with the value 7 now.


Address of m : 0x7ffcc3ad291c
Value of m : 7
#include <stdio.h>
int main()
{
int* ab;
int m;
m=29;
printf("\n\n Pointer : How to handle the pointers in the program :\n");
printf(" Here in the declaration ab = int pointer, int m= 29\n\n");
printf(" Address of m : %p\n",&m);
printf(" Value of m : %d\n\n",m);
ab=&m;
printf(" Now ab is assigned with the address of m.\n");
printf(" Address of pointer ab : %p\n",ab);
printf(" Content of pointer ab : %d\n\n",*ab);
m=34;
printf(" The value of m assigned to 34 now.\n");
printf(" Address of pointer ab : %p\n",ab);
printf(" Content of pointer ab : %d\n\n",*ab);
*ab=7;
printf(" The pointer variable ab is assigned the value 7 now.\n");
printf(" Address of m : %p\n",&m);//as ab contain the address of m
//so *ab changed the value of m and now m become 7
printf(" Value of m : %d\n\n",m);
return 0;
}
3. Write a program in C to demonstrate the use of the &(address of) and *(value at address)
operators.
Expected Output :

Pointer : Demonstrate the use of & and * operator :


--------------------------------------------------------
m = 300
fx = 300.600006
cht = z Using only pointer variable :

Using & operator : ----------------------------------


-----------------------
address of m = 0x7ffda2eeeec8 address of m = 0x7ffda2eeeec8
address of fx = 0x7ffda2eeeecc
address of cht = 0x7ffda2eeeec7 address of fx = 0x7ffda2eeeecc

Using & and * operator : address of cht = 0x7ffda2eeeec7


-----------------------------
value at address of m = 300
value at address of fx = 300.600006 Using only pointer operator :
value at address of cht = z
----------------------------------

value at address of m = 300


#include <stdio.h>
void main()
{
int m=300;
float fx = 300.60;
char cht = 'z';
printf("\n\n Pointer : Demonstrate the use of & and * operator :\n");
printf("--------------------------------------------------------\n");
int *pt1; printf ( " value at address of m = %d\n",*(&m));
float *pt2; printf ( " value at address of fx = %f\n",*(&fx));
char *pt3; printf ( " value at address of cht = %c\n",*(&cht));
pt1= &m; printf("\n Using only pointer variable :\n");
pt2=&fx; printf("----------------------------------\n");
pt3=&cht; printf ( " address of m = %p\n",pt1);
printf ( " m = %d\n",m); printf ( " address of fx = %p\n",pt2);
printf ( " fx = %f\n",fx); printf ( " address of cht = %p\n",pt3);
printf ( " cht = %c\n",cht); printf("\n Using only pointer operator :\n");
printf("\n Using & operator :\n"); printf("----------------------------------\n");
printf("-----------------------\n"); printf ( " value at address of m = %d\n",*pt1);
printf ( " address of m = %p\n",&m); printf ( " value at address of fx= %f\n",*pt2);
printf ( " address of fx = %p\n",&fx); printf ( " value at address of cht= %c\n\n",*pt3);
printf ( " address of cht = %p\n",&cht); }
printf("\n Using & and * operator :\n");
printf("-----------------------------\n");
4. Write a program in C to add two numbers using pointers.
Test Data :
Input the first number : 5
Input the second number : 6
Expected Output :

The sum of the entered numbers is : 11


#include <stdio.h>
int main()
{
int fno, sno, *ptr, *qtr, sum;

printf("\n\n Pointer : Add two numbers :\n");


printf("--------------------------------\n");

printf(" Input the first number : ");


scanf("%d", &fno);
printf(" Input the second number : ");
scanf("%d", &sno);

ptr = &fno;
qtr = &sno;

sum = *ptr + *qtr;

printf(" The sum of the entered numbers is : %d\n\n",sum);

return 0;
}
5. Write a program in C to add numbers using call by reference.
Test Data :
Input the first number : 5
Input the second number : 6
Expected Output :

The sum of 5 and 6 is 11


#include <stdio.h>
long addTwoNumbers(long *, long *);

int main()
{
long fno, sno, sum;

printf("\n\n Pointer : Add two numbers using call by reference:\n");


printf("-------------------------------------------------------\n");

printf(" Input the first number : ");


scanf("%ld", &fno);
printf(" Input the second number : ");
scanf("%ld", &sno);
sum = addTwoNumbers(&fno, &sno);
printf(" The sum of %ld and %ld is %ld\n\n", fno, sno, sum);
return 0;
}
long addTwoNumbers(long *n1, long *n2)
{
long sum;
sum = *n1 + *n2;
return sum;
}

You might also like