Introduction to C Language
C Language is a high level programming language developed at “AT & T’s Bell Laboratories”, USA in 1972.
It was written by Dennis Ritchie and Brian Kernighan.
It is also known as middle level language as it has inbuilt support for writing programs using assembly language instructions.
This language was created for a specific purpose i.e. to design the operating system named UNIX. The development of UNIX made C a popular language. This development of UNIX in C had two important consequences:
It was developed on the basis of a programming language known as BCPL(Basic Combined Programming Language) also named as B language. The B language was developed by Ken Thompson of Bell Labs.
Features of C Language
- It is easy to learn and remember the syntax of various statements or functions of C Language.
- C is a robust language having built-in functions, data types and operators to write complex programs.
- C Language is a case sensitive language i.e. capital letters and small letters are treated differently.
- The C language compiler combines the capabilities of a low level language with the features of a high level language. Therefore the language is suitable for writing both system software as well as application softwares.
- Program written in C language are very efficient and fast. This is due to its variety of data types and powerful operators.
- It is faster than other programming languages. Hence helps developers in saving their time.
- C has been used for different types of projects as operating systems, word processors, graphics, spreadsheets and even compilers for other languages.
- It has the ability to extend itself. A C program is basically a collection of various function supported by C library (also known as header files). We can also add our own functions to the C library. These functions can be reused in other programs.
- We can create useful, reusable code.
- Writing C program with user-defined functions makes program more simple and easy to understand. Breaking a problem in terms of functions makes program debugging, maintenance and testing easier.
- C language provides the use of pointers so that we can easily implement direct memory operations and we can also implement complex data structures like linked list, trees etc.
- It is easy to learn and use C language.
- Recursion is provided so that recursive and complex problems can be easily programmed.
Compilation of C language program
The compilation process of a C program takes place as follows:
- C program must be written by programmer and it should always be saved with extension “.C”. For example, the program is saved with name First.C.
- Header files should be included in the program if we want to use library functions of C like stdio.h, conio.h etc. They can be included in the program as #include<stdio.h>.
- We can also include other C programs in our program if we want to use functions defined within them. We can include other C program as #include “Other.c”. where Other.c is the name of an existing program whose functions we want to use in our program.
- After compilation and linking, three more files are generated named as:
First.bak – Back Up file which contains same code of c program.
First.Obj – Object program.
First.exe -Executable file which can be run directly by the programmer.
Structure of a C Language Program
There are various parts of a C Program They are as follows:
- Documentation Section
- Link Section
- Definition Section
- Global Declaration Section
- main() function Section
- Declaration Part
- Executable Part
- Subprogram Section [User Defined Function Section]
1. Documentation Section
This is the very first section of a C Program. In this section, we use comments to specify various aspects related to the program. We can specify the name of program, name of the programmer, name of the organization, date of program development, date of program modification etc. This section has no specific role in the working of program. It is designed only for the later reference. This section is an optional section.
In C language, we can use comments as follows
/* This is a comment */
In C language, Comments start with /* and end with */ Comments can lie in multiple lines. Comments can be used anywhere in a program.
2. Link Section
This is one of the most important sections of a C program. This section is used to specify the header files to be used in the program and to perform conditional compilation of the program. This section links the program with library of C Language.
stdio.h header file is included in a program if we want to use various input/output functions of C LANGUAGE in our program like printf(), scanf(), gets(), puts(), getchar(), putchar() etc.
3. Definition Section
This section is used to define the macros and symbolic constants to be used in the program.
#define PI 3.141
Here, symbolic constant with name PI has been defined which contains value 3.141. Its value can’t be changed during program execution. The symbolic constant automatically takes its data type depending upon the value stored in it.
#define cube(x) x*x*x
Here, macro with name cube has been defined. It contains one argument named x. When this macro is invoked, it will return cube of argument passed to it.
The data type of argument is automatically adjusted depending upon the value of actual argument passed.
4. Global Declaration Section
This section is used to define global variables. The global variables can be used throughout the program. They can be accessed across multiple functions and any changes made to them remain available throughout the program.
Global variables are usually declared at the top of the program. Global variables can be defined and initialized like ordinary variables but they can’t be declared inside a function body.
In this section, we can also define structures, unions, arrays which can be used within all other sections of the same program.
5. main() Function Section
Every program written in C LANGUAGE must have a main() function. A program which doesn’t contain main() function can’t run. The main() function has two sub parts.
(i) Declaration Part
In this part we can declare local variables to be used within function main(). Variables declared can be of any data type depending upon the requirement of the programmer.
The values of variables declared can be changed and will remain active until the end of the program.
(ii) Executable Part
In this part, we write the statements of C language which are to be executed when we run the program. It can contain any number of executable statements of C language.
6. Subprogram Section
This section contains all the user defined functions designed by the programmer. These functions can be called by the main() function as well as by other functions. These functions can also be defined before main() function.
Functions help to modularize the program into smaller sections which simplifies the task of programming as it becomes easier to locate errors, correct them and to understand the code easily.
Errors and its types
Any mistake in program is known as an error. Program may not run at all or may not give correct output due to presence of errors. So whatever program is made, it should be free from errors. Error is also known as a bug. The process of removing errors from a program is called debugging. Errors are categorized into four types:
- Syntax Errors
- Logical Errors
- Semantic Errors
- Run Time Errors
1 Syntax Errors
The first type of error is a syntax error. Such errors occur in a program if we do not obey the syntax rules of a programming language. Syntax rules are predefined formats or formulas to write any programming statement.
These errors are easily found by the compiler or interpreter. So they are easy to locate and correct. The most common syntax error is forgetting to terminate each statement with a semicolon.
2 Logical Errors
Logical errors are those errors that remain after all the semantic and syntax errors have been removed. Logical errors appear due to wrong logic of the program.
For example, programmer makes a program to add two numbers but at the place of expression c=a+b, he puts c=a-b by mistake. In such a case program would execute but would not show the expected result.
Logical errors are more difficult to locate and correct as they are not located by the compiler. They can be located only by the programmer by carefully observing the complete program.
3. Semantic Errors
A semantic error occurs when we obey the syntax rules of the programming language but are using the statement out of context.
For example, we have not declared a variable in a program but we are trying to use it in our program.
Similar example is we have defined a named constant in our program but we are trying to increment its value.
4. Run Time Errors
Run time errors emerge during the execution of the program. They are not detected by the compiler and more difficult to be detected by the programmer as well.
Program gets terminated abnormally due to run time errors. The most common example of run time error is dividing a number by zero.
In mathematics, on dividing a number by zero, infinity is produced. But in computer, there is nothing called infinity. So in such a case, the program would terminate without giving a suitable message or warning.
Other example of run time error is when we try to store string type data in an integer type variable, the program would terminate.
Character Set of C Language
Various characters and symbols which can be used in C language are known as the character set of C Language. They include:
- Alphabets: a-z,A-Z
- Digits:- 0-9
- Special Characters:- #, Colon(:),Semi Colon (;), Comma(,) ,Under Score,+,-,*, \, /, &, | , !
- White Spaces:- Blank Space, Tab, Backspace, Enter etc.
Token is the smallest identifiable part of a C Program. A program can’t be created without using a token. There are six types of tokens available in C.
Keyword is the reserved word provided by C language. It is predefined in C. It has a predefined meaning and we can’t change the meaning of a keyword.
Keywords are one of the basic requirements to develop a program in C. There are basically 32 standard keywords provided by C LANGUAGE.
Keywords can’t be used as names of identifiers, variables or constants.
Identifier refers to those quantities within a program which are defined by a programmer depending upon the requirements.
An identifier which may be a variable, function, array, constant or any other user defined item in a C program. There are few Rules which should be followed for naming an identifier. They are as follows:-
- Both digits and alphabets are allowed.
- First character must be an alphabet or underscore.
- Variable name can contain digits only after first character.
- Keyword can’t be used to name a variable.
- No special character other than underscore can be used.
- It can’t contain spaces.
- Capital letters and small letters are treated differently.
Punctuators are special symbols used in Python for different purposes depending upon requirements. Various punctuators are:
a) Colon (:) It is used with control structures, functions an classes to specify their start
b) Comma (,) It is used to separate variables during variable definition and expressions.
Test Your Knowledge
Q1. Define a programming language. Explain its types.
Q2. Explain high level language and its types.
Q3. Discuss about how C emerged?
Q4. Write various features of C.
Q5. Write various uses of C.
Q6. Explain compilation process of a C program.
Q7. Explain structure of a C program.
Q8. Why is C known as a middle level language?
Q9. What made C a popular programming language?
Q10. Define a token? Name different tokens of C.
Q11. What is character set?
Q12 Define an identifier. Write various rules to name an identifier.