You are on page 1of 49

C Language Theory Part

Prepared by
S. B. Mishra
Department of Computer Science
Viswa Niketan Secondary School
Tripureswor, Kathmandu
C Language
• C is a high-level and general purpose
programming language that is ideal for developing
firmware or portable applications.
• The C language is structured, middle level
programming language developed by Dennis
Ritchie.
• Operating system programs such as Windows,
Unix, Linux are written in C language.
• C has been written in assembly language.
C – Language History
• Originally developed for writing system software.
• C was developed at Bell Labs by Dennis Ritchie for
the Unix Operating System (OS) in the early
1970s.
• C programming language features were derived
from an earlier language called “B” (Basic
Combined Programming Language – BCPL).
Features of C Language
• Simple: C provides structured approach, rich
set of library functions, data types etc.
• Machine Independent or Portable
• Mid-level Programming Language: C supports
features of low level programming and high
level language. That is why it is known as mid-
level language.
• Structured Programming Language: We can
break the program into parts using functions.
So, it is easy to understand and modify.
• Rich Library: C provides a lot of inbuilt functions that
makes the development fast.
• Memory Management: It supports the feature of
dynamic memory allocation.
• Speed: The compilation and execution time of C
language is fast.
• Pointer: We can directly interact with the memory by
using the pointers.
• Recursion: In c, we can call the function within the
function.
• Extensible: C language is extensible because it can
easily adopt new features.
Advantages of C Language
• C is compiler based language rather than
interpreter based.
• C is portable language.
• C is the collection of lot of library files.
• C is easy to learn for beginners.
• C supports good graphics.
• C supports system programming.
• C supports number of operators.
• C supports string handling.
Disadvantages of C Language
• C language has no run time checking mechanism.
• C does not support OOP(Object Oriented
Programming features, so C++ language was
introduced.
• C does not have the concept of constructor or
destructor.
• The program takes more time to design and
implement the software.
• It is case sensitive so mixing case makes difficult
while writing a program.
Uses of C programming language
The C programming language is used for developing:
• System Software such as Windows, UNIX and Linux.
• Database systems
• Graphics packages
• Word processors
• Spreadsheets
• Compilers and Assemblers
• Network drivers
• Interpreters
Variable
• A variable is a symbol (memory address) which
hold value that may change during the execution
of program.
• Example: A = 5; where A is the variable name and
5 is value.
• Variable name consist with the combination of
letters, digits or underscore characters.
• The value of the C variable may get change in the
program.
Rules for naming C variable
• Variable name must begin with letter or underscore.
• Variables are case sensitive.
• The variable name should not be keywords.
• They can be constructed with digits, letters.
• White space and other special characters are not
allowed in between the name of variable.
• Different variables of the same name are not allowed.
• sum, height, _value are some examples for variable
name
Declaring & initializing C variable
• Variables should be declared in the C program
before to use.
• Memory space is not allocated for a variable while
declaration. It happens only on variable
definition.
• Variable initialization means assigning a value to
the variable.
Declaring & initializing C variable

S.No Type Syntax Example

int x, y, z;
1 Variable declaration data_type variable_name;
char flat, ch;

Variable data_type variable_name = int x = 50, y = 30;


2
initialization value; char flag = ‘x’, ch=’l’;
Constants/Literals
• A constant is a value or an identifier whose value
cannot be changed in a program. For example: 1,
2.5, etc.
• An identifier also can be defined as a constant.
• const double PI = 3.14 Here, PI is a constant.
Basically what it means is that, PI and 3.14 is
same for this program.
Types of constant
• Integer constants
• Floating-point constants
• Character constants
• String constants
Integer constants
• A sequence of numeric digits without decimal
point (whole numbers) are called integer
constant.
• decimal constant(base 10)
• octal constant(base 8)
• hexadecimal constant(base 16)
Floating-point constants
A sequence of numeric digits with decimal point
(fractional numbers) are called floating point
constants.
Character constants
• Any single character given within single quotes
(‘ ‘) is called character constant.
• Example: const char ch = ‘y’;
String constants
• Sequence of zero or more characters surrounded
by double quotes (“ “) is called string constant.
• This can also be defined as an array of character
constants. Whose last character is ‘\0’ which is
automatically placed at the end of the string by C
compiler.
Escape Sequences
• Sometimes, it is necessary to use characters
which cannot be typed or has special meaning in
C programming.
• For example: newline(enter), tab, question mark
etc.
• In order to use these characters, escape sequence
is used.
Escape Sequences
Escape Sequences character Purpose
\b Backspace
\f Form feed
\n Newline
\r Return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\' Single quotation mark
\" Double quotation mark
\? Question mark
C tokens
• C tokens are the basic buildings blocks in C language which
are constructed together to write a C program.
• Each and every smallest individual units in a C program are
known as C tokens.
• C tokens:
Seven types C tokens are:
• Keywords (eg: int, while),
• Identifiers (eg: main, total),
• Specifiers (eg. %d, %f, %c)
• Constants and variable (eg: 10, 20),
• Strings (eg: “total”, “hello”),
• Special symbols (eg: (), {}),
• Operators (eg: +, /,-,*)
C tokens example program
void main() where,
{ • main – identifier
int x, y, total; • {,}, (,) – delimiter
x = 10, y = 20;
• int – keyword
t = x + y;
printf (“Total = %d \n”, total); • x, y, total –
identifier
getch();
• main, {, }, (, ), int,
}
x, y, total – tokens
Identifiers in C language
• Each program elements in a C program are given a
name called identifiers.
• Names given to identify Variables, functions and
arrays are examples for identifiers.
• Example: x is a name given to integer variable in
above program.
Keywords in C language:
• Keywords are pre-defined words in a C compiler.
• Each keyword is meant to perform a specific
function in a C program.
• Since keywords are referred names for compiler,
they can’t be used as variable name.
C language supports 32 keywords
which are given below.
auto double int struct const float

break else long switch continue for

case enum register typedef default goto

char extern return union do if

short unsigned signed void sizeof volatile

static while
Format Specifiers
• Format Specifiers in C language tells us which type
of data to store and which type of data to print.
• This statement tells us that it is used in only 2
places
– In taking Input
– In displaying Output
Different Types of Format Specifiers
Defined in C
Data Types Format specifier
int %d
short %d
long %ld
char %c
float %f
double %lf
long double %Lf
string %s
Data types in C Language
• Data types specify how we enter data into our
programs and what type of data we enter.
• C language has some predefined set of data types
to handle various kinds of data that we can use in
our program.
• These data types have different storage
capacities.
C language supports 2 different type
of data types
• Primary data types: These are fundamental data
types in C namely integer(int), floating point(float),
character(char) and void.
• Derived data types: Derived data types are nothing
but primary data types but a little twisted or grouped
together like array, structure, union and pointer.
These are discussed in details later.
Data type determines the type of data a variable will
hold. If a variable x is declared as int. it means x can
hold only integer values. Every variable which is used
in the program must be declared as what data-type it
is.
C – Operators and Expressions
• The symbols which tells the computer to perform
logical and mathematical operations in a C
program are called C operators.
• These C operators join individual constants and
variables to form expressions.
• Operators, functions, constants and variables are
combined together to form expressions.
• Consider the expression A + B * 5.
• where, +, * are operators, A, B are variables, 5 is
constant and A + B * 5 is an expression.
Types of C operators
• Arithmetic operators
• Assignment operators
• Relational operators
• Logical operators
• Bit wise operators
• Conditional operators (ternary operators)
• Increment/decrement operators
• Special operators
Arithmetic Operators
C Arithmetic operators Arithmetic
Operation Example
are used to perform Operators
mathematical
+ Addition A+B
calculations like addition,
subtraction,
– Subtraction A–B
multiplication, division
and modulus in C
* multiplication A*B
programs.
/ Division A/B

% Modulus A%B
Example program for C arithmetic
operators
#include <stdio.h>
#include <conio.h> Output:
void main() Addition of a, b is : 60
{ Subtraction of a, b is : 20
int a=40, b=20, add, sub, mul, div, mod;
Multiplication of a, b is : 800
add = a+b;
sub = a-b; Division of a, b is : 2
mul = a*b; Modulus of a, b is : 0
div = a/b;
mod = a%b;
printf("Addition of a, b is : %d\n", add);
printf("Subtraction of a, b is : %d\n", sub);
printf("Multiplication of a, b is : %d\n",
mul);
printf("Division of a, b is : %d\n", div);
printf("Modulus of a, b is : %d\n", mod);
getch();
}
Special Operators in C
S. No. Operators Description

This is used to get the address of the


1 & variable.
Example : &a will give address of a.

This is used as pointer to a variable.


2 * Example : * a where, * is pointer to the
variable a.

This gives the size of the variable.


3 Sizeof ()
Example : size of (char) will give us 1.
Assignment operators in C
• In C programs, values Operators Example Explanation

for the variables are Simple


10 is assigned to
assigned using assignment = sum = 10 variable sum
operator
assignment operators.
• For example, if the += sum += 10
This is same as
sum = sum + 10
value “10” is to be
assigned for the -= sum -= 10
This is same as
sum = sum – 10
variable “sum”, it can
Compound
be assigned as “sum = assignment *= sum *= 10
This is same as
sum = sum * 10
10;” operators

This is same as
• Other assignment /= sum /= 10 sum = sum / 10
operators in C
This is same as
language are: %= sum %= 10 sum = sum % 10
Example program for C assignment
operators
# include <stdio.h> Output
#include <conio.h> Total = 45
void main()
i =i+1
{
int Total=0,i; Total = Total + i
for(i = 0; i < 10; i++)
{
Total+=i;
}
printf("Total = %d", Total);
getch()
}
Relational operators in C
between two variable Operators Example Description
Relational operators are
used to find the > x > y x is greater than y
relations. i.e. to
< x < y x is less than y
compare the values of
two variables in a C x is greater than or
>= x >= y equal to y
program.
x is less than or
<= x <= y equal to y

== x == y x is equal to y

!= x != y x is not equal to y
Example program for C Relational
operators
#include <stdio.h> Output:
#include <conio.h> a>b:1
void main()
a >= b : 1
{
int a = 9, b = 4; a <= b : 0
printf(" a > b: %d \n", a > b); a<b:0
printf("a >= b: %d \n", a >= b); a == b : 0
printf("a <= b: %d \n", a <= b); a != b : 1
printf("a < b: %d \n", a < b);
printf("a == b: %d \n", a == b);
printf("a != b: %d \n", a != b);
getch();
}
Logical operators in C
• These operators Operators Name Example Description
are used to
It returns true when
perform logical && logical AND (x>5)&&(y<5)
both conditions are true
operations on the
It returns true when at-
given expressions. || logical OR (x>=10)||(y>=10) least one of the
• There are 3 logical condition is true
operators in C
language. They It reverses the state of
are, logical AND the operand “((x>5) &&
(y<5))”
(&&), logical OR ! logical NOT !((x>5)&&(y<5))
If “((x>5) && (y<5))” is
(||) and logical true, logical NOT
NOT (!). operator makes it false
Example of Logical Operators
#include<stdio.h> Output
#include<conio.h>
True
void main()
{ False
int n1 = 30, n2 = 40; True
if(n1>=40 || n2>=40)
printf(“True");
if(n1>=40 && n2>=40)
printf(“False");
If(n1!=40)
Printf(“ True”);
getch();
}
Bit wise operators in C
• These operators are used
Operator_symbol Operator_name
to perform bit operations.
Decimal values are
converted into binary & Bitwise_AND
values which are the
sequence of bits and bit | Bitwise OR
wise operators work on
these bits. ~ Bitwise_NOT
• Bit wise operators in C
language are & (bitwise ^ XOR
AND), | (bitwise OR), ~
(bitwise OR), ^ (XOR), << << Left Shift
(left shift) and >> (right
shift).
>> Right Shift
Conditional or ternary operators in C
• Conditional Syntax :
operators return one (Condition? true_value: false_value);
value if condition is Example : (A > 100 ? 0 : 1);
true and returns
another value is
condition is false. In above example, if A is
• This operator is also greater than 100 then 0 is
called as ternary returned else 1 is returned.
operator. This is equal to if else
conditional statements.
Increment/decrement Operators
Increment Syntax:
operators are used to Increment operator:
increase the value of
++var_name; (or) var_name++;
the variable by one
and decrement Decrement operator:
operators are used to --var_name; (or) var_name – -;
decrease the value of Example:
the variable by one in Increment operator : ++ i ; i ++ ;
C programs.
Decrement operator : – – i ; i – – ;
Difference between pre/post
increment & decrement operators
S. No. Operator Type Operator Description

++i Value of i is incremented before


1 Pre increment
assigning it to variable i.

i++ Value of i is incremented after


2 Post–increment
assigning it to variable i.

— –i Value of i is decremented before


3 Pre decrement
assigning it to variable i.

i– — Value of i is decremented after


4 Post_decrement
assigning it to variable i.
Control Structures Statement
• Control structures are used to alter the flow of
execution of the program.
• Why do we need to alter the program flow? The
reason is “decision making“!
• We do make a decision by analyzing certain
conditions.
Types of Control Structures
There are three types of control structures
available in C and C++
1. Sequence structure (straight line paths)
2. Selection structure (one or many branches)
3. Loop structure (repetition of a set of activities)
1. Sequence Control Structure
Sequence is
the default control
structure;
instructions are
executed one after
another.
2. Selection Control Structures
• Selection structures are used
to perform ‘decision making‘
and then branch the program
flow based on the outcome of
decision making.
• Selection structures are
implemented in C/C++ with If,
If Else and Switch statements.
• If and If Else statements are 2
way branching statements
where as Switch is a multi
branching statement.
3. Loop Control Structure
• A loop structure is used to
execute a certain set of
actions for a predefined
number of times or until a
particular condition is
satisfied.
• There are 3 control
statements available in
C/C++ to implement loop
structures.
• while, do while and for
statements.

You might also like