Example : A C Program to find out the size/memory occupied by different data type using sizeof() operator.
# include <stdio.h>
# include <conio.h>

void main( )
 { 	
	clrscr();
	printf("The memory occupied by Integer is = %d bytes\n",sizeof(int));
	printf("The memory occupied by Float is = %d bytes\n",sizeof(float));
	printf("The memory occupied by Character is = %d bytes\n",sizeof(char));
		
	getch();
 }
Output :

The memory occupied by Integer is = 4 bytes 
(The memory occupied by Integer is = 2 bytes, in old version of system)
The memory occupied by Float is = 4 bytes
The memory occupied by Character is = 1 bytes
Example : A simple program in C to print value using pointer.
#include<stdio.h>
#include<conio.h>
int main()
{
   int x,*ptr;
   x = 10;   
   ptr = &x;   
   printf("%d", x);
   printf("%d",*ptr);
   
   return 0;
}
Example : A program in C to print swap value using pointer.
#include<stdio.h>
#include<conio.h>

void swap(int *a,int *b)
{
    int temp;
     temp = *a;
    *a = *b;
    *b = temp;
} 
int main()
{
    int num1,num2;
     
    printf("Enter first value : ");
    scanf("%d",&num1);
    
    printf("Enter second value : ");
    scanf("%d",&num2);     
    
    printf("Before Swapping: num1=%d, num2=%d\n",num1,num2);     
    
    swap(&num1,&num2);     
   
    printf("After  Swapping: num1=%d, num2=%d\n",num1,num2);    
     
    return 0;
}

Example : A program in C to print a string value using pointer.

#include <stdio.h>
#include <conio.h>
int main()
{
    char str[50];
    char *ptr;
     
    printf("Enter a string value: ");
    gets(str);    
    ptr=str;     
    printf("Entered string is: ");
       while(*ptr!='\0')
        printf("%c",*ptr++);
         
    return 0;
}

Example : A program in C to print values & address through different ways.

# include <stdio.h>
# include <conio.h>
void main( )
{
	int num = 15;
	int *ptr;
	ptr = #
	
	printf ("Address of Stored Number num is = %u \n", &num);
	printf ("Address of Stored Number num is= %u \n", ptr);
	
	printf ("\nAddress of num Stored by ptr is = %d \n", ptr);
	
	printf ("\nAddress of Pointer occupied by ptr is= %u \n", &ptr);	
	
	printf ("\nValue of Stored Number = %d \n", num);
	printf ("Value of Stored Number = %d \n", *(&num));
	printf ("Value of Stored Number = %d", *ptr);
	
	getch();
}

Output :
Address of Stored Number num is = 6487628
Address of Stored Number num is= 6487628

Address of num Stored by ptr is = 6487628
Address of Pointer occupied by ptr is= 6487616

Value of Stored Number = 15
Value of Stored Number = 15
Value of Stored Number = 15

Example : A program in C to print values & address through Array and Pointer.


# include <stdio.h>
# include <conio.h>

int main()
{
	int arr[ 5 ] = {100, 200, 300, 400, 500};
	int i;
	
	for (i = 0; i <=4; i=i+1)
	{
		printf ("Array Cell i = %d  Value of arr[i] = %d  Value of *(arr+i) = %d  ", 
                       i, arr[i], *(arr+i));
		
		printf (" Address of &arr[i] = %u  Address of arr+i = %u", &arr[i], (arr+i)); 
		
		printf("\n");
	}
}

Output :
Array Cell i = 0  Value of arr[i] = 100  Value of *(arr+i) = 100   Address of &arr[i] = 2358832  
Address of arr+i = 2358832
Array Cell i = 1  Value of arr[i] = 200  Value of *(arr+i) = 200   Address of &arr[i] = 2358836  
Address of arr+i = 2358836
Array Cell i = 2  Value of arr[i] = 300  Value of *(arr+i) = 300   Address of &arr[i] = 2358840  
Address of arr+i = 2358840
Array Cell i = 3  Value of arr[i] = 400  Value of *(arr+i) = 400   Address of &arr[i] = 2358844  
Address of arr+i = 2358844
Array Cell i = 4  Value of arr[i] = 500  Value of *(arr+i) = 500   Address of &arr[i] = 2358848  
Address of arr+i = 2358848

