Pointers in C

A Pointer is a variable in C that contains address of another variable.

A pointer variable is denoted by

int *p; //This means p is a pointer to an integer variable

* is Value at Address operator

& is Address of Operator

int x =15; //x is an integer variable

int *ptr1 = &x //Now p contains the address of x..i.e ptr is a pointer to x

int **ptr2 = &ptr1 //And ptr2 contains the address of ptr1..so prt2 is a pointer to a pointer


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

int main()
{
    int x=15;
    int *ptr1;
    int **ptr2;

    ptr1 = &x; //Now ptr contains the address of x
    ptr2 = &ptr1; //ptr2 contains the address of ptr

    printf("\nAddress of x = %u", &x);
    printf("\nValue of x = %d", x);
    printf("\nValue of x = %d", *(&x));

    printf("\n ");
    printf("\nAddress of ptr1 = %u", &ptr1);
    printf("\nValue of ptr1 / Address of x = %d", ptr1);
    printf("\nValue at address pointed by ptr1/Value of x = %d", *ptr1);

    printf("\n ");
    printf("\nAddress of ptr2 = %u", &ptr2);
    printf("\nValue of ptr2 / Address of ptr1 = %d", ptr2);
    printf("\nValue at address pointed ptr2 / Address of x= %d", *ptr2);
    printf("\nValue at address pointed by ptr1 which is pointed by ptr2/Value of x = %d", **ptr2);

    getch();
    return 0;
}


Snapshot of the o/p

Factorial Function in C

Hi Friends

I hope you missed me. Here’s a C Program showing Factorial functionality.

/* Program demonstrating Factorial Function */

 

#include<stdio.h>

#include<conio.h>

 

//Factorial Function 1st version

int factorial1(int num)

{

      int i;

      int result = 1;

     

      for(i=1; i<=num; i++)

      {

               result = result * i;

      }

     

      return result; 

}

 

//Factorial Function 2nd version

int factorial2(int num)

{

      int i;

      int result = 1;

     

      for(i=num; i>=1; i–)

      {

               result = result * i;

      }

     

      return result; 

}

int main()

{

     int num = 3;

    

     printf(“\n ##Factorial Function in C## \n”);

    

     printf(“\n Factorial of %d is %d”, num, factorial1(num));

    

     printf(“\n Factorial of 5 is %d”, factorial2(5));

    

     getch();

     return 0;  

}

Snapshot of the o/p

FactorialC

Power Function in C

/*

  Program demonstrating Power Function

*/

 

#include<stdio.h>

#include<conio.h>

 

//Power Function Integer Version

int power(int base, int exponent)

{

      int i;

      int result = 1;

     

      for(i=1; i<=exponent; i++)

      {

               result = result * base;

      }

     

      return result; 

}

//Power Function Floating-Point Version

float powerfloat(float base, float exponent)

{

      float i;

      float result = 1.0;

     

      for(i=1; i<=exponent; i++)

      {

               result = result * base;

      }

     

      return result; 

}

 

int main()

{

     int a, b, c;

     a=2;

     b=3;

     printf(“\n ~~~Power Functions C~~~ \n”);

    

     printf(“\n %d to the power %d is %d”, a,b, power(a,b));

    

     printf(“\n 2.0 to the power 4.0 is %f”, powerfloat(2.0, 4.0));

     getch();

     return 0;  

}

Snapshot of the o/p

PowerFunction

A Simple C Program

Hello Friends,

This is a very simple C Program demonstrating input, output using printf(), scanf() functions in C. It also shows how to write some simple functions in C without any argument or return type.

Level- Beginner

/*

  Program demonstrating Simple Functions, input, output in C

Hello.c

*/

 

#include<stdio.h>

#include<conio.h>

 

void hello_message()

{

     printf(“\n Hello World!! :) \n”); 

}

 

void bye_message()

{

     printf(“\n Good Bye World!!!\n”); 

}

 

void main()

{

     int num, a, b, c;

     a=5;

     b=7;

     printf(“\n ~~~Welcome to the world of C~~~ \n”);

     /*

     This is not allowed gives error

     printf(“\n Hello

    

     “);*/

    

     hello_message();

    

     printf(“\n Enter a Number : “);

    

     scanf(“%d”, &num);

    

     printf(“\n You entered %d”, num);

    

     printf(“\n c = %d (Garbage Value)”, c); //displays garbage value

     c=9;

     printf(“\n c = %d”, c);//o/p – 9

     printf(“\n a = %d”, a); //o/p – 5

    

     printf(“\n Experiments with printf \n”);

    

     printf(“\n number = %d”, 3); // o/p- 3

     printf(“\n a+b = %d”, a+b); //o/p -12

     printf(“\n a*b+c = %d”, a*b+c); //o/p-44

    

     printf(“\n Experiments with scanf \n”);

    

     printf(“\n Enter three Numbers : “);

     scanf(“%d %d %d”, &a, &b, &c);

     printf(“\n a = %d, b = %d, c = %d “, a,b,c);

     

     printf(“\n Enter three Numbers (separated by commas) : “);

     scanf(“%d, %d, %d”, &a, &b, &c);

     printf(“\n a = %d, b = %d, c = %d “, a,b,c);

    

     bye_message();

     getch();   

}

 Snapshot of the o/p

Simple Program O/P