C Programming (Bca-203)
Total Page:16
File Type:pdf, Size:1020Kb
‘C’ PROGRAMMING BCA 203 SELF LEARNING MATERIAL DIRECTORATE OF DISTANCE EDUCATION SWAMI VIVEKANAND SUBHARTI UNIVERSITY MEERUT – 250 005, UTTAR PRADESH (INDIA) 1 SLM Module Developed By : Author: Reviewed by : Assessed by: Study Material Assessment Committee, as per the SVSU ordinance No. VI (2) Copyright © Gayatri Sales DISCLAIMER No part of this publication which is material protected by this copyright notice may be reproduced or transmitted or utilized or stored in any form or by any means now known or hereinafter invented, electronic, digital or mechanical, including photocopying, scanning, recording or by any information storage or retrieval system, without prior permission from the publisher. Information contained in this book has been published by Directorate of Distance Education and has been obtained by its authors from sources be lived to be reliable and are correct to the best of their knowledge. However, the publisher and its author shall in no event be liable for any errors, omissions or damages arising out of use of this information and specially disclaim and implied warranties or merchantability or fitness for any particular use. Published by: Gayatri Sales Typeset at: Micron Computers Printed at: Gayatri Sales, Meerut. 2 C PROGRAMMING UNIT-I Arrays Definition, declaration and initialization of one dimensional array; Accessing array elements; Displaying array elements; Sorting arrays; Arrays and function; Two- Dimensional array: Declaration and Initialization, Accessing and Displaying, Memory representation of array [Row Major, Column Major]; Multidimensional array UNIT-II Pointers -Definition and declaration, Initialization; Indirection operator, address of operator; pointer arithmetic; dynamic memory allocation; arrays and pointers; function and pointers UNIT-III Strings Definition, declaration and initialization of strings; standard library function: strlen(), strcpy(), strcat(), strcmp(); Implementation without using standard library functions Structures Definition and declaration; Variables initialization; Accessing fields and structure operations; Nested structures; Union: Definition and declaration; Differentiate between Union and structure UNIT-IV Introduction C Preprocessor Definition of Preprocessor; Macro substitution directives; File inclusion directives; Conditional compilation Bitwise Operators Bitwise operators; Shift operators; Masks; Bit field UNIT-V File handling Definition of Files, Opening modes of files; Standard function: fopen(), fclose(), feof(), fseek(), fewind();Using text files: fgetc(), fputc(), fscanf() ,Command line arguments 3 UNIT-I Arrays Definition Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A specific element in an array is accessed by an index. All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. Declaring Arrays To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows − type arrayName [ arraySize ]; This is called a single-dimensional array. The arraySize must be an integer constant greater than zero and type can be any valid C data type. For example, to declare a 10- element array called balance of type double, use this statement − double balance[10]; 4 Here balance is a variable array which is sufficient to hold up to 10 double numbers. Initializing Arrays You can initialize an array in C either one by one or using a single statement as follows − double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; The number of values between braces { } cannot be larger than the number of elements that we declare for the array between square brackets [ ]. If you omit the size of the array, an array just big enough to hold the initialization is created. Therefore, if you write − double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0}; You will create exactly the same array as you did in the previous example. Following is an example to assign a single element of the array − balance[4] = 50.0; The above statement assigns the 5th element in the array with a value of 50.0. All arrays have 0 as the index of their first element which is also called the base index and the last index of an array will be total size of the array minus 1. Shown below is the pictorial representation of the array we discussed above – Accessing Array Elements An element is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array. For example − double salary = balance[9]; 5 The above statement will take the 10th element from the array and assign the value to salary variable. The following example Shows how to use all the three above mentioned concepts viz. declaration, assignment, and accessing arrays − #include <stdio.h> int main () { int n[ 10 ]; /* n is an array of 10 integers */ int i,j; /* initialize elements of array n to 0 */ for ( i = 0; i < 10; i++ ) { n[ i ] = i + 100; /* set element at location i to i + 100 */ } /* output each array element's value */ for (j = 0; j < 10; j++ ) { printf("Element[%d] = %d\n", j, n[j] ); } return 0; } When the above code is compiled and executed, it produces the following result − Element[0] = 100 Element[1] = 101 Element[2] = 102 Element[3] = 103 Element[4] = 104 Element[5] = 105 Element[6] = 106 Element[7] = 107 Element[8] = 108 Element[9] = 109 Arrays in Detail Arrays are important to C and should need a lot more attention. The following important concepts related to array should be clear to a C programmer − 6 Sr.No. Concept & Description 1 Multi-dimensional arrays C supports multidimensional arrays. The simplest form of the multidimensional array is the two-dimensional array. 2 Passing arrays to functions You can pass to the function a pointer to an array by specifying the array's name without an index. 3 Return array from a function C allows a function to return an array. 4 Pointer to an array You can generate a pointer to the first element of an array by simply specifying the array name, without any index. Declaration and initialization of one dimensional array The variable allows us to store a single value at a time, what if we want to store roll no. of 100 students? For this task, we have to declare 100 variables, then assign values to each of them. What if there are 10000 students or more? As you can see declaring that many variables for a single entity (i.e student) is not a good idea. In a situation like these arrays provide a better way to store data. What is an Array? An array is a collection of one or more values of the same type. Each value is called an element of the array. The elements of the array share the same variable name but each element has its own unique index number (also known as a subscript). An array can be of any type, For example: int, float, char etc. If an array is of type int then it's elements must be of type int only. To store roll no. of 100 students, we have to declare an array of size 100 i.e roll_no[100]. Here size of the array is 100 , so it is capable of 7 storing 100 values. In C, index or subscript starts from 0, so roll_no[0] is the first element, roll_no[1] is the second element and so on. Note that the last element of the array will be at roll_no[99] not at roll_no[100] because the index starts at 0. Arrays can be single or multidimensional. The number of subscript or index determines the dimensions of the array. An array of one dimension is known as a one-dimensional array or 1-D array, while an array of two dimensions is known as a two-dimensional array or 2-D array. Let's start with a one-dimensional array. One-dimensional array Conceptually you can think of a one-dimensional array as a row, where elements are stored one after another. 8 Syntax: datatype array_name[size]; datatype: It denotes the type of the elements in the array. array_name: Name of the array. It must be a valid identifier. size: Number of elements an array can hold. here are some example of array declarations: 1 int num[100]; 2 float temp[20]; 3 char ch[50]; num is an array of type int, which can only store 100 elements of type int. temp is an array of type float, which can only store 20 elements of type float. ch is an array of type char, which can only store 50 elements of type char. Note: When an array is declared it contains garbage values. The individual elements in the array: 1 num[0], num[1], num[2], ....., num[99] 2 temp[0], temp[1], temp[2], ....., temp[19] 3 ch[0], ch[1], ch[2], ....., ch[49] We can also use variables and symbolic constants to specify the size of the array. 1 #define SIZE 10 2 9 3 int main() 4 { 5 int size = 10; 6 7 int my_arr1[SIZE]; // ok 8 int my_arr2[size]; // not allowed until C99 9 // ... 10 } Note: Until C99 standard, we were not allowed to use variables to specify the size of the array. If you are using a compiler which supports C99 standard, the above code would compile successfully.