You are on page 1of 35

C Lab

Program Number Program Name Date Implemented

: 1 : Implementation of the various Data Types with Modifiers and type conversions in C : July 4, 2012

___________________________________________________________________________________

Program to Simulate Subscriber Billing & Account Management System.


/*************************************************************** * Author: Nisha Singh ***************************************************************/ #include <stdio.h> #include <conio.h> int main() { char name[15]; char phonenumber[15]; int billamt,remamt1,remamt2,payment1; float payment2; printf("**********************************************************************\n") ; printf("Program to Simulate Subscriber Billing & Account Management System\n"); printf("**********************************************************************\n") ; printf("Please enter subscriber's name, telephone number and total bill amount in INR\n"); scanf("%s%s%d",name,phonenumber,&billamt);

printf("**********************************************************************\n") ; printf("Subscriber %s with telephone number %s has a total bill amount of INR %d\n",name,phonenumber,billamt); printf("**********************************************************************\n") ; printf("Please enter first payment for subscriber %s, with telephone number %s, \nfor a total outstanding bill amount of INR %d\n",name,phonenumber,billamt); scanf("%d",&payment1); remamt1 = billamt - payment1; if ( remamt1 > 0 ) { printf("\nDear %s, you have already made a payment of INR %d for your telephone number %s for \nyour last outstanding bill amount of INR %d\n",name,payment1,phonenumber,billamt); printf("To continue enjoying our services,please make a second payment of INR %d for telephone number %s\n",remamt1,phonenumber); scanf("%f",&payment2); remamt2 = remamt1 - payment2; printf("\nPayment recieved of INR %f for telephone number %s\n",payment2,phonenumber); printf("Payment recieved of INR in float is %0.2f\n",payment2); printf("Payment recieved of INR is rounded to integer is %d\n",(int)payment2); printf("Payment recieved of INR in unsigned long with u specification is %u\n",(unsigned long)payment2); Department of Computer Science, Christ University

C Lab

printf("Payment recieved of INR in float type converted to double with f specification is %0.2f\n",(double)payment2); printf("Payment recieved of INR in double with g specification is %.3g\n",(double)payment2); printf("Payment recieved of INR in double with e specification is %.3e\n",(double)payment2);

if ( remamt2 <= 0 ) { printf("Thank You %s, for clearing off your outstanding bill amount.Enjoy our world class services !!!!!!!\n",name); } } else { printf("Thank You %s, for clearing off your outstanding bill amount.Enjoy our world class services !!!!!!!\n",name); } if ( remamt2 > 0 ) { printf("Dear %s, your services have been barred for non-payment of complete outstanding bill amount !!!\n",name); printf("To continue enjoying our services, kindly contact customer care.\n",phonenumber); } getch(); return (0); }

Department of Computer Science, Christ University

C Lab

Output:

Department of Computer Science, Christ University

C Lab

Program Number Program Name Date Implemented

: 2 : Demonstration of nested if and switch case structure : July 10, 2012

___________________________________________________________________________________

Program to enter subscriber details name and age, and perform various operations on it.
/*************************************************************** * Author: Nisha Singh ***************************************************************/

#include <stdio.h> int main() { char name[16]; int age; int ch; printf("***************************************************************************** *********************\n"); printf("Demonstration of nested if and switch case structure \n"); printf("***************************************************************************** *********************\n"); while(1) { printf("\nPress 1 to Enter Subscriber Details : \nPress 2 to Modify Subscriber Details : \nPress 3 to Search Subscriber Details : \nPress 4 to Delete Subscriber Details : \nPress 5 to exit : " ); scanf("%d",&ch); switch(ch) { case 1: printf("\nThis is Add Subscriber Module using nested if-else,switch and break construct\n"); printf("\nFunctionality of this module is added\n\n"); printf("Enter your name: "); scanf("%s",&name); printf("Enter your age: "); scanf("%d",&age); if (age < 21) printf("You young whippersnapper, %s !\n", name); else if (age < 40) printf("%s, you're still in your prime !\n", name); else if (age < 60) printf("You're over the hill, %s !\n", name); else if (age < 80) printf("I bow to your wisdom, %s !\n", name); else printf("Are you really %d , %s ?\n", age, name); break; case 2: printf("\nThis is Modify Subscriber Module using while construct\n"); printf("\nCore functionality of this module will be added later\n"); printf("Enter your name: "); scanf("%s",&name); printf("Enter your age: "); Department of Computer Science, Christ University

