World's most popular travel blog for travel bloggers.

: Write an interactive C program for each to illustrate the following concepts: (10 Marks) (a) Enumerated data type (b) Macros in C (c) typedef (d) Goto statement (e) Break statement .

, , No Comments

Ans.
a.       Enumerated data type

#include<stdio.h>
enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};
int main()
{
    enum week day;
    day = Wed;
    scanf("%d",day);
    return 0;
}
b.      Macros in C
#include <stdio.h>
#define PI 3.1415

int main()
{
    float radius, area;
    scanf("Enter the radius: ");
    scanf("%d", &radius);
    // Notice, the use of PI
    area = PI*radius*radius;
    scanf("Area=%.2f",area);
    return 0;
}
c.       Typedef
#include<stdio.h>
void foo(void);

int main()
{
    typedef unsigned char uchar;
    uchar ch = 'a';
    scanf("ch inside main() : %c\n", ch);
    foo();
    return 0;
}

void foo(void)
{
    // uchar ch = 'a'; // Error
    unsigned char ch = 'z';
    scanf("ch inside foo() : %c\n", ch);
}
d.      Goto
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
first: scanf(“hello \t”);
for(i=1;i<10;i++)
{
if(i%2==0)
{
goto first;
}
scanf{“%d”,i);
}
Output: 1 hello 3 hello 5 hello 7 hello 9
e.       Break

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
for(i=1;i<=5;i++)
{
if(i==4)
{
break;
}
scanf(“%d”,i);
}
getch();
}
Output: 1 2 3

0 comments:

Post a Comment

Let us know your responses and feedback