Overview of C Language

Overview of C Language

Introduction:
The C programming language is a popular and widely used programming language for creating computer programs. Programmers around the world embrace C because it gives maximum control and efficiency to the programmer.

C is a structured programming language developed by Dennis Ritchie in 1970's at Bell Laboratories. It is one of the most popular computer languages today because of its structure, high-level abstraction, machine independent feature. C language was developed with UNIX operating system, so it is strongly associated with UNIX, which is one of the most popular network operating system in use today and heart of internet data superhighway. The C has now become a widely used professional language for various reasons.
•    Easy to learn
•    Structured language
•    It produces efficient programs.
•    It can handle low-level activities.
•    It can be compiled on a variety of computer platforms.

How is C Language Used?
•    System programming is one of the most common use case of this language. C is especially known to be the language of the Linux operating system. In fact, almost every operating system is implemented in C.
•    Its wide availability, low system resources usage, speed, and portability have made it used in a wide range of computer platforms, from embedded systems and mobile devices to supercomputers.
•    Its speed makes it a good language in order to write compilers or interpreters for other languages. For example, the Haskell’s compiler is written in C.
•    Even though it’s not as common as it once was, you can write user applications in C. But higher level languages such as Java are often preferred today to write these, as their additional layers of abstraction enable the programmer to quickly write working programs.

Characteristics:
•    There are a small, fixed number of keywords, including a full set of flow of control primitives: for, while, if, do while and switch.
•    More than one assignment may be performed in a single statement.
•    There are a large number of arithmetical and logical operators, such as +, +=, ++, &, ~, etc.

Structure Of C Program:
•    Documentation Section //optional
•    Link section //optional
•    Defining section //optional
•    Global declaration section //optional
•    Main function section //Must
{   
Declaration part
Executable part.
}
•    Sub program section //optional
•    Function 1
•    Function 2
•    Function n

What is Data Type?
A data type in a programming language is a set of data values having predefine characteristics. There are three classes of data types:
•    Primitive
•    Derived
•    User define

Types of Data Type:

•    In c language compiler support five fundamental data type namely integer (int), character (char), floating point (float), double and void.
•    Derived data types are array, structure, pointer, function.
•    A user define data type is basically made by the user itself.

Variables:
Variables are memory location in computer's memory to store data. To indicate the memory location, each variable should be given a unique name called identifier. Variable names are just the symbolic representation of a memory location. Examples of variable name: sum, car_no, count etc.
int num;
Here, num is a variable of integer type.
In C programming, you have to declare variable before using it in the program.

Constants:
Constants are the terms that can't be changed during the execution of a program. For example: 1, 2.5, "Programming is easy." etc. In C, constants can be classified as:

Integer constants:
Integer constants are the numeric constants(constant associated with number) without any fractional part or exponential part. There are three types of integer constants in C language: decimal constant(base 10), octal constant(base 8) and hexadecimal constant (base 16).
Decimal digits: 0 1 2 3 4 5 6 7 8 9
Octal digits: 0 1 2 3 4 5 6 7
Hexadecimal digits: 0 1 2 3 4 5 6 7 8 9 A B C D E F.
Floating-point constants:
Floating point constants are the numeric constants that has either fractional form or exponent form. For example:
-2.0
0.0000234
-0.22E-5

Character constants:
Character constants are the constant which use single quotation around characters. For example: 'a', 'l', 'm', 'F' etc.
Escape Sequences:
Sometimes, it is necessary to use newline(enter), tab, quotation mark etc. in the program which either cannot be typed or has special meaning in C programming. In such cases, escape sequence are used. For example: \n is used for newline. The backslash(\) causes "escape" from the normal way the characters are interpreted by the compiler.
String constants:
String constants are the constants which are enclosed in a pair of double-quote marks. For example:
"good"                  //string constant
""                     //null string constant
"      "               //string constant of six white space
"x"                    //string constant having single character.
"Earth is round\n"         //prints string with newline
Enumeration constants:
Keyword enum is used to declare enumeration types. For example:
enum color {yellow, green, black, white};
Here, the variable name is color and yellow, green, black and white are the enumeration constants having value 0, 1, 2 and 3 respectively by default.

Decision making in C:
Decision making is about deciding the order of execution of statements based on certain conditions or repeat a group of statements until certain specified conditions are met. C language handles decision-making by supporting the following statements,
•    if statement
•    switch statement
•    conditional operator statement
•    goto statement

Decision making with if statement:
The if statement may be implemented in different forms depending on the complexity of conditions to be tested. The different forms are,
•    Simple if statement
•    If....else statement
•    Nested if....else statement
•    else if statement
•    Simple if statement

The general form of a simple if statement is,
if (expression)
{
 statement-inside;
}
 statement-outside;
