Structure
- the structure is a collection of variables referred under a single name, i.e., it provides a convenient means of keeping relevant information together.
- A structure is declared using the keyword ‘struct’.
- The elements present in a structure are called members. Usually, the members of a structure are logically related.
Example:
struct addr
{
char name[30];
char street[40];
char city[20];
char state[3];
};
Here, the declaration is terminated using a semicolon because a structure declaration is a statement.
Declaration of variables in a structure:
Syntax:
struct tag
{
type member-name;
type member-name;
.
.
.
} structure-variables;
Program:
#include<stdio.h>
#include<string.h>
struct scripwithme
{
int topicNo;
char topicName[30];
}s;//declaring variable s for structure
int main()
{
s.topicNo=20;
strcpy(s.topicName, "Structure");//copying string into char array
printf("topicNo: %d\n", s.topicNo);
printf("topicName: %s\n", s.topicName);
return 0;
}
Output:
topicNo: 20
topicName: Structure
Arrays of Structure:
- Structures are often arrayed.
- To declare an array of structures, we need first to define a structure, and then we have to declare an array variable of that type.
Example:
#include<stdio.h>
#include<string.h>
struct scriptwithme
{
int topicNo;
char topicName[30];
};
int main()
{
int i;
struct scriptwithme s[2];
printf("Enter 2 records");
for(i=0;i<2;i++)
{
printf("\nEnter Topic-No: ");
scanf("%d",&s[i].topicNo);
printf("\nEnter Topic-Name: ");
scanf("%s",&s[i].topicName);
}
printf("\n ScriptWithMe Information List:");
for(i=0;i<2;i++)
{
printf("\n Topic-No: %d, Topic-Name: %s", s[i].topicNo,s[i].topicName);
}
return 0;
}
Output:
Enter 2 records
Enter Topic-No: 01
Enter Topic-Name: Structure
Enter Topic-No: 02
Enter Topic-Name: Recursion
ScriptWithMe Information List:
Topic-No: 1, Topic-Name: Structure
Topic-No: 2, Topic-Name: Recursion
Union
- A union in C is a memory location that is shared by different types of variables.
- Declaring a union is similar to declaring a structure.
Syntax:
union tag
{
type member-name;
type member-name;
type member-name;
.
.
.
} union-variables;
For example:
union Data
{
int x;
char ch;
} data;
- In union, all the members share the same memory location.
Example:
#include<stdio.h>
#include<string.h>
union datatypes
{
int x;
float y;
char str[32];
}d;
int main()
{
printf("Memory size occupied by data types is %d\n", sizeof(d));
return 0;
}
Output:
Memory size occupied by data types is 32
Accessing Union Members:
- To access any union member, we have to use the member access operator (.).
Example:
#include<stdio.h>
#include<string.h>
struct scripwithme
{
int topicNo;
char topicName[30];
}s;
int main()
{
s.topicNo=20;
printf("topicNo: %d\n", s.topicNo);
strcpy(s.topicName, "Structure");
printf("topicName: %s\n", s.topicName);
return 0;
}
Output:
topicNo: 20
topicName: Structure
Who is the course for?
- This course is designed for the students, employees, software programmers who want to learn C programming from scratch to end.
- After successfully completing this course you will be able to understand how the programming works and use it in the real time environment.
Prerequisites before starting this course:
- Before entering into this course you should have the basic understanding of how computer technology works.
- If you don’t have any experience in C or any other programming language then this course is for you.
- If your intermediate level of knowledge in C programming and you want to enhance your C skills to advance level then this course is for you.
- All you need is just any of the one device mobile, laptop, or PC and internet connection.
Questions 🙵 Answers.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum
Video 48 Min + 2 Min read to complete
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum
Video 48 Min + 2 Min read to complete
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum
Video 48 Min + 2 Min read to complete
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum
Video 48 Min + 2 Min read to complete
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum
Video 48 Min + 2 Min read to complete