Monday, 19 August 2013

Chapter 7 solutions problem 7[C][a]

Question-Write down macro definitions for the following: 
1.  To test whether a character entered is a small case letter or 
not. 
2.  To test whether a character entered is a upper case letter or 
not. 
3.  To test whether a character is an alphabet or not. Make 
use of the macros you defined in (1) and (2) above. 
4.  To obtain the bigger of two numbers. 
Solution-

with this question i want to start 7th chapter of the book. macro is pretty easy to understand but sometimes extremely dangerous to use. so, i must suggest you that just use macros for defining constants or writing few statements. frequent use would lead to suicide.
so let's check the solution of this program.

#include<stdio.h>
#define uppercase  (a>=65&&a<=90)        /*Macro definition */
#define lowercase  (a>=97&&a<=122)        /*Macro definition */
#define alphabet  (uppercase||lowercase)    /*Macro definition */
#define bignum(a,b) (a>b?printf("first number is bigger\n"):printf("Second num is bigger\n"));
int main()
{
char a;
int b,c;
if uppercase
printf("Upper case letter\n");
else if lowercase
printf("lower case letter\n");
else
printf("Neither an uppercase nor lowercase letter\n");
printf("Enter the 2 numbers you want to compare\n");
scanf("%d%d",&b,&c);
bignum(b,c);
return 0;
}

No comments:

Post a Comment