0% found this document useful (0 votes)
11 views68 pages

Unit 1

Uploaded by

shitaldumbre10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views68 pages

Unit 1

Uploaded by

shitaldumbre10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 68

Unit-1

General Problem Solving Concept and


Imperative Languages
Algorithm
An algorithm is a description of a procedure which
terminates with a result. Algorithm is a step-by-step method
of solving a problem.
• Properties of an Algorithm:
• 1)Finiteness: - An algorithm terminates after a finite
numbers of steps.
• 2)Definiteness: - Each step in algorithm is unambiguous.
This means that the action specified by the step cannot
be interpreted (explain the meaning of) in multiple ways &
can be performed without any confusion.
Algorithm
• 3)Input: - An algorithm accepts zero or more inputs
• 4)Output:- An algorithm should produce at least one
output.
• 5) Effectiveness: - It consists of basic instructions that are
realizable. This means that the instructions can be
performed by using the given inputs in a finite amount of
time.
Algorithm

Writing an algorithm-
• An algorithm can be written in English, like sentences and
using mathematical formulas. Sometimes algorithm
written in English like language is Pseudo code.
Pseudocode

It is a high-level description of an algorithm that uses


natural language and simple programming constructs to
convey the logic of an algorithm. Pseudocodes are like a
special language software developers use to plan an
algorithm before writing the code.
In simple terms, it is considered a way or medium to
communicate algorithms to other people (or non-
programmers) and to document algorithms for future
reference.
Pseudocode
Parameter Algorithm Pseudocode

An end-to-end description of an
A step-by-step procedure for
algorithm in formally English (or in
Definition performing the task or solving the
natural language) to convey the logic
problem.
of an algorithm.

Formality Highly formal and very precise. Less formal and more flexible.

Difficult to understand for non- Easy to read; even non-programmers


Readability
programmers can understand it too.
It can be directly executed in a
Executibility It can’t be directly executed.
programming language.
It may be tied to any specific Not tied to any specific platform and
Portability
programming language. programming language.
Structured, but not specific to any
Structure Unstructured
programming language.
Use for the actual implementation of Used as a planning tool before writing
Use Cases
code. any complex code.
Algorithm to find whether a number even or odd:
• Step1: Begin • Step1: START
• Step2: Take a number • Step2: Read num
• Step3: if the number is • Step3: if(num%2=0) then
divisible by2 then print that print num is even
number is even otherwise
number is odd otherwise number is odd
• Step4: End • Step4: STOP
( natural language)
(using pseudo code)
Flowcharts

The pictorial representation of algorithm is called


flowchart.
Uses of flow chart:
1 : flow chart helps to understand the program easily.
2 : as different symbols are used to specify the type
of operation performed, it is easier to understand the
complex programs with the help of flowcharts.
Flowcharts Symbols
S.N Description Symbols
O
1 Flowlines : These are the left to right or top to
bottom lines connection symbols. These lines
shows the flow of control through the
program.
2 Terminal Symbol : The oval shaped symbol
always begins and ends the flowchart. Every
flow chart starting and ending symbol is start End
terminal symbol.

3 Input / Output symbol : The parallelogram is


used for both input (Read) and Output (Write)
is called I/O symbol. This symbol is used to
denote any function of an I/O device in the
program.
Flowcharts Symbols
S.NO Description Symbols

4 Process Symbol : The rectangle symbol


is called process symbol. It is used for
calculations and initialization of memory
locations.
5 Decision symbol : The diamond shaped
symbol is called decision symbol. This box
is used for decision making. There will be
always two exists from a decision symbol
one is labeled YES and other labeled NO.
6 Connectors : The connector symbol is
represented by a circle. Whenever a
complex flowchart is morethan one page,
in such a situation, the connector symbols
are used to connect the flowchart.
Flowcharts
Flowcharts
Flowcharts
difference between imperative and declarative
languages
difference between imperative and declarative
languages
What is C Language?
• C is a general-purpose computer programming
language.

• C is also said to be structures programming


language or function oriented programming
language.

• C is a High level programming language.


Why do we use C?
• Generally C Language is used to create the
following…
Operating Systems
Language Compilers
Assemblers
Interpreters
Text Editors
Network Drivers
Databases
History of C Language?
• CPL – Common Programming Language invented by
Martin Richards in 1960’s
• BCPL – Basic Combined Programming Language by
Martin Richards in 1966
• B Language – by Ken Thompson & Dennis Ritchie in
1969
• Traditional C – by Dennis Ritchie in 1972
• K&R C – by Kernighan & Dennis Ritchie in 1978
• ANSI C – by ANSI Committee in 1989
• ANSI/ISO C – by ISO Committee in 1990
• C99 – by Standardization Committee in 1999 Dennis
Ritchie
Getting Started with C
Tokens of ‘C’

