Structure
- A structure is a user-defined data type that allows you to group together variables of different data types under a single name.
- It is defined using the "struct" keyword, followed by the name of the structure and the list of variables within braces.
- The variables within a structure are called members, and they can be accessed using the dot notation.
- What is the output of the following C code?
- What is the output of the following C code?
- What is the compilation status of the following code in C and C++?
- What is the output of the following C code?
- What is the output of the following C code?
- What is the output of the following C code?
#include <stdio.h>
struct demo
{
char a;
char b;
};
int main()
{
struct demo obj;
obj.a = 'A';
printf("%d\n", sizeof(struct demo));
printf("%d\n", sizeof(obj));
printf("%d\n", sizeof(obj.a));
printf("%d\n", sizeof(obj.b));
printf("%c\n", obj.b);
printf("%c", obj.a);
return 0;
}
#include <stdio.h>
struct demo
{
char a;
int b;
};
int main()
{
struct demo obj;
printf("%d\n", sizeof(struct demo));
printf("%d\n", sizeof(obj.a));
printf("%d", sizeof(obj.b));
return 0;
}
#include <stdio.h>
struct emp
{
char name[20];
int age;
};
int main()
{
emp xx;
return 0;
}
#include <stdio.h>
struct num
{
float f;
};
int main()
{
struct num n1, n2, n3;
n1.f = 4;
n2.f = 3;
n3 = n1 + n2;
printf("%f", n2.f);
}
#include <stdio.h>
struct num
{
float f;
};
int main()
{
struct num n1, n2, n3;
n1.f = 4;
n2.f = 3;
printf("%f", n2);
}
#include <stdio.h>
struct emp
{
char name[] = "Deepak";
int age = 20;
};
int main()
{
struct emp *ptr;
printf("%s\n", ptr->name);
printf("%d", ptr->age);
return 0;
}
Union
- A union is also a user-defined data type that allows you to store different types of data in the same memory location.
- Unlike a structure, a union can only hold the value of one member at a time.
- A union is defined using the "union" keyword, followed by the name of the union and the list of members within braces.
- A union can only hold one value at a time, accessing one member will overwrite the value of any other member that was previously stored in the union.
- What is the output of the following C code?
- What is the output of the following C code?
#include <stdio.h>
union demo
{
char a;
int b;
};
int main()
{
union demo obj;
obj.a = 'A';
printf("%d\n", sizeof(obj));
printf("%c\n", obj.a);
printf("%d", obj.b);
}
#include <stdio.h>
union demo
{
int i;
char ch[2];
};
int main()
{
union demo obj;
obj.ch[0] = 3;
obj.ch[1] = 2;
printf("%d %d %d\n", obj.ch[0], obj.ch[1], obj.i);
obj.i = 0;
printf("%d %d %d", obj.ch[0], obj.ch[1], obj.i);
}
Enumerator
- Enumeration (or enum) is a user-defined data type in C. It is mainly used to assign names to integral constants, the names make a program easy to read and maintain.
- Two or more enum names can have the same value.
- If we do not explicitly assign values to enum, the compiler by default assigns values starting from 0.
- We can assign values to some name in any order. All unassigned names get value as value of previous name plus one.
- The value assigned to enum names must be some integral constant, i.e., the value must be in range from minimum possible integer value to maximum possible integer value.
- All enum constants must be unique in their scope.
- What is the output of the following C code?
- What is the output of the following C code?
- What is the output of the following C code?
- What is the output of the following C code?
- What is the output of the following C code?
- What is the output of the following C code?
#include <stdio.h>
#include <math.h>
int main()
{
float f = 5, g = 10;
enum
{
i = 10,
j = 20,
k = 50
};
printf("%d", ++k);
printf("%d", f << 2);
printf("%d", f % g);
printf("%lf", fmod(f, g));
return 0;
}
#include <stdio.h>
enum week
{
Mon,
Tue,
Wed,
Thu,
Fri,
Sat,
Sun
};
int main()
{
int day;
for (day = Mon; day <= Sun; day++)
printf("%d ", day);
return 0;
}
#include <stdio.h>
enum week
{
Tubelight = 2,
Fan = 2,
AC = 5
};
int main()
{
printf("%d %d %d", Tubelight, Fan, AC);
return 0;
}
#include <stdio.h>
enum week
{
Mon = 1,
Tue,
Wed = 5,
Thu,
Fri = 10,
Sat,
Sun
};
int main()
{
printf("%d %d %d %d %d %d %d", Mon, Tue, Wed, Thu, Fri, Sat, Sun);
return 0;
}
#include <stdio.h>
enum state
{
high = 1,
low = 0,
high = 9
};
int main()
{
printf("%d", high);
return 0;
}
#include <stdio.h>
enum CSK
{
mahi = 7,
faf = 13
};
enum RCB
{
faf = 13,
virat = 18
};
int main()
{
printf("%d", faf);
return 0;
}
Enum VS Macro
- We can also use macros to define names constants.
- There are multiple advantages of using enum over macro when many related named constants have integral values:
- Enum follow scope rules.
- Enum variables are automatically assigned values.
- Also we can assign float constants with macros, but in enum we can only assign integer constants.
#define PI 3.14
Typedef
- typedef gives us a opportunity to rename a data type.
- It can be used with built-in data types as well as user-defined data types.
- What is the output of the following C code?
#include <stdio.h>
int main()
{
typedef int i;
i num = 10;
printf("%d", num);
return 0;
}
Comments
Post a Comment