C Lab

scanf("%d",&age); while (age < 21) { printf("You young whippersnapper, %s !\n", name); break; } while (age > 22 && age < 40) { printf("%s, you're still in your prime !\n", name); break; } while (age > 41 && age < 60) { printf("You're over the hill, %s !\n", name); break; } while (age > 61 && age < 80) { printf("I bow to your wisdom, %s !\n", name); break; } while (age >= 80) { printf("Are you really %d , %s ?\n", age, name); break; } break; case 3: printf("\nThis is Search Subscriber Module using do while construct\n"); printf("\nCore functionality of this module will be added later\n"); printf("Enter your name: "); scanf("%s",&name); printf("Enter your age: "); scanf("%d",&age); do { printf("You young whippersnapper, %s !\n", name); break; } while (age < 21); do { printf("%s, you're still in your prime !\n", name); break; } while (age > 22 && age < 40); do { printf("You're over the hill, %s !\n", name); break; } while (age > 41 && age < 60); do { printf("I bow to your wisdom, %s !\n", name); break; } while (age > 61 && age < 80); do { Department of Computer Science, Christ University

C Lab

printf("Are you really %d , %s ?\n", age, name); break; } while (age >= 80); break; case 4: printf("\nThis is Delete Subscriber Module using for looping construct\n"); printf("\nCore functionality of this module will be added later\n"); printf("Enter your name: "); scanf("%s",&name); printf("Enter your age: "); scanf("%d",&age); for (;age < 21;) { printf("You young whippersnapper, %s !\n", name); break; } for (;age > 22 && age < 40;) { printf("%s, you're still in your prime !\n", name); break; } for (;age > 41 && age < 60;) { printf("You're over the hill, %s !\n", name); break; } for (;age > 61 && age < 80;) { printf("I bow to your wisdom, %s !\n", name); break; } for (;age >= 80;) { printf("Are you really %d , %s ?\n", age, name); break; } break; case 5: exit(0); break; default: printf("\nThis is an invalid input\n"); } } }

Department of Computer Science, Christ University

C Lab

Output:

Department of Computer Science, Christ University

C Lab

Program Number : 3 Program Name : Implementation of various control structures. Date Implemented : July 12, 2012 ___________________________________________________________________________

Program to store telecom subscriber name and age and do various operations on it.
/*************************************************************** * Author: Nisha Singh ***************************************************************/ #include <stdio.h> int main() { char name[16]; int age; int ch; printf("***************************************************************************** *********************\n"); printf("Demonstration of all control structure(s) [if-else,while,do-while,for,switch and break statement] \n"); printf("***************************************************************************** *********************\n"); while(1) { printf("\nPress 1 to Enter Subscriber Details : \nPress 2 to Modify Subscriber Details : \nPress 3 to Search Subscriber Details : \nPress 4 to Delete Subscriber Details : \nPress 5 to exit : " ); scanf("%d",&ch); switch(ch) { case 1: printf("\nThis is Add Subscriber Module using nested if-else,switch and break construct\n"); printf("\nFunctionality of this module is added\n\n"); printf("Enter your name: "); scanf("%s",&name); printf("Enter your age: "); scanf("%d",&age); if (age < 21) printf("You young whippersnapper, %s !\n", name); else if (age < 40) printf("%s, you're still in your prime !\n", name); else if (age < 60) printf("You're over the hill, %s !\n", name); else if (age < 80) printf("I bow to your wisdom, %s !\n", name); else printf("Are you really %d , %s ?\n", age, name); break; case 2: printf("\nThis is Modify Subscriber Module using while construct\n"); printf("\nCore functionality of this module will be added later\n"); printf("Enter your name: "); scanf("%s",&name); printf("Enter your age: "); scanf("%d",&age); Department of Computer Science, Christ University

