C Language

 



How C developed?

  • B Language(Basic Combined Programming Language [BCPL]) was modified call C language by Dennis Ritchie and was implemented AT bell laboratory in year 1972.

 

Why C is very popular

  • Reliable
  • Simple
  • Easy

 

Application

  • System OS
  • Mobile Programs
  • Game Programs

C Features

  • High level structured language (using functions)
  • Fast and efficient
  • Build in functions
  • Programming task became simple
  • It has a 32 keywords
  • Highly Portable(Any kind of machine can run)
  • Case sensitivity


Array

  • It is very difficult to define more variables name when use more data.
  • So can create a array to solve this difficult(single variable name and more data).
  • If the index of the array size is exceeded, the program will crash. But the modern compilers will take care of this kind of errors.
  • We cannot initialize the size of array dynamically. Constant expression is required.
  • Rules
    • only store the same data types in array structures
    • Arrays are always passed by reference
  • What is Base address of array??
    •  Eg :  a[10] = {0,1,2,3,4,5,6,7,8,9}     // The starting address of the array is called as the base address of the array.
  • When we pass an array as a function argument, the base address of the array will be passed.
  • Array three types
    • One Dimensional Array
    • Two Dimensional Array and
    •  Multi Dimensional Array

 One Dimensional Array

  • When declare one index value is called One dimensional array
  • Syntax:  data_type array_name [ size];
    • data_type  – What kind of array data type
    • array_name  – name of the array
    • size  – array size
  • Syntax Declaration:
    • int a[100];  //Data type- int (2Byte), array_name – ‘a’, size – 100, Memory Occupies- 200 byte (100×2).
    • float s [50];  //Data type- float(4byte), array_name – ‘s’, size – 50, Memory Occupies- 200 byte (50×4).
    • char name[15];  // Data type- char(1byte), array_name – “name”, size – 15, Memory Occupies- 15 byte (15×1).
  • Declaration :  int a[100];
  • Reading : for(i=0; i<100; i++) scanf("%d", &a[i]);
  • Writing :  for(i=0; i<100; i++) printf("%d", a[i]);
  • Memory Representation :
  • Array initialization Syntax :  data_type array_name [size] = {list_of_values}; 
    • Examples
      • static int age [3] = {10,20,30};  //its means – a[0]=10; a[1]=20; a[3]=30;
      • static float salary[] = {1000,2200,3300};  //salary[0]=1000; salary[2]=3300;
      • static char a[10] = "ArunEworld";  //its means – a[0]=’A’; a[1]=’r’; like… a[9]=’d’;
  • Example Programs

