C PROGRAMMING:
1.FEATURES OF C:
The main features of C language are given below:
o Simple: C is a simple language because it follows the structured approach, i.e.,
a program is broken into parts
o Portable: C is highly portable means that once the program is written can be
run on any machine with little or no modifications.
o Mid Level: C is a mid-level programming language as it combines the low- level
language with the features of the high-level language.
o Structured: C is a structured language as the C program is broken into parts.
o Fast Speed: C language is very fast as it uses a powerful set of data types and
operators.
o Memory Management: C provides an inbuilt memory function that saves the
memory and improves the efficiency of our program.
o Extensible: C is an extensible language as it can adopt new features in the
future.
2.LOCAL VARIABLE AND GLOBAL VARIABLES:
Variables in any programming language have a crucial role. Variables are classified into Global
variables and Local variables based on their scope. The main difference between Global and
local variables is that global variables can be accessed globally in the entire program, whereas
local variables can be accessed only within the function or block in which they are defined
Global Variable Local Variable
Global variables are declared outside all the Local Variables are declared within a function block.
function blocks.
The scope remains throughout the program. The scope is limited and remains within the function only in
which they are declared.
Any change in global variable affects the whole Any change in the local variable does not affect other
program, wherever it is being used. functions of the program.
A global variable exists in the program for the A local variable is created when the function is executed, and
entire time the program is executed. once the execution is finished, the variable is destroyed.
It can be accessed throughout the program by It can only be accessed by the function statements in which it
all the functions present in the program. is declared and not by the other functions.
If the global variable is not initialized, it takes If the local variable is not initialized, it takes the garbage value
zero by default. by default.
Global variables are stored in the data segment Local variables are stored in a stack in memory.
of memory.
We cannot declare many variables with the We can declare various variables with the same name but in
same name. other functions.
3.STATIC VARIABLE:
A variable that is declared with the static keyword is called static variable.
It retains its value between multiple function calls.
1. void function1(){
2. int x=10;//local variable
3. static int y=10;//static variable
4. x=x+1;
5. y=y+1;
6. printf("%d,%d",x,y);
7. }
If you call this function many times, the local variable will print the same value for each
function call, e.g, 11,11,11 and so on. But the static variable will print the incremented
value in each function call, e.g. 11, 12, 13 and so on.
4.CALL BY VALUE &CALL BY REFERENCE:
No. Call by value Call by reference
1 A copy of the value is passed into the function An address of value is passed into the function
2 Changes made inside the function is limited to the Changes made inside the function validate outside
function only. The values of the actual parameters of the function also. The values of the actual
do not change by changing the formal parameters. parameters do change by changing the formal
parameters.
3 Actual and formal arguments are created at the Actual and formal arguments are created at the
different memory location same memory location
5.RECURSION:
Recursion is the process which comes into existence when a function calls a copy of itself to
work on a smaller problem. Any function which calls itself is called recursive function, and such
function calls are called recursive calls. Recursion involves several numbers of recursive calls.
However, it is important to impose a termination condition of recursion.
Recursion cannot be applied to all the problem, but it is more useful for the tasks that can be
defined in terms of similar subtasks. For Example, recursion may be applied to sorting,
searching, and traversal problems.
In the following example, recursion is used to calculate the factorial of a number
1. #include <stdio.h>
2. int fact (int);
3. int main()
4. {
5. int n,f;
6. printf("Enter the number whose factorial you want to calculate?");
7. scanf("%d",&n);
8. f = fact(n);
9. printf("factorial = %d",f);
10. }
11. int fact(int n)
12. {
13. if (n==0)
14. {
15. return 0;
16. }
17. else if ( n == 1)
18. {
19. return 1;
20. }
21. else
22. {
23. return n*fact(n-1);
24. }
25. }
Output
Enter the number whose factorial you want to calculate?5
factorial = 120
6.POINTERS:
The pointer in C language is a variable which stores the address of another variable.
This variable can be of type int, char, array, function, or any other pointer. The size of
the pointer depends on the architecture. However, in 32-bit architecture the size of a
pointer is 2 byte.
Consider the following example to define a pointer which stores the address of an
integer.
1. int n = 10;
2. int* p = &n; // Variable p of type pointer is pointing to the address of the vari
able n of type integer.
Declaring a pointer
The pointer in c language can be declared using * (asterisk symbol). It is also known as
indirection pointer used to dereference a pointer.
1. int *a;//pointer to int
2. char *c;//pointer to char
6.NULL POINTER &DANGLING POINTER:
A pointer that is not assigned any value but NULL is known as the NULL pointer. If you don't
have any address to be specified in the pointer at the time of declaration, you can assign NULL
value. It will provide a better approach
int *p=NULL;
If a pointer is pointing any memory location, but meanwhile another pointer deletes
the memory occupied by the first pointer while the first pointer still points to that
memory location, the first pointer will be known as a dangling pointer. This problem is
known as a dangling pointer problem.
Dangling pointer arises when an object is deleted without modifying the value of the
pointer. The pointer points to the deallocated memory.
1. #include<stdio.h>
2. void main()
3. {
4. int *ptr = malloc(constant value); //allocating a memory space.
5. free(ptr); //ptr becomes a dangling pointer.
6. }
In the above example, initially memory is allocated to the pointer variable ptr,
and then the memory is deallocated from the pointer variable. Now, pointer
variable, i.e., ptr becomes a dangling pointer.
How to overcome the problem of a dangling pointer
The problem of a dangling pointer can be overcome by assigning a NULL value to the
dangling pointer. Let's understand this through an example:
1. #include<stdio.h>
2. void main()
3. {
4. int *ptr = malloc(constant value); //allocating a memory space.
5. free(ptr); //ptr becomes a dangling pointer.
6. ptr=NULL; //Now, ptr is no longer a dangling pointer.
7. }
7.ARRAY:
An Array is a group of similar types of elements. It has a contiguous memory location.
It makes the code optimized, easy to traverse and easy to sort. The size and type of
arrays cannot be changed after its declaration.
Arrays are of two types:
o One-dimensional array: One-dimensional array is an array that stores the
elements one after the another.
Syntax:
1. data_type array_name[size];
Multidimensional array: Multidimensional array is an array that contains more than
one array.
Syntax:
1. data_type array_name[size];
8.POINTER TO POINTER IN C:
In case of a pointer to pointer concept, one pointer refers to the address of another pointer.
The pointer to pointer is a chain of pointers. Generally, the pointer contains the address of a
variable. The pointer to pointer contains the address of a first pointer. Let's understand this
concept through an example:
1. #include <stdio.h>
2. int main()
3. {
4. int a=10;
5. int *ptr,**pptr; // *ptr is a pointer and **pptr is a double pointer.
6. ptr=&a;
7. pptr=&ptr;
8. printf("value of a is:%d",a);
9. printf("\n");
10. printf("value of *ptr is : %d",*ptr);
11. printf("\n");
12. printf("value of **pptr is : %d",**pptr);
13. return 0;
14. }
9.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 the array.
o The lifetime of a variable in static memory is the lifetime of a program.
o The static memory is allocated using static keyword.
o The static memory is implemented using stacks or heap.
o The pointer is required to access the variable present in the static memory.
o The static memory is faster than dynamic memory.
o In static memory, more memory space is required to store the variable.
o For example:
o int a[10];
10.DYNAMIC MEMORY ALLOCATION:
o In case of dynamic memory allocation, memory is allocated at runtime and
memory can be increased while executing the program. It is used in the linked
list.
o The malloc() or calloc() function is required to allocate the memory at the
runtime.
o An allocation or deallocation of memory is done at the execution time of a
program.
o No dynamic pointers are required to access the memory.
o The dynamic memory is implemented using data segments.
o Less memory space is required to store the variable.
o For example
o int *p= malloc(sizeof(int)*10);
11.CALLOC &MALLOC
calloc() malloc()
Description The malloc() function allocates a single The calloc() function allocates multiple blocks
block of requested memory. of requested memory.
Initialization It initializes the content of the memory to It does not initialize the content of memory, so
zero. it carries the garbage value.
Number of It consists of two arguments. It consists of only one argument.
arguments
Return value It returns a pointer pointing to the It returns a pointer pointing to the allocated
allocated memory. memory.
12.STRUCTURE & UNIONS:
o The structure is a user-defined data type that allows storing multiple types of
data in a single unit. It occupies the sum of the memory of all members.
o The structure members can be accessed only through structure variables.
o Structure variables accessing the same structure but the memory allocated for
each variable will be different.
Syntax of structure
1. struct structure_name
2. {
3. Member_variable1;
4. Member_variable2
5. .
6. .
7. }[structure variables];
The union is a user-defined data type that allows storing multiple types of data in a
single unit. However, it doesn't occupy the sum of the memory of all members. It holds
the memory of the largest member only.
In union, we can access only one variable at a time as it allocates one common space
for all the members of a union.
Syntax of union
1. union union_name
2. {
3. Member_variable1;
4. Member_variable2;
5. .
6. .
7. Member_variable n;
8. }[union variables];
13.AUTO KEYWORD:
In C, every local variable of a function is known as an automatic (auto) variable. Variables which
are declared inside the function block are known as a local variable. The local variables are also
known as an auto variable. It is optional to use an auto keyword before the data type of a
variable. If no value is stored in the local variable, then it consists of a garbage value.
14.SPRINTF FUNCTION:
The sprintf() stands for "string print." The sprintf() function does not print the output
on the console screen. It transfers the data to the buffer. It returns the total number of
characters present in the string.
Syntax
1. int sprintf ( char * str, const char * format, ... );
Let's see a simple example
1. #include<stdio.h>
2. int main()
3. {
4. char a[20];
5. int n=sprintf(a,"javaToint");
6. printf("value of n is %d",n);
7. return 0;}
15.TOKEN, GETCHE():
The Token is an identifier. It can be constant, keyword, string literal, etc. A token is the
smallest individual unit in a program. C has the following tokens:
1. Identifiers: Identifiers refer to the name of the variables.
2. Keywords: Keywords are the predefined words that are explained by the
compiler.
3. Constants: Constants are the fixed values that cannot be changed during the
execution of a program.
4. Operators: An operator is a symbol that performs the particular operation.
5. Special characters: All the characters except alphabets and digits are treated as
specia characters.
The getche() function reads a single character from the keyword, but data is
displayed on the output screen. Press Alt+f5 to see the entered character.
16.ANSI, GETCH():
The ANSI stands for " American National Standard Institute." It is an organization
that maintains the broad range of disciplines including photographic film,
computer languages, data encoding, mechanical parts, safety and more.
The getch() function reads a single character from the keyboard. It doesn't use
any buffer, so entered data will not be displayed on the output screen.
17.TYPE CASTING:
The typecasting is a process of converting one data type into another is known as
typecasting. If we want to store the floating type value to an int type, then we will
convert the data type into another data type explicitly.
Syntax
1. (type_name) expression;