C Notes

Download as pdf or txt
Download as pdf or txt
You are on page 1of 39

C Programming Reference Notes

Introduction to C Computers are capable of handling various complex problems, which are tedious and routine in nature. In order to solve the problem, programmers need to create a program. Program is a set of instructions to complete a particular task. Program is created to solve the problem. The problem solving involves: Detailed study of the problem Identification of input data, output requirements and conditions and limitations. Alternative methods of solution. Selection of best suitable method Generating the output To create any program we need to know different programming languages used. Any programming language has some rules and syntax to create a program. There are different programming approaches as well, they are: 1. Linear Programming approach: - This was the traditional approach of using programming language. In this, the entire program was written in a single file. For example a software or system hospital management in which we need to have all the transaction like case-paper generation, bed allotment, medical prescription etc. and all these transaction are done in a single file. If a system gives any error in a particular part of the software (bed allotment) then the entire software or system stops and we have to search for the error occurred and rectify it, recompile and reload it, till then no transaction can be done in other parts of the system. So this approach of writing programs has been dropped. Modular Programming approach: - To overcome the drawbacks of Linear programming approach, the developer introduced modular approach in which the entire software has been divided into different modules and then all these modules are then called in the main module. So, if any error occurs in any module, the entire software is not stopped and only the particular module stops working, we could get where the error has occurred very easily and rectify it and recompile and reload it. Main module

2.

Module 1

Module 2

Module 3

Module 4

C is categorized in modular programming language. Features of C language: C programming language was developed at AT&Ts Bell laboratories in the USA in 1972. This language has been designed and written by Dennis Ritchie. C is a very robust programming language. It is also known as middle level language because it is suitable for writing both system software (O/S, Compiler, interpreter etc) and application software (all business packages like tally etc). Unix Operating System is almost coded in C.

Introduction to Flowchart Flowchart: - Flowchart is graphical representation of program. The flowchart is used to show the program structure, logic flow and operations performed. Before writing any program the programmer has to create a flowchart. Once the flowchart is fully ready, it easy to write a program. Flowcharting has many standard symbols. Flowchart uses boxes of different shapes denoting different types of instruction. The different symbols used are as follows: Symbol Description

Ashwath Computers, Solapur

Page - 1

C Programming Reference Notes

Start

Used to indicate the starting point of the flowchart.

Input

It is used to accept input from the user

Process It is used to process the data to get the result

Decision

It is used to test the condition.

It is On-Page Connector

It is Off-Page Connector

Output It is used display results

Flow Lines to connect each symbol with other

Stop

It is used to indicate the end of the flowchart

Variables and Data-types

Ashwath Computers, Solapur

Page - 2

C Programming Reference Notes

Structure of C Program C has 32 Keywords but number of built in functions. Thus a C program is basically a collection of functions that are supported by the C library. One can also add new functions to the library. Before studying the language in detail, we will first begin with a structure of C Program and to understand the structure we will write a sample program and compile it and execute it. Structure of C Program: Header file inclusion Constant definition Function declaration main ( ) { set of instructions; } Example: #include<stdio.h> main( ) { printf(Welcome to C); } The output of this program will be Welcome to C # determines a preprocessor directive, include is keyword which is used to include the files into a program. stdio.h(Standard input/output) is a header file in which the standard I/O functions are defined. main() is a special function in C. From main() the execution of the program begins. It is mandatory to include main() function in a program, without main() function program does not execute. There is a pair of parenthesis after the function name main, which is necessary. The open brace bracket { indicates the beginning of the function main() and the close brace bracket } in the last line marks the end of the function. The statements which are included within these braces make up the body of main() function. Thus the function body contains the instructions to perform the given task. printf() is an executable function, which is predefined for printing the output from a program. A predefined function means a function which is already been written and compiled. Every C statement must end with a semicolon (;). After writing a program save it with .c extension. Once you have saved your file with .c extension, you have to compile the program. If any syntax errors are found during compilation they will be reported and the compilation will not be completed. Then we have to remove the errors and once again we have to compile the corrected program. Successful Compilation will generate the object file of the program. This file will have the .obj extension. Compilation of the program is done through compiler, which converts the program to assembly language and then the linker links, and the executable code of the program will be generated. The executable file of the program has the .exe extension. sample.c
compiler

sample.obj

linker

sample.exe

Variable: Variable is name given to the memory location where the actual data is stored. Variable as the name, its value always changes. To declare a variable we have to follow some rules, they are: 1. Variable name should start with a character and then it could have numbers. 2. It should not have any special symbols other than underscore (_). 3. It should not have any tab or spaces between the variable declarations. 4. Variable name should be specific defining its purpose Example: Name; No1; Emp_name; Ashwath Computers, Solapur Page - 3

C Programming Reference Notes

Salary; Emp Name ; 1no; Emp Salary Stud#name invalid invalid invalid invalid

Data-Types: Data type defines which type of data to be stored into a variable. C supports a number of data types. These different data types allow the user to select the data type according to the needs of the application. There are different data types supported by C, they are: 1. Primitive data types 2. User defined data types 3. Derived data types In this chapter we will study primitive data types. Primitive Data Types: There are three fundamental or primary data types: 1. Integer (int) 2. Character (char) 3. Floating point (float)

Data Type Integer short signed short unsigned Int unsigned int long signed long unsigned Floating point Float double long double Character Signed Unsigned

Range -128 to +127 0 to 255 -32768 to +32767 0 to 65535 -2,147,483,648 to +2,147,483,647 0 to 4,294,967,295 3.4e-38 to 3.4e+38 1.7e-308 to 1.7e+308 -3.4e-4932 to 1.1e+4932 -128 to +127 0 to 255 1 1 2 2 4 4

Bytes

Format Specification %d or %i %u %d %u %ld %lu %f %lf %Lf %c %u

4 8 10 1 1

Syntax of declaring a variable with the data type: data-type variable-name; eg: int a; char ch; float salary; double income_tax; Assigning values to variables: Variables can be assigned values with the use of the assignment = operator. Eg: a = 12; ch =D; salary = 4500.00; income_tax = 12000.890; We can also assign an expression on the right to the variable on the left Eg: c = a + b; Ashwath Computers, Solapur Page - 4

C Programming Reference Notes

z = p/q*r; p = p +10; Values to variables can also be assigned at the time of declaration: int i = 10; float pi = 3.14; char ch = A; Constants, Keywords and Identifiers Keywords: Keywords are words whose meanings have already been defined and these meanings cannot be changed. Keywords are also called as reserved words. Keywords should not be used as variable names. All keywords are in lowercase. There are 32 keywords in C. We will come across with these keywords in the entire module. Identifiers: Identifiers are names given to variables, functions and arrays. These are the names, which are user defined. They are made up by a combination of letters and digits. Normally identifiers should not be more than 8 characters long. Examples: min1 max_temp temp() etc. Constants: A constant is a fixed value, which does not change during the execution of a program. C supports several types of constants as follows: Numeric Constants and Character Constants

