Wednesday, September 17, 2014

Defining macro's in c language


Problem

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 - Code (a)

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#define LOWER(ch) (ch>=97&&ch<=122 ? printf("\nlower"):printf("\n not lower"))
main()
{
 char ch;

 printf("\n enter the character=");
 scanf("%c",&ch);

 LOWER (ch);

 printf ("\npress any key to close.");
 getch();
}

Sample Output (a)


character is lower case or not

Solution - Code (b)

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#define UPPER(ch) (ch>=65&&ch<=90 ? printf("\nUpper case"):printf("\n not upper case"))
main()
{
 char ch;

 printf("\n enter the character=");
 scanf("%c",&ch);

 UPPER (ch);

 printf ("\npress any key to close.");
 getch();
}

Sample Output (b)


character is upper case or not

Solution - Code (c)

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#define ALPHA(ch) (isalpha (ch) ? printf("\n alphabet"):printf("\n not alphabet"))
main()
{
 char ch;

 printf("\n enter the character=");
 scanf("%c",&ch);

 ALPHA (ch);

 printf ("\npress any key to close.");
 getch();
}

Sample Output (c)


character is alphabet or not

Solution - Code (d)

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#define LARGE(a,b) (a>b ? printf("\n a is greater"):printf("\n b is greater"))
main()
{
 int a,b;

 printf("\n enter the two number =");
 scanf("%d%d",&a,&b);

 LARGE (a,b);

 printf ("\npress any key to close.");
 getch();
}

Sample Output (d)


bigger of two numbers

No comments :

Post a Comment

Note: Only a member of this blog may post a comment.

Copyright 2017. All Rights Reserved. Privacy Policy / Terms And Conditions / Sitemap / Contact