Unit-4 (Scope Rules and Arrays).pptx for

pattinsonhenry524 11 views 52 slides Apr 25, 2024
Slide 1
Slide 1 of 52
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52

About This Presentation

for my all domboooos


Slide Content

Unit-4: Scope Rules and Arrays

Storage Classes are used to describe the features of a variable/function. These features basically include the scope, visibility, and lifetime which help us to trace the existence of a particular variable during the runtime of a program. Use of Storage Class in C Storage class & type are two of the attributes that a variable in a C program will have. The storage class, which controls the variable's lifetime, visibility, and scope, and type, which refers to the data type of any particular variable, are both used in this context. Storage class in C

In C language, each variable has a storage class which decides the following things: scope i.e where the value of the variable would be available inside a program. default initial value i.e if we do not explicitly initialize that variable, what will be its default initial value. lifetime of that variable i.e for how long will that variable exist. Types of Storage Class in C Automatic storage class / variable Register storage class / variable Static storage class / variable External storage class / variable Storage class in C

Storage class in C Storage Classes Names of class Storage Place Default Value Scope Lifetime auto Automatic RAM Garbage Value Local Within function register Register CPU Register Garbage Value Local Within function static Static RAM Zero Local Till the end of the main program, Retains value between multiple functions call extern External RAM Zero Global Till the end of the main program Maybe declared anywhere in the program

Automatic variables are allocated memory automatically at runtime. The visibility and scope of the automatic variables is limited to the block in which they are defined. The automatic variables are initialized to garbage by default. Auto storage class variables are created and destroyed automatically. The keyword used for defining automatic variables is auto . Every local variable is automatic in C by default. Syntax: auto_data type_name of variable; auto int a; Automatic Storage Class in C

Automatic Storage Class in C

Automatic Storage Class in C Example:

Automatic Storage Class in C

Automatic Storage Class in C

This storage class declares register variables that have the same functionality as that of the auto variables. The variables defined as the register is allocated the memory into the CPU registers depending upon the size of the memory remaining in the CPU. We can not dereference the register variables, i.e., we can not use &operator for the register variable. The access time of the register variables is faster than the automatic variables. The initial default value of the register local variables is garbage . The register keyword is used for the variable which should be stored in the CPU register. However, it is compilers choice whether or not; the variables can be stored in the register. Syntax: register_data type_name of variable; register int a; Register Storage Class in C

Register Storage Class in C

Register Storage Class in C Note: Program illustrates that, the register variable is declared & initialized inside the main function.

The variables defined as static specifier can hold their value between the multiple function calls. Static local variables are visible only to the function or the block in which they are defined. A same static variable can be declared many times but can be assigned at only one time. Default initial value of the static integral variable is ‘0’ otherwise null. The visibility of the static global variable is limited to the file in which it has declared. The keyword used to define static variable is static . Syntax: static_data type_name of variable; static int a; Static Storage Class in C

Static Storage Class in C

Static Storage Class in C

Static Storage Class in C

Extern stands for external storage class. Extern storage class is used when we have global functions or variables which are shared between two or more files . Keyword extern is used to declaring a global variable or function in another file to provide the reference of variable or function which have been already defined in the original file. The variables defined using an extern keyword are called as global variables. These variables are accessible throughout the program. Notice that the extern variable cannot be initialized it has already been defined in the original file. Syntax: extern_data type_name of variable; extern int a; External Storage Class in C

External Storage Class in C Output

Arrays in C An array is defined as the collection of similar type of data items stored at contiguous memory locations. Arrays are the derived data type in which can store the primitive type of data such as int, char, double, float, etc. It also has the capability to store the collection of derived data types , such as pointers, structure, etc. The array is the simplest data structure where each data element can be randomly accessed by using its index number.

Types of Array in C There are two types of arrays based on the number of dimensions it has. They are as follows: One Dimensional (1D) Array Multidimensional Array 1. One Dimensional Array The One-dimensional arrays, also known as 1-D arrays in C are those arrays that have only one dimension. Syntax of 1D Array in C

Array in C (1D Array) Declaration of One-Dimensional Array In C, we have to declare the array like any other variable before using it. We can declare an array by specifying its name, the type of its elements, and the size of its dimensions . When we declare an array in C, the compiler allocates the memory block of the specified size to the array name. Syntax of Array Declaration: Ex . int marks[5];

Initialization of One-Dimensional Array Initialization in C is the process to assign some initial value to the variable. When the array is declared or allocated memory, the elements of the array contain some garbage value. So, we need to initialize the array to some meaningful value. There are multiple ways in which we can initialize an array in C. 1. Array Initialization with Declaration: The simplest way to initialize an array is by using the index of each element. We can initialize each element of the array by using the index. Array in C (1D Array)

Array in C (1D Array) 2. Array Initialization with Declaration without Size: If we initialize an array using an initializer list, we can skip declaring the size of the array as the compiler can automatically deduce the size of the array in these cases. The size of the above arrays is 5 which is automatically deduced by the compiler. 3. Array Initialization after Declaration: (Using Loops) We initialize the array after the declaration by assigning the initial value to each element individually. We can use for loop, while loop, or do-while loop to assign the value to each element of the array.

Array in C (1D Array) Example of Array Initialization (One Dimensional)

Array in C (1D Array) Accessing Elements of One-Dimensional Array We can access any element of an array in C using the array subscript operator [ ] and the index value i of the element. One thing to note is that the indexing in the array always starts with 0, i.e., the first element is at index and the last element is at N – 1 where N is the number of elements in the array.