C Lab

while (age < 21) { printf("You young whippersnapper, %s !\n", name); break; } while (age > 22 && age < 40) { printf("%s, you're still in your prime !\n", name); break; } while (age > 41 && age < 60) { printf("You're over the hill, %s !\n", name); break; } while (age > 61 && age < 80) { printf("I bow to your wisdom, %s !\n", name); break; } while (age >= 80) { printf("Are you really %d , %s ?\n", age, name); break; } break; case 3: printf("\nThis is Search Subscriber Module using do while construct\n"); printf("\nCore functionality of this module will be added later\n"); printf("Enter your name: "); scanf("%s",&name); printf("Enter your age: "); scanf("%d",&age); do { printf("You young whippersnapper, %s !\n", name); break; } while (age < 21); do { printf("%s, you're still in your prime !\n", name); break; } while (age > 22 && age < 40); do { printf("You're over the hill, %s !\n", name); break; } while (age > 41 && age < 60); do { printf("I bow to your wisdom, %s !\n", name); break; } while (age > 61 && age < 80); do { printf("Are you really %d , %s ?\n", age, name); Department of Computer Science, Christ University

C Lab

10

break; } while (age >= 80); break; case 4: printf("\nThis is Delete Subscriber Module using for looping construct\n"); printf("\nCore functionality of this module will be added later\n"); printf("Enter your name: "); scanf("%s",&name); printf("Enter your age: "); scanf("%d",&age); for (;age < 21;) { printf("You young whippersnapper, %s !\n", name); break; } for (;age > 22 && age < 40;) { printf("%s, you're still in your prime !\n", name); break; } for (;age > 41 && age < 60;) { printf("You're over the hill, %s !\n", name); break; } for (;age > 61 && age < 80;) { printf("I bow to your wisdom, %s !\n", name); break; } for (;age >= 80;) { printf("Are you really %d , %s ?\n", age, name); break; } break; case 5: exit(0); break; default: printf("\nThis is an invalid input\n"); } } }

Department of Computer Science, Christ University

C Lab

11

Output:

Department of Computer Science, Christ University

C Lab

12

Program Number : 4 Program Name : Implementation of array Date Implemented : July 16, 2012 ___________________________________________________________________________

Program to store customer name and do sum of its 3 unbilled amounts using 1-d array.
/*************************************************************** * Author: Nisha Singh ***************************************************************/

#include<stdio.h> #include<conio.h> int main() { char customer[20]; int j; float billamt[3]; float sum=0; float average; printf("Enter customer's name :\n"); scanf("%s",customer); printf("Enter last 3 unpaid bill amounts for %s :\n",customer); for(j=0;j<3;j++) { scanf("%f",&billamt[j]); sum+= billamt[j]; } printf("Subscriber %s last 3 unpaid bill amounts are : \n",customer); for(j=0;j<3;j++) { printf("\tRs. %0.2f\n",billamt[j]); } printf("Subscriber %s total sum of unpaid bill amounts is %0.2f: \n",customer,sum); average = sum/3.0; printf("Subscriber %s average sum of unpaid bill amounts is %0.2f: \n",customer,average); getch(); return 0; }

Department of Computer Science, Christ University

C Lab

13

Output:

Department of Computer Science, Christ University

C Lab

14

Program Number : 5 Program Name : Implementation of multidimensional arrays. Date Implemented : July 18, 2012 ___________________________________________________________________________