---------------------   OR   -----------------------


# include <stdio.h>
# include <conio.h>

int main()
{
	int arr[ 3 ][ 3 ] = {{10, 20, 30}, {40, 50, 60}, {70, 80, 90}};
	int i,j;
	
	for (i = 0; i <3; i=i+1)
	{
	   for (j = 0; j <3; j=j+1)
	   {
	     printf ("Array Cell [i][j] = %d %d  Value of arr[i][j] = %d  Value of *(*(arr+i)+j)=
                  %d ", i,j, arr[i][j], *(*(arr+i)+j));
		
	     printf (" Address of &arr[i][j] = %u  Address of ((arr+i)+j) = %u", &arr[i][j], 
                  ((arr+i)+j));
             printf("\n");		
	   }
	     printf("\n");
	}
	return 0;
}

Output :

Array Cell [i][j] = 0 0  Value of arr[i][j] = 10  Value of *(*(arr+i)+j)= 10   
Address of &arr[i][j] = 2358816  Address of ((arr+i)+j) = 2358816
Array Cell [i][j] = 0 1  Value of arr[i][j] = 20  Value of *(*(arr+i)+j)= 20   
Address of &arr[i][j] = 2358820  Address of ((arr+i)+j) = 2358828
Array Cell [i][j] = 0 2  Value of arr[i][j] = 30  Value of *(*(arr+i)+j)= 30   
Address of &arr[i][j] = 2358824  Address of ((arr+i)+j) = 2358840

Array Cell [i][j] = 1 0  Value of arr[i][j] = 40  Value of *(*(arr+i)+j)= 40   
Address of &arr[i][j] = 2358828  Address of ((arr+i)+j) = 2358828
Array Cell [i][j] = 1 1  Value of arr[i][j] = 50  Value of *(*(arr+i)+j)= 50   
Address of &arr[i][j] = 2358832  Address of ((arr+i)+j) = 2358840
Array Cell [i][j] = 1 2  Value of arr[i][j] = 60  Value of *(*(arr+i)+j)= 60   
Address of &arr[i][j] = 2358836  Address of ((arr+i)+j) = 2358852

Array Cell [i][j] = 2 0  Value of arr[i][j] = 70  Value of *(*(arr+i)+j)= 70   
Address of &arr[i][j] = 2358840  Address of ((arr+i)+j) = 2358840
Array Cell [i][j] = 2 1  Value of arr[i][j] = 80  Value of *(*(arr+i)+j)= 80   
Address of &arr[i][j] = 2358844  Address of ((arr+i)+j) = 2358852
Array Cell [i][j] = 2 2  Value of arr[i][j] = 90  Value of *(*(arr+i)+j)= 90   
Address of &arr[i][j] = 2358848  Address of ((arr+i)+j) = 2358864

Example : A program in C to print values & address through various ways & pointer to pointer.

# include <stdio.h>
# include <conio.h>
void main( )
{
		int num = 100;
		
		int *ptr1;
		int **ptr2;
		
		ptr1 = #
		ptr2 = &ptr1;
		
		printf ("Address of num = %u \n", &num);		
		printf ("Address of num = %u \n", ptr1);		
		printf ("Address of num = %u \n", *ptr2);
		
		printf ("\nAddress of ptr1 = %d \n",  ptr2);
		printf ("Address of ptr1 = %u \n", &ptr1);		
		printf ("Address of ptr1 = %u \n", ptr2);
		
		printf ("\nAddress of ptr2 = %u \n", &ptr2);
		
		printf ("\nValue of num = %d \n", num);
		printf ("Value of num = %d \n", *(&num));
		printf ("Value of num = %d \n", *ptr1);
		printf ("Value of num = %d", **ptr2);

	getch();
}

Output : 
Address of num = 6487628
Address of num = 6487628
Address of num = 6487628

Address of ptr1 = 6487616
Address of ptr1 = 6487616
Address of ptr1 = 6487616

Address of ptr2 = 6487608

Value of num = 100
Value of num = 100
Value of num = 100
Value of num = 100

 614 total views,  1 views today

Categories: C

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.