Array in C (1D Array)

Array in C (1D Array)

Array in C (1D Array)

Array in C (1D Array)

Array in C (1D Array) Updating Elements of One-Dimensional Array We can update the value of an element at the given index i in a similar way to accessing an element by using the array subscript operator [ ] and assignment operator = . Traversal of One-Dimensional Array Traversal is the process in which we visit every element of the data structure. For C array traversal, we use loops to iterate through each element of the array.

Array in C (1D Array) The following program demonstrates how to use an array in the C language:

Array in C (2D Array) 2. Multidimensional Array Multi-dimensional Arrays in C are those arrays that have more than one dimension. Some of the popular multidimensional arrays are 2D arrays and 3D arrays. Two-Dimensional Array A Two-Dimensional array or 2D array in C is an array that has exactly two dimensions. They can be visualized in the form of rows and columns organized in a two-dimensional plane. Syntax of 2D Array in C Here, size1: Size of the first dimension. size2: Size of the second dimension.

Array in C (2D Array) Declaration of Two-Dimensional Array The basic form of declaring a 2D array with x rows and y columns in C is shown below. Syntax of Array Declaration: where, data_type: Type of data to be stored in each element. array_name: name of the array x: Number of rows. y: Number of columns. We can declare a two-dimensional integer array say ‘x’ with 10 rows and 20 columns as:

Initialization of Two-Dimensional Array The various ways in which a 2D array can be initialized are as follows: Using Initializer List Using Loops 1. Array Initialization using Initializer List: First Method: The above array has 3 rows and 4 columns. The elements will be filled in the array in order: the first 4 elements from the left will be filled in the first row , the next 4 elements in the second row , and so on. Array in C (2D Array)

1. Array Initialization using Initializer List: (Cont…) Second Method (Better): This type of initialization makes use of nested braces. Each set of inner braces represents one row. In the above example, there is a total of three rows so there are three sets of inner braces. Array in C (2D Array) 2. Initialization of 2D array using Loops: We can use any C loop to initialize each member of a 2D array one by one as shown in the side example. Note: This method is useful when the values of each element have some sequential relation.

Array in C (2D Array) Accessing Elements of Two-Dimensional Array Elements in 2D arrays are accessed using row indexes and column indexes. Each element in a 2D array can be referred to by: Syntax: where, i: The row index. j: The column index. Example: 

Array in C (2D Array)

Array in C (2D Array)

Strings in C A String is a sequence of characters terminated with a null character ‘\0’. The String is stored as an array of characters. The difference between a character array and a string is that the string is terminated with a unique character ‘\0’.

Strings in C String Declaration Declaring a string in C is as simple as declaring a one-dimensional array. Below is the basic syntax for declaring a string. Syntax In the above syntax string_name is any name given to the string variable and size is used to define the length of the string , i.e the number of characters strings will store. There is an extra terminating character which is the Null character (‘\0’) used to indicate the termination of a string that differs strings from normal character arrays.

Strings in C String Initialization A string in C can be initialized in different ways. We will explain this with the help of an example. Below are the examples to declare a string with the name str and initialize it with “HelloWorld”. We can initialize string in 4 different ways which are as follows: 1. Assigning a String Literal without Size: String literals can be assigned without size. Here, the name of the string str acts as a pointer because it is an array. char str[ ] = “HelloWorld”;

Strings in C String Initialization (Cont…) 2. Assigning a String Literal with a Predefined Size: String literals can be assigned with a predefined size. But we should always account for one extra space which will be assigned to the null character . If we want to store a string of size n then we should always declare a string with a size equal to or greater than n+1. char str[11] = “HelloWorld”; 3. Assigning Character by Character with Size: We can also assign a string character by character . But we should remember to set the end character as ‘\0’ which is a null character. char str[11] = { 'H','e','l','l','o','W','o','r','l','d','\0'};

Strings in C String Initialization (Cont…) 4. Assigning Character by Character without size: We can assign character by character without size with the NULL character at the end. The size of the string is determined by the compiler automatically. char str[ ] = { 'H','e','l','l','o','W','o','r','l','d','\0'}; Note: When a Sequence of characters enclosed in the double quotation marks is encountered by the compiler, a null character ‘\0’ is appended at the end of the string by default.

Strings in C

Strings in C

Array of Strings in C In C programming String is a 1-D array of characters and is defined as an array of characters. But an array of strings in C is a two-dimensional array of character types. Each String is terminated with a null character (\0). It is an application of a 2d array. Syntax: char variable_name[r] = {list of string}; Here, var_name is the name of the variable. r is the maximum number of string values that can be stored in a string array. c is the maximum number of character values that can be stored in each string array.

Array of Strings in C

String functions in C String Length: strlen( ) function The strlen( ) function in C calculates the length of a given string . The strlen( ) function is defined in string.h header file. It doesn’t count the null character ‘\0’.

String functions in C Copy String: strcpy( ) The strcpy(destination, source) function copies the source string in destination.

String functions in C Concatenation: strcat( ) The strcat(first_string, second_string) function concatenates two strings and result is returned to first_string.

String functions in C Compare String: strcmp( ) The strcmp(first_string, second_string) function compares two string and returns if both strings are equal.

String functions in C Reverse String: strrev( ) The strrev(string) function returns reverse of the given string .