Two Dimensional Array

  • When declare two index value is called two dimensional array
  • Two dimensional arrays can consider as a table. size_1 is row size and size_2 is column size.
  • Syntax:  data_type array_name [ size_1] [size_2];
    • data_type  – What kind of array data type
    • array_name  – name of the array
    • size_1 , size_2 – two dimensional array size
  • Syntax Declaration:
    • int a[100] [20];  //Data type- int (2Byte), array_name – ‘a’, size_1(row) – 100,size_2(column)-20, Memory Occupies- 200 byte (100×2).
  • Declaration :  int a[10][15];
  • Reading : for(i=0; i<10; i++) for(j=0; j<15; j++) scanf("%d", &a[i][j]); 
  • Writing : for(i=0; i<10; i++) for(j=0; j<15; j++) printf("%d", a[i][j]); 
  • Memory Representation :
  • Array initialization Syntax :  data_type array_name [size_1] [size_2] = {v1, v2,.....vn}; 
    • Examples
      • static int mark[3][2] = {60,70,80, 35,90,18};  //its means – a[0][0]=60; a[0][1]=70; a[0][3]=30; a[1][0]=35; a[1][1]=90; a[1][2]=18;
      • static mark[3][2] = {{50},{0},{35}; //a[0][0]=50; a[0][1]=50; a[0][3]=0; a[1][0]=0; a[1][1]=35; a[1][2]=35;
  • Example Programs
    • Two_dimensional_array_read_row_column.c
    • Two_dimensional_array_print_row_column.c
    • Matrix_transpose_using_two_dimensional_array.c
    • Diagonal_elements_using_two_dimensional_array.c
    • Sum_of_diagonal_elements_using_two_dimensional_array.c
    • Sum_of_all_matrix_elements_using_two_dimensional_array.c
    • Add_a_given_matrix_elements_using_two_dimensional_array.c
    • Multiply_matrix_elements_using_two_dimensional_array.c
    • Largest_and_smallest_elements_using_two_dimensional_array.c

Multi Dimensional Array

  • In C, can use 3 or more dimension called as multi dimensional array
  • Multi Dimension limit is based on the compiler
  • Syntax: data_type array_name [size-1][size_2]….[size_n]; 
    • data_type  – What kind of array data type
    • array_name  – name of the array
    • size  – array size
  • Example : 
    •   int a[10][10][10];  //Data type- int (2Byte), array_name – ‘a’, size – 2000(10x10x10x2), Memory Occupies- 2000bytes

 

Array disadvantage:

  • One disadvantage of using arrays to store data is that arrays are static structures and therefore cannot be easily extended or reduced to fit the data set.
  • Arrays are also expensive to maintain new insertions and deletions

Pointer

  • It’s a pointer variable which can hold the address of another pointer variable.
  • That two variables should be same data type.
  • It de-refers twice to point to the data held by the designated pointer variable.
  • //Eg:    int x = 5, *p=&x, **q=&p;     //Therefore ‘x’ can be accessed by **q.
  • Advantage
    • Use pointer to increase the speed of program execution
    • Reduce program complexity
    • To use pointer to access the inside function’s variable value.
    • To use pointer to transfer the data from outside or inside the functions.
  • Access the address of the variable syntax :  &variable_name
  • Declaring pointers syntax: datatype *var;  Example : int *a;
  • Initializing pointer syntax :  pointer_name =&variable_name;  Example : int *a; int x; a =&x;
  • Pointer expressions : +  for addition, -  for subtraction, ++  for incremanet, --  for decrement.

What is a NULL pointer?

  • A pointer pointing to nothing is called so.
  • NULL pointer is always initialized.
  • NULL pointer assignment :  char *q=0;  (or) char *q=(char*)0;  (or) char *p=NULL;
  • Example
    • What is (void*)0 ? //Representation of NULL pointer
  • NULLMacro is defined in locale.h , stddef.h , stdio.h , stdlib.h , string.h , time.h , and wchar.h

What is a dangling pointer?

  • A pointer initially holding valid address, but later the held address is released or freed.
  • Then such a pointer is called as dangling pointer.

Near pointer

  • Near pointer occupies 2 bytes in DOS

What is Far Pointer?

  • far pointer occupies 4 bytes in DOS

huge pointer

  • huge pointer occupies 4 bytes in DOS

What is the advantage of declaring void pointers?

  • When we do not know what type of the memory address the pointer variable is going to hold, then we declare a void pointer for such.

What is a pointer to a function?

  • A pointer holding the reference of the function is called pointer to a function.

What is pointer to pointer in C?

  • one pointer refers to the address of another pointer

What is Function Pointer?

  • syntax : int(*fun)(int, int, int);
  • A function pointer is a variable that stores the address of a function that can later be called through that function pointer.
  • This is useful because functions encapsulate behavior.

What is a pointer on pointer?

  • It’s a pointer variable which can hold the address of another pointer variable.
  • It de-refers twice to point to the data held by the designated pointer variable.
  • Eg: int x = 5, *p=&x, **q=&p;
    • Therefore x  can be accessed by **q .

Pointer and Array

  • int (*ptr)[10]; //  ptr is a pointer to an array of 10 integers.

Reference Video

 

 

 

 


Memory Allocation

  • Static Memory allocation
    • In case of static memory allocation, memory is allocated at compile time and memory can’t be increased while executing the program. It is used in array
  •  Dynamic memory allocation
    • In case of dynamic memory allocation, memory is allocated at run time and memory can be increased while executing the program. It is used in linked list.
    • The following functions are used in Dynamic memory allocation
      • malloc()    –    allocates single block of requested memory. It has garbage value initially.
      • calloc()     –    allocates multiple block of requested memory. It initially initializes all bytes to zero.
      • realloc()    –    reallocates the memory occupied by malloc()  or calloc()  functions.
      • free()         –    frees the dynamically allocated memory.

Difference b/w Static and dynamic memory allocation?


Structure ( struct  keyword)

  • Structure is the group of heterogeneous data structure which can store data of different data types or Structure is a collection of variables of different types under a single name
  • In the case of array, we can store the data of same data type but what if we have to save data of different data type in structure
  • Keyword struct  is used for creating a structure.
  • Note : Structure elements cannot be initialized
  • Can use single structure name for  more type of data in structure and union.
  • Storage area is not reserved by the compiler to structure definition.
  • Structure is a user defined data structure.
  • Syntax :
  • Rules
    • Use declare semicolon  };  at end of structure. (Don’t forget the semicolon in  the ending line.)
  • structure variable declaration
    • struct defined : (Ex: struct struct_name var_1, var_2;
    • struct define : (ex : struct struct_name { data_type member_1; data_type member_2;} var_1, var_2;
  • Referencing structure members :  struct_var-name . member_variable
  • Structure cannot be compared using ==
  • In Stucture bit cant set in float variable.
  • structure initializing : static struct structure_name var-name= {var_1, var_2,...var_n};

Example

In a school we need to store data like name, roll no, address and percentage. But we are able to store only one data type at a time. So, C programming provides structure data type which can store data of different types such as integer, float, character etc. In the case of above school, we can initialize structure as :

 

 Nested structure [structure with structure]

 

  • A structure containing an element of another structure as its member is referred so.

What is a self-referential structure?

  • A structure containing the same structure pointer variable as its element is called as self-referential structure.

When to user ->  (arrow) operator.

  • If the structure/union variable is a pointer variable, to access structure/union elements the arrow operator is used.

Array of structure :Syntax : struct structure_name var_name [size];


Union

  • Syntax :  union union_name {data_type_1 member_1; data_type_2 member_2; };
  • Declaration :  union union_name {data_type_1 member_1; data_type_2 member_2; }uv_1,uv_2;
  •  

What is the difference between union and structure?

  • The difference between structure and union is, … The amount of memory required to store a structure variable is the sum of the size of all the members.
  • On the other hand, in case of unions, the amount of memory required is always equal to that required by its largest member.

 


Enum

  • --  or ++  are con’t be done on enum value.