Input/Output functions: There are different I/O functions are available in the stdio.h file. These functions are broadly classified into different categories, they are: 1. Console I/O function: - In this the input is supplied by the Keyboard and output is displayed on to the VDU (Video Display Unit) or Screen. 2. Disk I/O function: - These functions are useful to perform I/O operations on floppy disks or hard-disk i.e. secondary storage device. In this chapter we will study Console I/O functions and Disk or File I/O functions will be covered in the "File Management" chapter. 1. Console I/O functions: These functions are further classified into Formatted and Unformatted I/O functions. Formatted I/O functions:- These are categorized for performing I/O of all types of data-types. There are two functions, i.e., a. printf( ) is a function used to display output on to the screen. b. scanf( ) is a function used to accept input from the user. syntax: scanf("Format specification", &variablename); Here format specification specifies which type of data to be stored in the variable name. & Is an address-of operator. Eg: int no1; scanf("%d",&no1); synatx:printf("Message"); Ashwath Computers, Solapur Page - 5

C Programming Reference Notes

or printf("Message formatspecification",variablename); Messages can any string that is to be displayed on the screen. Eg: printf("Welcome to C"); printf("The number is: %d",no1); Operators: Expression: a = b + c; In this expression value of c is added to b and than the value of b is assigned to a. Here a, b and c are called as operands and = and + are called as operators. There are various types of operators in C, they are:1. Arithmetic Operator Binary Unary 2. Relational Operator 3. Logical Operator 4. Arithmetic Assignment Operator 5. Ternary or Conditional Operator

1. Binary Arithmetic Operator: - Binary Operator operates on two operand. They are classified as follows: 1. 2. 3. 4. 5. + / * % Addition Subtraction Division Multiplication Modulus e.g.:- a + b e.g.:- a - b e.g.:- a / b e.g.:- a * b e.g.:- a % b

Modulus operator is used to get the remainder from an expression These operator have a precedence /priority i.e. * / % have the highest Priority + - have the lowest priority For e.g.: a = b + c * d; In this expression c*d will be calculated first and than the result is added to b, because * has a higher priority than the +. To change the precedence of operator we can make use of () parenthesis. a = ( b + c ) * d; Now b + c will be calculated first and than the result is multiplied with d. If in an expression we have two operators with same priority or precedence than the operation will be performed from left to right. E.g.: a = b / c * d; Ashwath Computers, Solapur Page - 6

C Programming Reference Notes

In this expression b / c will be calculated first and than multiplied with d. Arithmetic Unary Operators: A Unary operator operates on single operand. There are two types of unary operators, they are:1. Increment Operator (++). 2. Decrement Operator (- -). 1. Increment Operator: - It increases the value of an operand or variable by 1. These are further classified as:- Pre-Increment Operator:- In this the value of an operand is increased first and then it is assigned to another variable. E.g.: a = 5, b; b = ++a; a = 6 and b = 6 - Post-Increment Operator:- In this the value of an operand is assigned to a variable first and then its value is increased. E.g.: a = 5, b; b = a++; a = 6 and b = 5

2. Decrement Operator: - It decreases the value of an operand or variable by 1. These are further classified as: - Pre-Decrement Operator:- In this the value of an operand is decreased first and then it is assigned to another variable. E.g.:a = 5, b; b = --a; a = 4 and b = 4 - Post-Decrement Operator: - In this the value of an operand is assigned to a variable first and then its value is decreased. E.g.: a = 5, b; b = a--; a = 4 and b = 5 Relational Operators: Relational Operators are used to check the relationship between any two values. When we need to compare any values, we have to make use of relational operators. An expression, which uses a relational operator, is a relational expression. The result of relational expression is either zero or one i.e. (True = 1 or False = 0). There are different relational operators:1. < 2. <= 3. > Is less than Is less than equal to Is greater than

Ashwath Computers, Solapur

Page - 7

C Programming Reference Notes

4. >= 5. == 6.! =

Is greater than equal to Is equal to Is not equal to

For eg: 2 > 10 2 < 10 is false hence it will return a is true hence it will return a value 0 value 1

Relational expression can be used to check relationship between variables, variable and constant, constants. Arithmetic expression can be used on either side of a relational operator E.g.:p-q > a+b; In such situation, both the arithmetic expressions are evaluated first and then the results so obtained are compared. Thus arithmetic operators have higher priority than relational operators. Relational operators are used in decision statements. Logical Operators: Logical operators are used when we want to test more than one condition and then make decisions. The logical operators are: 1. 2. 3. && || ! logical AND logical OR logical NOT

E.g.: - if a<b && p+q == 40 The above expression combines two relational expressions by the logical operators. Such an expression is called as a logical expression. It also gives the results in True (1) or False (0) 1.AND (&&): - AND is an operator which results to true only if all the relational expression gives the answer true. Any of one expression is false it results to false. AND Cond1 T T F F Cond2 T F T F Result T F F F

2. OR (| |): - OR is an operator which results to true if any of the one condition gets true.

OR

Cond1 T T F F

Cond2 T F T F

Result T T T F

3. NOT (!): - NOT is an operator that negates the condition i.e if the result is true it will give false and if result is false it will give true.

Ashwath Computers, Solapur

Page - 8

C Programming Reference Notes

NOT

Cond1 Result ! (T) ! (F) F T

Arithmetic assignment operators:These are operators, which are used to assign the result of an expression to a variable. 1. 2. 3. 4. 5. = += -= *= /= a = b assignment operator a+=b a-=b a*=a a/=b a = a+b a = a-b a = a*a a = a/b

Conditional/Ternary Operator: This operator is used to check the condition and display the output according to the result. The syntax is: exp1 ? exp2(T) : exp3(F); Here exp1 is evaluated first and then if the result is true exp2 will get evaluated else if result is false exp3 get evaluated. For eg: a = 10; b = 5; z =(a>b) ? a : b Here exp1 (a>b) exp2 is a exp3 is b First (a>b) is evaluated. Since in this case a>b is true, exp2 is evaluated. Therefore z is assigned the value of a. Defining Symbolic Constants: In a program there may be numerous situations where we require to use certain values repeatedly. For eg: - 3.14 is a constant which is used for geometrical problems. While using such constants it may happen the value of it may change. So to avoid this C provides a facility of assigning such constants to a symbolic name. These constant values are assigned to symbolic names at the beginning of the program. Once these values have been assigned symbolic names, they are automatically substituted at the appropriate places wherever the symbolic name appears. Syntax: #define symbolic_name value_of_the _Constant eg:#define PI 3.14 #define MIN 0 Rules while declaring symbolic constants -Usually they are written in upper case to differentiate them from the variables. -They can appear anywhere in the program, but they have to be defined before they are referenced. -There should not be a space between # and define

Ashwath Computers, Solapur

Page - 9

C Programming Reference Notes

-No semicolon after defining a symbolic constant

There are three different ways to create a program: 1. Sequential 2. Decision control 3. Iterative Decision Control Statements: Until now we had seen Sequential programs like addition of two numbers etc. in which we had not changed the order of execution. It was in a sequence. But, while writing program, it is often necessary to change the order of execution of program depending upon certain condition. It is also required to execute an instruction or a set of instructions until the condition is met. Thus, it is necessary to take decisions and depending upon the decisions the results are executed. C language supports decision making instructions as follows:1. if.....else statement 2. switch.....case statement 3. conditional statement

1. if.....else statement It is used to control the flow of the execution of statements in a program. Syntax: if(test condition) { set of instructions; } else { set of instructions; } In this, the condition to be evaluated is placed in a pair of parenthesis immediately following the keyword 'if '. First, the test condition is evaluated. If it is true then the statement (or set of statements) following 'if ' are executed. If the condition evaluates to false, the statement (or set of statements) of the else block is executed. Thus there is two way branching for the ' if ' statement: one for the true condition and other for the false condition. The test condition is generally expressed with the help of the relational or Logical expression.

