cp unit-1 notes
cp unit-1 notes
1
Faculty: Ch.Lavanya Susanna, dept of cse
instruction written in the file. The result generated from the execution is placed in a window called User
Screen.
Step 4: Check Result
After running the program, the result is placed into User Screen. Just we need to open the User Screen
to check the result of the program execution.
Storing integers:
Integers are whole numbers (no fractional part) or An integer is a number with no fractional part;
it can be positive, negative or zero which will be stored in computer memory using 2 bytes (16 bits) or 4
bytes (32 bits) depending on machine.
The MSB (most significant bit) bit is used to indicate whether the number is positive or negative.
2
Faculty: Ch.Lavanya Susanna, dept of cse
For positive numbers MSB will be 0.
For negative numbers MSB will be 1.
Storing unsigned values: First, compiler converts decimal number system to binary number system
(100010110) internally and then takes into consideration only the first 16 bits or 32 bits (depending on
machine)from the right of that number represented in binary and stores it because the size of integer is 2
or 4 bytes
Storing signed values: Compiler first takes its 2 complement and then displays the number with
negative sign.
Computer uses special mechanism to store negative numbers which is 2‘s complement format.
For this the compiler does ones complement to the binary number and then add 1 to the ones
complement
1’s complement of a number
1‘s compliment of number is just inverting binary bits of an actual number.
Example:
10
Binary representation will be (1 0 1 0) 2
1‘s compliment of 10 is, 0 1 0 1 (switching 0 to 1 and 1 to 0).
2’s complement of a number
To get 2‘s complement of a number, just add 1 to 1‘s complement of an actual number.
3
Faculty: Ch.Lavanya Susanna, dept of cse
Performing addition and subtraction by computer:
Binary addition:
The method of addition, there are four cases or steps to remember before proceeding through the
operation. These are
As there no numbers other than 0 and 1 in the binary number system these four steps include all the
possible operations of addition.
A= 149 B=134
Binary subtraction:
Computers though, don‘t perform subtraction; they don‘t have the circuits built in that support it. Instead
they take another approach
C = A + (-B)
“Computers actually perform subtraction by adding”.
The steps are as follows.
Step 1: Calculate the Two‘s Complement of our number B.
Step 2: Add the Two‘s Complement of B to A to Get Our Result (C)
4
Faculty: Ch.Lavanya Susanna, dept of cse
2. Floating Point Notation: Floating point number allows for a varying number of digits after the
decimal point. This representation does not reserve a specific number of bits for the integer part or the
fractional part.
5
Faculty: Ch.Lavanya Susanna, dept of cse
1 1 1 1 0 1 1 1 1
To calculate the exponent, it is necessary to determine how many places the decimal point would need to
move to give the correct number. In this case the decimal point would need to move six places to the
right i.e 1111011.11
So the exponent for our number is 6. In binary, the number six is i.e 110 in binary
In order to represent 123.75 the mantissa would be 111101111 and the exponent would be 110. This can
be thought of as:M X 2E (where M represents the mantissa and E represents the exponent)
1.11101111 x 2110
6
Faculty: Ch.Lavanya Susanna, dept of cse
Background of c:
C is a general-purpose, high-level language that was originally developed by Dennis Ritchie to develop
the UNIX operating system at Bell Labs.
Taxonomy of C:
Characteristics of C language
Programs Written in C are efficient and fast. This is due to its variety of data type and powerful
operators
C is super fast. The compilation and execution of programs is much faster on C than with most
other language
C is highly portable this means that programs once written can be run on another machines with
little or no modification.
C program is basically a collection of functions that are supported by C library. We can also
create our own function and add it to C library.
C is modular, which means C programs can be divided into small modules, which are much
easier to understand.
C is a structured programming language which emphasis on writing set of instructions step by
step to get the output rather than concentrating on data or code
Uses of C language
C programming language can be used to design the system software like operating system and
Compiler.
To develop application software like database and spread sheets.
For Develop Graphical related application like computer and mobile games.
To evaluate any kind of mathematical equation use c language.
C programming language can be used to design the compilers.
UNIX Kernal is completely developed in C Language.
7
Faculty: Ch.Lavanya Susanna, dept of cse
For Creating Compilers of different Languages which can take input from other language and
convert it into lower level machine dependent language.
C programming language can be used to design Operating System.
C programming language can be used to design Network Devices.
To design GUI Applications. Adobe Photoshop, one of the most popularly used photo editors
since olden times, was created with the help of C.
C is best used for
Systems that require fast and direct access to hardware
Systems with limited resources (like memory)
Systems where performance is the most important attribute
Some examples are BIOSes, Operating Systems, Embedded Systems, Real-time systems
1. Source file: Program is a set of instructions created using programming languages like C, C++, Java,
etc., are written using a high-level language like English. The C program or source code contains set of
instructions written in C language and it is saved as ‗filename. c‘. The file extension of c program is ‗.c‘
2. Header file: Header files are helping file of C program which holds the definitions of various
functions/Subroutines. The entire header file have a '.h' an extension which are included into C program
using preprocessor directive ―#include ―.Including a header file means that using the content of header
file in the source program. When header files are included in C program using “#include <filename.h>”
command, all C code of the header files are included in C program. Missing the inclusion of appropriate
header file will generate an error. Some of the header files are
• Stdio.h - for standard input and output functions
• String.h - for string handling functions
• Math.h - for mathematical functions
• Conio.h - for clearing the screen
3. Object file: An object file is the real output from the compilation phase which contains mostly
machine code i.e. binary representation of the instructions of the source code. Linker uses object file to
produce executable file. Object files have ‗.o‘ file extension.
4. Executable file: After completing compilation successfully, an executable file is created with
a .exe extension. The processor can understand this .exe file. On receiving .exe file, CPU performs the
task according to the instruction written in the file. The result generated from the execution is placed in a
window called User Screen.
8
Faculty: Ch.Lavanya Susanna, dept of cse
Structure of c program: A C program contains more sections as shown below
5. Main () section: Every C program must have one main() section. This contains two parts:
a) Declaration part: This part contains all variables which are used in executable part.
b)Executable part: This part contains the statements/instructions of the program.
6. Subprogram section: This section contains all the subprograms which are placed immediately after
main ().
Character set:
A character set is a set of alphabets, letters and some special characters that are valid in C language.
• Alphabets
Uppercase: A B C ................................... X Y Z
Lowercase: a b c ...................................... x y z
C language is case sensitive i.e. uppercase letters and lowercase letters are not same
• Digits
0123456789
• Special Characters
, < ,>, ., _ , ( , ) , ; , $ , : , % , [ , ] , # , ? , ‗ , & , { , } , ― , ^ , ! , * , / , | , - , \ , ~ , +
• White space Characters
Blank space, newline, horizontal tab etc…
9
Faculty: Ch.Lavanya Susanna, dept of cse
C Tokens: Tokens are smallest individual unit Identifiers: Identifier refers to name given to
of program program elements such as variables, functions
etc.
Rules
• An identifier can only have
alphanumeric characters (a-z , A-Z , 0-9)
(i.e. letters & digits) and underscore( _ )
symbol.
• Identifier names must be unique
• The first character must be an alphabet
or underscore.
• Keywords cannot be used as identifiers.
• Only first thirty-one (31) characters are
Keywords: Keywords are predefined; reserved significant.
words used in programming that have special • Must not contain white spaces.
meanings to the compiler. • Identifiers are case-sensitive i.e
uppercase and lowercase letters are
different
Examples:
Valid invalid
the value of x is 20
13
Faculty: Ch.Lavanya Susanna, dept of cse
How To Use Constants In A C Program? printf("\n The area of the circle with radius %f
We can define constants in a C program in the is: %f\n", radius, area);
following ways. }
1. By ―const‖ keyword
Syntax: const datatype variable = value ; Output:
Enter the radius of the circle:3
2. By ―#define‖ preprocessor directive
The area of the circle with radius 3.000000
Syntax: #define name value is:29.608951
Rules: 1.symbolic name must be in uppercase Basic data types:
2. No semicolon at the end of statement
3. No symbols used between name and
value
Trying to change constant values after defining
in C program, it will through error.
Evaluating expressions
An expression is a collection of operators and
operands.
operator is a symbol that performs tasks like
arithmetic operations, logical operations, and
conditional operations, etc.
Operands are the values on which the
operators perform the task
Example
A+B
Here operator is + and operands are A and B
15
Faculty: Ch.Lavanya Susanna, dept of cse
Step1: calculate the value of operator / which has Syntax: operator operand1 operand2;
higher precedence over + and – Example: a b +;
Side effects:
Ans is -8
Examples:
int x = 10;
y = ++x
This statement is similar to x = x + 1
y=x
first x value gets incremented i.e 1 is added to
value x and second the value which is
incremented of x is assigned to y.
Ans- 41
x = x+1 = 10 + 1 = 11
Types of expressions:
y =11
1. Infix expression: An expression in which an
operator is placed in-between operands is called as
Infix expression
Syntax: operand1 operator operand2;
Example: a + b ;
2. Prefix expression: An expression in which an
operator is placed before operands is called as Prefix Here the variable x has a side effect as its value
expression. is changed from 10 to 11.
Syntax: operator operand1 operand2; Pre decrement operator: The pre decrement
Example: + a b ; operator is ‗--‘ .This operator is placed before the
3. Postfix expression: An expression in which an operand which subtracts 1 from its operand
operator is placed after operands is called as Postfix before it is used.
expression.
16
Faculty: Ch.Lavanya Susanna, dept of cse
Consider the following example: In a programming language, the
int x = 10; expression contains data values of the same
y = ++x datatype or different data types. When the
This statement is similar to x = x - 1 expression contains similar datatype values then
y=x it is evaluated without any problem. But if the
first x value gets decremented i.e 1 is subtracted expression contains two or more different
from value x and second the value which is datatype values then they must be converted to
decremented of x is assigned to y. the single datatype.
Type conversion refers to changing an
variable of one data type into another type.
Type conversion in c can be classified
x = x-1 = 10 - 1 = 9 into the following two types:
y =9 1. Implicit Type Conversion
2. Explicit Type Conversion
Examples:
int x = 10;
y = x ++
This statement is similar to y = x
x=x+1
first the value of x is assigned to y and then the
value of x is incremented.
All lower datatype values are automatically
converted to higher datatype values
#include <stdio.h>
y = 10
void main()
x = 10 + 1 = 11
{
float x = 90 ;
float f = 'A' ;
printf("x value is %f\n",x);
printf("f value is %f\n",f);
Here the variable x has a side effect as its value }
is changed from 10 to 11. Output:
(Similarly post decrement also) x value is 90.000000
Evaluate the following expression with d value is 65.000000
sideeffect: --3 * ( 3 + 4) / 2 – 5 * 4 ans = - 13
Type conversion:
17
Faculty: Ch.Lavanya Susanna, dept of cse
Program to demonstrate implicit type The following rules have to be followed while
conversion of higher datatype value to lower converting the expression from one type to
datatype value another to avoid the loss of information:
#include <stdio.h>
void main() All integer types to be converted to float.
{ All float types to be converted to double.
int i = 95.3 ; All character types to be converted to
float x = 90.9945678934567 ; integer.
printf("i value is %d\n",i);
printf("x value is %f\n",x); C program to find average of two numbers:
} #include <stdio.h>
Output: void main()
i value is 95 x {
value is 90.994568 int a,b;
float avg;
If higher datatype value is assigned to lower printf("enter a and b values\n");
datatype value then the data will be lost. scanf("%d%d",&a,&b);
We cannot perform implicit type casting on avg=(float)a/(float)b;
the data types which are not compatible with printf("the average of two numbers is
each other such as: %f\n",avg);
1. Converting float to an int will truncate the }
fraction part hence losing the meaning of Output:
the value. enter a and b values
2. Converting double to float will round up 3 2
the digits. the average of two numbers is 1.500000
3. Converting long int to int will cause
dropping of excess high order bits. Input and output streams: A stream is
sequence of bytes.
2. Explicit Type Conversion (type casting):
Explicit type conversion is also known as type
casting. The type conversion performed by the
programmer by posing the data type of the
expression of specific type is known as explicit
type conversion.
Type casting in c is done in the following form:
SYNTAX: (Datatype) Expression the input streams provides data to the program
and output stream receives data from the
Example: program.
int a =3 , b= 2 C language supports two functions:
float c; 1.prinf()
c=a/b 2.scanf()
ans is c =1
c = (float) a / (float) b ; 1.prinf():this function is used to print the
ans is c= 1.5 information on the user screen.
Here integer value is explicitly converted to float syntax: printf("control string", variable list);
value so correct result displayed without data here control string contains two parts
lost.
18
Faculty: Ch.Lavanya Susanna, dept of cse
a)sequence of characters to be displayed on 2)Real numbers:
the screen float a=10.123456;
b)format specifiers
variable list contains variables, separated by
commas
example:
int a=10;
printf("the a value is %d\n",a);
output:The a value is 10.
The format specifier has following
specification
% W.V typespecifier
% - indicates conversion
fieldwidth(W.V) - W specifies the total no:of
columns for output. Rules for printf():
- V specifies no:of digits to 1.characters that are specified in control string
be printed. are displayed as it is
type specifiers 2.escape sequences linke \n,\t are not displaed on
c - character screen
d - integer 3.for each format specifier in control string there
f - float must be a vaiable
lf - double 2)scanf():used to read the data from the
s - string keyboard according the format specified.
u - unsigned int syntax: scanf("control string",&var1,....,&var n);
h - short int the control string contains two parts:
o - octal a)precent sign
x - hexa decimal b)followed by typespecifier
examples:1)integers variables contains & symbol before them which
int a= 4567 ; indicates address.
example:
int a,b;
scanf("%d%d",&a,&b);
The format specifier has following
specification:
% wd
% - indicates conversion
w - no:of digits to be read
d - typespeifier
Examples inputting integers:
Data entered by user is 50 314268
19
Faculty: Ch.Lavanya Susanna, dept of cse
20