C language consist of some characters set, numbers and


some special symbols. The character set of C consist of all
the alphabets of English language. C consist of

• Alphabets a to z, A to Z

• Numeric 0,1 to 9
• Special Symbols {,},[,],?,+,-,*,/,%,!,;,and more
Tokens of ‘C’

The words formed from the character set are building


blocks of C and are sometimes known as tokens. These
tokens represent the individual entity of language.

The following different types of token are used in C-

• 1) Identifiers 2)Keywords 3)Constants


• 4) Operators 5)Punctuation Symbols
Identifiers
• A 'C' program consist of two types of elements , user
defined and system defined. Idetifiers is nothing but a
name given to these elements.
• An identifier is a word used by a programmer to name a
variable , function, or label.
• Identifiers consist of letters and digits if any order,except
that the first character must be letter or _.
• Both Upper and lowercase letters can be used
Constant
• A constant is an entity that doesn’t change whereas a
variable is an entity that may change.

Types of C Constants

C constants can be divided into two major categories:

(a) Primary Constants


(b)Secondary Constants
Constant
Rules for Constructing Integer Constants

(a) An integer constant must have at least one digit.


(b)It must not have a decimal point.
(c)It can be either positive or negative. If no sign precedes an
integer constant it is assumed to be positive.
(d)No commas or blanks are allowed within an integer constant.
(e)The allowable range for integer constants is -32768 to 32767.
Rules for Constructing Integer Constants

Truly speaking the range of an Integer constant depends upon the


compiler. For a 16-bit compiler like Turbo C or Turbo C++ the
range is –32768 to 32767. For a 32-bit compiler the range would
be even greater.

Ex.: 426
+782
-8000
-7605
Rules for Constructing Real Constants

Real constants are often called Floating Point constants. The real
constants could be written in two forms—Fractional form and
Exponential form.
Following rules must be observed while constructing real
constants expressed in fractional form:
(a) A real constant must have at least one digit.
(b) It must have a decimal point.
(c) It could be either positive or negative.
(d) Default sign is positive.
(e) No commas or blanks are allowed within a real constant.
Rules for Constructing Real Constants

The exponential form of representation of real constants is usually


used if the value of the constant is either too small or too large.In
exponential form of representation, the real constant is
represented in two parts. The part appearing before ‘e’ is called
mantissa, whereas the part following ‘e’ is called exponent.
Ex.: +3.2e-5
4.1e8
-0.2e+3
-3.2e-5
Rules for Constructing Real Constants

Following rules must be observed while constructing real


constants expressed in exponential form:
(a) The mantissa part and the exponential part should be
separated by a letter e.
(b) The mantissa part may have a positive or negative sign.
(c) Default sign of mantissa part is positive.
(d) The exponent must have at least one digit, which must be a
positive or negative integer. Default sign is positive.
(e) Range of real constants expressed in exponential form is
-3.4e38 to 3.4e38.
Rules for Constructing Character Constants

(a)A character constant is a single alphabet, a single digit or a


single special symbol enclosed within single inverted commas.
Both the inverted commas should point to the left.
For example
’A’ is a valid character constant whereas ‘A’ is not.
(b)The maximum length of a character constant can be1
character.
Ex.: 'A'
'I'
'5'
'='
Keywords
• Keywords are nothing but auto double int struct

system defined identifiers. break else long switch


• Keywords are reserved words of
case enum register typedef
the language.
• They have specific meaning in char extern return union
the language and cannot be
const float short unsigned
used by the programmer as
variable or constant names continue for signed void
• C is case senitive, it means
default goto sizeof volatile
these must be used as it is
• 32 Keywords in C Programming do if static while
Variables
• A variable is nothing but a name given to a storage area
that our programs can manipulate. Each variable in C has
a specific type, which determines the size and layout of
the variable's memory; the range of values that can be
stored within that memory; and the set of operations that
can be applied to the variable.
• The name of a variable can be composed of letters, digits,
and the underscore character. It must begin with either a
letter or an underscore. Upper and lowercase letters are
distinct because C is case-sensitive
Rules for Constructing Variable Names
(a) A variable name is any combination of 1 to 31 alphabets,
digits or underscores. Some compilers allow variable names whose
length could be up to 247 characters. Still, it would be safer to stick
to the rule of 31 characters. Do not create unnecessarily long
variable names as it adds to your typing effort.
(b) The first character in the variable name must be an alphabet or
underscore.
(c) No commas or blanks are allowed within a variable name.
(d) No special symbol other than an underscore (as in gross_sal)
can be used in a variable name.
Rules for Constructing Variable Names
(a) A variable name is any combination of 1 to 31 alphabets,
digits or underscores. Some compilers allow variable names whose
length could be up to 247 characters. Still, it would be safer to stick
to the rule of 31 characters. Do not create unnecessarily long
variable names as it adds to your typing effort.
(b) The first character in the variable name must be an alphabet or
underscore.
(c) No commas or blanks are allowed within a variable name.
(d) No special symbol other than an underscore (as in gross_sal)
can be used in a variable name.
Syntax For Declaring Variable Names
C compiler is able to distinguish between the variable names by making
it compulsory for you to declare the type of any variable name that you
wish to use in a program. This type declaration is done at the beginning
of the program.
Syntax :
• <type> <variable_name>; or type variableName ;
• Multiple Variables:
• <type> <variable1>, <variable2>, ..., <variableN>;
Initializing Variables:
• <type> <variable_name> = <initial_value>; or type variableName =
value;
Syntax For Declaring Variable Names
Following are the examples of type declaration statements:

