C Theory Notes
C Theory Notes
PROGRAMMING LANGUAGE
LOW LEVEL LANGUAGE
ASSEMBLY LANGUAGE:
Assembly language is only understand by human beings not by the computers.
In assembly language data can be represented with the help of mnemonics such
as Mov, Add, Sub, End etc. Assembly language is easy to understand by the
human being as compare to machine language. Modifications and error fixing
can be done in assembly language. Easy to memorize the assembly language
because some alphabets and mnemonics are used. Execution is slow as
compared to machine language. Assembler is used as translator to convert
mnemonics into machine understandable form. Assembly language is the
machine dependent and it is not portable.
MACHINE LANGUAGE:
Machine language is only understand by the computers. In machine language
data only represented with the help of binary format(0s and 1s), hexadecimal
and octadecimal. Machine language is very difficult to understand by the human
beings. Modifications and error fixing cannot be done in machine language.
Machine language is very difficult to memorize so it is not possible to learn the
machine language. Execution is fast in machine language because all data is
already present in binary format. There is no need of translator.The machine
understandable form is the machine language. Machine language is hardware
dependent.
1
What is C?
The C language was developed by Dennis Ritchie in 1970s at AT&T Bell
laboratories) Murray Hill, New Jersey. Initially it was designed for programming
in the operating system called UNIX. After the advent of C, the whole UNIX
operating system was rewritten using it. Now almost the entire UNIX operating
system and the tools supplied with it including the C compiler itself are written
in C. The C language is derived from the B language, which was written by Ken
Thompson at AT&T Bell laboratories. The B language was, adopted from a
language called BCPL (Basic Combined Programming Language), which was
developed by Martin Richards at Cambridge University. In 1982 a committee
was formed by ANSI(American National Standards Institute) to standardize the
C language. Finally in 1989, the standard for C language was introduced known
as ANSI C. Generally most of the modern compilers conform to this standard.
Why Learn C?
It is one of the most popular programming languages in the world
If you know C, you will have no problem learning other popular
programming languages such as Java, Python, C++, C#, etc, as the
syntax is similar
C is very fast, compared to other programming languages,
like Java and Python
C is very versatile; it can be used in both applications and technologies
Characteristics of C
C is a middle level language. It has the simplicity of a high level language as
well as the power of a low level language. This aspect of C makes it suitable for
writing both application programs and system programs. Thus it is an excellent,
efficient and general-purpose language for most of the applications such as
mathematical, scientific, business and system software applications
Translators
The three types of translators used are-
Assembler
Compiler
Interpreter
2
Assembler is used for converting the code of low-level language (assembly
language, into machine language.
4
2 FUNDAMENTALS OF C
CHARACTER SET
THE CHARACTER THAT ARE USED IN C PROGRAMS ARE GIVEN BELOW
GRAPHIC CHARACTER
5
Whitespace Characters
Space character, newline character, horizontal tab, vertical tab,
and form feed.
Other characters
Alert, backspace, carriage return and null character
6
DELIMITERS
: Colon Used for label
; Semicolon End of statement
( ) Parentheses Used in expression
[ ] Square Brackets Used for arrays
{ } Curly Braces Used for block of statements
# Hash Preprocessor directive
, Comma Variable delimeter
RESERVED WORDS/KEYWORDS
IDENTIFIERS
All the words that we will use in our C programs will be either keywords or
identifiers. Keywords are predefined and can't be changed by the user, while
identifiers are user defined words and are used to give names to entities like
variables, arrays, functions, structures etc.
(1) The name should consist of only uppercase and lower case letters, digits
and underscore sign().
(4) Since C is case sensitive, the uppercase and lowercase letters are
considered different. For example code, Code and CODE are three different
identifiers.
7
(5) An identifier name may be arbitrarily long. Some implementations of C
recognize only the first eight characters, though most implementations
recognize 31 characters. ANSI standard compilers recognize 31characters.
DATA TYPES
A data type defines a domain of allowed values and the operations that can
be performed on those values. Storage representation of different data types
is different in memory.
There are four fundamental datatypes in C. which are int, char, float and
double.
We can use type qualifiers with these basic types to get some more types,
There are two types of type qualifiers-
1. Size qualifiers - short, long
2. Sign qualifiers – signed, unsigned
The qualifiers signed and unsigned can be applied to char and integer types.
When the qualifier unsigned is used the number is always positive, and when
signed is used number may be positive or negative.
The qualifiers short and long can be applied to int type to get types short int
and long int. qualifier long can be applied to double to get type long double
which stores extended precision floating point number. The size and range of
different data types on a 16-bit machine is given in the following table. The
size and range may vary on machines with different word sizes
8
Basic data Data types with type Size Range
type qualifiers (bytes)
char Signed char 1 -128 to 127
Unsigned char 1 0 to 255
int Int or signed int 2 -32768 to 32767
Unsigned int 2 0 to 65535
Short int 1 -128 to 127
Short int or signed int 1 0 to 255
Long int or signed int 4 -2147483648 to 2147483647
Unsigned long int 4 0 to 4294967295
float Float 4 3.4E-38 to 3.4E+38
double Double 8 1.7E-308 to 1.7E+308
Long double 10 3.4E-4932 to 1.1E+4932
CONSTANTS
constants
Integer Real
constant constants
Numeric Constants
Numeric constants consist of numeric digits, they may or may not have
decimal point for defining numeric constants-These are the rules
9
Integer Constants
There are three types of integer constants based on different number
systems(decimal, octal, hexadecimal). The permissible characters that can
be used in these constants are-
Character Constants
A character constant is a single character that is enclosed within single
quotes. Some valid character constants
String Constants
A string constant is enclosed within double quotes (""). At the end of string,
the null character "\0" is automatically placed by the compiler. Some
examples of string constants are-
Variables
Variable is a name that can be used to store values, it can take different
values but one at a time. These values can be changed during execution of
the program. A data type is associated with each variable and it decides
what values the variable can take. The rules for naming variables are same
as that for naming identifiers.
10
Declaration of Variables
It is must to declare a variable before it is used in the program. Declaration
of a variable specifies its name and datatype. The type and range of values
that a variable can store depends upon its datatype. The syntax of
declaration of a variable is- declare more than one variable in a single
declaration. For example- int x,y,z,total;
Initialization of Variables
When a variable is declared it contains undefined value commonly known as
garbage value. If we want we can assign some initial value to the variable
during the declaration itself, this is called initialization of the variable.
For example-
int a-5;
float x=6.9;
y=10.5;
char ch='y':
double num=0.15197e-7;
int l, m, n, total=0;
11
3
33
Input and output
(There are three main functions of any program: it takes data as input,
processes this data and gives output. The Input operation involves
movement of data from an input device (generally keyboard) to computer
memory, while in output operation the data moves from computer memory
to the output device (generally screen). C lan- guage does not provide any
facility for input-output operations. The input output is performed through a
set of library functions that are supplied with every C compiler. These
functions are formally not a part of the language but they are considered
standard for all input-output operations in C. The set of library functions that
per-forms input-output operations-is known as standard I/O library.
There are several header files that provide necessary information in support
of the various library functions.These header files are entered in the
program using the #include directive at the beginning of the program.For
example if a program uses any function from the standard I/O library, then
it should include the header file
stdio.h as-
#include<stdio.h>
Similarly there are other header files like math.h, string.h, stdlib.h that
should be included when certain library functions are used. In this chapter
we will discuss about the input functions scanf() and getchar() and the
output functions printf() and putchar(). There are several other input-output
functions that will be discussed in other chapters.A simple method of
providing value to variables is by assignment statement.
Conversion Specifications
12
%c print a single character
%d print a integer
%f print a floating point number(by default 6 digits printed after
the point)
%s print a string
%o print an unsigned octal integer
%x print an unsigned hexadecimal integer using a,b,c,d,e,f
%x print an unsigned hexadecimal integer using A,B,C,D,E,F
%u print an unsigned decimal integer
13
4 OPERATORS AND
EXPRESSION
This is a symbol use to perform some operation on variables, operands or
with the constant. Some operator required 2 operand to perform operation
or Some required single operation.
Several operators are there those are,
arithmetic operator
Assignment Operator
Increment Operator
Decrement Operator
Logical Operator
Conditional Operator
Comma Operator
Size Of Operator
Bitwise Operator
1. Arithmatic Operator
This operator used for numeric calculation. These are of either Unary
arithmetic operator, Binary arithmetic operator.
And these operators are addition, subtraction, multiplication, division.
Binary arithmetic operator on other hand required two operand and its
operators are +(addition), -(subtraction), *(multiplication), /(division),
%(modulus). But modulus cannot applied with floating point operand as
well as there are no exponent operator in c. Unary (+) and Unary(-) is
different from addition and subtraction. When both the operand are
integer then it is called integer arithmetic and the result is always integer.
2. Assignment Operator
A value can be stored in a variable with the use of assignment operator. The
assignment operator(=) is used in assignment statement and assignment
expression. Operand on the left hand side should be variable and the
operand on the right hand side should be variable or constant or any
expression. When variable on the left hand side is occur on the right hand
side then we can avoid by writing the compound statement. For example, int
x= y; int Sum=x+y+z;
14
3. Increment & Decrement Operator
The Unary operator ++, --, is used as increment and decrement which acts
upon single operand. Increment operator increases the value of variable by
one .Similarly decrement operator decrease the value of the variable by one.
And these operator can only used with the variable, but can't use with
expression and constant as ++6 or ++(x+y+z) It again categories into
prefix post fix . In the prefix the value of the variable is incremented 1st,
then the new value is used, where as in postfix the operator is written after
the operand(such as m++,m--).
4. Relational Operator
It is use to compared value of two expressions depending on their relation.
Expression that contain relational operator is called relational expression.
Here the value is assign according to true or false value.
A. (A>=B) || (B>20)
B. (B>A) && (E>B)
C. 0(B!=7)
5. Conditional Operator
It sometimes called as ternary operator. Since it required three expressions
as operand and it is represented as (? , :)
SYNTAX
exp1 ? exp2 :exp3 Here exp1 is first evaluated. It is true then value return
will be exp2 . If false then exp3.
6. Comma Operator
Comma operator is use to permit different expression to be appear in a
situation where only one expression would be used. All the expression are
separator by comma and are evaluated from left to right.
15
7. Sizeof Operator
Size of operator is a Unary operator, which gives size of operand in terms of
byte that occupied in the memory. An operand may be variable, constant or
data type qualifier. Generally it is used make portable program(program that
can be run on different machine) . It determines the length of entities,
arrays and structures when their size are not known to the programmer. It is
also use to allocate size of memory dynamically during execution of the
program.
8. Bitwise Operator
Bitwise operator permit programmer to access and manipulate of data at bit
level. Various bitwise operator enlisted are
one's complement (~)
bitwise AND (&)
bitwise OR (|)
bitwise XOR (^)
left shift (<>)
These operator can operate on integer and character value but not on float
and double. In bitwise operator the function showbits( ) function is used to
display the binary representation of any integer or character value. In one's
complement all 0 changes to 1 and all 1 changes to 0. In the bitwise OR its
value would obtaining by 0 to 2 bits. As the bitwise OR operator is used to
set on a particular bit in a number. Bitwise AND the logical AND. It operate
on 2operands and operands are compared on bit by bit basic. And hence
both the operands are of same type.
16