- UNIT-I (Array)
strlen(),strcpy(), strcat() ,strcmp()
Definition
Macro substitution directives
File inclusion directives
Conditional compilation
Bitwise Operators
Shift operators
Masks
Bitfield
Macro substitution directives
File inclusion directives
Conditional compilation
Bitwise Operators
Shift operators
Masks
Bitfield
Declaration of Structure in c
We use a struct keyword to create a structure in C. A struct keyword is a short form of structured data type
Syntax
struct name variables;
Here struct_name can be anything of your choice. The members' data type can be the same or different. Once we have declared the structure we can use the struct name as a data type like int, float, etc
Example
struct num
{
char name[100];
float price;
};
struct num num1, num2, num3;
we declare variables like,
<data type> variables;
Example
int i;
float f;
In structure, data type is <struct name>. So, the declaration will be
<struct name> variables;
Example
struct num num1;
Initialize structure
We can initialize the structure
Example
struct num
{
char name[100];
float price;
};
//num1 name as "xyz"
//price as 987432.50
struct num num1 ={"xyz", 987432.50};
Use of typedef in Structure
typedef makes the code short and improves lucidness. In the above conversation, we have seen that while utilizing structs each time we need to utilize the extensive linguistic structure, which makes the code confounding, long, unpredictable and less coherent. The basic answer for this issue is the utilization of typedef. It resembles the false name of the struct.
Code without typedef
struct home_address {
int local_street;
singe *town;
singe *my_city;
singe *my_country;
};
...
struct home_address var;
var.town = "Agra";
Code utilizing typedef
typedef struct home_address{
int local_street;
singe *town;
singe *my_city;
singe *my_country;
}addr;
..
..
addr var1;
var.town = "Agra";
Rather than utilizing the struct home_address each time you have to pronounce struct variable, you can essentially utilize addr, the typedef that we have characterized.
Example of Structure
#include <stdio.h>
/* Created a structure here. The name of the structure is
* StudentData.
*/
struct StudentData{
char *stu_name;
int stu_id;
int stu_age;
};
int main()
{
/* student is the variable of structure StudentData*/
struct StudentData student;
/*Assigning the values of each struct member here*/
student.stu_name = "Aryan";
student.stu_id = 4321;
student.stu_age = 30;
/* Displaying the values of struct members */
printf("Student Name is: %s", student.stu_name);
printf("\nStudent Id is: %d", student.stu_id);
printf("\nStudent Age is: %d", student.stu_age);
return 0;
}
Output:
Student Name is: Aryan
Student Id is: 4321
Student Age is: 30
Syntax
struct name variables;
Here struct_name can be anything of your choice. The members' data type can be the same or different. Once we have declared the structure we can use the struct name as a data type like int, float, etc
Example
struct num { char name[100]; float price; }; struct num num1, num2, num3;
we declare variables like,
<data type> variables;
Example
int i; float f;
In structure, data type is <struct name>. So, the declaration will be
<struct name> variables;
Example
struct num num1;
Initialize structure
We can initialize the structure
Example
struct num { char name[100]; float price; }; //num1 name as "xyz" //price as 987432.50 struct num num1 ={"xyz", 987432.50};
Use of typedef in Structure
typedef makes the code short and improves lucidness. In the above conversation, we have seen that while utilizing structs each time we need to utilize the extensive linguistic structure, which makes the code confounding, long, unpredictable and less coherent. The basic answer for this issue is the utilization of typedef. It resembles the false name of the struct.
Code without typedef
struct home_address {
int local_street;
singe *town;
singe *my_city;
singe *my_country;
};
...
struct home_address var;
var.town = "Agra";
Code utilizing typedef
typedef struct home_address{
int local_street;
singe *town;
singe *my_city;
singe *my_country;
}addr;
..
..
addr var1;
var.town = "Agra";
Rather than utilizing the struct home_address each time you have to pronounce struct variable, you can essentially utilize addr, the typedef that we have characterized.
Example of Structure
#include <stdio.h> /* Created a structure here. The name of the structure is * StudentData. */ struct StudentData{ char *stu_name; int stu_id; int stu_age; }; int main() { /* student is the variable of structure StudentData*/ struct StudentData student; /*Assigning the values of each struct member here*/ student.stu_name = "Aryan"; student.stu_id = 4321; student.stu_age = 30; /* Displaying the values of struct members */ printf("Student Name is: %s", student.stu_name); printf("\nStudent Id is: %d", student.stu_id); printf("\nStudent Age is: %d", student.stu_age); return 0; }
Output:
Student Name is: Aryan Student Id is: 4321 Student Age is: 30
Advertisements
कोई टिप्पणी नहीं:
एक टिप्पणी भेजें