Structure The structure in C is a user defined data type that can be used to group items of possibly different types into a single type The struct keyword is used to define the structure in the C programming language The items in the structure are called its member struct structure name data type member name1 data type member name1 After defining structure we can create variables struct structure name data type member name1 data type member name1 variable1 varaible2 Or structure declared beforehand struct structure name variable1 variable2 The memory allocation take place only after variable are declared Access Structure Members We can access structure members by using the dot operator Syntax structure name member1 strcuture name member2 Create a structure called myStructure main struct myStructure int myNum char myLetter Create a structure variable of myStructure called s1 struct myStructure s1 Struct myStructure s1 5 hello Assign values to members of s1 s1 myNum 13 s1 myLetter B Print values printf My number d n s1 myNum printf My letter c n s1 myLetter return 0 Array of Structures An array of structures in C can be defined as the collection of multiple structures variables where each variable contains information about different entities The array of structures in C are used to store information about multiple entities of different data types The array of structures is also known as the collection of structures Need for Array of Structures Suppose we have 50 employees and we need to store the data of 50 employees So for that we need to define 50 variables of struct Employee type and store the data within that However declaring and handling the 50 variables is not an easy task Let s imagine a bigger scenario like 1000 employees So if we declare the variable this way it s not possible to handle this struct Employee emp1 emp2 emp3 emp1000 Declaration of Array of Structures Struct number of elements Initialization of Array of Structures struct number of elements structure name structure name array name array name inside a structure members are declared element1 value1 element1 value2 element2 value1 element2 value2 Refer Lab Execrise Nested Structure A nested structure in C is a structure within structure One structure can be declared inside another structure in the same way structure struct Employee int employee id char name 20 int salary struct Organisation char organisation name 20 char org number 20 nested structure struct Employee emp include stdio h struct address char city 20 int pin char phone 14 struct employee char name 20 struct address add void main struct employee emp printf Enter employee information n scanf s s d s emp name emp add city emp add pin emp add phone printf Printing the employee information n printf name s nCity s nPincode d nPhone s emp name emp add city emp add pin emp add phone File File handling in C is the process in which we create open read write and close operations on a file Definition of file File is a set of records that can be accessed through the set of library functions Stream Stream means reading and writing of data Streams are designed to allow the user to access the file efficiently FILE object contains all the information about stream like current position EOF end of file File Types Sequential File Read only in sequential manner Random Access File read and modify randomlyis Steps for file operation Opening of file fopen Reading or writing file fprintf fscanf Closing File fclose Opening file FILE fptr used to declare a pointer to a Fie object fptr fopen filename mode w Writes to a file a Appends new data to a file r Reads from a fileExample FILE fptr Create a file fptr fopen filename txt w Close the file fclose fptr or fptr fopen C directoryname filename txt w Write To a File FILE fptr Open a file in writing mode fptr fopen filename txt w Write some text to the file fprintf fptr Some text Close the file fclose fptr Append Content To a File FILE fptr Open a file in append mode fptr fopen filename txt a Append some text to the file fprintf fptr nHi everybody Close the file fclose fptr Read a File FILE fptr Open a file in read mode fptr fopen filename txt r FILE fptr Open a file in read mode fptr fopen filename txt r Store the content of the file char myString 100 Read the content and store it inside myString fgets myString 100 fptr Print the file content printf s myString Close the file fclose fptr FILE fptr Open a file in read mode fptr fopen loremipsum txt r Print some text if the file does not exist if fptr NULL printf Not able to open the file Close the file fclose fptr FILE fptr Open a file in read mode fptr fopen filename txt r Store the content of the file char myString 100 If the file exist if fptr NULL Read the content and print it while fgets myString 100 fptr printf s myString If the file does not exist else printf Not able to open the file Close the file fclose fptr Dynamic memory allocation Dynamic memory allocation in C allows you to allocate and manage memory at runtime 1 malloc 2 calloc 3 free 4 realloc malloc method The malloc or memory allocation method in C is used to dynamically allocate a single large block of memory with the specified size calloc method calloc or contiguous allocation method in C is used to dynamically allocate the specified number of blocks of memory of the specified type free method free method in C is used to dynamically de allocate the memory The memory allocated using functions malloc and calloc is not de allocated on their own Hence the free method is used whenever the dynamic memory allocation takes place realloc method allocated a previously allocation of realloc or re allocation method in C is used to dynamically change the memory memory Command Line arguments Command line arguments in C allow you to pass arguments to your program when you run it from the terminal or command prompt This can be useful for providing data or options without modifying the source code Syntax The main function can be defined to accept command line arguments as follows int main int argc char argv Your code here argc Argument count It indicates the number of command line arguments passed including the program s name argv Argument vector It is an array of strings character pointers that contain the arguments is an array of strings that holds the command line arguments passed to a program argv allows your program to accept and process input dynamically at runtime languages including C C 2 Open GNU Compiler Collection
View Full Document