a) Simple if statement: - In this if the condition gets true it executes the statement of the if block and if the condition gets false then it skips the if block and come out of the decision statement. In this it will not have else block. Syntax: if(test-condition) {

Ashwath Computers, Solapur

Page - 10

C Programming Reference Notes

statements; } b) if.....else statement: - There are situations where you may want to execute a statement when the if condition evaluates to true and another statement when the condition evaluates to false. In such cases we make use of the if.....else statement. Syntax: if(test-condition) { set of statements; } else { set of statements; } The test condition is evaluated. If it is true than statement block following if is executed, if it is false the statement block following else is executed. In any case only one statement block get executed. c) if.....else ladder: - When a multipath decisions are involved we make use of the if.....else ladder. The if....else ladder is a chain of if's where each else has an associated if. The form of the if....else ladder is:if(condition1) statements1; else if(condition2) statements2; else if(condition_n) statements_n; else default statement;

d) Nested if statements: - It is possible to nest if...else statement, i.e we can make use of more than one if...else statements by nesting them. There is no limit of if....else nesting. Syntax: if(condition1) { if(condition2) { statement block 1; } else { statement block 2;

Ashwath Computers, Solapur

Page - 11

C Programming Reference Notes

} } The switch statement: We have seen that we can make use of multiple if-else statements in our programs to control selections. However, as the number of options goes on increasing the complexity of programs also increases. The program becomes difficult to read and understand. C provides the switch statement to avoid the use of such series of if statements. Syntax:switch(variable) { case constant-1: Statement-block1; break; case constant-2: Statement-block2; break; .................. .................. default : default statement block; } Variable can be any variable value. It is enclosed in a parenthesis following the switch keyword. The keyword case is followed by an integer or character constant. Each constant in each case should be different from all others. The switch statements work as follows: - First, the expression or variable following switch is evaluated. -Then, the value of the expression is matched one after the other, with each case constant. When a match is found, the statement block following that particular case is executed. The break statement signifies the end of each case and causes an exit from the switch statement and control is transferred to outside the switch block. The default statement is optional. It will be executed if the value does not match any of the case values. Here care has to be taken to include the break statement. If a break statement is not inserted in the case then the program not only executes the statements following the case where the match has been met, but also the following case statements and the default statement as well.

Loops: We had seen how to use sequential and decision control structure to write programs. In this chapter we will study loop control structure. Loops are used to execute an instructions repeatedly until a specific condition is met. Loops are used to repeat a specific task for certain number of times. There are two types of loops i.e. variable loop and fixed loop. A variable loop is a loop where we don't know the number of times of execution of the loop. A fixed loop is the loop where the number of times of execution of loop is fixed. A program loop has two segments: The body of the loop (It is the set of statements which are to be executed till the condition is satisfied)

Ashwath Computers, Solapur

Page - 12

C Programming Reference Notes

The control statement (The statement that will check the condition and direct the program to execute the body till the condition is true.

initialize

initialize

Test cond=?

False

Body of the Loop False

True Body of the Loop

True

Test cond=?

a) Entry-Controlled Loop

b) Exit-Controlled Loop

In figure a, the test condition is first checked and then the body of the loop is executed if the condition is true. If the condition is not true the body of the loop will not be executed. Such a loop is called as Entry Controlled Loop" In figure b, the body of the loop is executed first without checking the condition for the first time. Then, the test condition is checked. If the condition is true it continues the loop else terminates. Such a loop is called as "Exit Controlled Loop". Thus, in an exit-controlled loop, the body of the loop will be executed at least once (even if the test condition is false) C Language provides three types of loop constructs to repeat the body of the loop. They are: - while statement - do-while statement - for statement 1. while statement: The general syntax of while statement is:while(test condition) { body of the loop; } The while constructs starts with the while keyword followed by the test condition in parenthesis (). The body of the loop is included in the pair of braces. It is an Entry controlled loop. In this the condition is checked first and then if the condition is true it executes the body of loop. Then the control again goes back to while, condition is checked and if it is true loop get executed. This process repeats until the condition in the while loop evaluates to

Ashwath Computers, Solapur

Page - 13

C Programming Reference Notes

true. When condition becomes false, body of the loop is skipped and the controlled is transferred to the statement that immediately follows the body of the loop.

A while loop has to include the following steps:1. Initialize the counter (to execute the instructions for specified number of times) 2. Check the test condition. 3. Execute the body of the loop 4. Increment the counter again and go back to check the condition. The test condition may be any relational or logical expression.

2. do.....while statement As we have seen in the previous section, the while loop checks the test condition before executing the body of the loop i.e. it is an entry-controlled loop. Hence, if the test condition is false for the first time itself the loop may not get executed at all. Another form of loop control is the do.....while. This loop construct is an exitcontrolled loop i.e. it checks the test condition after executing the loop body. The body of the loop thus gets executed at least once in a do.....while loop. This is the only difference between the while and do....while. Otherwise, while and do.....while behave exactly in the same way. There are very few programming situations in actual practice where the do.....while loop is required. Syntax: do { body of the loop; }while(test condition);

When the program encounters the do statement, it executes the body of the loop first. It then checks the test condition and if true transfers the control back to the first statement in the loop body. This process continues till the test condition is true. When the condition becomes false, the subsequent statement is executed. 3. The for statement: The for statement is probably the most frequently used loop construct. The for statement provides initialization of counter. Test condition and counter increment all in single line. Syntax: for(initalisation; test-condition; increement) { body of the loop; } The for loop is an entry controlled loop construct. It first checks the condition and if it is true then it executes the body of the loop. Basically for loop is used for fixed number of iteration. In for loop we can omit one or more sections, i.e. the loop can be written as: i =1; for( ; i<=10;)

Ashwath Computers, Solapur

Page - 14

C Programming Reference Notes

{ printf("%d",i); i++; }

k = 1; for(i=1;i<=10;i++) { body of the loop; } for(k=1,i=1; i<=10; i++) { body of the loop; }

Arrays: Array is collection or group of elements of similar or same data type. We declare variables using different data types, but in real life if we want to handle data of 100 students for eg: to accept marks of 100 students then declaration of 100 variables will be very tedious job and it is time consuming. To overcome this problem C introduced 'Arrays', which will help us to declare any number of variables of same data type using single statement. Syntax: data-type array-name[size]; Here data-type can any type of data like int, float, char etc. array-name is the variable name used to represent an array and the size defines number of elements in an array. Eg: int Mark[10]; Here we can store marks in 10 subjects or 10 student marks and it occupies 10*2 = 20 bytes in the memory float salary[30]; Here the salary of 30 employees can be stored and it occupies 30*4 = 120 bytes in the memory The array elements are stored in contiguous memory location. They are accessed using the index numbers. Index number starts with 0 Eg: - int num[5];

int num[5] = {10,20,60,30,67}; If we want to access the third element in an array we have to use it num[2]; will give 60 Initialization of an array elements: Initial values to an array can be assigned in different ways, int num[5] = {10,40,67,4,55};

Ashwath Computers, Solapur

Page - 15

C Programming Reference Notes

int num[4] = {10,20}; here the first two elements are initialized with 10 and 20 and remaining 2 will be initalized with 0. int num[10] = { 0 }; here all the elements in an array are initialized with 0 As we can assign value of one variable to another Like a=5; and b = a; We cannot assign an array to another array.