If the expression is true, then 'statement-inside' it will be executed, otherwise 'statement-inside' is skipped and only 'statement-outside' is executed.
if...else statement:
The general form of a simple if...else statement is,
if (expression)
{
 statement-block1;
}
else
{
 statement-block2;
}
If the 'expression' is true, the 'statement-block1' is executed, else 'statement-block1' is skipped and 'statement-block2' is executed.

Nested if....else statement:
The general form of a nested if...else statement is,
if( expression )
{
if( expression1 )
   {
     statement-block1;
   }
else
   {
statement-block 2;
   }
}
else
{
statement-block 3;
}
if 'expression' is false the 'statement-block3' will be executed, otherwise it continues to perform the test for 'expression 1' . If the 'expression 1' is true the 'statement-block1' is executed otherwise 'statement-block2' is executed.
else-if ladder:
The general form of else-if ladder is,
if(expression 1)
{
 statement-block1;
}
else if(expression 2)
{
 statement-block2;
}
else if(expression 3 )
{
 statement-block3;
}
else
 default-statement;
The expression is tested from the top(of the ladder) downwards. As soon as the true condition is found, the statement associated with it is executed.

Switch Case:
Switch statement is used to solve multiple option type problems for menu like program, where one value is associated with each option. The expression in switch case evaluates to return an integral value, which is then compared to the values in different cases, where it matches that block of code is executed, if there is no match, and then default block is executed. The general form of switch statement is,
syntax:
switch(expression)
{
case labels:
break;
       }

Looping:

To execute a set of instructions repeatedly until a particular condition is being satisfied.

A sequence of statements are executed until a specified condition is true. This sequence of statements to be executed is kept inside the curly braces { } known as the Loop body. After every execution of loop body, condition is verified, and if it is found to be true the loop body is executed again. When the condition check returns false, the loop body is not executed.There are 3 type of Loops in C language
•    while loop
•    for loop
•    do-while loop

while loop: while loop can be addressed as an entry control loop. It is completed in 3 steps.
•    Variable initialization. (E.g. int x=0;)
•    Condition (e.g. while(x<=10))
•    Variable increment or decrement (x++ or x-- or x=x+2)

Syntax :
variable initialization ;
while (condition)
{
statements ;
variable increment or decrement ;
}
for loop: for loop is used to execute a set of statements repeatedly until a particular condition is satisfied. We can say it an open ended loop. General format is,
for(initialization; condition ; increment/decrement)
{
  statement-block;
}
In for loop we have exactly two semicolons, one after initialization and second after condition. In this loop we can have more than one initialization or increment/decrement, separated using comma operator. for loop can have only one condition.
Nested for loop: We can also have nested for loops, i.e. one for loop inside another for loop. Basic syntax is,
for(initialization; condition; increment/decrement)
{
for(initialization; condition; increment/decrement)
     {
statement ;
     }
}
do while loop: In some situations it is necessary to execute body of the loop before testing the condition. Such situations can be handled with the help of do-while loop. do statement evaluates the body of the loop first and at the end, the condition is checked using while statement. General format of do-while loop is,
do
{
 ....
 .....
}
while(condition)

Jumping Out of Loops:

Sometimes, while executing a loop, it becomes necessary to skip a part of the loop or to leave the loop as soon as certain condition becomes true, that is called jumping out of loop. C language allows jumping from one statement to another within a loop as well as jumping out of the loop.

1) break statement:

When break statement is encountered inside a loop, the loop is immediately exited and the program continues with the statement immediately following the loop.

2) continue statement:

It causes the control to go directly to the test-condition and then continue the loop process. On encountering continue, cursor leave the current cycle of loop, and starts with the next cycle.

Array:
An array is a collection of elements of the same data type.In C language, arrays are referred to as structured data types. An array is defined as finite ordered collection of homogenous data, stored in contiguous memory locations.
Here the words,
•    finite means data range must be defined.
•    ordered means data must be stored in continuous memory addresses.
•    homogenous means data must be of similar data type.
Since arrays provide an easy way to represent data, it is classified amongst the data structures in C. Other data structures in c are structure, lists, queues and trees. Array can be used to represent not only simple list of data but also table of data in two or three dimensions.

Syntax : data type arrayname[size];

Types of array:
•    1 dimensional
•    2 dimensional
•    Multi-dimensional
1-dimensional: It is a linear list of fixed number of data items of the same type. All these data items are accessed using the same name using a single subscript. It is similar to a row or column matrix. It is also called as a single-dimensional Array or one subscripted variable.
Declaration:                             
The syntax of declaring a one-dimensional Array is as follows:
data_type arrayname [size]; 
where,
arrayname => name of the Array.
size => number of elements of same data type.