Program for Implementation of two-dimensional array and show telecom circles and number of subscribers of all operators in that area.
/*************************************************************** * Author: Nisha Singh ***************************************************************/ #include <stdio.h> int validate_number(char []); int main() { int i, j; int mat[2][2]; printf("\nProgram for Implementation of two-dimensional array and show telecom circles \nand number of subscribers of all operators in that area\n"); printf("\nProgram reads through integer table, 2*2 dimensioned which stores total area \nand number of subscribers and displays them on screen\n"); printf("\nInput area and number of subscribers for 2 telecom circles/regions :\n"); for (i = 0; i < 2; i++) { if (i+1 == 1) printf("\nRegion %d : %s\n", i+1,"KORAMANGALA"); if (i+1 == 2) printf("\nRegion %d : %s\n", i+1,"INDIRANAGAR"); for (j = 0; j < 2; j++) { if (j+1 == 1) printf("\n%d . Area :\n",j+1); if (j+1 == 2) printf("\n%d . Number of Subscribers :\n",j+1); char temp[100]; if(fgets(temp, 100, stdin) != NULL) { if (validate_number(temp)) { mat[i][j] = atoi(temp); } else { printf("\nERROR !!!\nYou did not enter valid integer value.Exiting ...\n\n"); getch(); return 1; } } } } printf("\n***PRINTING AREA AND NUMBER OF SUBSCRIBER FOR ALL OPERATORS \nIN 2 DIFFERENT AREAS OF KARNATAKA (BANGALORE)***\n"); for (i = 0; i < 2; i++) { if (i+1 == 1) printf("\nRegion %d : %s\n", i+1,"KORAMANGALA"); Department of Computer Science, Christ University

C Lab

15

if (i+1 == 2) printf("\nRegion %d : %s\n", i+1,"INDIRANAGAR"); for (j = 0; j < 2; j++) { if (j+1 == 1) printf("Area : "); if (j+1 == 2) { printf(", "); printf("Number of Subscribers : "); } printf("%d", mat[i][j]); } printf("\n"); } getch(); return 0; } int validate_number(char s[]) { int valid = 1,i; for (i = 0; i < strlen(s) - 1; i++) { if (!isdigit(s[i])) { valid = 0; break; } } return valid; }

Department of Computer Science, Christ University

C Lab

16

Output:

Department of Computer Science, Christ University

C Lab

17

Program Number Program Name

: 6 : Implementation of functions: call by value, call by reference, Passing of arrays to functions Date Implemented : July 23, 2012 ___________________________________________________________________________

Program for Implementation of functions: call by value, call by reference, Passing of arrays to functions on an array of customer call codes stored in memory
/*************************************************************** * Author: Nisha Singh ***************************************************************/ #include <stdio.h> void modify_array( int [], int ); void modify_element( int ); int validate_number(char []); int main() { int cust_call_code[5], i; printf("\nImplementation of functions : call by value, call by reference, Passing of arrays \nto functions on an array of customer call codes stored in memory\n\n"); printf("\nEnter 5 customer call codes:\n\n"); for ( i = 0; i <5; i++ ) { char temp[100]; if(fgets(temp, 100, stdin) != NULL) { if (validate_number(temp)) { cust_call_code[i] = atoi(temp); } else { printf("\nYou did not enter valid integer customer call code.Exiting ...\n\n"); getch(); return 1; } } } printf("\nEffects of passing entire array call " "by reference:\n\nThe 5 customer call codes stored in " "original array are:\n"); for ( i = 0; i <5; i++ ) printf("%3d", cust_call_code[i]); printf("\n"); modify_array( cust_call_code, 5 ); printf("The values of the modified array are:\n"); for ( i = 0; i <5; i++ ) printf( "%3d", cust_call_code[i] );

Department of Computer Science, Christ University

C Lab

18

printf("\n\n\nEffects of passing array element call " "by value:\n\nThe value of cust_call_code[3] is %d\n", cust_call_code[3] ); modify_element( cust_call_code[3] ); printf("The value of cust_call_code[3] is %d\n", cust_call_code[3] ); getch(); return 0; } void modify_array(int cust_call_code[],int size) { int k; for(k = 0; k<size; ++k) cust_call_code[k] = cust_call_code[k]+1; } void modify_element(int i) { printf( "Value of customer call code in modify_element is %d\n", i *= 2 ); } int validate_number(char s[]) { int valid = 1,i; for (i = 0; i < strlen(s) - 1; i++) { if (!isdigit(s[i])) { valid = 0; break; } } return valid; }

Department of Computer Science, Christ University

C Lab

19

Output:

Department of Computer Science, Christ University

C Lab

20

Program Number : 7 Program Name : Demonstration of recursion Date Implemented : July 25, 2012 ___________________________________________________________________________

