Top Programming on Structure, Enumeration (enum), and their output in C Language.
by Md Farrukh Asif Run
C Structures (structs)
Structures:
Structures
is very important data member. It (also called structs) is a way to group
several related variables into one place. Each variable in the structure is
known as a member of the structure.
Unlike an array, a structure can contain many different data types (int,
float, char, etc.).
struct MyStructure
{ // Structure declaration
int myNum; //
Member (int variable)
char myLetter; //
Member (char variable)
}; //
End the structure with a semicolon
Access Structure Members
To access members of a structure, use the dot syntax (.):
// Create a structure called studStructure
struct myStructure {
int Num;
char Letter;
};
int main() {
// Create a structure variable of myStructure called s1
struct studStructure s1;
// Assign values to members of s1
s1.Num = 13;
s1.Letter = 'B';
// Print values
printf("My number: %d\n", s1.Num);
printf("My letter: %c\n", s1.Letter);
return 0;
}
==============
Output:
My number: 13
My letter: B
===============
Simpler Syntax
You can also assign values to members of a structure variable
at declaration time, in a single line.
Just insert the values in a comma-separated list inside curly
braces {}. Note that you don't have to use the strcpy() function for string values with this
technique:
//
Create a structure
struct studentStructure {
int Num;
char Letter;
char String[30];
};
int main() {
// Create a structure variable and assign values to it
struct myStructure s1 = {13, 'B', "Example of string data member"};
// Print values
printf("%d %c %s", s1.Num, s1.Letter, s1.String);
return 0;
}
================
Output:
13 B Exxample
of string ddata member
=================================
Copy Structures:
You can also assign one structure to another.
In the following example, the values of s1 are copied to s2:
Example
struct
myStructure s1 = {13, 'B', "Example of string data member"};
struct
myStructure s2;
s2 = s1;
Here,
s1 data structure will be copied into s2.
Modify Values
If you want to change/modify a value, you can use the dot
syntax (.).
And to modify a string value, the strcpy() function is
useful again:
Example
struct myStructure {
int myNum;
char myLetter;
char myString[30];
};
int main() {
// Create a structure variable and assign values to it
struct myStructure s1 = {13, 'B', "Some
text"};
// Modify values
s1.myNum = 30;
s1.myLetter = 'C';
strcpy(s1.myString, "Write something else");
// Print values
printf("%d %c %s", s1.myNum, s1.myLetter, s1.myString);
return 0;
}
==============
Real-Life
Example
Use a structure to store different information about Cars:
Example
struct
Car {
char brand[50];
char model[50];
int year;
};
int main() {
struct Car car1 = {"BMW", "X5", 1999};
struct Car car2 = {"Ford", "Mustang", 1969};
struct Car car3 = {"Toyota", "Corolla", 2011};
// Printing the Object or variables
printf("%s %s %d\n", car1.brand, car1.model, car1.year);
printf("%s %s %d\n", car2.brand, car2.model, car2.year);
printf("%s %s %d\n", car3.brand, car3.model, car3.year);
return 0;
}
===============
C Enums
An enum is a special type that represents a group of
constants or numeric only (unchangeable values).
To create an enum, use the enum keyword,
followed by the name of the enum, and separate the enum items with a comma:
enum Level {
LOW,
MEDIUM,
HIGH
};
Note that the last item does not need a comma.
It is not required to use uppercase, but often
considered as good practice.
Enum is short for "enumerations", which
means "specifically listed".
To access the enum, you must create a variable of it.
Inside the main() method, specify the enum keyword,
followed by the name of the enum (Level) and then the name of the enum variable (myVar in
this example):
enum Level
myVar;
Now that you have created an enum variable (myVar), you can
assign a value to it.
The assigned value must be one of the items inside the enum (LOW, MEDIUM or HIGH):
enum Level
myVar = MEDIUM;
By default, the first item (LOW) has the value 0, the second (MEDIUM) has the value 1, etc.
If you now try to print myVar, it will output 1, which
represents MEDIUM:
#include
<stdio.h>
enum
Level {
LOW,
MEDIUM,
HIGH
};
int
main() {
// Create an enum variable and assign a value
to it
enum Level myVar1 = MEDIUM;
enum Level myVar2 = LOW;
enum Level myVar3 = HIGH;
// Print the enum variable
printf("%d", myVar1);
printf("\n%d", myVar2);
printf("\n%d", myVar3);
return 0;
}
===============
Output:
1
0
2
==========
Enum in a
Switch Statement
Enums are often used in switch statements to check for
corresponding values:
enum Level {
LOW = 1,
MEDIUM,
HIGH
};
int main() {
enum Level myVar = MEDIUM;
switch (myVar) {
case 1:
printf("Low Level");
break;
case 2:
printf("Medium level");
break;
case 3:
printf("High level");
break;
}
return 0;
}
=================
C Language:
C for the beginner 's by Md Farrukh Asif
Operators
in C by Md. Farrukh Asif
C's
Control Flow by Md. Farrukh Asif
String handling in C by Md Farrukh Asif
The
Function of C Language by Md. Farrukh Asif
File
Handling With Various Statements/Syntaxes In Programming And Their Output In C
Language.
C
For Beginner's Programming With Output. Course Code – 106 [‘C’]) TMBU,BGP
Bca
Solved Examination Questions With Answers Course Code – 106 [‘C’]) Tmbu,Bgp
Top Programming on Structure, Enumeration (enum), and their output in C Language by Md Farrukh Asif
Here’s a FAQ on structures and
enumerations (enum) in C:
Structures in C:
Q1: What is a structure in C?
A structure is a user-defined data type in C that allows grouping variables of
different data types together under a single name. This is particularly useful
for representing more complex data entities.
Q2: How do you define a structure in C?
struct StructureName {
dataType1 member1;
dataType2 member2;
...
};
Example:
struct Student {
char name[
50];
int rollNumber;
float marks;
};
Q3: How do you declare and initialize a structure
variable?
struct Student student1 = {
"John Doe",
101,
85.5};
Q4: How do you access members of a structure?
You can access the members of a structure using the dot (.
) operator.
printf(
"Name: %s\n", student1.name);
printf(
"Roll Number: %d\n", student1.rollNumber);
printf(
"Marks: %.2f\n", student1.marks);
Q5: Can you have a pointer to a structure?
Yes, you can have a pointer to a structure and access its members using the
arrow (->
) operator.
struct Student *ptr = &student1;
printf(
"Name: %s\n", ptr->name);
Q6: Can structures contain other structures?
Yes, structures can contain other structures, allowing for the creation of
complex data types.
struct Address {
char city[
50];
int pin;
};
struct Student {
char name[
50];
struct Address address;
};
Q7: What is the size of a structure?
The size of a structure is the sum of the sizes of its members, but it may also
include padding bytes to ensure proper alignment in memory.
Q8: Can structures be passed to functions?
Yes, structures can be passed to functions by value or by reference (using
pointers).
Enumerations (enum) in C:
Q1: What is an enumeration in C?
An enumeration (enum) is a user-defined data type that consists of integral
constants. It is used to assign names to the integral constants which make the
code more readable.
Q2: How do you define an enumeration in C?
enum EnumName {
constant1,
constant2,
...
};
Example:
enum WeekDay {
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
};
Q3: What are the default values of enum constants?
By default, the first constant is assigned the value 0, and each subsequent
constant is assigned the value incremented by 1.
enum WeekDay {
Sunday,
// 0
Monday,
// 1
Tuesday,
// 2
...
};
Q4: Can you assign specific values to enum constants?
Yes, you can assign specific values to enum constants.
enum WeekDay {
Sunday =
1,
Monday =
2,
Tuesday =
5,
Wednesday =
10
};
Q5: How do you declare and use an enum variable?
enum WeekDay today;
today = Wednesday;
printf(
"Day: %d\n", today);
// Output will be 10
Q6: What is the size of an enum in C?
The size of an enum in C is the same as the size of an int
, since enums are implemented as integers in C.
Q7: Can enums be used in switch statements?
Yes, enums are often used in switch statements to handle different cases.
switch (today) {
case Sunday:
printf(
"It's Sunday\n");
break;
case Monday:
printf(
"It's Monday\n");
break;
...
}
Q8: Can two enum constants have the same value?
Yes, two enum constants can have the same value, but it is generally
discouraged as it can lead to confusion.
*** See You Again ***
************************
Share and Subscribe
with Like
************************
No comments:
Post a Comment