For eg: int a[3] = {10,20,30}; int b[3]; b = a; //is invalid and will generate error. Two Dimensional Arrays: We had seen one-dimensional arrays. It is also possible to declare arrays with two or more dimensions. A two dimensional array is also called a matrix. Such a matrix or table can be stored in a two dimensional array. eg:- we may have the following table:Student_Id 1100 2100 3100 1400 Sub1 40 52 60 50 Sub2 50 65 48 59 Sub3 67 72 87 67

This table shows the data for the four students, where marks of three subjects of each student are listed. Thus, this table is made up of four rows and four columns. Each row represents the marks obtained by a particular student in all the three subjects, whereas each column represents the marks obtained by all the students in a particular subject. We define such tables using two dimensional arrays. Syntax: data-type array-name[row_size][col_size]; Thus the above table can be represented in a 2D array as: - int Stud[4][4]; The elements in 2D array are stored row wise. Each dimension of the array is indexed from 0 onwards to its maximum size minus one. The first index selects the row and the second index selects the column. thus Stud[3][1] will select the second item from the fourth row, Stud[0][2] will select the third item from the first row etc. 2D array are also stored in a contiguous memory. The element of the first row and first column is stored first, then the first row and second column and so on, when all the elements of first row completes, the elements of the second row are stored in the same way. Initializing 2D arrays: - These are initialized like 1D arrays with their declaration followed by the list of values of the elements. The list is enclosed in braces, with their element being separated by comma. The elements are initialized row wise. Eg: int arr[2][3] = { 2,3,5,6,7,8};

Ashwath Computers, Solapur

Page - 16

C Programming Reference Notes

Here it is represented as:arr[0][0] = 2; arr[0][1] = 3; arr[0][2] = 5; arr[1][0] = 6; arr[1][1] = 7; arr[1][2] = 8; Alternative methods for the above initialization are: int arr[2][3] = { {2,3,5}, {6,7,8} }; or int arr[2][3] = { {2, 3, 5}, {6, 7, 8} }; To initialize all the elements of 2D array with 0 is done as follows:int arr[2][3] = { {0}, {0}, {0} };

Multidimensional Array: C allows arrays of more than 2 dimensions. Such arrays are multidimensional arrays. The exact limit of the number of dimensions is dependant on the compiler. These arrays are rarely required. Eg: int arr[2][3][4]; float a[4][3][2][2]; The first array is type of integer will contain 24 elements whereas the second array will contain 48 elements of type float. These are initialized as follows: int arr[2][3][4] = { { {1, 1, 1, 1}, {2, 2, 2, 2}, {3, 3, 3, 3} }, { {5, 5, 5, 5}, {6, 6, 6, 6}, {8, 8, 8, 8} } }; It is represented as: 0th 2-D array 1111

Ashwath Computers, Solapur

Page - 17

C Programming Reference Notes

[3][4]

2222 3333

1st 2-D array [3][4]

5555 6666 8888

In memory they are stored in contigous location 111122223333555566668888 The first element of the array can be reffered as arr[0][0][0]. Strings: An array of characters is a string. Any group of characters within double quotation marks is a constant string. Thus a string constant is a one-dimensional array of characters. Eg: - "Programming in C Language" Since the string is enclosed in double quotes if we wish to include double quotes in our string we can implement it in the following way with the use of the backslash (\): "\"Programming in C Language\"" The output of the above declaration will be "Programming in C Language"

A string variable is any valid C variable name; since a string is an array of char we always declare a string as an array. The general form of a string variable is: char String_name[size]; The size determines the number of characters in the String_name. eg:char name[20]; char address[25]; A string constant is an array of characters terminated by a null ('\0'). A string constant can be initialized as follows: char str1[ ] = {'P', 'R', 'O', 'G', 'R', 'A', 'M', '\0'}; Each character in the array occupies one byte of memory. The last character is always '\0'(null character). When declaring the character array as above, the null character is a must. The elements of a character array are also stored in contiguous memory locations. The above string can also be initialized as follows: char str1[ ] = "PROGRAM"; In this declaration the '\0' is not required. The null character is inserted automatically. Thus the difference in both the initialization is that if you enter elements each as a separate character then you enclose each character in single quotes, separate the characters with a comma and terminate the string with the '\0'. In case of initializing the complete string within double quotes the '\0' is not required. As you have seen in both the above examples the number of characters (size of the string) need not be specified while initializing. C will automatically determine the size of the array on the basis of the number of elements initialized.

Ashwath Computers, Solapur

Page - 18

C Programming Reference Notes

To read a string from the user we can use gets() function and to write a string(display) we can use puts() function. Strings can be read and write by using scanf and printf function as well but while reading and writing we have to use %s format specifier.

String Handling Functions: We had seen different programs to perform a number of operations on a string like copying one string to another, concatenating strings etc. C provides a number of useful functions of these types for handling strings. These functions are in the common string library functions, which are supported by most C compilers. We can directly use these functions in the program. List of functions and their description: 1. strcat() :- Appends one string at the end of the other string. 2. strncat() :- Appends first n characters of a string at the end of another string. 3. strcpy() :- copies one string into another 4. strncpy() :- copies the first n characters of one string into another string. 5. strcmp() :- compares two strings. 6. strncmp() :- compares the first n characters of two strings 7. strcmpi() / strnicmp() :- compares the first n characters of two strings without regard to case. 8. strdup() :- duplicates the string. 9. strchr() :- finds the first occurence of a given character in a string. 10. strrchr() :- finds the last occurence of a given character in a string. 11. strstr() :- finds the first occurence of a given string in another string. 12. strlen() :- finds the length of the string 13. strlwr() :- converts a string to lowercase. 14. strupr() :- converts a string to uppercase. 15. strset() :- sets all the characters of a string to a given character. 16. strrev() :- reverses the string.

Pointer: Pointer is a variable, which is used to store the address of another variable. Pointers are an important feature of the C language. To understand it, let us revise about computer memory. Data and instructions are stored in the memory of computer. The computers memory is a sequential collection of storage cells. Each cell is known as byte of memory. Each cell also has a unique address associated with it. This address is a positive number and they are numbered sequentially. Whenever a variable is declared in a program, the system allocates memory to hold the value of the variable. Each byte has a unique address associated with it For eg: - int i =10; This declaration reserves space in memory to store the value of the variable i and, i gets associated with that particular memory address. The value 10, which has been assigned to the variable, gets stored in that particular memory location. We can represent the location of the variable in memory as follows: Variable_name i

Ashwath Computers, Solapur

Page - 19

C Programming Reference Notes

Value Address 3245

10

Declaring a Pointer Variable: Pointer variable is declared as int *ptr The address of a particular variable is accessed by using address operator (&).

For eg: int i = 10; int *ptr; ptr = &i;

The ' * ' operator indicates " value at address" or it is called as "value at address operator". Thus the declaration: - int *ptr; Tells the compiler that ptr is a varaible, which is used to store the address of an integer variable.

Variable i Value Address 3245

j 10 4000 3245