Program to convert numeric telecom subscriber billed amount to words for printing in invoice using recursion.
/*************************************************************** * Author: Nisha Singh ***************************************************************/ #include<stdio.h> #define MAX_LEN 100 int validate_number(char []); void converter(long,char[]); char *one[]={" "," One"," Two"," Three"," Four"," Five"," Six"," Seven","Eight"," Nine"," Ten"," Eleven"," Twelve"," Thirteen"," Fourteen","Fifteen"," Sixteen"," Seventeen"," Eighteen"," Nineteen"}; char *ten[]={" "," "," Twenty"," Thirty"," Forty"," Fifty"," Sixty","Seventy"," Eighty"," Ninety"};

int main() { printf("\n*********************************************************************** ***********************"); printf("\n\nProgram to convert numeric telecom subscriber billed amount to words for printing in invoice"); printf("\n*********************************************************************** ***********************"); long n; printf("\n\n\t Enter any 9 digit no : "); char temp[MAX_LEN]; if(fgets(temp, MAX_LEN, stdin) != NULL) { if (validate_number(temp)) { n = atoi(temp); } else { printf("\nERROR !!!\nYou did not enter valid integer amount. Exiting ...\n\n"); getch(); return 1; } } if(n<=0) printf("\n\n\t Enter numbers greater than greater than zero"); else { converter((n/10000000),"Crore"); converter(((n/100000)%100),"Lakh"); converter(((n/1000)%100),"Thousand"); converter(((n/100)%10),"Hundred"); converter((n%100)," "); Department of Computer Science, Christ University

C Lab

21

} getch(); return 0; } void converter(long n,char ch[]) { (n>19)?printf("%s %s ",ten[n/10],one[n%10]):printf("%s ",one[n]); if(n)printf("%s ",ch); } int validate_number(char s[]) { int valid = 1,i; for (i = 0; i < strlen(s) - 1; i++) { if (!isdigit(s[i])) { valid = 0; break; } } return valid; }

Department of Computer Science, Christ University

C Lab

22

Output:

Department of Computer Science, Christ University

C Lab

23

Program Number : 8 Program Name : Demonstration of various string operations Date Implemented : July 29, 2012 ___________________________________________________________________________

Program for demonstration of various string operations on a telecom subscriber first and last name with validation
/*************************************************************** * Author: Nisha Singh ***************************************************************/ #include<stdio.h> #include<ctype.h> #define MAX_LEN 100 int stringcompare(char [],char []); int stringlen(char []); void stringconcat(char [],char [],char []); void stringcpy(char [],char []); int validate_name(char []); int main() { int i=0,j=0; char str1[MAX_LEN],str2[MAX_LEN],dest[MAX_LEN],temp1[MAX_LEN],temp2[MAX_LEN]; printf("\n*************************************************************************** *****************************************"); printf("\n\nProgram for demonstration of various string operations on a telecom subscriber first and last name with validation"); printf("\n*************************************************************************** *****************************************"); puts("\n\nEnter first name of subscriber"); if(fgets(temp1, MAX_LEN, stdin) != NULL) { if (validate_name(temp1)) { stringcpy(str1,temp1); } else { printf("\nERROR !!!\nYou did not enter valid subscriber first name. Exiting ...\n\n"); getch(); return 1; } } printf("\nThe length of first name of subscriber is %d\n",stringlen(str1)); puts("\nEnter last name of subscriber"); if(fgets(temp2, MAX_LEN, stdin) != NULL) { if (validate_name(temp2)) { stringcpy(str2,temp2); } else { Department of Computer Science, Christ University

C Lab

24

