C Programming Tutorial
Structures are user defined data types in C,Structures aggregate heterogeous data types into single unit.On the other hand Union is a structure with overlapping members; only the last member stored is valid.
Structures are also called as a Compound Types, where as int float,char,double called as primitive data types or built-in data types, whcih holds single value.Structure holds multiple values.
for ex: Person has Id,Name,profession,bank details, credit card etc.,, all these fileds can be combined together to form a compound type called Person.
Structure Syntax:
struct [tag name]{ datatype memeber1; datatype memeber2; . . . datatype memeberN; }[variable names....]; **Tag name and variable names are optional.
Declaring the structure
struct Book { int bookId; char title[50]; char author[50]; float price; }; Structure book has 4 attributes or members called bookId,Title,author and price information. Here "Book" is the tag name. It is a mere template, No storage been allocated in the declaration.
Creating variables of structure data type
In the structure declartion itself variables can be created. struct Book { int bookId; char title[50]; char author[50]; float price; }b1,b2,b3; Here b1,b2,b3 are variables of type struct Book. This is one way of creating variables of structure. whenever user delcares varaibles then only memory will be allocated for each memeber in the structure. Another way of creating structure type struct keyword tag name followed by comma seperated varaible names. struct Book b1,b2,b3; Both declarions yields same.
Initializing the Structure variables
By Default all data memebers will be assigned based on data type. for ex:
Initializing few fields struct Book b={1001,"A Tour of C++"}; // right first 2 fields intialized struct Book b={1001,"A Tour of C++","Bjarne Stroustrup"};// right first 3 fields intialized struct Book b={1001,"A Tour of C++",59.99f}; //// wrong first 2 fields intialized , third value ignored struct Book b={0};// right struct Book b={"A Tour of C++","Bjarne Stroustrup",59.99f};// wrong bookid is int, author is char array, so 1st and 3rd ignored, 2nd value copied to authors array
Accessing Structure members
Accessing Structure members using dot(.) operator
syntax:
structure_variable.datamembername
struct Book b={1001,"A Tour of C++","Bjarne Stroustrup",69.99f};
printf("Book id=%d ",b.bookId); printf("Book title=%s ",b.title); printf("Book author=%s ",b.author); printf("Book Price=%.2f ",b.price);
Accessing Structure members using -> operator
syntax:
structure_pointer->datamembername
struct Book b={1001,"A Tour of C++","Bjarne Stroustrup",69.99f};
struct Book *pb=&b; printf("Book id=%d ",pb->bookId); printf("Book title=%s ",pb->title); printf("Book author=%s ",pb->author); printf("Book Price=%.2f ",pb->price);
Structure Objects can be passed to a function or returned from a Function.
Synatx:void function_name(struct struct_name variable) { statement(s); }
Print Book Details using Function,passing structure as a Object
void print(struct Book b) { printf("Book id=%d ",b.bookId); printf("Book title=%s ",b.title); printf("Book author=%s ",b.author); printf("Book Price=%.2f ",b.price); } -- Calling print function print(b);Synatx:
void function_name(struct struct_name* variable) { statement(s); }
Print Book Details using Function,passing structure as a Pointer
void print(struct Book*pb) { if(pb==null) {printf('Book Pointer is NULL\n');return;} printf("Book id=%d ",pb->bookId); printf("Book title=%s ",pb->title); printf("Book author=%s ",pb->author); printf("Book Price=%.2f ",pb->price); } -- Calling print function //Passing address of the structure variable print(&b);
struct Book books[3]; //unintialized array of books
Structures has different memory locations for each field, Where as in Union memory location will be only one. i.e largest size of field, i.e suppose uinion has int,float,double fields, among these double has more space occupied compared to other types in the union, so size of the union is equal to double type.
Enumerated types define new user defined type. Enumerated types mainly used for grouping related items together.
for ex: Days , Months etc.,
enum DAYS {SUN,MON,TUE,WEB,THU,FRI,SAT} enum MONTHS {JAN,FEB,MAR,APR,MAY,JUN,JULY,AUG,SPET,OCT,NOV,DEC} /* in DAYS enum SUN has value 0, MON (SUN+1)=1 TUE=MON+1=2 ..... SAT=FRI+1=6 compiler assigns these values. */ /* programmer can set any values these constants... enum DAYS {SUN=1,MON,TUE,WEB,THU,FRI,SAT} In this SUN set to 1 , MON becomes (SUN)+1=2 ..... SAT=(FRI)+1=7 */ /* Simlilaryly for MONTHS enum enum MONTHS {JAN=1,FEB,MAR,APR,MAY,JUN,JULY,AUG,SPET,OCT,NOV,DEC=12} JAN set to 1 , FEB value not set, so it becomes JAN+1=2, MAR becomes FEB+1=3 ... DEC set to 12 */
enum MEMBERSHIP {SILVER=2000,GOLD=5000,PLATINUM=10000}; In this enum MEMBERSHIP each enum constant set to different values.
Displaying Enum values
printf("%d",JAN);/* enum values are constants, In MONTHS enum, all Integer constants */
enum MEMBERSHIP{SILVER=5000,GOLD=10000,PLATINUM=20000}; printf("Please enter Number 1..3\n 1 for SILVER \n 2 for GOLD \n 3 for PLATINUM\n"); int m; scanf("%d",&m); switch(m) { case 1: printf("SILVER membership with %d points\n", SILVER);break; case 2: printf("GOLD membership with %d points\n", GOLD);break; case 3: printf("PLATINUM membership with %d points\n", PLATINUM);break; default:printf("Not a valid membership\n");break; }
Changing the value of ENUM constants, SILVER=10000; or doing arithmetic operations on enum constants SILVER+=10000;
#include <stdio.h> enum EMPLOYEE_TYPE{PERMANENT='P',CONTRACT='C',FREELANCE='F',NOTAPPLICABLE='N'}; enum EMPLOYEE_TYPE getEmpType(char c); int main() { printf("Employee Type=%c\n",getEmpType('C')); } enum EMPLOYEE_TYPE getEmpType(char c) { enum EMPLOYEE_TYPE val; switch(c){ case 'P':val=PERMANENT;break; case 'C':val=CONTRACT;break; case 'F':val=FREELANCE;break; case 'N':val=NOTAPPLICABLE;break; } return val; }
typedef is a keyword is used to create new name for an existing data type.
typedef built-in-type Synonym;
typedef int Integer;
Integer is a synonym for int.
Now user can declare int types as
Integer i=10,j=20; Valid declaration
size_t is a unsigned integer or synonym for unsigned integer defined in string.h
ADS