Friday, 16 April 2010

Pointers: C program for addition for two numbers using Pointers

/*    This program adds two numbers using pointers to demonstrate the concept of pointers.*/
#include < stdio.h >

    int main(void) {
        //    Local Declarations
        int a;
        int b;
        int r;
        int * pa = & a;
        int * pb = & b;
        int * pr = & r;

        //    Statements
        printf("\nEnter the first number : ");
        scanf("%d", pa);
        printf("Enter the second number: ");
        scanf("%d", pb); * pr = * pa + * pb;
        printf("\n%d + %d is %d\n\n", * pa, * pb, * pr);
        return 0;
    } // main

/*    Results:
    Enter the first number : 15
    Enter the second number: 51

    15 + 51 is 66
*/

No comments:

Post a Comment