Two dimensional Arrays:

C language supports multidimensional arrays. The simplest form of the multidimensional array is the two-dimensional array.

Two-dimensional array is declared as follows,
type array-name[row-size][column-size]
Example: int a[3][4];

Pointer:
A pointer is a variable that points to another variable. This means that a pointer holds the memory address of another variable. Put another way, the pointer does not hold a value in the traditional sense; instead, it holds the address of another variable. A pointer "points to" that other variable by holding a copy of its address.
Because a pointer holds an address rather than a value, it has two parts. The pointer itself holds the address. That address points to a value. There is the pointer and the value pointed to.
Syntax: data type *ptrname;
 

Structure:Structure is a user-defined data type in C which allows you to combine different data types to store a particular type of record. Structure helps to construct a complex data type in more meaningful way. It is somewhat similar to an Array. The only difference is that array is used to store collection of similar datatypes while structure can store collection of any type of data.
Structure is used to represent a record. Suppose you want to store record of Student which consists of student name, address, roll number and age.
Defining a structure:
struct keyword is used to define a structure. struct define a new data type which is a collection of different type of data.
Syntax :
structstructure_name
{
 //Statements
};

Functions in C:
A function is a block of code that performs a particular task. There are times when we need to write a particular block of code for more than once in our program. This may lead to bugs and irritation for the programmer. C language provides an approach in which you need to declare and define a group of statements once and that can be called and used whenever required. This saves both time and space.
C functions can be classified into two categories,
•    Library functions
•    User-defined functions

Types of functions in C:
•    Library functions are those functions which are defined by C library, example printf (), scanf (), strcat () etc. You just need to include appropriate header files to use these functions. These are already declared and defined in C libraries.
•    User-defined functions are those functions which are defined by the user at the time of writing program. Functions are made for code reusability and for saving time and space.

Benefits of Using Functions:
•    It provides modularity to the program.
•    Easy code Reusability. You just have to call the function by its name to use it.
•    In case of large programs with thousands of code lines, debugging and editing becomes easier if you use functions.

Function declaration:
General syntax of function declaration is,
return-type function-name (parameter-list);
Like variable and an array, a function must also be declared before it’s called. A function declaration tells the compiler about a function name and how to call the function. The actual body of the function can be defined separately.

File Handling:
A file represents a sequence of bytes on the disk where a group of related data is stored. File is created for permanent storage of data. It is a ready-made structure.
In C language, we use a structure pointer of file type to declare a file.
FILE *fp;

Error Handling:
C language does not provide direct support for error handling. However few method and variable defined in error.h header file can be used to point out error using return value of the function call. In C language, a function return -1 or NULL value in case of any error and a global variable errno is set with the error code. So the return value can be used to check error while programming.

C language uses the following functions to represent error
•    Perror() return string pass to it along with the textual representation of current errno value.
•    Strerror() is defined in string.h library. This method returns a pointer to the string representation of the current errno value.

Dynamic Memory Allocation:

The process of allocating memory at runtime is known as dynamic memory allocation. Library routines known as "memory management functions" are used for allocating and freeing memory during execution of a program. These functions are defined in stdlib.h.
•    malloc (): allocates requested size of bytes and returns a void pointer pointing to the first byte of the allocated space
•    calloc (): allocates space for an array of elements, initialize them to zero and then return a void pointer to the memory
•    free: releases previously allocated memory
•    realloc: modify the size of previously allocated space

Command line Argument:

Command line argument is a parameter supplied to the program when it is invoked. Command line argument is an important concept in C programming. It is mostly used when you need to control your program from outside. Command line arguments are passed to main () method.

Syntax:

int main( int argc, char *argv[])
Here argc counts the number of arguments on the command line and argv[ ] is a pointer array which holds pointers of type char which points to the arguments passed to the program.

Advantages:
Here are some advantages of programming in C:

•    C is a general purpose programming language, meaning that it is not limited to any one specific kind of programming. This is different from languages like COBOL which was built for business applications, and FORTRAN for scientific calculations. You can write all sorts of software using C.
•    C is not a very high-level language. A high-level language tries to isolate the programmer from the hardware as much as possible. In contrast, C allows you to directly access memory addresses, create bit fields and structures and map them to memory, perform bitwise operations and so on. C facilitates hardware programming. .
•    Not being high-level also means there is little overhead; it is highly efficient and provides fast execution speed. .
•    There are C language compilers and development tools available for many different platforms from small embedded systems to large mainframes and supercomputers. .
•    C has been around for almost 40 years. In that time there has much software written in C. If there is some functionality you need in a C program you are writing, chances are someone has already written it. It may even be available for free.

0 comments:

Post a Comment