Ex.: int si, m_hra ;


float bassal ;
char code ;
Hungarian Notation For Declaring Variable Names
Hungarian notation is a naming convention used in programming,
originally popularized by Charles Simonyi at Microsoft. It involves
prefixing variable names with identifiers that denote the variable's
data type or purpose.
Components of Hungarian Notation:
1) Prefixes: Hungarian notation uses prefixes to indicate the type or
purpose of a variable. The prefix typically consists of one or more
lowercase letters followed by an underscore.

Cont--
Hungarian Notation For Declaring Variable Names
2) Type Indicator: The prefix often denotes the data type of the
variable. For example:
i for integer (int)
f for float (float)
c for character (char)
b for boolean (bool)
ptr for pointer (*)

Cont--
Hungarian Notation For Declaring Variable Names
3) Purpose Indicator: Sometimes, the prefix indicates the purpose
or usage of the variable:
– n for a count or number
– sz for a zero-terminated string (char*)
– p for a pointer
Examples of Hungarian Notation:
• int iCount; (where i indicates integer)
• float fTemperature; (where f indicates float)
• char *pszName; (where psz indicates a pointer to a zero-terminated string)
• BOOL bIsReady; (where b indicates a boolean)
Datatypes in c
Datatypes in c
What is Endianness?

Endianness refers to the order in which bytes are arranged


in memory. Different languages read their text in different
orders. for example, English reads from left to right, while
Arabic reads from right to left. Endianness works similarly
for computers.
Endianness comes in two primary forms: Big-endian (BE)
and Little-endian (LE).
What is Endianness?

• Big-endian (BE): Stores the most significant byte (the “big


end”) first. This means that the first byte (at the lowest
memory address) is the largest, which makes the most
sense to people who read left to right.
• Little-endian (LE): Stores the least significant byte (the
“little end”) first. This means that the first byte (at the
lowest memory address) is the smallest, which makes the
most sense to people who read right to left.
What is Endianness?
Operators in c language
Increment and Decrement Operators
C programming has two operators increment ++ and
decrement -- to change the value of an operand (constant
or variable) by 1.
1. Increment ++ increases the value by 1 whereas
decrement -- decreases the value by 1.
2. These two operators are unary operators, meaning
they only operate on a single operand.
Compound Assignment Operators
An assignment operator is used for assigning a value to a
variable. The most common assignment operator is =
Operator Example Same as
= a=b a=b
+= 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
C Instructions
There are basically three types of instructions in C:
1) Type Declaration Instruction
2) Arithmetic Instruction
3) Control Instruction
The purpose of each of these instructions is given below:
• Type declaration instruction − To declare the type of
variables used in a C program.
• Arithmetic instruction − To perform arithmetic operations
between constants and variables.
• Control instruction − To control the sequence of
execution of various statements in a C program.
C Instructions-Type Declaration Instruction
This instruction is used to declare the type of variables
being used in the program. Any variable used in the
program must be declared before using it in any
statement. The type declaration statement is written at the
beginning of main( ) function.
Syntax:
type variableName = value;

Ex.: int bas ;


float rs, grosssal ;
char name, code ;
C Instructions-Arithmetic Instruction
A C arithmetic instruction consists of a variable name on
the left hand side of = and variable names & constants on
the right hand side of =. The variables and constants
appearing on the right hand side of = are connected by
arithmetic operators like +, -, *, and /.

Ex.
deta = alpha * beta / gamma + 3.2 * 2 / 5 ;
C Instructions-Arithmetic Instruction
A C arithmetic statement could be of three types. These
are as follows:

(a) Integer mode arithmetic statement - This is an arithmetic