printf("\nERROR !!!\nYou did not enter valid subscriber last name. Exiting ...\n\n"); getch(); return 1; } } stringconcat(str1,str2,dest) ; printf("\nConcantenated full name of subscriber is %s\n",dest); if(stringcompare(str1,str2)) { printf("\nSubscriber first name and last name are SAME"); } else { printf("\nSubscriber first name and last name are NOT SAME"); } stringcpy(str1,str2); printf("\n\nFirst name %s stored in Last name (String 2) => %s",str1,str2); getch(); return 0; } int stringcompare(char str1[],char str2[]) { int i=0,flag=0; while(str1[i]!='\0' && str2[i]!='\0') { if(str1[i]!=str2[i]) { flag=1; break; } i++; } if (flag==0 && str1[i]=='\0' && str2[i]=='\0') return 1; else return 0; } int stringlen(char s[]) { int x; x=0; while (s[x] != '\0') { x=x+1; } x=x-1; return(x); } void stringcpy(char s1[],char s2[]) { int x,len; len=strlen(s2); for (x=0; x<=len; x++) s1[x]=s2[x]; } Department of Computer Science, Christ University

C Lab

25

void stringconcat(char s1[],char s2[],char s3[]) { int i=0,j,l; strcpy(s3,s1); l=strlen(s1); s3[l++]=' '; while(s2[i]!= '\0') s3[l++]=s2[i++]; s3[l]= '\0'; } int validate_name(char s[]) { int valid = 1,i; for (i = 0; i < strlen(s) - 1; i++) { if (!isalpha(s[i])) { valid = 0; break; } } return valid; }

Department of Computer Science, Christ University

C Lab

26

Output:

Department of Computer Science, Christ University

C Lab

27

Program Number : 9 Program Name : Implementation of storage types Date Implemented : August 4, 2012 ___________________________________________________________________________

Program to demo storage classes using customer outstanding amounts


/*************************************************************** * Author: Nisha Singh ***************************************************************/ #include <stdio.h> #define MAXNUM 3 #define MAX_LEN 100 void funct1(void); void funct2(void); int sum_up(void); int validate_number(char []); /* external variable, scope is global to main(), funct1() and funct2() */ int GLOBALVAR = 10; int main() { printf("\n****STORAGE CLASSES AND SCOPE****\n"); printf("\n****EXTERN AND AUTO STORAGE****\n"); /* external variable */ GLOBALVAR = 20; printf("\nVariable GLOBALVAR, in main() = %d\n", GLOBALVAR); funct1(); printf("\nVariable GLOBALVAR, in main() = %d\n", GLOBALVAR); funct2(); printf("\nVariable GLOBALVAR, in main() = %d\n", GLOBALVAR); /*Static example*/ int count; printf("\n*****STATIC STORAGE*****\n"); printf("Key in %d outstanding amounts of customer to be summed ", MAXNUM); for(count = 0; count < MAXNUM; count++) { sum_up(); } printf("\n*****COMPLETED*****\n"); getch(); return 0; } /* external variable, scope is global to funct1() and funct2() */ int GLOBALVAR2 = 30; void funct1(void) { /* auto variable, scope local to funct1() and funct1() cannot access the external GLOBALVAR */ char GLOBALVAR; /* local variable to funct1() */ GLOBALVAR = 'A'; /* external variable */ Department of Computer Science, Christ University

C Lab

28

GLOBALVAR2 = 40; printf("\nIn funct1(), GLOBALVAR = %c and GLOBALVAR2 = %d\n", GLOBALVAR, GLOBALVAR2); } void funct2(void) { /* auto variable, scope local to funct2(), and funct2() cannot access the external GLOBALVAR2 */ double GLOBALVAR2; /* external variable */ GLOBALVAR = 50; /* auto local variable to funct2() */ GLOBALVAR2 = 1.234; printf("\nIn funct2(), GLOBALVAR = %d and GLOBALVAR2 = %.4f\n", GLOBALVAR, GLOBALVAR2); } int sum_up(void) { /* at compile time, sum is initialized to 0 */ static int sum = 0; int num; printf("\nEnter bill amount of customer to be summed : "); char temp[MAX_LEN]; if(fgets(temp, MAX_LEN, stdin) != NULL) { if (validate_number(temp)) { num = atoi(temp); } else { printf("\nYou did not enter valid amount rounded to nearest whole number for customer.Skipping ...\n\n"); getch(); return 1; } } sum += num; printf("\nThe current total is: %d\n", sum); return 0; } int validate_number(char s[]) { int valid = 1,i; for (i = 0; i < strlen(s) - 1; i++) { if (!isdigit(s[i])) { valid = 0; break; } } return valid; }

