Skip to main content

Posts

Showing posts with the label Abhilekh Gautam

File Handling and Various File Opening Modes in C

Every programs we were doing before were using Memory for storage and all those memory would wipe out once the program is terminated.But sometime the case is different,what if we need all those data again,We need to re-enter all those bulky data again,which will again be wiped out once the program is terminated. So we need to store the data in such a place such that it can be retrieved and displayed again and again.This is why we need files.The place about which we previously talked is the file on the disk.So we need File Handling to work with files. Let us see how a file can be handled. Various Operations in Files Creating a File Opening a File Reading from a File Writing to a File Closing a File File Opening Modes in C Before performing any operations in files,we must create/open the file . Here are the various File opening modes in C. "r" : It searches the file.If the file opens up successfully,the function fopen() loads the file from disk to memory and sets a pointer

What is Storage class and What are their types in C

 Variable declaration is not complete until we mention its storage class( there are some default conditions as well). Yeah you got it right ,Variables not only have data types they have storage class too. Why storage class? They tell us about The scope of the variable. Life time of the variable. Initial value of the the variable, if left unassigned. We have four storage class in C Automatic storage class External storage class Static storage class Register storage class Let us look at them one by one. Automatic Storage Class Scope : Local to the block where it is defined. Life Time : Until the control remains in the block. Initial value : A garbage value, one cannot predict. We use the keyword auto to declare a variable of automatic storage class. Demonstration of  Automatic storage class # include < stdio.h > void   myFunc (); int   main () {     auto  int   x   =   5 ;      printf ( " %d " , x ) ;      myFunc () ;      return   0 ; } void   myFunc () { int x;  

Working with String Handling Functions in C

 C provides large set of string handling library function under string.h header. Few of them are discussed below. Strlen This function returns the length of the given string which is an integer value. Syntax strlen (name_of_array); The actual parameter for the function is the address of the first character of the array. Usage # include < stdio.h > # include < string.h > int   main () { char   name [] = " Abhilekh " ; printf ( " length is  %d " , strlen ( name )) ; return   0 ; } The output is: The  length  of the string is  8 Strcpy It should be noted that a string cannot be assigned to another string using a assignment operator(=). This is where we use this library function. It copies one string to another. Syntax strcpy (string1,string2); It copies the string from string2 to string1. Usage # include < stdio.h > # include < string.h > int   main () {   char   name [] = " Abhilekh " ;   char   copyName [ 8 ];   strcpy ( copyName , n

What is Structure and why do we need them in C

 In one of our previous post we dealt with Array .Here in this post we'll be dealing with structure which  I prefer to call 'The advanced version of arrays'. Structure Similar to an array, A structure is a collection, however of a dissimilar data types. Take an example of an office, Where the Manager wants to keep  the records of his/her employees.The record may include employees' name, address, department, pay scale , age and so on.So, why not create a variable with all those features in it. So when we want an entity with multiple features in it we can go with structures. First Glance of Structure struct   student {      int   rollNo , intake Year;      char   name [ 20 ], address [ 20 ]; }; Above is a structure student with some of the features of the student like:Roll number, intake year, their name and address. The keyword struct is used to define a structure. Structure definition always ends with a semicolon. The general form of Structure definition is struct   <