statement in which all operands are either integer variables
or integer constants.
Ex.: int i, king, issac, noteit ;
i=i+1;
king = issac * 234 + noteit - 7689 ;
C Instructions-Arithmetic Instruction
A C arithmetic statement could be of three types. These
are as follows:

(b) Real mode arithmetic statement - This is an arithmetic


statement in which all operands are either real constants or
real variables.
Ex.: float qbee, antink, si, prin, anoy, roi ;
qbee = antink + 23.123 / 4.5 * 0.3442 ;
si = prin * anoy * roi / 100.0 ;
C Instructions-Arithmetic Instruction
A C arithmetic statement could be of three types. These
are as follows:

(c) Mixed mode arithmetic statement - This is an arithmetic


statement in which some of the operands are integers and
some of the operands are real.
Ex.: float si, prin, anoy, roi, avg ;
int a, b, c, num ;
si = prin * anoy * roi / 100.0 ;
avg = ( a + b + c + num ) / 4 ;
C Instructions-Arithmetic Instruction
While writing C arithmetic statement Note the following
points carefully.
(a) C allows only one variable on left-hand side of =. That
is, z = k * l is legal, whereas k * l = z is illegal.
(b) C also provides a modular division operator (%). This
operator returns the remainder on dividing one integer
with another.
(c) An arithmetic instruction is often used for storing
character constants in character variables.
Ex. char a = 'F' ;
C Instructions-Arithmetic Instruction
(d) Arithmetic operations can be performed on ints, floats
and chars
(e) No operator is assumed to be present. It must be written
explicitly.
a = c.d.b(xy) usual arithmetic statement
b = c * d * b * ( x * y ) C statement

(f) Unlike other high level languages, there is no operator


for performing exponentiation operation.
Integer and Float Conversions
Rules that are used for the implicit conversion of floating
point and integer values in C
(a) An arithmetic operation between an integer and integer
always yields an integer result.
(b) An operation between a real and real always yields a
real result.
(c) An operation between an integer and real always yields
a real result. In this operation the integer is first promoted
to a real and then the operation is performed. Hence the
result is real.
Type Conversion in Assignments
It may so happen that the type of the expression and the type of the
variable on the left-hand side of the assignment operator may not
be same. In such a case the value of the expression is promoted
or demoted depending on the type of the variable on left-hand
side of =.
Ex.
int i ;
float b ;
i = 3.5 ;
b = 30 ;
float a, b, c ;
int s ;
s = a * b * c / 100 + 32 / 4 - 3 * 1.1 ;
Hierarchy of Operations

The priority or precedence in which the operations in an


arithmetic statement are performed is called the hierarchy
of operations. The hierarchy of commonly used operators
is shown in Figure below-
Hierarchy of Operations
Associativity of Operators
When an expression contains two operators of equal priority
the tie between them is settled using the associativity of
the operators. Associativity can be of two types—
Left to Right or Right to Left.

Left to Right associativity means that the left operand must


be unambiguous.Similarly, in case of Right to Left
associativity the right operand must be unambiguous. Let
us understand this with an example.
Associativity of Operators
a=3/2*5;

only / has unambiguous left operand (necessary condition


for L to R associativity) it is performed earlier.
Associativity of Operators
a=b=3;

the second = has unambiguous right operand (necessary


condition for R to L associativity) the second = is
performed earlier.
C Instructions-Control Instructions
There are four types of control instructions in C. They are:

(a) Sequence Control Instruction


(b) Selection or Decision Control Instruction
(c) Repetition or Loop Control Instruction
(d) Case Control Instruction
Sections of the C Program
Sections of the C Program

There are 6 basic sections responsible for the proper


execution of a program. Sections are mentioned below:
1. Documentation
2. Preprocessor Section
3. Definition
4. Global Declaration
5. Main() Function
6. Sub Programs
Structure of C Program

// Documentation
/* description: program to find sum. */
// Link or Preprocessor Section
#include <stdio.h>
// Definition
#define X 20
// Global Declaration
int sum;
Structure of C Program
// Main() Function
int main(void)
{
statement 1;

statement n;
}
// Subprogram
return type fun_name(argument list)
{ statements }
C Input Output
• The general form of printf( ) function is,
printf ( "<format string>", <list of variables> ) ;
Ex:-
printf ( "%f", si ) ;
printf ( "%d %d %f %f", p, n, r, si ) ;
printf ( "Simple interest = Rs. %f", si ) ;
• The general form of scanf( ) function is,
scanf ( "<format string>",&<list of variables> ) ;
Ex:
scanf ( "%d", &p ) ; or scanf ( "%d %d %f", &p, &n, &r ) ;

You might also like