Department of Computer Science, Christ University

C Lab

29

Output:

Department of Computer Science, Christ University

C Lab

30

Program Number : 10 Program Name : Demonstration of pointer operations Date Implemented : August 10, 2012 ___________________________________________________________________________

Program to demonstrate pointer operations on an array of customer call codes.


/*************************************************************** * Author: Nisha Singh ***************************************************************/ #include<stdio.h> #define MAX_LEN 10 int validate_number(char []); int main(){ int custcallcode[MAX_LEN]; int *ptrArr, *ptrArr1,i; printf("\nEnter %d customer call codes:\n\n",MAX_LEN); for ( i = 0; i <MAX_LEN; i++ ) { char temp[MAX_LEN]; if(fgets(temp, MAX_LEN, stdin) != NULL) { if (validate_number(temp)) { custcallcode[i] = atoi(temp); } else { printf("\nYou did not enter valid integer customer call code.Exiting ...\n\n"); getch(); return 1; } } } ptrArr = custcallcode; printf("\nPrinting the first call code in array: %d\n\n",*ptrArr); /*Increased pointer points to the next element of the same array*/ printf("Printing value at pointer location by increasing it by 1\n"); for(i=0;i<MAX_LEN;i++) { printf("%d\t", *ptrArr); ptrArr++; } printf("\n\n"); printf("Printing value at pointer location that is not reserved by the program\n"); printf("%d This is garbage\n\n",*ptrArr); /*Decreased pointer variable points to previous element in the same array*/ printf("Printing value at pointer location by decreasing it by 1\n"); for(i=0;i<MAX_LEN;i++) { ptrArr--; printf("%d\t", *ptrArr); } printf("\n\n"); /*ptrArr will point to 3rd element of array*/ ptrArr = &custcallcode[2]; printf("\nThird element of the call code array : %d",*ptrArr); /* no of elements in between these two pointer locations*/ Department of Computer Science, Christ University

C Lab

31

ptrArr1 = &custcallcode[5]; printf("\n\nSubtraction of two pointers ptrArr1 - ptrArr ( No. of elements between two pointer locations ) : %d\n",ptrArr1 - ptrArr); getch(); return 0; } int validate_number(char s[]) { int valid = 1,i; for (i = 0; i < strlen(s) - 1; i++) { if (!isdigit(s[i])) { valid = 0; break; } } return valid; }

Department of Computer Science, Christ University

C Lab

32

Output:

Department of Computer Science, Christ University

C Lab

33

Program Number : 11 Program Name : Demonstration of macro processing Date Implemented : August 26, 2012 ___________________________________________________________________________

Program to demonstrate macro processing using customer name and number and printing that records by macro.
/*************************************************************** * Author: Nisha Singh ***************************************************************/ #include<stdio.h> #define MAX_LEN 100 /* Single Line Macro*/ /* Multi Line Macro*/ # define getcustomernumber printf("Enter the Subscriber Mobile Number: "); if(fgets(temp, MAX_LEN, stdin) != NULL) \ { \ if (validate_number(temp)) \ { \ num = atoi(temp); \ } \ else \ { \ printf("\nERROR !!!\nYou did not enter valid integer value.Exiting ...\n\n"); getch(); \ return 1; \ } \ }

#define getcustomername printf("Enter the Subscriber Name: "); scanf("%s",&name); /* Single Line Macro*/ #define printoutput printf("\nThe Subscriber Record is: "); printf("\t\nSubscriber Name: %s",name); printf("\t\nSubscriber Number: %d",num); /* Single Line Macro*/ int validate_number(char []); int main() { int num; char name[MAX_LEN]; char temp[MAX_LEN]; getcustomernumber; getcustomername; printoutput; getch(); return 0; } int validate_number(char s[]) { int valid = 1,i; for (i = 0; i < strlen(s) - 1; i++) { if (!isdigit(s[i])) { valid = 0; Department of Computer Science, Christ University

C Lab

34

break; } } return valid; }

Department of Computer Science, Christ University

C Lab

35

Output:

Department of Computer Science, Christ University