Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
14 views2 pages

C Struct

Uploaded by

super gamer TV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

C Struct

Uploaded by

super gamer TV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

C Struct

In C programming, a struct (or structure) is a collection of variables (can be of


different types) under a single name.

Syntax of struct
struct structureName { struct Person {
dataType member1; char name[50];
dataType member2; int citNo;
... float salary;
}; };

Create struct Variables


When a struct type is declared, no storage or memory is allocated. To allocate
memory of a given structure type and work with it, we need to create variables.
Here's how we create structure variables:

struct Person {
// code
};

int main() {
struct Person person1, person2, p[20];
return 0;
}

Another way of creating a struct variable is:


struct Person {
// code
} person1, person2, p[20];

In both cases,
person1 and person2 are struct Person variables
p[] is a struct Person array of size 20.
Example 1: C structs

#include <stdio.h> int main() {

#include <string.h>
// assign value to name of person1

// create struct with person1 strcpy(person1.name, "George Orwell");


variable
struct Person {
// assign values to other person1 variables
char name[50];
person1.citNo = 1984;
int citNo;
person1. salary = 2500;
float salary;
} person1;
// print struct variables
printf("Name: %s\n", person1.name);
printf("Citizenship No.: %d\n", person1.citNo);
printf("Salary: %.2f", person1.salary);

return 0;
}

You might also like