Lecture Notes
Lecture Notes
It was designed
and written by a man named Dennis Ritchie. The C Language is developed by Dennis Ritchie
for creating system applications that directly interact with the hardware devices such as drivers,
kernels, etc.
C programming is considered as the base for other programming languages, that is why it is
known as mother language.
It was initially designed for programming UNIX operating system. Now the software tool as
well as the C compiler is written in C.
Major parts of popular operating systems like Windows, UNIX, Linux is still written in C. This
is because even today when it comes to performance (speed of execution) nothing beats C.
Moreover, if one is to extend the operating system to work with new devices one needs to write
device driver programs. These programs are exclusively written in C. C seems so popular is
because it is reliable, simple and easy to use. often heard today is – “C has been already
superceded by languages like C++, C# and Java. ``
C as a mother language
C language is considered as the mother language of all the modern programming languages
because most of the compilers, JVMs, Kernels, etc. are written in C language, and most of the
programming languages follow C syntax, for example, C++, Java, C#, etc.
C is considered as a middle-level language because it supports the feature of both low-level and
high-level languages. C language program is converted into assembly code, it supports pointer
arithmetic (low-level), but it is machine independent (a feature of high-level).
There is a close analogy between learning English language and learning C language. The
classical method of learning English is to first learn the alphabets used in the language, then
learn to combine these alphabets to form words, which in turn are combined to form sentences
and sentences are combined to form paragraphs. Learning C is similar and easier. Instead of
straight-away learning how to write programs, we must first know what alphabets, numbers
and special symbols are used in C, then how using them constants, variables and keywords are
constructed, and finally how are these combined to form an instruction. A group of instructions
would be combined later on to form a program.
A computer program is just a collection of the instructions necessary to solve a specific problem.
The basic operations of a computer system form what is known as the computer’s instruction
set. And the approach or method that is used to solve the problem is known as an algorithm.
Low level languages are machine level and assembly level language. In machine level
language computer only understand digital numbers i.e. in the form of 0 and 1. So, instruction
given to the computer is in the form binary digit, which is difficult to implement instruction in
binary code. This type of program is not portable, difficult to maintain and also error prone.
The assembly language is on other hand modified version of machine level language. Where
instructions are given in English like word as ADD, SUM, MOV etc. It is easy to write and
understand but not understand by the machine. So the translator used here is assembler to
translate into machine level. Although language is bit easier, programmer has to know low level
details related to low level language. In the assembly level language the data are stored in the
computer register, which varies for different computer. Hence it is not portable.
These languages are machine independent, means it is portable. The language in this category is
Pascal, Cobol, Fortran etc. High level languages are understood by the machine. So it need to
translate by the translator into machine level. A translator is software which is used to translate
high level language as well as low level language in to machine level language.
Compiler and interpreter are used to convert the high-level language into machine level
language. The program written in high level language is known as source program and the
corresponding machine level language program is called as object program. Both compiler and
interpreter perform the same task but there working is different. Compiler read the program at-
a-time and searches the error and lists them. If the program is error free then it is converted into
object program. When program size is large then compiler is preferred. Whereas interpreter
read only one line of the source code and convert it to object code. If it checks error, statement
by statement and hence of take more time.
Assembler converts assembly level language code into machine language code.
The process of editing, compiling, running, and debugging programs is often managed by a
single integrated application known as an Integrated Development Environment, or IDE for
short. An IDE is a windows-based program that allows us to easily manage large software
programs, edit files in windows, and compile, link, run, and debug programs.
Why choose IDEs? Because it makes writing programs easy, efficient, and effective. It saves a lot
of time by converting the program to machine-level code or byte code. And the best part, is you
also get the freedom to choose the programming language of your interest.
Example of IDEs Eclipse, NetBeans, Visual Studio, Dev C++, VI/VIM editor. In this module we
shall use Dev C++.
Dev-C++ is a fully featured graphical IDE (Integrated Development Environment) that uses the
MinGw compiler system to create Windows as well as Console based C/C++ applications.
1. Comment line
2. Preprocessor directive
3. Global variable declaration
4. main function( )
Local variables;
Statements;
}
}
Comment line
/*………………………………*/
Comment line is used for increasing the readability of the program. It is useful in explaining the
program and generally used for documentation. It is enclosed within the decimeters. Comment
line can be single or multiple line but should not be nested. It can be anywhere in the program
except inside string constant & character constant.
Preprocessor Directive:
#include<stdio.h> tells the compiler to include information about the standard input/output
library. It is also used in symbolic constant such as #define PI 3.14(value). The stdio.h (standard
input output header file) contains definition &declaration of system defined function such as
printf( ), scanf( ), pow( ) etc. Generally, printf () function used to display and scanf () function
used to read value
Global Declaration:
This is the section where variable are declared globally so that it can be access by all the
functions used in the program. And it is generally declared outside the function :
main()
It is the user defined function and every function has one main() function from where actually
program is started and it is encloses within the pair of curly braces. The main( ) function can be
anywhere in the program but in general practice it is placed in the first position.
Syntax :
main()
……..
……..
……..
The main( ) function return value when it declared by data type as int main( )
return 0;
The main function does not return any value when void (means null/empty) as
void main(void ) or void main()
Output: C language
The program execution start with opening braces and end with closing brace. And in between
the two braces declaration part as well as executable part is mentioned. And at the end of each
line, the semi-colon is given which indicates statement termination.
#include<stdio.h>
int main ()
return 0;
The preprocessor tool helps in comments removal, macros expansion (Macros are some
constant values or expressions defined using the #define directives in C Language.), file
inclusion (It is done using the #include directive. File inclusion during pre-processing
causes the entire content of filename to be added to the source code, replacing
the #include<filename> directive), and conditional compilation. These commands are
executed in the first step of the compilation process. Compiler software helps boost the
program's performance and translates the intermediate file to an assembly file.
Assembler helps convert the assembly file into an object file containing machine-level
code.
Linker is used for linking the library file with the object file. It is the final step in
compilation to generate an executable file.
Syntax Error
Syntax errors occur when a programmer makes mistakes in typing the code's syntax correctly.
In other words, syntax errors occur when a programmer does not follow the set of rules defined
for the syntax of C language.
Syntax errors are sometimes also called compilation errors because they are always detected by
the compiler. Generally, these errors can be easily identified and rectified by programmers.
Runtime Error
Errors that occur during the execution (or running) of a program are called RunTime Errors.
These errors occur after the program has been compiled successfully. When a program is
running, and it is not able to perform any particular operation, it means that we have
encountered a run time error. For example, while a certain program is running, if it encounters
the square root of -1 in the code, the program will not be able to generate an output because
calculating the square root of -1 is not possible. Hence, the program will produce an error.
Runtime errors can be a little tricky to identify because the compiler cannot detect these errors.
They can only be identified once the program is running. Some of the most common run time
errors are: number not divisible by zero, infinity loop.
Logical Error
Sometimes, we do not get the output we expected after the compilation and execution of a
program. Even though the code seems error free, the output generated is different from the
expected one. These types of errors are called Logical Errors. Logical errors are those errors in
which we think that our code is correct, the code compiles without any error and gives no error
while it is running, but the output we get is different from the output we expected.
Semantic Error
Errors that occur because the compiler is unable to understand the written code are called
Semantic Errors. A semantic error will be generated if the code makes no sense to the compiler,
even though it is syntactically correct. It is like using the wrong word in the wrong place in the
English language. For example, adding a string to an integer will generate a semantic error.
Semantic errors are different from syntax errors, as syntax errors signify that the structure of a
program is incorrect without considering its meaning.
Linker Error
Linker is a program that takes the object files generated by the compiler and combines them
into a single executable file. Linker errors are the errors encountered when the executable file of
the code cannot be generated even though the code gets compiled successfully. This error is
generated when a different object file is unable to link with the main object file.
Keyword
There are certain words reserved for doing specific task, these words are known as reserved
word. These words are predefined and always written in lower case or small letter. These
keywords can’t be used as a variable name as it assigned with fixed meaning. Some examples
are int, short, signed, unsigned, default, volatile, float, long, double, break, continue, typedef,
static, do, for, union, return, while, do, extern, register, enum, case, goto, struct, char, auto,
const etc.
Data types
Data types refer to an extensive system used for declaring variables or functions of different
types before its use. The type of a variable determines how much space it occupies in storage
and how the bit pattern stored is interpreted.
C Programming Operators
1. C Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition, subtraction,
multiplication, division etc on numerical values (constants and variables).
* multiplication
/ division
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
4. C Relational Operators
A relational operator checks the relationship between two operands. If the relation is
true, it returns 1; if the relation is false, it returns value 0.
Relational operators are used in decision making and loops.
== It is used to check if the values of the two operands are equal or (A == B) is not true.
not. If the values of the two operands are equal, then the condition
becomes true.
!= It is used to check if the values of the two operands are equal or (A != B) is true.
not. If the values are not equal, then the condition becomes true.
> It is used to check if the value of left operand is greater than the (A > B) is not true.
value of right operand. If the left operand is greater, then the
condition becomes true.
< It is used to check if the value of left operand is less than the value (A < B) is true.
of right operand. If the left operand is lesser, then the condition
becomes true.
>= It is used to check if the value of left operand is greater than or (A >= B) is not true.
equal to the value of right operand. If the value of the left operand
is greater than or equal to the value, then the condition becomes
true.
<= It is used to check if the value of left operand is less than or equal (A <= B) is true.
to the value of right operand. If the value of the left operand is less
than or equal to the value, then the condition becomes true.
5. C Logical Operators
An expression containing logical operator returns either 0 or 1 depending upon whether
expression results true or false. Logical operators are commonly used in decision
making in C programming.
Operator Meaning Example
&& Logical AND. True only if all If c = 5 and d = 2 then, expression ((c==5)
operands are true && (d>5)) equals to 0.
|| Logical OR. True only if either one If c = 5 and d = 2 then, expression ((c==5)
operand is true || (d>5)) equals to 1.
6. Other Operators
Comma Operator
Comma operators are used to link related expressions together. For example:
int a, c = 5, d;
Variables
Variable is a data name which is used to store some data value or symbolic names
for storing program, computations and results.
It is a way to represent memory location through symbol so that it can be easily identified.
o A variable name can start with the alphabet, and underscore only. It can't start with a
digit.
o A variable name must not be any reserved word or keyword, e.g., int, float, etc.
Types of Variables in C
2. global variable
3. static variable
Local Variable
A variable that is declared inside the function or block is called a local variable.
Example:
1. void function1(){
3. }
Global Variable
A variable that is declared outside the function or block is called a global variable. Any function
can change the value of the global variable.
Example:
2. void function1(){
4. }
Static Variable
A variable that is declared with the static keyword is called static variable.
Format specifiers
Format specifiers in C are used to take inputs and print the output of a type. The symbol we use
in every format specifier is %. Format specifiers tell the compiler about the type of data that
must be given or input and the type of data that must be printed on the screen.
%d A decimal integer
%c Signed character
%f Signed float
%lf double