To print the address we have to use %u since the addresses are unsigned integers Pointer Arithmetic: Pointer variables can be used in expression Eg: float x,p,q,z,*ptr1,*ptr2; ptr1 = &p; ptr2 = &q; Then we can perform: x = *ptr1/ *ptr2; *ptr1 = *ptr2 - 10; z = *ptr1 * 10; All the above expression is valid. While using ' / ' division operator care should be taken to have a space between / and * else /* will be mistaken as a comment. Pointers can also be incremented or decremented. Thus ptr1++ or ptr2-- is valid in C. In this case we have to understand what happens when pointers are incremented or decremented. ptr1++ will cause the pointer ptr1 to point the next value of its type. Thus if 'a' is an integer and ptr1 is a pointer to 'a', then when ptr1 is incremented, its value increments by 2 since integer is stored in 2 bytes of memory. Eg: int a, *ptr1; ptr1 = &a;

Ashwath Computers, Solapur

Page - 20

C Programming Reference Notes

If ptr1 has a address as 4322 then ptr1++ will cause its value as 4324 i.e. its value is incremented by the length of the data type to which it points. This length is called scale factor. We cannot add, divide and multiply any pointer value, i.e. ptr1 + ptr2 is invalid ptr1 / 2 is invalid ptr1 * 10 is invalid

Pointers and Arrays: We have already seen that when we declare an array, the elements of the array are stored in contiguous memory locations. In pointers we have studied that whenever we increment a pointer it points to the next memory location of its type. Using this, we will study pointers and arrays. When an array is declared, the compiler immediately allocates a base address and sufficient memory to hold all the elements of the array in contiguous memory locations. The base address is the address of the first element (i.e. 0th index) of the array. The compiler also defines the array name as a constant pointer to the first element of the array. Pointers to One-dimensional arrays: For eg we declare an array as follows int arr[5] = { 10,20,30,40,50}; arr is an array of type int whose size is five and the elements are initialized. Let us assume the base address of arr is 2400. Since each element is of type int and there are five elements, the array is stored in memory as follows: arr is a constant pointer which points to the first element arr[0]. Thus the address of arr[0] is 2400 in our example. We can declare an int pointer ptr to point to the array arr as ptr = arr; or ptr = &arr[0]; Therefore incrementing the pointer ptr will cause its value to increase by 2 and hence will point to the next element of the array which is arr[1] and so on.

Thus we can obtain the address of the element as: Address of nth element = base address + (n * scale factor of data type) Eg: Address of 4th element = 2400 + (4 * scale factor of int) 2400 + (4 * 2) 2400 + 8 2408 We can use pointers to access the elements of an array. Thus we can access arr[3] as *(ptr+3), arr[2] as *(ptr+2) and so on

Ashwath Computers, Solapur

Page - 21

C Programming Reference Notes

Pointers and 2D Arrays: We already know that elements of a two dimensional array are stored row wise. Thus the base address of a two dimensional array arr[] is the address of the arr[0][0]th element which will be obtained as &arr[0][0]. The compiler then allocates contiguous memory locations to the array elements row wise i.e. first all the elements of row 1 are stored then all elements of row 2 and so on. For eg: int arr[2][5] = {(1,2,3,4,5), (6,7,8,9,10) }; The elements will be stored in memory row-wise as: -------Row0----- |--------- Row1------1 | 2| 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10

if we want to access the element arr[1][2] then arr[1][2] = *(ptr + no.of cols * i + j) = *(ptr + 5 * 1 + 2) = *(ptr + 7) =8 Hence it is essential to define the number of column i.e. size of each row when declaring a 2D array so that the compiler can determine the storage mapping for the array elements Pointer to string: A string is an array of charcters, which is terminated by the null character '\0'. Thus the concept of pointers and 1D arrays can be extended to array of characters. In this we can make a pointer to point to the first element of string and by incrementing the pointer we can get the next character of the string till we reach to null. The concept of single dimension array of characters can be extended to the table of strings. When we declare a two-dimensional array of strings, each string will be allocated equal length as specified in its declaration. However, in practice the all strings are not of equal length, hence instead of making each row of fixed number we can make each row a pointer to a string of varying lengths. Eg: char *name[3]; The declaration means name is an array of three pointers where each points to the particular name.

Pointers to Pointers: We know that a pointer variable contains the address of a data type. Since the addresses are always whole numbers, so the pointers will contain whole numbers. The declaration char *ch does not imply that ch contains a data type char. It implies that ch is a pointer to a data type char. i.e. ch contains the address of a char variable. i.e. the value pointed to by ch is of type char. The concept of pointers can thus be extended further i.e., a pointer contains the address of a variable. This variable itself can be a pointer. We can have a pointer to contain the address of another pointer variable. Eg: -

Ashwath Computers, Solapur

Page - 22

C Programming Reference Notes

int i, *j , **k; j = &i; k = &j; Where i is an int data type and j is a pointer to i. k is a pointer variable which points to the integer pointer j. The value at k will be the address of j. We can also create a pointer to store the address of k and so on. Dynamic memory Allocation: In real life programming situations, many times it so happens that the number of data items keeps on changing during program execution. eg:- if you are processing a list of travelers, new passengers may get added to the list as also passengers may be cancelled. Such data, which is subject to change during program execution, is said to be dynamic in nature. In order to handle such type of data efficiently and easily we make use of dynamic memory management. Structures, which are dynamic in nature, provide flexibility to add, delete and rearrange data items at run time. They make it possible to free unwanted memory and allocate new memory as and when required. In this section we will study the same i.e. dynamic storage management functions in C. The process of allocating memory to data items during run time is called as dynamic memory allocation. C has four library functions, which are useful for allocating and releasing memory during the

program execution. These functions make intelligent use of memory these functions are: -

Function Use malloc() - Allocates required memory in bytes and returns a pointer to the the space allocate. first byte of

calloc()

- Allocates space for an array of elements, initializes them to zero And returns the pointer of the first byte of memory

free()

- frees previously allocated memory

realloc() - modifies the size of the previously allocated memory.

1. malloc() :- memory can be allocated using the malloc() function. Syntax: ptr = (cast-type*)malloc(bytes); malloc() reserves a size of memory equal to bytes and returns a pointer of type void i.e we can assign this pointer to any type of pointer. Thus ptr is a pointer of cast-type. Eg: ptr1 = (int*)malloc(25 * sizeof(int)); This declaration will allocate a memory equivalent to 25 times the size of int in bytes. ch_ptr = (char*) malloc(10); Will allocate 10 bytes of space for the pointer ch_ptr of type char. The storage, which is allocated, has no name; hence it can be accessed only through pointers. malloc() allocates contiguous bytes of memory.

2. calloc() :- It is generally used to allocate memory for derived data types like arrays and structures. calloc() allocates multiple blocks of storage, each of the same size and sets all the bytes to zero.

Ashwath Computers, Solapur

Page - 23

C Programming Reference Notes

Syntax: ptr = (cast-type*)calloc(n,element-size); with this declaration, n contigous block each of size element-size are allocates and all the bytes are set to zero. If there is not enough memory, a NULL pointer is returned. eg:int arr1[10]; int *ptr; ptr = (arr1*)calloc(10,sizeof(arr1)); Here we have defined an array arr1 having 10 elements of type int. We use the calloc() function to reserve 10 blocks of size each equal to the size of arr1. The pointer is returned to ptr of type int. It is important to check whether space has been made available or not before further execution.

3. free(): - It is used to release the used space in dynamic memory allocation, when it is not required can be done with the free() function. When we release the block of memory, which we have allocated dynamically and no longer need it, it can become available for future use. Syntax: free(ptr); 4. realloc(): - In situations, where the previously allocated memory is not sufficient and there is additional memory requirement, it is possible to reallocate memory for more data. Also, if the memory allocated is much larger than required and we need to free the excess memory which has been dynamically allocated previously is possible. realloc() helps us to alter the original allocation of memory as:ptr = malloc(size); ptr = realloc(ptr,newsize); realloc() will also return a pointer to the first byte of the reallocated memory block. The newsize will be memory space allocated to the pointer. newsize may be larger or smaller. Also the new block may or may not begin at the same place as the old block. If contigous additional space is not available, then realloc() will create an entirely new memory space and move the contents of the old block to new block. A NULL will be returned if the function fails to locate additional memory space and the original block is lost.

Functions: In the introduction of C language, we have seen that C is a modular programming language. The modules in C are known as functions. C program is nothing but a collection of functions. A function is a self-contained block of statements that performs a specific task of some kind. Until now in all the programs we had seen many functions, such as: -main, printf, scanf etc. These are the functions, which has been already defined. We can also create our own functions. On the basis of this, functions are categorized in two types: Library function, User defined function. Library functions are those, which are not written by us, we can directly use them in our program. User defined functions are developed by the user while writing the programs. Later, these user-defined functions can become part of the library. We can divide our program into smaller functional sub-programs where each sub-program may be independently coded, debugged and tested. These sub-programs are nothing but functions.

Ashwath Computers, Solapur

Page - 24

C Programming Reference Notes

Advantages of Functions: - Functions facilitate top-down modular approach. The overall problem is solved first and the details of various lower levels are solved later. - Functions reduce the length of the source program. -Functions can be independently tested and debugged. -Functions can be used by many other programs. This means that it is not necessary to write functions, which have already been written, we use them directly in the program. In a function we have return statement, function name and argument list. Syntax: return_type Function_name(arg list......); The return statement is optional. If any value is to be returned to the calling function, it is returned with the help of the return statement. The argument list is used to pass values to the called function from the calling function. Thus it provides a means of data communication between two functions. Eg: sq(a); prod(a,b); power(x,n); All the variables in the argument list must be declared with their types immediately after the function header. On the basis of return type/value and argument list, functions are categorized as:1. Functions with no arguments and no return values 2. Functions with arguments and no return values 3. Functions without arguments and with return values. 4. Function with arguments and return values.

1. Function with no arguments and no return values: - When a function has no arguments, no data is sent to the function from the calling function. Also when the function is returning any value, the called function does not receive any data from the calling function i.e. there is only a transfer of control and not data. Syntax: void fn1();

2. Functions with arguments and no return values: - In calls to function with arguments we can send the list of arguments from the calling function to called function. Syntax: void fn1(arg1,arg2....);

Eg: main() { ........

Ashwath Computers, Solapur

Page - 25

C Programming Reference Notes

fn1(actual argument list); ........ ........ }

fn1(formal argument list) { ........ ........ } 3. Function without arguments and with return values: - In this the called function will return the value or data to the calling function. Syntax: int fn1(); Eg: main() { int a; a = fn1(); ........ ........ }

int fn1() { ........ ........ return value; }

4. Function with arguments and return values: - In practical situations, we may need to pass argument to a function and get the return values for further processing. Since a function may be called by a number of other functions each function may require the return values to be output in different form also. Thus a self-contained function should be one that receives a predefined form of input and outputs a desired value. This will effect a two-way communication by the way of passing values between functions.

Syntax: return-type fn1(arg1,arg2....);

Eg: -

Ashwath Computers, Solapur

Page - 26

C Programming Reference Notes

int fn1(int,float); main() { ........ a = fn1(10,2.4); //actual arguments ........ ........ }

int fn1(int x,float y) //formal arguments { ........ return x*y; }

Return value and their types: We had seen the 'return' statement. The return statement is used to return a value to the calling function. The return statement can take one of the following forms: return; or return(expression); The first form of return does not return any value to the calling function. The second form return returns a value to the calling function. In both the cases, the control is transferred back to the calling program when the retrun is encountered. Thus the return statement: - On execution transfers control back to the calling function. - Returns the value of the expression in the parenthesis to the calling function. Eg: - return(sum); return(x+y+z); return(p); return(a*b); A calling function can pass any number of values as arguments to the calls function. However, the called function can return value per call to the calling function. Thus the statements return(a,b) or return(a,10); are not valid. There can be more than one return statement in certain situations eg: -

Ashwath Computers, Solapur

Page - 27

C Programming Reference Notes

if(x<0) return(1); else return(0); All functions have default return data of type int. If we want the function to return a particular type of data other than int then we required to specify the data type. Nesting of functions: C allows nesting of functions. This means that a function, which is being called, can itself call another function, which itself may call another function and so on. We have already made use of a number of standard library functions. This means that the function that we are calling from another function is itself calling other functions. For eg: - if we want to convert distance of km to cm, then we have to write a function to calculate the distance in km to meter and then meter to cm. Passing arrays as arguments to functions: We had seen to pass simple variables of type int, float etc to function. It is also possible to pass an array to a function. To pass an array we have to call the function as: avg(num,n); Here num is an array and n is the total number of elements in the array. While passing the array as an argument, only the array name without any subscript is listed followed by size of the array. The function to which array is passed should be defined as; float avg(float num[], int n); here float num[] tells the compiler that num is an array of type float. If necessary we can declare the size of an array.

Type Casting: We already know that C allows mixing of variables of different types and constants in expressions. While evaluating these expressions, certain rules are followed. When performing an operation, the compiler considers two operands and the operator associated with them. If the operands differ in type, then first the lower type is automatically converted to the higher type. The final result of the expression is converted to the type of the variable on the left side of the assignment operator, before the value is assigned to it. But if the variable on the left of the assignment operator is an int and the result you have obtained is float then the fractional part is truncated, in case, if double has been converted to float, the digits are rounded. In case of converting of long int to int while assigning to the variable to the left of the assignment operator, the excess higher order bits are dropped. In some situations it becomes necessary to force a type conversion, which is different from the automatic conversion done by C. We are required to explicitly convert a value of a particular expression to a specific data type. This is where; local conversion of a data type is done which is known as type casting. Syntax: (type-name)expression; type-name is one of the standard data types of c. The expression can be any valid C expression i.e a constant, variable or expression. The parentheses around the type name are essential.

Ashwath Computers, Solapur

Page - 28

C Programming Reference Notes

Eg: int i=9,j=2; float p; p = (float) i/j; When the expression i /j casted to type float, it will cause the variable i to be first converted to type float, before performing division. Storage classes in C: Variables in C not only have data types but also storage classes. The storage class provides information about the location of a variable. There are basically two locations where the variable values are stored: memory and CPU registers. The variable's storage class decides in which of these locations the value is stored. The storage class of a variable tells us: - Where the variable would be stored - The default initial value of the variable (if the initial value has not been specifically assigned) - The scope of the variable (in which function the value of variable would be available) - The life of the variable The scope of the variable means over what part or parts of a program is the variable actually for use. The life of the variable is the period during which the variable retains its value during the execution of the program. Variables are also classified on the place of their declaration. Those variables, which are declared within a particular function, are local variables. Those variables, which are declared outside of any function, are called global variables. Storage classes explicitly declare the scope and life of the variables. These useful mainly in multifunction and multiple file programs: 1.Automatic Storage class: When the storage class of a variable is declared automatic (auto) it means: - The variable is stored in memory - The default value of the variable is auto, which is unpredictable and often refers to as garbage. - The scope of the variable is local only to the function in which it is declared. - The life of the variable is only till the control is in the block in which the variable is defined. By default the variables declared without any storage class are automatic.

2.External Storage class: When the storage class of a variable is declared extern it means:- The variable is stored in memory - The initial default value of the variable is zero. - The scope of the variable is global (this means that the variable can be accessed by any function in the program) - The life of the variable is as long as the program execution does not come to an end. External variables are declared outside all function, but they are available to all functions.

3.Static Storage class: The features of static storage class are: -

Ashwath Computers, Solapur

Page - 29

C Programming Reference Notes

- The variable is stored in memory - Its default initial value is zero. - The scope of the variable is local to the block in which it is defined. - The value of the variable persists until the end of the program. Though the scope is local to the block but the variable does not disappear but remains alive throughout the program. Static variables are initialized only once when the program is compiled. 4.Register Storage class: The features of register storage class are: - The variable is stored in CPU register. - Its default initial value is garbage. - The scope of the variable is local to the block in which it is defined. - The life of the variable is only till the control remains within the block in which it is defined. The variable stored in CPU registres is accessed much faster then those stored in memory. So the frequently accessed variables should be stored in CPU register (eg :- for loop controls). In CPU the number of registers are limited so care should be taken while declaring storage class of variables as register. Registers in microcomputer are usually 16 bit and hence cannot be used to store float or double values which occupy 4 bytes or above in the program.

Structures and Unions: We have seen that arrays can be used to store elements of the same data type such as int or float. If we wish to represent data items of different types arrays are not useful. To do this C had introduced a feature called as Structures. Structures are derived data types used to represent a collection of data items of different data types. Structures are useful to handle logically related data items. For eg: - the personal data of people like names, addresses, phone numbers etc., or data of students like their names, roll numbers, marks, grades etc. The data types in structures used can be of same or different type. Syntax: struct tag_name { data_type member_1; data_type member_2; . .......... ............ }; Eg: - to represent student data using structure as follows: struct Stud { int roll; char name[10]; int mk1;

Ashwath Computers, Solapur

Page - 30

C Programming Reference Notes

int mk2; float per; }; The declaration of structure simply describes a format or a template of the structure. Once you have defined this structure data type you can declare one or more variables of that type. Eg: struct Stud s1,s2,s3; This declaration will declare s1, s2, and s3 as three variables of type struct Stud specifying the data members of the template structure. Structure can also be declared as follows: struct Emp { int ECode; char EName[10]; } e1,e2,e3;

When a structure is declared the compiler allocates memory to each individual member. All the members are stored in a contiguous memory locations. struct Num { int i; char ch; float f; }; struct Num n1; 7 bytes of storage space will be made available to hold the structure members: 2 bytes for i, 1 byte for ch and 4 bytes for f.

Initialization of a structure: Like variables and array, structure variables too can be initialized at the time of declaration. It is done as follows: struct Stud { int roll; char name[15]; float per; }; struct Stud s1 = {1, "John", 56.7}; struct Stud s2 = {2, "Jane", 86.7};

Accessing Structure Elements: - To access structure elements we have to use dot operator (.)

Ashwath Computers, Solapur

Page - 31

C Programming Reference Notes

eg:s1.roll; s1.name;

Syntax: struct_var . struct_ele Assigning values to individual members of the structures: Values can be assigned individually to structure members as follows s1.roll = 101; strcpy(s1.name,"John");

Arrays of Structures:We have seen declaration of structure variables, but in actual practice when we need to handle large amount of data for structure for eg 100 student data then declaration of 100-structure variable is tedious. To overcome this problem we can create array of structure variables. struct Stud s[100];

Assigning values of one structure variable to another: The values of a structure variable can be assigned to another structure variable of same type using the assignment operator (=) We can assign entire structure or individual structure elements to another structure. Nesting of structure: One structure can be nested in another structure. This means that you can have a structure within a structure. For example if we want to create a structure of student having data as name, age, rollno, sub1, sub2, total and per. Then, we use a single structure to represent this information: struct Student { char name[10]; int rollno; int age; float sub1; float sub2; float tot; float per; }; On the other hand we can declare the items related to Result part in another substructure as: Struct Result { float sub1; float sub2;

Ashwath Computers, Solapur

Page - 32

C Programming Reference Notes

float tot; float per; }; struct Student { int rollno; char name[10]; int age; struct Result res; }; Passing a structure variable to a function: C supports passing a structure variable to functions. The following methods are used to pass structure variables to a function: - Passing individual member of the structure as an actual argument to function. In this method, as the number of structure elements goes on increasing, the method becomes unmanageable and also cumbersome to use. -In the second method we can pass the entire structure to a function. When we pass the entire structure to a function, no changes are made to a structure passed. This means that we have to return the entire structure back to the calling function. -We can even create a pointer of struct variable and pass it to the function. In this method any changes made to the structure in function will affect to the original structure variable. Syntax: Function_name (struct Structure_name structure_variable) -The function must be declared for its type depending upon the data type it is supposed to return. If the function is supposed to return the entire structure then it has to be declared as type struct with the appropriate tag name. -Both the structure variables used as actual and formal arguments must be of the same struct type. Pointers and Structures: We can pass structure to a function using call by value. Another way to do the same is by using the call by reference method of functions. Making use of pointers to structures can use this method. struct Emp { char name[15]; int age; }e, *eptr; Here e is a structure variable of structure type Emp and pointer eptr is apointer point to data object of structure type Emp. We can initialize the pointer as: eptr = e; This will assign the address of the first member of e to eptr. To acces the elements of the structure variable we have to make use of arrow operator as follows: eptr name;

Ashwath Computers, Solapur

Page - 33

C Programming Reference Notes

eptr

age;

Unions: Unions are derived data types. They are used to group different variables together. However, there is a major difference between structures and unions in terms of storage. In a structure, each member of the structure has its own storage location. In an union however, all the members of the union use the same location. Thus although the union can contain a number of members of different types, it can handle only one member at a time. Eg: union num { int i; char ch; float f; }num1; Here the size of union num1 will be 4 bytes, because the compiler allocates storage space large enough to hold the largest variable type declared in the union. Typedef declaration: - C allows the user to define an identifier to represent an existing data type. This type is then used to declare variables. Syntax: typedef type identifier type is an existing data type, identifier is a new name to the data type. This identifier can be then used to define variables. The new type is only a new name and not the new data type. Eg:typedef int age; age mom, dad, son; here age is defined to be of type int. It is then used to declare variables mom, dad, and son. Thus mom, dad and son are actually declared of type int. The advantage of using typedef is that meaningful data names can be used to increase the readability of the program.

File Handling: In the last chapters we had seen console input and output operations, where the data get lost as soon as we come out from the program. But, in real life we need to manage large volume of data and console input/output does not become a convenient method. Here we need to store the data on disks and read it from the disks whenever required. Thus a file is a place on a disk where related data is stored. C has number of standard library functions to perform basic file operations. These functions include: Opening a file. Reading data from a file. Writing to a file. Closing a file.

Ashwath Computers, Solapur

Page - 34

C Programming Reference Notes

Naming a file.

To deal with file in C, we need to study the data structure of the file FILE that is defined in the header file stdio.h. This header file is always required to be included in our programs when we perform file operations. FILE is a defined data type. Declaring and opening a file: Every file, which we open, has its own FILE structure, which contains information about the file like its size, its current location in memory etc. The FILE structure contains a character pointer, which points to the first character that is to be read. Syntax to open file: FILE *fp; fp = fopen (filename , mode); fp is declared to be a pointer to the data type FILE. fp contains the address of the structure FILE. The second statement opens the file whose name is filename and the mode indicates the purpose of opening the file, i.e. whether to read a file or write into a file. Different modes of the file: 1. read (r ) : - searches for the file. If it exists, it is loaded in memory and the pointer is set to the first character in the file. If the file does not exists it returns NULL. Reading is possible using this mode. 2. write (w ) : - searches for the file. If it exists, its contents are overwritten. If the file does not exist, a new file is created. If unable to open the file returns NULL. Writing to the file is possible. 3. append (a ) : - searches for the file. If it exists, it is loaded in memory and the pointer is set to the last character in the file. If the file does not exist, a new file is created. If unable to open the file returns NULL. Appending new contents at the end of the file is possible. 4. r+ : - searches for the file. If it exists, it is loaded in memory and the pointer is set to the first character in the file. If the file does not exist returns NULL. It is possible to read, write new contents, and modify existing contents. 5. w+ : - searches for the file. If found its contents are destroyed. If the file is not found a new file is created. If unable to open file returns NULL. Writing new contents, reading them back and modifying existing contents is possible. 6. a+ : - searches for the file. If it exists, it is loaded in memory and the pointer is set to the last character in the file. If the file does not exist, a new file is created. Returns NULL if unable to open the file. Reading existing contents, appending new contents is possible. Cannot modify existing contents. Multiple files can be opened and used at a given time. Closing a File: When we finish all the operations on a file, the file must be closed. This ensures that the information associated with the file is removed from the buffer and all the links to the file are broken. We can prevent the misuse of the file, and we need to open the same file in different mode. Syntax: fclose ( filepointer ); This function will close the file associated with the FILE pointer filepointer. Closing a file deactivate the file and the file is no longer accessible.

Ashwath Computers, Solapur

Page - 35

C Programming Reference Notes

Unformatted File Input/ Output functions: 1. fgetc and fputc functions: - The fgetc() and fputc() functions are used to handle single character at a time. fgetc() is used to read a character from a file and fputc() is used to write a character to a file. Syntax: fgetc(fp); fputc(ch,fp); Reading of the file should be stopped when the EOF (end of file) is encountered. 2. getw and putw functions: - getw and putw are similar to fgetc and fputc. They are used to read integer values. Can be used when we are dealing with integer data. Syntax: putw(integer, fp); to write integer to a file. getw(fp); to read an integer from a file.

3.String I/O files (fgets and fputs): - To read a string from a file use fgets() function and to write string to a file use fputs() function. Syntax: fgets(str); fputs(str, fp); to read string from a file. to write string to a file.

Formatted Disk I/O Functions: We had seen functions to write and read data of particular format, but if we need to write data to a file in different format then we can make use of fscanf() and fprintf() function. fprintf() and fscanf() re identical to printf and scanf except they work on files. Syntax: fprintf( fp, format specifier, variable list); Here fp is the file pointer associated with the file to which we are writing. Format specifier indicates which format of data is written and variable list could include variables, constants of different data type. fscanf( fp, format specifier, variable list); Here fp is the file pointer associated with the file to which we are reading. Format specifier indicates which format of data is read and variable list could include variables, constants of different data type. We can make use of structures to write and read the related data into file. To do so, we can make use of the formatted functions.

Reading and Writing data in binary mode: When we deal with files the data is written or read in character form. For e.g. if an integer value 3421 is to be stored in file requires 4 bytes. Originally in memory it requires 2 bytes, but as each digit is considered as a single character it takes 4 bytes. To avoid this we can make use of reading and writing in binary mode where the data is read or write in its original format. To do so we have to specify the file mode as binary b. Functions to read and write in binary form: Syntax: fread(&variable, sizeof (variable), 1, filepointer); eg:-

Ashwath Computers, Solapur

Page - 36

C Programming Reference Notes

struct Emp e; fread(&e, sizeof(e), 1, fp);

fwrite(&variable, sizeof (variable), 1, filepointer); eg:struct Emp e; fwrite(&e, sizeof(e), 1, fp);

Random File Access: Until now we had studied to access data sequentially for reading and writing. However, in real life there could be a situation where we need to access the data randomly. To do so, C provides functions as fseek, ftell and rewind. 1. ftell: - This function takes a file pointer as its argument and returns a long integer value which corresponds to the current position in the file. Syntax: n = ftell(fp); Where n is a long integer. n gives the relative offset from the current position (in bytes), which tells that n bytes have been read or written so far. 2. rewind: - rewind takes the file pointer as its argument and resets the position to the start of the file. eg: - rewind (fp); 3. fseek: - It is a function which is used to move the file position to the required location. Syntax: fseek (filepointer, offset, position); Where filepointer is a pointer to the file. Offset is the value to type long and position is an integer. The offset specifies the number of bytes to be moved from the location specified in position. Position can take one of the following three values: Position Value 0 1 2 Beginning of file Current position End of file

If the offset is positive, the position moved forward, if it is negative, the position is moved backward. E.g. fseek(fp, m, 1); Will move the position forward by m bytes from current position fseek(fp, -m, 2); Will move the position backward by m bytes from end of file. fseek(fp, m, 0); Will move forward by m bytes from beginning of file. If the operation is successful fseek returns a zero. If we try to re4ad beyond the limits of file, fseek returns a value 1 and an error occurs.

Ashwath Computers, Solapur

Page - 37

C Programming Reference Notes

Macros: A C preprocessor is nothing but a program that processes our source program before passing it to the compiler. These are placed in the source program before main(). Each of the preprocessor directives begins with the # symbol. There is no semicolon at the end. Macro substitution describes the preprocessor. Replacement of an identifier in a program by a predefined string that comprises of one or more tokens is known as macro substitution. The #define statement is used to define macros. Syntax: #define identifier string When this statement is placed in the program at the beginning, every occurrence of the identifier is replaced in the source code by the preprocessor with string. The identifier is generally called the macro template and their corresponding string is the macro expansion. The macro is commonly used to define constants. E.g.: - #define PI 3.14 A macro definition can include expression as well: E.g.: #define VAL #define Area 10*10 L*B

We can also make use of #define operators as follows: #define OR #define MIN #define IN Macros with arguments: We had seen simple macros. It is possible to have arguments in macros. Syntax: #define identifier(arg1,arg2,.argn) string || if(x<y) printf(Enter number:);

There should not be space between the identifier and left parenthesis. The arg1, arg2 are the formal arguments. Macros with arguments are called as macro call. Eg: #define SQ(a) If we use a statement: Square = SQ (len); The preprocessor will expand this statement as: Square = (len*len) Nesting of Macros: It is possible to use macro definition in another macro definition. Eg: #define Square(a) ((a) * (a)) a*a

#define Cube(a) (Square(a) *(a)) The macro Cube will be expanded as: -

Ashwath Computers, Solapur

Page - 38

C Programming Reference Notes

((a) * (a) * (a))

Macros Vs Functions: We have seen the use of macros and functions. Thus, macros and functions work much similarly. However the main difference is that in a macro call the preprocessor simply replaces the macro template with its corresponding expansion whereas in a function call the control of the program is actually passed to the function along with the associated parameters. Macros make programs run faster, but they increase the program size. Functions make programs smaller and compact. If we are making use of macro several times in program, every time the macro expansion will be placed in the source code and the program size will increase. On the other hand, incase of a function, there may be numerous calls to a function but the function would take the same amount of space in the program.

Ashwath Computers, Solapur

Page - 39

You might also like