Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 4

Computer Fundamental, Number System

C Programming
Structure
Topics Covered: 1. Representation of Number

Structure: Array is used to represent a group of data items t at belongs to same data!type, suc as int or float. "o#ever, if #e #ant to represent a collection of data items of different data!types using a single name, t en #e cannot use an array. For t at C supports constructed data type $no#n as structure. %A structure is a used!defined data type, # ic contains group of multiple different data type related variable.& 'e can define t e structure by follo#ing format: struct tag_name { datatype element_1; datatype element_2; datatype element_n; }; (n above synta), *struct+ $ey#ord declares a structure olds multiple different data type variable. T ese variables are $no#n as t e structure elements or members of structure. ,ac member may ave same or different data type. T e name of t e structure is $no#n as tag name. (n above format t ere is no any declaration is made but it defines only t e format of structure variable so it is $no#n as structure template. Structure variable can be declared as follo#ing #ay: struct tag_name e.g. variable_1, variable_2, , variable_n; // structure template

struct student { int rollno; char name[100 ; char address[100 ; }; struct student s1,s2,s!;

// structure declaration

(t is also allo#s bot structure declaration and template creation in one statement: struct tag_name { datatype member_1; datatype member_2; datatype member_!; datatype member_n; variable_1, variable_2, variable_!;

"ere bot t e tas$, one is structure template definition and variable declaration is made in one statement. *variable-1+, *variable-.+, *variable-/+ are structure variable after declaration. 0uring defining structure #e ave to consider follo#ing points: 1. T e structure template is terminated #it semicolon 1232 4y ###.s arebca.com S are 4CA.Com A place to learn and s are all about 4CA

Computer Fundamental, Number System .. /. 0uring structure template definition, no any memory location is made for structure elements. T e total si5e of structure variable is e6ual to sum of individual si5e of structure elements.

'e can define structure out of any function or inside function. ' en #e define structure out of any function t e structure definition is global and can be used by any ot er function.

Accessing structure elements for assignment, print, and read: 'e can access structure elements by using member operator ., # ic also $no#n as dot operator or period operator. e.g. struct student { int rollno; char name[100 ; char address[100 ; }; struct student s1; // structure template

// structure declaration

main" # { s1$rollno % 1; strcpy "s1$name, &'hantilal(#; strcpy "s1$address, &)hmedabad(#; } "ere #e ave assign t e rollno, name, and address of student by using dot operator. Similarly #e can read structure elements and print it. scan*"& +d(, ,s1$rollno#; scna*"& +s(, s1$name#; print*"& +d-n(, s1$rollno#; print*"& +s-n(, s1$name#;

Initialization of structure: 'e can initiali5e structure as li$e as array by specifying list of values in curly brac$et. T e format of initiali5ation is: struct variable_1% { list o* values separated by comma }; e.g. struct student { int rollno; char name[100 ; char address[100 ; }; // structure template

main" # { struct student s1%{1,('hantilal(, &)hmedabad( }; }

// .nitiali/ation

(n above e)ample #e ave initiali5ed t e structure elements by separating #it comma in curly brac$et. 'e assigned values 1, S antilal, A medabad to rollno, name, and address respectively. 4ut #e cannot initiali5e t e structure element during structure definition. 4y ###.s arebca.com S are 4CA.Com A place to learn and s are all about 4CA

Computer Fundamental, Number System e.g. struct student { int rollno%0; }

// 0ives compilation error

ANS( C standard allo#s t e initiali5ation of auto storage class variable but non ANS( compiler allo#s only initiali5ation of static and extern storage class variable. So for t at #e must #rite static or e)tern before declaration. static struct variable_1% { list o* values separated by comma }; // 1or non )2'. compiler

Arra of Structure: As #e $no#n t at structure is a group of related different data type variables. Sometimes #e need multiple variables of structure for t at #e can use t e array of structure. For e)ample #e ave structure of student. 'e #ant t e list of students of entire class but t e one variable of structure can represent only one student. So to represent entire class variable #e ave to use array of student structure. 'e can declare t e array of structure as simply as ot er data type array. And same #ay to ot er data type #e can use t is variable #it t e subscript. struct arrayname[si/e ; e.g. struct student { int rollno; char name[100 ; char address[100 ; }; main" # { struct student s[10 ; int i; print*"&3nter 10 student data 4 -n(#; *or"i%0 ; i 5 10 ; i 66# { scan*"&+d(, ,s[i $rollno#; scan*"&+s(, s[i $name#; scan*"&+s(, s[i $address#; } } 'e can initiali5e t e array of structure as li$e as t e t#o dimensional array: struct structurename e.g. variablename[si/e % {{list o* values o* 1 st element}, {list o* values o* 2nd element} , $ } // structure template

struct student s[2 % {{1, &'hantilal(, &)hmedabad(}, {2, &7ul8i(,(9hadara(}};

4y ###.s arebca.com

S are 4CA.Com A place to learn and s are all about 4CA

Computer Fundamental, Number System

4y ###.s arebca.com

S are 4CA.Com A place to learn and s are all about 4CA

You might also like