language is considered as the mother language of all the modern languages because most of the compilers, JVMs, Kernals etc. are written in C language and most of languages follows c syntax e.g. C++, Java etc.
It provides the core concepts like array, functions, file handling etc. that is being used in many languages like C++, java, C# etc.
C as a system programming language
A system programming language is used to create system softwares. C language is a system programming language because it can be used to do low level programming (e.g. driver and kernel). It is generally used to create hardware devices, OS, drivers, kernels etc. For example, linux kernel is written in C.
It can?t be used in internet programming like java, .net, php etc.
C as a procedural language
A procedure is known as function, method, routine, subroutine etc. A procedural language specifies a series of steps or procedures for the program to solve the problem.
A procedural language breaks the program into functions, data structures etc.
C is a procedural language. In C, variables and function prototypes must be declared before being used.
C as a structured programming language
A structured programming language is a subset of procedural language. Structure means to break a program into parts or blocks so that it may be easy to understand.
In C language, we break the program into parts using functions. It makes the program easier to understand and modify.
C as a mid-level programming language
C is considered as a middle level language because it supports the feature of both low-level and high level language. C language program is converted into assembly code, supports pointer arithmetic (low level), but it is machine independent (feature of high level).
Low level language is specific to one machine i.e. machine dependent. It is machine dependent, fast to run. But it is not easy to understand.
High Level language is not specific to one machine i.e. machine independent. It is easy to understand.
C is the widely used language. It provides a lot of features that are given below.
Simple
Machine Independent or Portable
Mid-level programming language
structured programming language
Rich Library
Memory Management
Fast Speed
Pointers
Recursion
Extensible
1) Simple
C is a simple language in the sense that it provides structured approach (to break the problem into parts), rich set of library functions, data types etc.
2) Machine Independent or Portable
Unlike assembly language, c programs can be executed in many machines with little bit or no change. But it is not platform-independent.
3) Mid-level prorgramming language
C is also used to do low level programming. It is used to develop system applications such as kernel, driver etc. It also supports the feature of high level language. That is why it is known as mid-level language.
4) Structured prorgramming language
C is a structured programming language in the sense that we can break the program into parts using functions. So, it is easy to understand and modify.
5) Rich Library
C provides a lot of inbuilt functions that makes the development fast.
6) Memory Management
It supports the feature of dynamic memory allocation. In C language, we can free the allocated memory at any time by calling the free() function.
7) Speed
The compilation and execution time of C language is fast.
8) Pointer
C provides the feature of pointers. We can directly interact with the memory by using the pointers. We can use pointers for memory, structures, functions, array etc.
9) Recursion
In c, we can call the function within the function. It provides code reusability for every function.
10) Extensible
C language is extensible because it can easily adopt new features.
History of C language is interesting to know. Here we are going to discuss brief history of c language.
C programming language was developed in 1972 by Dennis Ritchie at bell laboratories of AT&T (American Telephone & Telegraph), located in U.S.A.
Dennis Ritchie is known as the founder of c language.
It was developed to overcome the problems of previous languages such as B, BCPL etc.
Initially, C language was developed to be used in UNIX operating system. It inherits many features of previous languages such as B and BCPL.
Let's see the programming languages that were developed before C language.
A variable is a name of memory location. It is used to store data. Its value can be changed and it can be reused many times.
It is a way to represent memory location through symbol so that it can be easily identified.
Let's see the syntax to declare a variable:
type variable_list;
The example of declaring variable is given below:
int a;
float b;
char c;
Here, a, b, c are variables and int,float,char are data types.
We can also provide values while declaring the variables as given below:
int a=10,b=20;//declaring 2 variable of integer type
float f=20.8;
char c='A';
There are many types of variables in c:
local variable
global variable
static variable
automatic variable
external variable
Local Variable
A variable that is declared inside the function or block is called local variable.
It must be declared at the start of the block.
void function1(){
int x=10;//local variable
}
You must have to initialize the local variable before it is used.
Global Variable
A variable that is declared outside the function or block is called
global variable. Any function can change the value of the global
variable. It is available to all the functions.
It must be declared at the start of the block.
int value=20;//global variable
void function1(){
int x=10;//local variable
}
Static Variable
A variable that is declared with static keyword is called static variable.
It retains its value between multiple function calls.
void function1(){
int x=10;//local variable
staticint y=10;//static variable
x=x+1;
y=y+1;
printf("%d,%d",x,y);
}
If you call this function many times, local variable will print the same value for each function call e.g, 11,11,11 and so on. But static variable will print the incremented value in each function call e.g. 11, 12, 13 and so on.
Automatic Variable
All variables in C that is declared inside the block, are automatic
variables by default. By we can explicitly declare automatic variable
using auto keyword.
void main(){
int x=10;//local variable (also automatic)
auto int y=20;//automatic variable
}
External Variable
We can share a variable in multiple C source files by using external variable. To declare a external variable, you need to use extern keyword.
myfile.h
externint x=10;//external variable (also global)
program1.c
#include "myfile.h"
#include <stdio.h>
void printValue(){
printf("Global variable: %d", global_variable);
}
A C constant is usually just the written version of a number. For
example 1, 0, 5.73, 12.5e9. We can specify our constants in octal or
hexadecimal, or force them to be treated as long integers.
Octal constants are written with a leading zero - 015.
Hexadecimal constants are written with a leading 0x - 0x1ae.
Long constants are written with a trailing L - 890L.
Character constants are usually just the character enclosed in single
quotes; 'a', 'b', 'c'. Some characters can't be represented in this
way, so we use a 2 character sequence as follows.
'\n' newline
'\t' tab
'\\' backslash
'\'' single quote
'\0' null ( Usedautomatically to terminate character string )
In addition, a required bit pattern can be specified using its octal equivalent.
'\044' produces bit pattern 00100100.
Character constants are rarely used, since string constants are more
convenient. A string constant is surrounded by double quotes eg "Brian
and Dennis". The string is actually stored as an array of characters.
The null character '\0' is automatically placed at the end of such a
string to act as a string terminator.
A character is a different type to a single character string. This is important poing to note.
Defining Constants
ANSI C allows you to declare constants. When you declare a constant it is a bit like a variable declaration except the value cannot be changed.
The const keyword is to declare a constant, as shown below:
int const a = 1;
const int a =2;
Note:
You can declare the const before or after the type. Choose one an stick to it.
It is usual to initialise a const with a value as it cannot get a value any other way.
The preprocessor #define is another more flexible (see Preprocessor Chapters) method to define constants in a program.
Here TRUE, FALSE and NAME_SIZE are constant
You frequently see const declaration in function parameters. This says simply that the function is not going to change the value of the parameter.
The following function definition used concepts we have not met (see
chapters on functions, strings, pointers, and standard libraries) but
for completenes of this section it is is included here:
void strcpy(char *buffer, char const *string)
Data Types in C
A data type specifies the type of data that a variable can store such as integer, floating, character etc.
There are 4 types of data types in C language.
Types
Data Types
Basic Data Type :
int, char, float, double
Derived Data Type :
array, pointer, structure, union
Enumeration Data Type:
enum
Void Data Type :
void
Basic Data Types
The basic data types are integer-based and floating-point based. C language supports both signed and unsigned literals.
The memory size of basic data types may change according to 32 or 64 bit operating system.
Let's see the basic data types. It size is given according to 32 bit OS.
The if statement in C language is used to perform operation on the
basis of condition. By using if-else statement, you can perform
operation either condition is true or false.
There are many ways to use if statement in C language:
If statement
If-else statement
If else-if ladder
Nested if
If Statement
The single if statement in C language is used to execute the code if
condition is true. The syntax of if statement is given below:
if(expression){
//code to be executed
}
If-else Statement
The if-else statement in C language is used to execute the code if
condition is true or false. The syntax of if-else statement is given
below:
if(expression){
//code to be executed if condition is true
}else{
//code to be executed if condition is false
}
If else-if ladder Statement
The if else-if statement is used to execute one code from multiple
conditions. The syntax of if else-if statement is given below:
if(condition1){
//code to be executed if condition1 is true
}elseif(condition2){
//code to be executed if condition2 is true
}
elseif(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are false
}
C Switch Statement
The switch statement in C language is used to execute the code from multiple conditions. It is like if else-if ladder statement.
The syntax of switch statement in c language is given below:
switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not matched;
}
Rules for switch statement in C language
1) The switch expression must be of integer or character type.
2) The case value must be integer or character constant.
3) The case value can be used only inside the switch statement.
4) The break statement in switch case is not must. It is
optional. If there is no break statement found in switch case, all the
cases will be executed after matching the case value. It is known as fall through state of C switch statement.
Let's try to understand it by the examples. We are assuming there are following variables.
The loops in C language are used to execute a block of code or a part of the program several times.
In other words, it iterates a code or group of code many times.
Advantage of loops in C
1) It saves code.
2) It helps to traverse the elements of array (which is covered in next pages).
Types of C Loops
There are three types of loops in C language that is given below:
do while
while
for
do-while loop in C
It iterates the code until condition is false. Here, condition is
given after the code. So at least once, code is executed whether
condition is true or false.
It is better if you have to execute the code at least once.
The syntax of do-while loop in c language is given below:
do{
//code to be executed
}while(condition);
while loop in C
Like do while, it iterates the code until condition is false. Here,
condition is given before the code. So code may be executed 0 or more
times.
It is better if number of iteration is not known by the user.
The syntax of while loop in c language is given below:
while(condition){
//code to be executed
}
for loop in C
Like while, it iterates the code until condition is false. Here,
initialization, condition and increment/decrement is given before the
code. So code may be executed 0 or more times.
It is good if number of iteration is known by the user.
The syntax of for loop in c language is given below:
The function in C language is also known as procedure or subroutine in other programming languages.
To perform any task, we can create function. A function can be called many times. It provides modularity and code reusability.
Advantage of functions in C
There are many advantages of functions.
1) Code Reusability
By creating functions in C, you can call it many times. So we don't need to write the same code again and again.
2) Code optimization
It makes the code optimized, we don't need to write much code.
Suppose, you have to check 3 numbers (781, 883 and 531) whether it is
prime number or not. Without using function, you need to write the
prime number logic 3 times. So, there is repetition of code.
But if you use functions, you need to write the logic only once and you can reuse it several times.
Syntax to declare function in C
The syntax of creating function in c language is given below:
The syntax of calling function in c language is given below:
variable=function_name(arguments...);
1) variable: The variable is not mandatory. If function return type is void, you must not provide the variable because void functions doesn't return any value. 2) function_name: The function_name is name of the function to be called. 3) arguments: You need to provide same number of arguments as defined in the function at the time of declaration or definition.
Source : javatpoind.com
Array in C language is a collection or group of elements (data). All the elements of c array are homogeneous (similar). It has contiguous memory location.
C array is beneficial if you have to store similar elements. Suppose
you have to store marks of 50 students, one way to do this is allotting
50 variables. So it will be typical and hard to manage. For example we
can not access the value of these variables with only 1 or 2 lines of
code.
Another way to do this is array. By using array, we can access the
elements easily. Only few lines of code is required to access the
elements of array.
Advantage of C Array
1) Code Optimization: Less code to the access the data. 2) Easy to traverse data: By using the for loop, we can retrieve the elements of an array easily. 3) Easy to sort data: To sort the elements of array, we need a few lines of code only. 4) Random Access: We can access any element randomly using the array.
Disadvantage of C Array
1) Fixed Size: Whatever size, we define at the time of
declaration of array, we can't exceed the limit. So, it doesn't grow the
size dynamically like LinkedList which we will learn later.
Declaration of C Array
We can declare an array in the c language in the following way.
data_type array_name[array_size];
Now, let us see the example to declare array.
int marks[5];
Here, int is the data_type, marks is the array_name and 5 is the array_size.
Initialization of C Array
A simple way to initialize array is by index. Notice that array index starts from 0 and ends with [SIZE - 1].
marks[0]=80;//initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
Two Dimensional Array in C
The two dimensional array in C language is represented in the form of
rows and columns, also known as matrix. It is also known as array of arrays or list of arrays.
The two dimensional, three dimensional or other dimensional arrays are also known as multidimensional arrays.
Declaration of two dimensional Array in C
We can declare an array in the c language in the following way.
data_type array_name[size1][size2];
A simple example to declare two dimensional array is given below.
int twodimen[4][3];
Here, 4 is the row number and 3 is the column number.
Initialization of 2D Array in C
A way to initialize the two dimensional array at the time of declaration is given below.