SlideShare a Scribd company logo
Computer Fundamentals
&
C Programming
Ajit K Nayak, Ph.D.
S„O‟A University, Odisha
Module I
Contents
• Introduction to Computers
• Evolution of Computers
• Computer Generations
• Basic Computer
Organization
• Memory Hierarchy
• I/O devices
• Computer Software
• Planning Computer Program
• Computer Languages
• Introduction to C
programming
• Structure of C Programming
• Datatype
• Constant
• Variable
• Expression
• Conditional Expression
• Precedence & order of
evaluation
Introduction
• Computer is a fast computing device
• Today‟s Computing refers to computer
supported commercial & scientific
applications.
• Example of computing
– Web services, Control system (missile, vehicular,
household device, Industry) . . .
• Is it comparable to human brain?
– It is fast but not smart!!!
Definitions
• A computer is an electronic device that
performs arithmetic & logic operation at a
very high speed
• It is also called a data processor that can
store, process and retrieve data as and
when desired.
Input
(Data)
Process /
Manipulate
Output
(Information)
Evolution (1)
• 1642: Blaise Pascal invented “Mechanical
Adding Machine”
• 1671: Baron Gottfried invented “Calculator”
for multiplication
• 1880: “Keyboard machines” designed in
USA
• 1880: Herman Hollerith invented “punched
cards” for input media & is used till late 70s
Evolution (2)
• Charles Babbage is considered to be the
father of modern digital computers
– Designed difference engine in 1822
– Designed a fully automatic analytical engine in 1842
for performing basic arithmetic operations
– Established a number of principles that are
fundamental to the design of any digital computer
Well known Early Computers
• MARK I computer (1937 - 1944)
– Electro-mechanical calculating device, 50 ft-long x 8 ft-high
– Add: 0.3 sec, mul:4.5 sec
• Atanasoff-Berry Computer (1939-1942)
– Electronic machine to solve some mathematical functions
• ENIAC (1943-1946)
– All electronic computer 20x40 sqft, add: 200 us, mul: 2000us
• EDVAC (1946-52)
– Used stored program concept, instructions & data stored in
binary form
• UNIVAC I (1951)
– First digital computer
Generation (I,II)
Generation (III)
Generation (IV)
Generation (V)
Basic Operations
• Input
– The process of entering instructions and data to the computer system
• Store
– Saving instruction & data to make them available as and when required
for processing
• Process
– Performing arithmetic & logic operations on data using corresponding
instructions to produce information
• Output
– To produce the information/result of the processing as a printed report or
visual display
• Control
– Directing the manner and sequence in which all above operation are
performed
Basic Organization
Input
Unit
Output
Unit
Secondary Storage
Primary Storage
Control Unit
Arithmetic
Logic Unit
Program & Data
Storage Unit
Information (Results)
Centarl Processing Unit
(CPU)
Data
Storage (Memory)
• Primary Storage
– Used to hold data, ongoing job’s instructions,
intermediate results
– Fast in operation
– Small capacity, expensive, volatile
– Ex: RAM, ROM, PROM, EPROM
• Secondary Storage
– Used to store stored program & data
– Slower, large capacity, cheaper, persistent
– Ex: Hard Disk, Magnetic Tape, Floppy, CD, Flash
Memory Hierarchy
Cache
Memory
MainMemory
On-line, Direct-access,
and sequential access secondary
storage such as hard disk
Off-line, Direct-access, and sequential
access secondary storage such as magnetic tape,
floppy disk, zip disk, worm etc
Mass storage device such as tape library, CD etc
CPU
• Arithmetic & Logic Unit (ALU)
– Actual execution of the instruction takes place
– Arithmetic Operations like: add, subtract, etc
– Logic Operation: <, >, and, or, etc
• Control Unit (CU)
– Manages & coordinates all other components of a
computer system.
– It acts like a central nervous system of computer
– Ex: It decides when to execute which instruction
Input Devices
• Keyboard
• Point & Draw devices
– Mouse, Trackball, Joystick
• Data Scanning Devices
– Image Scanner, OCR, Barcode reader, MICR
• Digitizer
– Used to pictures, maps etc to digital form
• Electronic Card
• Speech Recognition system
Output Device
• Monitors
• Printers
• Plotters
• Screen Image Projector
CFCP / Module I / AKN / 19
Basic Terminologies
• Hardware
– Physical devices of a computer system
• Software
– A Set of programs
• Program
– Sequence of instructions written using a computer
language to solve a small problem.
• Software Package
– A group of program that solve a specific type of job
CFCP / Module I / AKN / 20
H/W vs. S/W
• S/W makes H/W usable
• They are complementary to each other
• Same H/W may perform different jobs using
different S/W
CFCP / Module I / AKN / 21
Types of S/W
• System Software
– Are designed to control/manage and extend the
processing capabilities of a computer system.
• Application Software
– Designed to solve a specific problem or to do a
specific task.
CFCP / Module I / AKN / 22
Logical System Architecture
USERS
(Normally interact with the system via the user
interface provided by the application S/W)
APPLICATION SOFTWARE
(Software that do a specific task to solve a
specific problem)
SYSTEM SOFTWARE
(Constitute the operating and
programming environment)
HARDWARE
(Physical
devices/components of a
computer system)
CFCP / Module I / AKN / 23
Program Planning
• To write a correct program, a programmer
must write each and every instruction in the
correct sequence
• i.e. it should be ensured that the
instructions are
– Appropriate for the program &
– In the correct sequence
CFCP / Module I / AKN / 24
Algorithm
• Also called the ‘program logic’ is a step-by-
step description of how to arrive at solution
of a given problem.
• Algorithm has following characteristics
– Each and every instructions should be precise &
unambiguous
– Each instruction should be executed in finite time.
– After execution of all the instructions, it should
produce the desired result.
CFCP / Module I / AKN / 25
Example 1
• Problem
– Mark-sheets of 50 students are available
– Calculate & produce the total number of student passed in
‘first division’
• Solution
1. Initialize totalFirstDivision, and totalMarksheetsChecked to
zero.
2. Take the marksheet of next student and check the division.
3. If it is not first division go to step 5
4. Add 1 to totalFirstDivision
5. Add 1 to totalMarksheetsChecked
6. Check if totalMarksheetsChecked = 50 ? If no go to step 2
7. Print totalFirstDivision
8. Stop
CFCP / Module I / AKN / 26
Representation of Algo.
• As flowcharts
– Pictorial representation of an algorithm
• As Pseudo code
– Is an imitation of actual computer instruction. i.e. it
written in English language, but looks like a
program.
• As Program
– Represented in the form of a programming language
CFCP / Module I / AKN / 27
Flowcharts
• Terminal
– Start or stop
• Processing
– Operations like add subtract etc.
• Flow ines
– Flow / sequence of operation.
• Connectors
– Required if the chart spreads over multiple pages
Terminal Input /
Output
Decision
Processing
Flow Lines
Connectors
CFCP / Module I / AKN / 28
Example Decision
Is I=10?
No
Yes
Compare
A & B
A>B
A=B
A<B
I = ?
= 0 = 1 = 2 = 3 = 4 = 5 . . .
Two – way
Branching Three – way
Branching
Multi – way
Branching
CFCP / Module I / AKN / 29
Example (1)
• Find the % mark secured by a student, given all marks
Start
Read mark
details
Add marks of all
subjects giving total
Percentage =
total / totalSubject
Print
percentage
Stop
CFCP / Module I / AKN / 30
Example (2)
• Find the % mark secured by 50 students, given all marks
Start
Read mark details
of one student
Add marks of all
subjects giving total
Counter = 1
Percentage =
total / totalSubject
Print
percentage
Add 1 to
Counter
Is Counter > 50
NO
Stop
yes
CFCP / Module I / AKN / 31
Flowchart Guidelines
• Chart the main logic, then incorporate detail
• Do not chart everything in the chart. A
consistent level of detail to be maintained
• Names used should be prob. Specific
• Crossing of flow lines may be avoided
• Connectors may be leveled
• Construction should be left->right & top-
>bottom
CFCP / Module I / AKN / 32
Flowchart advantages
• Better communication
• Proper program documentation
• Efficient coding
• Systematic debugging
• Systematic testing
CFCP / Module I / AKN / 33
Flowchart disadvantages
• Very time consuming and laborious to draw
• Difficult to incorporate changes
• No standards available determining the
amount of detail to be produced
CFCP / Module I / AKN / 34
Pseudocode
• Program logic is written in an ordinary
natural language
• Also called “Program Design Language”
(PDL)
• Basic Logic (control) Structure
– Sequence
• Sequence of execution
– Selection
• Decision (branching)
– Iteration
• Repetition (loop)
CFCP / Module I / AKN / 35
Pseudocode example
• Counter = 0
• Read first student record
• DO WHILE counter < 50
– Calculate totalMark
– percentage = totalMark / totalSubject
– Print percentage
– Counter = counter + 1
– IF counter < 49
• Read next student record
– END IF
• ENDDO
CFCP / Module I / AKN / 36
Classification of Languages
• Low-level
– Machine Language
– Assembly Language
• High-level Language
CFCP / Module I / AKN / 37
Machine Language
• Only language the machine understands directly
• Normally written as a string of binary numbers
• Example
– 000000 00001 00010 00110 00000 100000
– add the registers 1 and 2 and place the result in register 6
• Advantage & Limitation
– Fast execution
– Machine dependent
– Difficult to program or modify the program
– Error prone
CFCP / Module I / AKN / 38
Assembly Language
• Uses alphanumeric mnemonic codes for the instructions in
the instruction set
• Needs an assembler (translator) to translate Assembly
language program into equivalent machine language
program.
• Example
– MOV AL, 61h
– Load AL with 97 decimal
• Adv & Limitation
– Easier to understand, code & modify
– Error tracing is easier
– Efficiency of ML
– Machine dependant
– Knowledge of H/W required
CFCP / Module I / AKN / 39
High level Language
• Written in English like language with mathematical
symbols & expression.
– Ex: FORTRAN, COBOL, PASCAL, BASIC, C, C++,
JAVA, . . .
• Machine independent
• Knowledge of hardware is not needed
• Another software (compiler, interpreter) is needed
to translate the code to equivalent machine code.
• For different programs different translator
software is required.
• Lower efficiency
• Less flexible to control Hardware resources.
CFCP / Module I / AKN / 40
Translators
• Interpreter
– takes one statement of high level language, checks
for syntax error, translates to machine language
instruction and executes it.
– Then it considers the next statement & repeats above
process till the last statement.
• Compiler
– Checks and translates entire source program into
object program and not involved in execution.
CFCP / Module I / AKN / 41
C Programming Language
• A General purpose programming language
with low level programming support. (“High-
level assembly”)
• Currently, the most commonly-used
language for embedded systems
• Very portable: compilers exist for virtually
every processor
• Produces efficient code
• Fairly concise
• Structured, strictly-typed, case-sensitive
CFCP / Module I / AKN / 42
History
• Developed between 1969 and 1973
along with Unix at AT & T Bell Labs,
USA
• Due mostly to Dennis Ritchie & Brian
Kernighan
• Designed for systems programming
– Operating systems
– Utility programs
– Compilers
– Evolved from B, which evolved from BCPLBrian Kernighan
Denis Ritchi
CFCP / Module I / AKN / 43
Character set
• The set of characters which can be used to write a valid
token in C language
– Alphabates (a-z, A-Z)
– Digits (0-9)
– White Spaces (New line, Horizontal Tab, Blank Space)
– Special Characters
~ ! # % ^ & * ( ) _ - + = { } [ ] |  : ; “ ‘ < > , . / ?
• Tokens
– The smallest element in the C language is the token.
– It may be a single character or a sequence of characters to form a
single item.
• Tokens can be:
– Numeric constants, Character constants, String constants
Keywords, Names (identifiers), Operators ,
Punctuation Ex ; : , ‘ “ [ ] { } ( ) ,
CFCP / Module I / AKN / 44
C Keywords (I)
• Certain words have a special meaning to
the C compiler.
• They are called reserved words or
keywords.
• We should not try to use these words as
names of variables or function names in a
program.
• The keyword list for C contains 32 words
and all small-case letters.
CFCP / Module I / AKN / 45
C Keywords (II)
User
defined
Data types
struct
union
enum
typedef
void
sizeof
Type
Qualifier
short
long
signed
unsigned
const
volatile
Control
Statements
if
else
do
for
while
break
continue
goto
return
switch
case
default
Datatypes
char
int
float
double
Storage
Class
auto
static
extern
register
CFCP / Module I / AKN / 46
First Program in C
1./* Filename: hello.c
2. Author: Brian Kernighan & Dennis Ritchie
3. Date written: ?/?/1978
4. Description: This program prints the greeting
5. “Hello, World!”
6. */
7.#include <stdio.h>
8.
9.void main()
10.{
11. printf(“Hello, world!n”);
12.}
Preprocessor Directive
Header file
“main” function special:
the entry point
I/O performed by a library
function:
Comments
exit point
CFCP / Module I / AKN / 47
Comments (Line 1-6)
• A comment is descriptive text used to help a
reader of the program understand its content
• Formal Comments (multiline):
/* Comment ….. */
– Used for detailed description of functions or operations (for
our benefit, not compiler’s).
– Can take multiple lines in source file.
• Informal Comments (single line):
// Comment ….. Ends at the end of line
– Used for quick comments like:
int temp; // temporary variable for storing
// the input value
CFCP / Module I / AKN / 48
Preprocessor (Line 7)
• Lines that begin with a # in column 1 are
called preprocessor directives (commands).
# include <stdio.h>
#include directive causes the preprocessor to
include a copy of the standard input/output
header file stdio.h at this point in the code.
#define -- define constants and macros
Examples:
#define e 2.7182818
#define pi 3.14159265359
CFCP / Module I / AKN / 49
C Program Structure (I)
• Is a function oriented structured
programming language.
– Is a collection of functions.
– It should contain one & only one ‘main’
function and may contain any number of
other functions.
– Beginning of main function is the entry point
for program execution & end of main
function is the exit point of program
execution.
CFCP / Module I / AKN / 50
C Program Structure (II)
/* Program Detail Comment */
<any global variable & function declarations >
main( )
{
<variable & function declarations & initializations>
<Input Section>
<Program Logic>
<Output Section>
} //end of the program
CFCP / Module I / AKN / 51
C Data Types and Variables
• Data types are divided into 3 Category
– Basic data type (primitive data type)
– Derived data type
– User defined data types
• Basic data types: char, int, float, and double
• Variable (or Identifier) declaration syntax
– Data type <variable name>;
• Variable name can be of anything length.
sensitive: abc is different from ABC
• Must begin with a letter and the rest can be
letters, digits, and underscores.
CFCP / Module I / AKN / 52
Variable & Constants (I)
• Integer variables: int a, b ;
• Integer constants: 1, 2, 3, . . .
• Integer variables, like a or b, store only whole
numbers like 3 or 7, not 3.33 or 7.65, and only up
to certain maximum values.
• Floating point variables: float c, d ;
• Floating point constants: 23.178, 0.34, . . .
• Floating point variables, like c or d, store rational
numbers, like 3.14159, but only a limited number
of digits of precision.
CFCP / Module I / AKN / 53
Variable & Constants(II)
• double variables: double a, b ;
– Same as float but higher precession
• character variables: char a, b ;
• character constants: „a‟, „b‟, „n‟,. . . ;
– Stored as an integer. (0-255). i.e. each character
const. is mapped to one integer of the above range
CFCP / Module I / AKN / 54
Basic Definitions
• The type of an variable (object) determines the
set of values it can have and what operations can
be performed on it. Where
– set of values is finite
– When declaring a new variable and assigning a type for it,
you have to keep in mind the values and operations you'll be
needing.
• An operator computes new values out of old ones.
• An expression consists of variables, constants,
and operators combined to perform some useful
computation.
CFCP / Module I / AKN / 55
Datatypes
• Normally Data types are divided into
following Categories
– Basic data type (primitive data type)
• char, int, float, double
– Derived data type
• array, pointer
– User defined data types
• structure, union, enum
– A Special type
• void
CFCP / Module I / AKN / 56
Basic Datatypes
• Only 4 basic data types in C.
• char: a character, a single byte storage.
– how the computer stores characters?
– uses a character set, which is simply a mapping
between some set of characters and some set of
integers called ASCII character set,
– letter ‘A’ is represented by the code 65, ‘1’ is
represented by the code 49 etc.
– Number of characters possible?
CFCP / Module I / AKN / 57
Basic Types
• int: an integer, reflects the natural size of integers
in the host machine
– Find the size of storage & hence the range of vaules in your
machine
• float: a single precision floating-point number.
– Find the storage size.
• double: a double-precision floating-point number
– with more precision and perhaps greater range than float
– Find the storage size
• Qualifiers: may be used with basic types to
increase/decrease the normal storage size.
– long, short (int, double), signed, unsigned (char, int).
CFCP / Module I / AKN / 58
Constants (Integer)
• A constant is just an immediate, absolute value
found in an expression.
• Decimal integer constants: 0, 1, 2, 123 .
• Octal integer:(0 used as prefix) 0144
• Hexadecimal integer const: (0x prefix)
• the constants 100, 0144, and 0x64 all represent
the same number.
• However, The compiler doesn't care; it always
converts everything into binary internally, anyway.
• A constant can be forced to be of type long int by
suffixing it with the letter L (upper or lower case)
CFCP / Module I / AKN / 59
Constants (Floating Point)
• Floating point constant:
– 3.14, 10., .01, 123e4, 123.456e7 .
– The e indicates multiplication by a power of 10;
123.456e7 is 123.456 times 10 to the 7th, or
1,234,560,000.
– Floating-point constants are of type double by
default.
CFCP / Module I / AKN / 60
Constants (char & String)
• Character constants: represented in single quote
– ‘A’, ‘f’, ‘t’
– May contain multiple symbols with escape char: ‘n’, ‘t’ etc.
• String constants. a string is a set of zero or more
characters;
– enclosed in double quotes: "apple", "hello, world", "this is a
test"
– a string containing one character is distinct from a lone
character?.
– A string is represented in C as a sequence or array of
characters.
CFCP / Module I / AKN / 61
Variable (I)
• A variable is a place you can store a value. So
that you can refer to it unambiguously, a variable
needs a name.
• A declaration tells the compiler, the name and
type of a variable you'll be using in your program.
• In its simplest form, a declaration consists of
– the type, the name of the variable, and a terminating
semicolon (;)
– char c; int i; float f;
• Several variables of the same type may be
declared with separating coma in one declaration
– int age, rollNumber;
CFCP / Module I / AKN / 62
Variable (II)
• A declaration for a variable can also contain an
initial value.
– This initializer consists of an equal sign and an expression,
which is usually a single constant:
– int i = 1; int a = i*3, i2 = func(20);
• Declarations may also contain qualifiers, storage
classes, and that we can declare arrays,
functions, pointers, and other kinds of data
structures.
CFCP / Module I / AKN / 63
Variable (III)
• You can't place variable declarations just
anywhere! They must either be placed
– at the beginning of a function, or
– at the beginning of a brace-enclosed block of statements or
– outside of any function.
• Within limits, you can give your variables and
functions any names (identifiers) you want but
first 32 characters are significant.
– Names may contain alphabets, numbers, and underscores
– The names abc & ABC are different
– keywords can not be used as the names of variables or
functions (or as identifiers of any kind).
CFCP / Module I / AKN / 64
Operators
• Used to operate upon operand (s)
• Arithmetic
– Binary: +, -, *, /, % Unary: -
• When applied to integers, the division operator /
discards any remainder,
– so 1 / 2 is 0 and 7 / 4 is 1.
– And 1 / 2.0 is 0.5, and 7.0 / 4.0 is 1.75.
• The modulus operator % gives you the remainder
when two integers are divided: 1 % 2 is 1; 7 % 4
is 3.
– 2.5 % 5.0 ?
CFCP / Module I / AKN / 65
Assignment Operators
• The assignment operator „=„ assigns a
value to a variable. For example,
 x = 1 sets x to 1, and a = b sets a to whatever b's
value is.
• Groups from right to left
 c = a = b is equivalent to c = (a = b)
• Other Assignment Operators
 +=, i.e. a += b; is equivalent to a = a + b;
 Similarly, -=, *=, /+, %=, . . .
CFCP / Module I / AKN / 66
Increment/decrement Operators
• ++: increments the value of operand by one
• -- : decrements the value of operand by one
• Use
 Postfix: i++; j-- is equivalent to i = i+1; & j = j-1;
 Prefix: ++i; --j
 ++5 ?
• Difference
 t = 5; x = t++;
 k =3; y = -- k
CFCP / Module I / AKN / 67
Operators (IV)
• Relational Operators
 < , >, <=, >=, ==, !=
• Logical Operators
 &&, ||, !
• Numeric value of a relational or logical
expression is 1 for true and 0 for false.
• Bit wise operators
 &, |, ^ (ex-or), << (left-shift), >> (right shift), ~
(one’s complement)
CFCP / Module I / AKN / 68
Operators (V)
• sizeof: size of variables/data types
– Use: sizeof(int)
• & -> address of
• * -> pointer
• ?: -> conditional expression (ternary operator)
– Use: y = (x = = 1) ? 10 : 20;
• , -> series opeartor
CFCP / Module I / AKN / 69
Precedence & Associativity
• Precedence
– Which operator executes first
– i.e. 3 + 4 * 5 ?
• Their associativity indicates in what order
operators of equal precedence in an
expression are applied
CFCP / Module I / AKN / 70
Control Statements
• The statement which controls the flow of
execution of a program is called control statement
Control Statement
Branching Statement Looping Statement
One Way Branching (if)
Two Way Branching (if else)
Multi Way Branching
If .. else .. if, switch .. case
Entry Control (while, for)
Exit Control (do … while)
CFCP / Module I / AKN / 71
if Statement (I)
• Simple Syntax of an if statement is:
if ( expression )
statement ;
• If you have a series of statements, then enclose
them in braces:
if( expression )
{
statement1
statement2
statement3
}
Example:
if (x > max)
max = x;
CFCP / Module I / AKN / 72
if Statement (II)
• Syntax of an if statement
is:
if ( expression )
{statement (s);}
elseif ( expression )
{statement (s);}
else
{statement (s);}
Example:
if ( a < b )
printf ("a is less thann") ;
else if ( a == b )
printf (" a is equal to bn") ;
else
printf ("a is larger than bn") ;
CFCP / Module I / AKN / 73
Assignments
• Write a program to print the size of all
datatypes discussed
• Write a program to find largest of 3 integers,
using if statement
• Write the outputs for the following & explain
a=10;
if (a)
b=25
else
b=12
CFCP / Module I / AKN / 74
Readings
• Pradeep K Sinha, Priti Sinha, Computer
Fundamentals
• Computer Fundamentals & C Programming
by Balagurusamy

More Related Content

PPTX
Programming for Problem Solving
PPT
Intro To Programming Concepts
PPTX
FIT-Unit3 chapter2- Computer Languages
PDF
C Language
PPT
Block diagram of computer 02
PPTX
Introduction to c programming
PPTX
Programming Fundamentals lecture 1
PPTX
Programming Paradigm & Languages
Programming for Problem Solving
Intro To Programming Concepts
FIT-Unit3 chapter2- Computer Languages
C Language
Block diagram of computer 02
Introduction to c programming
Programming Fundamentals lecture 1
Programming Paradigm & Languages

What's hot (20)

PPTX
Introduction to programming
PPTX
Unit 1. Problem Solving with Computer
PPTX
Algorithms and Flowcharts
PPTX
Components of a computer system
PPT
High level and Low level Language
PDF
Computer Programming
PPTX
Computer Organisation & Architecture (chapter 1)
PPTX
Computer organization
PPT
Overview of computer system
PPT
BINARY NUMBER SYSTEM
PDF
Basic_Computer_Skills, A_seminar_by_Mohan_Kumar_G_Lecturer
PPTX
Analog Computer: Definition, Examples, Types, Characteristics, and Advantages!!
PPTX
Types of memory in Computer
PPT
Parts of a Computer
PDF
Introduction to Operating Systems
PPTX
Algorithm and Flowcharts
PPTX
Types of computer
PDF
Algorithm and Programming (Introduction of Algorithms)
PPTX
Introduction to computer system
PPT
Peter Norton’s Introduction to Computers
Introduction to programming
Unit 1. Problem Solving with Computer
Algorithms and Flowcharts
Components of a computer system
High level and Low level Language
Computer Programming
Computer Organisation & Architecture (chapter 1)
Computer organization
Overview of computer system
BINARY NUMBER SYSTEM
Basic_Computer_Skills, A_seminar_by_Mohan_Kumar_G_Lecturer
Analog Computer: Definition, Examples, Types, Characteristics, and Advantages!!
Types of memory in Computer
Parts of a Computer
Introduction to Operating Systems
Algorithm and Flowcharts
Types of computer
Algorithm and Programming (Introduction of Algorithms)
Introduction to computer system
Peter Norton’s Introduction to Computers
Ad

Viewers also liked (20)

PPT
Uml Omg Fundamental Certification 3
PPTX
Enterprise Architecture for Dummies
PPTX
One thing you can do to increase your charisma
PDF
Software Engineering : Process Models
PDF
NS2: AWK and GNUplot - PArt III
PPTX
I BELIEVE I CAN FLY ( version française)
PDF
Computer Networks Module II
PDF
Introduction to database-Normalisation
PDF
Object Oriented Analysis Design using UML
PPTX
Misiones en Honduras Mayo 2012
PDF
Object Oriented Programming using C++ Part I
PDF
Ns2: Introduction - Part I
PPTX
The Bad Guy in your company and how have him under control
PDF
Object Oriented Programming using C++ Part III
PDF
Introduction to database-ER Model
PDF
Software Engineering an Introduction
PPTX
The badguy summary
PPT
Uml Omg Fundamental Certification 2
PDF
Software Engineering : OOAD using UML
PDF
Software Engineering :Behavioral Modelling - II State diagram
Uml Omg Fundamental Certification 3
Enterprise Architecture for Dummies
One thing you can do to increase your charisma
Software Engineering : Process Models
NS2: AWK and GNUplot - PArt III
I BELIEVE I CAN FLY ( version française)
Computer Networks Module II
Introduction to database-Normalisation
Object Oriented Analysis Design using UML
Misiones en Honduras Mayo 2012
Object Oriented Programming using C++ Part I
Ns2: Introduction - Part I
The Bad Guy in your company and how have him under control
Object Oriented Programming using C++ Part III
Introduction to database-ER Model
Software Engineering an Introduction
The badguy summary
Uml Omg Fundamental Certification 2
Software Engineering : OOAD using UML
Software Engineering :Behavioral Modelling - II State diagram
Ad

Similar to Computer Fundamentals & Intro to C Programming module i (20)

PPT
Computer Programming Computer Programming
PPTX
UNIT-1.pptx
PPTX
Cs1123 2 comp_prog
PDF
Computer Programming_Unit 1
PPTX
Introduction.pptx
PPTX
Ch01.pptxxxxxxxxxcxcxcxxccxxxxxxxxxxxcccccc
PPTX
Ch01.pptxcomputer organization and architecture schools
PDF
C progrmming
PDF
6272 cnote
PPTX
Introduction to computer programming
PDF
Lec02-CS110 Computational Engineering
PPT
01CHAP_1.PPT
PDF
01 - Introduction to Computer and Algorithm (1).pdf
PPTX
Chapter 1 Introduction to computer components
PPT
Lecture 2 - Introductory Concepts
PPTX
Pj01 1-computer and programming fundamentals
PPT
Comp102 lec 1
PPTX
Esc1.Introduction to Computerssssss.pptx
PPTX
UNIT-111.pptx
PPTX
Introduction and history of Computers.pptx
Computer Programming Computer Programming
UNIT-1.pptx
Cs1123 2 comp_prog
Computer Programming_Unit 1
Introduction.pptx
Ch01.pptxxxxxxxxxcxcxcxxccxxxxxxxxxxxcccccc
Ch01.pptxcomputer organization and architecture schools
C progrmming
6272 cnote
Introduction to computer programming
Lec02-CS110 Computational Engineering
01CHAP_1.PPT
01 - Introduction to Computer and Algorithm (1).pdf
Chapter 1 Introduction to computer components
Lecture 2 - Introductory Concepts
Pj01 1-computer and programming fundamentals
Comp102 lec 1
Esc1.Introduction to Computerssssss.pptx
UNIT-111.pptx
Introduction and history of Computers.pptx

More from Ajit Nayak (16)

PDF
Software Engineering : Software testing
PDF
Software Engineering :Behavioral Modelling - I Sequence diagram
PDF
Software Engineering :UML class diagrams
PDF
Software Engineering : Requirement Analysis & Specification
PDF
Database Programming using SQL
PDF
Ns2: OTCL - PArt II
PDF
Socket programming using C
PDF
Parallel programming using MPI
PDF
Operating Systems Part III-Memory Management
PDF
Operating Systems Part I-Basics
PDF
Operating Systems Part II-Process Scheduling, Synchronisation & Deadlock
PDF
Introduction to database-Transaction Concurrency and Recovery
PDF
Introduction to database-Formal Query language and Relational calculus
PDF
Computer Networks Module III
PDF
Computer Networks Module I
PDF
Object Oriented Programming using C++ Part II
Software Engineering : Software testing
Software Engineering :Behavioral Modelling - I Sequence diagram
Software Engineering :UML class diagrams
Software Engineering : Requirement Analysis & Specification
Database Programming using SQL
Ns2: OTCL - PArt II
Socket programming using C
Parallel programming using MPI
Operating Systems Part III-Memory Management
Operating Systems Part I-Basics
Operating Systems Part II-Process Scheduling, Synchronisation & Deadlock
Introduction to database-Transaction Concurrency and Recovery
Introduction to database-Formal Query language and Relational calculus
Computer Networks Module III
Computer Networks Module I
Object Oriented Programming using C++ Part II

Recently uploaded (20)

PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Insiders guide to clinical Medicine.pdf
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Institutional Correction lecture only . . .
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Basic Mud Logging Guide for educational purpose
PDF
TR - Agricultural Crops Production NC III.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Insiders guide to clinical Medicine.pdf
Sports Quiz easy sports quiz sports quiz
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Anesthesia in Laparoscopic Surgery in India
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Abdominal Access Techniques with Prof. Dr. R K Mishra
Institutional Correction lecture only . . .
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
human mycosis Human fungal infections are called human mycosis..pptx
Renaissance Architecture: A Journey from Faith to Humanism
O5-L3 Freight Transport Ops (International) V1.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
O7-L3 Supply Chain Operations - ICLT Program
VCE English Exam - Section C Student Revision Booklet
Supply Chain Operations Speaking Notes -ICLT Program
STATICS OF THE RIGID BODIES Hibbelers.pdf
Basic Mud Logging Guide for educational purpose
TR - Agricultural Crops Production NC III.pdf

Computer Fundamentals & Intro to C Programming module i

  • 1. Computer Fundamentals & C Programming Ajit K Nayak, Ph.D. S„O‟A University, Odisha Module I
  • 2. Contents • Introduction to Computers • Evolution of Computers • Computer Generations • Basic Computer Organization • Memory Hierarchy • I/O devices • Computer Software • Planning Computer Program • Computer Languages • Introduction to C programming • Structure of C Programming • Datatype • Constant • Variable • Expression • Conditional Expression • Precedence & order of evaluation
  • 3. Introduction • Computer is a fast computing device • Today‟s Computing refers to computer supported commercial & scientific applications. • Example of computing – Web services, Control system (missile, vehicular, household device, Industry) . . . • Is it comparable to human brain? – It is fast but not smart!!!
  • 4. Definitions • A computer is an electronic device that performs arithmetic & logic operation at a very high speed • It is also called a data processor that can store, process and retrieve data as and when desired. Input (Data) Process / Manipulate Output (Information)
  • 5. Evolution (1) • 1642: Blaise Pascal invented “Mechanical Adding Machine” • 1671: Baron Gottfried invented “Calculator” for multiplication • 1880: “Keyboard machines” designed in USA • 1880: Herman Hollerith invented “punched cards” for input media & is used till late 70s
  • 6. Evolution (2) • Charles Babbage is considered to be the father of modern digital computers – Designed difference engine in 1822 – Designed a fully automatic analytical engine in 1842 for performing basic arithmetic operations – Established a number of principles that are fundamental to the design of any digital computer
  • 7. Well known Early Computers • MARK I computer (1937 - 1944) – Electro-mechanical calculating device, 50 ft-long x 8 ft-high – Add: 0.3 sec, mul:4.5 sec • Atanasoff-Berry Computer (1939-1942) – Electronic machine to solve some mathematical functions • ENIAC (1943-1946) – All electronic computer 20x40 sqft, add: 200 us, mul: 2000us • EDVAC (1946-52) – Used stored program concept, instructions & data stored in binary form • UNIVAC I (1951) – First digital computer
  • 12. Basic Operations • Input – The process of entering instructions and data to the computer system • Store – Saving instruction & data to make them available as and when required for processing • Process – Performing arithmetic & logic operations on data using corresponding instructions to produce information • Output – To produce the information/result of the processing as a printed report or visual display • Control – Directing the manner and sequence in which all above operation are performed
  • 13. Basic Organization Input Unit Output Unit Secondary Storage Primary Storage Control Unit Arithmetic Logic Unit Program & Data Storage Unit Information (Results) Centarl Processing Unit (CPU) Data
  • 14. Storage (Memory) • Primary Storage – Used to hold data, ongoing job’s instructions, intermediate results – Fast in operation – Small capacity, expensive, volatile – Ex: RAM, ROM, PROM, EPROM • Secondary Storage – Used to store stored program & data – Slower, large capacity, cheaper, persistent – Ex: Hard Disk, Magnetic Tape, Floppy, CD, Flash
  • 15. Memory Hierarchy Cache Memory MainMemory On-line, Direct-access, and sequential access secondary storage such as hard disk Off-line, Direct-access, and sequential access secondary storage such as magnetic tape, floppy disk, zip disk, worm etc Mass storage device such as tape library, CD etc
  • 16. CPU • Arithmetic & Logic Unit (ALU) – Actual execution of the instruction takes place – Arithmetic Operations like: add, subtract, etc – Logic Operation: <, >, and, or, etc • Control Unit (CU) – Manages & coordinates all other components of a computer system. – It acts like a central nervous system of computer – Ex: It decides when to execute which instruction
  • 17. Input Devices • Keyboard • Point & Draw devices – Mouse, Trackball, Joystick • Data Scanning Devices – Image Scanner, OCR, Barcode reader, MICR • Digitizer – Used to pictures, maps etc to digital form • Electronic Card • Speech Recognition system
  • 18. Output Device • Monitors • Printers • Plotters • Screen Image Projector
  • 19. CFCP / Module I / AKN / 19 Basic Terminologies • Hardware – Physical devices of a computer system • Software – A Set of programs • Program – Sequence of instructions written using a computer language to solve a small problem. • Software Package – A group of program that solve a specific type of job
  • 20. CFCP / Module I / AKN / 20 H/W vs. S/W • S/W makes H/W usable • They are complementary to each other • Same H/W may perform different jobs using different S/W
  • 21. CFCP / Module I / AKN / 21 Types of S/W • System Software – Are designed to control/manage and extend the processing capabilities of a computer system. • Application Software – Designed to solve a specific problem or to do a specific task.
  • 22. CFCP / Module I / AKN / 22 Logical System Architecture USERS (Normally interact with the system via the user interface provided by the application S/W) APPLICATION SOFTWARE (Software that do a specific task to solve a specific problem) SYSTEM SOFTWARE (Constitute the operating and programming environment) HARDWARE (Physical devices/components of a computer system)
  • 23. CFCP / Module I / AKN / 23 Program Planning • To write a correct program, a programmer must write each and every instruction in the correct sequence • i.e. it should be ensured that the instructions are – Appropriate for the program & – In the correct sequence
  • 24. CFCP / Module I / AKN / 24 Algorithm • Also called the ‘program logic’ is a step-by- step description of how to arrive at solution of a given problem. • Algorithm has following characteristics – Each and every instructions should be precise & unambiguous – Each instruction should be executed in finite time. – After execution of all the instructions, it should produce the desired result.
  • 25. CFCP / Module I / AKN / 25 Example 1 • Problem – Mark-sheets of 50 students are available – Calculate & produce the total number of student passed in ‘first division’ • Solution 1. Initialize totalFirstDivision, and totalMarksheetsChecked to zero. 2. Take the marksheet of next student and check the division. 3. If it is not first division go to step 5 4. Add 1 to totalFirstDivision 5. Add 1 to totalMarksheetsChecked 6. Check if totalMarksheetsChecked = 50 ? If no go to step 2 7. Print totalFirstDivision 8. Stop
  • 26. CFCP / Module I / AKN / 26 Representation of Algo. • As flowcharts – Pictorial representation of an algorithm • As Pseudo code – Is an imitation of actual computer instruction. i.e. it written in English language, but looks like a program. • As Program – Represented in the form of a programming language
  • 27. CFCP / Module I / AKN / 27 Flowcharts • Terminal – Start or stop • Processing – Operations like add subtract etc. • Flow ines – Flow / sequence of operation. • Connectors – Required if the chart spreads over multiple pages Terminal Input / Output Decision Processing Flow Lines Connectors
  • 28. CFCP / Module I / AKN / 28 Example Decision Is I=10? No Yes Compare A & B A>B A=B A<B I = ? = 0 = 1 = 2 = 3 = 4 = 5 . . . Two – way Branching Three – way Branching Multi – way Branching
  • 29. CFCP / Module I / AKN / 29 Example (1) • Find the % mark secured by a student, given all marks Start Read mark details Add marks of all subjects giving total Percentage = total / totalSubject Print percentage Stop
  • 30. CFCP / Module I / AKN / 30 Example (2) • Find the % mark secured by 50 students, given all marks Start Read mark details of one student Add marks of all subjects giving total Counter = 1 Percentage = total / totalSubject Print percentage Add 1 to Counter Is Counter > 50 NO Stop yes
  • 31. CFCP / Module I / AKN / 31 Flowchart Guidelines • Chart the main logic, then incorporate detail • Do not chart everything in the chart. A consistent level of detail to be maintained • Names used should be prob. Specific • Crossing of flow lines may be avoided • Connectors may be leveled • Construction should be left->right & top- >bottom
  • 32. CFCP / Module I / AKN / 32 Flowchart advantages • Better communication • Proper program documentation • Efficient coding • Systematic debugging • Systematic testing
  • 33. CFCP / Module I / AKN / 33 Flowchart disadvantages • Very time consuming and laborious to draw • Difficult to incorporate changes • No standards available determining the amount of detail to be produced
  • 34. CFCP / Module I / AKN / 34 Pseudocode • Program logic is written in an ordinary natural language • Also called “Program Design Language” (PDL) • Basic Logic (control) Structure – Sequence • Sequence of execution – Selection • Decision (branching) – Iteration • Repetition (loop)
  • 35. CFCP / Module I / AKN / 35 Pseudocode example • Counter = 0 • Read first student record • DO WHILE counter < 50 – Calculate totalMark – percentage = totalMark / totalSubject – Print percentage – Counter = counter + 1 – IF counter < 49 • Read next student record – END IF • ENDDO
  • 36. CFCP / Module I / AKN / 36 Classification of Languages • Low-level – Machine Language – Assembly Language • High-level Language
  • 37. CFCP / Module I / AKN / 37 Machine Language • Only language the machine understands directly • Normally written as a string of binary numbers • Example – 000000 00001 00010 00110 00000 100000 – add the registers 1 and 2 and place the result in register 6 • Advantage & Limitation – Fast execution – Machine dependent – Difficult to program or modify the program – Error prone
  • 38. CFCP / Module I / AKN / 38 Assembly Language • Uses alphanumeric mnemonic codes for the instructions in the instruction set • Needs an assembler (translator) to translate Assembly language program into equivalent machine language program. • Example – MOV AL, 61h – Load AL with 97 decimal • Adv & Limitation – Easier to understand, code & modify – Error tracing is easier – Efficiency of ML – Machine dependant – Knowledge of H/W required
  • 39. CFCP / Module I / AKN / 39 High level Language • Written in English like language with mathematical symbols & expression. – Ex: FORTRAN, COBOL, PASCAL, BASIC, C, C++, JAVA, . . . • Machine independent • Knowledge of hardware is not needed • Another software (compiler, interpreter) is needed to translate the code to equivalent machine code. • For different programs different translator software is required. • Lower efficiency • Less flexible to control Hardware resources.
  • 40. CFCP / Module I / AKN / 40 Translators • Interpreter – takes one statement of high level language, checks for syntax error, translates to machine language instruction and executes it. – Then it considers the next statement & repeats above process till the last statement. • Compiler – Checks and translates entire source program into object program and not involved in execution.
  • 41. CFCP / Module I / AKN / 41 C Programming Language • A General purpose programming language with low level programming support. (“High- level assembly”) • Currently, the most commonly-used language for embedded systems • Very portable: compilers exist for virtually every processor • Produces efficient code • Fairly concise • Structured, strictly-typed, case-sensitive
  • 42. CFCP / Module I / AKN / 42 History • Developed between 1969 and 1973 along with Unix at AT & T Bell Labs, USA • Due mostly to Dennis Ritchie & Brian Kernighan • Designed for systems programming – Operating systems – Utility programs – Compilers – Evolved from B, which evolved from BCPLBrian Kernighan Denis Ritchi
  • 43. CFCP / Module I / AKN / 43 Character set • The set of characters which can be used to write a valid token in C language – Alphabates (a-z, A-Z) – Digits (0-9) – White Spaces (New line, Horizontal Tab, Blank Space) – Special Characters ~ ! # % ^ & * ( ) _ - + = { } [ ] | : ; “ ‘ < > , . / ? • Tokens – The smallest element in the C language is the token. – It may be a single character or a sequence of characters to form a single item. • Tokens can be: – Numeric constants, Character constants, String constants Keywords, Names (identifiers), Operators , Punctuation Ex ; : , ‘ “ [ ] { } ( ) ,
  • 44. CFCP / Module I / AKN / 44 C Keywords (I) • Certain words have a special meaning to the C compiler. • They are called reserved words or keywords. • We should not try to use these words as names of variables or function names in a program. • The keyword list for C contains 32 words and all small-case letters.
  • 45. CFCP / Module I / AKN / 45 C Keywords (II) User defined Data types struct union enum typedef void sizeof Type Qualifier short long signed unsigned const volatile Control Statements if else do for while break continue goto return switch case default Datatypes char int float double Storage Class auto static extern register
  • 46. CFCP / Module I / AKN / 46 First Program in C 1./* Filename: hello.c 2. Author: Brian Kernighan & Dennis Ritchie 3. Date written: ?/?/1978 4. Description: This program prints the greeting 5. “Hello, World!” 6. */ 7.#include <stdio.h> 8. 9.void main() 10.{ 11. printf(“Hello, world!n”); 12.} Preprocessor Directive Header file “main” function special: the entry point I/O performed by a library function: Comments exit point
  • 47. CFCP / Module I / AKN / 47 Comments (Line 1-6) • A comment is descriptive text used to help a reader of the program understand its content • Formal Comments (multiline): /* Comment ….. */ – Used for detailed description of functions or operations (for our benefit, not compiler’s). – Can take multiple lines in source file. • Informal Comments (single line): // Comment ….. Ends at the end of line – Used for quick comments like: int temp; // temporary variable for storing // the input value
  • 48. CFCP / Module I / AKN / 48 Preprocessor (Line 7) • Lines that begin with a # in column 1 are called preprocessor directives (commands). # include <stdio.h> #include directive causes the preprocessor to include a copy of the standard input/output header file stdio.h at this point in the code. #define -- define constants and macros Examples: #define e 2.7182818 #define pi 3.14159265359
  • 49. CFCP / Module I / AKN / 49 C Program Structure (I) • Is a function oriented structured programming language. – Is a collection of functions. – It should contain one & only one ‘main’ function and may contain any number of other functions. – Beginning of main function is the entry point for program execution & end of main function is the exit point of program execution.
  • 50. CFCP / Module I / AKN / 50 C Program Structure (II) /* Program Detail Comment */ <any global variable & function declarations > main( ) { <variable & function declarations & initializations> <Input Section> <Program Logic> <Output Section> } //end of the program
  • 51. CFCP / Module I / AKN / 51 C Data Types and Variables • Data types are divided into 3 Category – Basic data type (primitive data type) – Derived data type – User defined data types • Basic data types: char, int, float, and double • Variable (or Identifier) declaration syntax – Data type <variable name>; • Variable name can be of anything length. sensitive: abc is different from ABC • Must begin with a letter and the rest can be letters, digits, and underscores.
  • 52. CFCP / Module I / AKN / 52 Variable & Constants (I) • Integer variables: int a, b ; • Integer constants: 1, 2, 3, . . . • Integer variables, like a or b, store only whole numbers like 3 or 7, not 3.33 or 7.65, and only up to certain maximum values. • Floating point variables: float c, d ; • Floating point constants: 23.178, 0.34, . . . • Floating point variables, like c or d, store rational numbers, like 3.14159, but only a limited number of digits of precision.
  • 53. CFCP / Module I / AKN / 53 Variable & Constants(II) • double variables: double a, b ; – Same as float but higher precession • character variables: char a, b ; • character constants: „a‟, „b‟, „n‟,. . . ; – Stored as an integer. (0-255). i.e. each character const. is mapped to one integer of the above range
  • 54. CFCP / Module I / AKN / 54 Basic Definitions • The type of an variable (object) determines the set of values it can have and what operations can be performed on it. Where – set of values is finite – When declaring a new variable and assigning a type for it, you have to keep in mind the values and operations you'll be needing. • An operator computes new values out of old ones. • An expression consists of variables, constants, and operators combined to perform some useful computation.
  • 55. CFCP / Module I / AKN / 55 Datatypes • Normally Data types are divided into following Categories – Basic data type (primitive data type) • char, int, float, double – Derived data type • array, pointer – User defined data types • structure, union, enum – A Special type • void
  • 56. CFCP / Module I / AKN / 56 Basic Datatypes • Only 4 basic data types in C. • char: a character, a single byte storage. – how the computer stores characters? – uses a character set, which is simply a mapping between some set of characters and some set of integers called ASCII character set, – letter ‘A’ is represented by the code 65, ‘1’ is represented by the code 49 etc. – Number of characters possible?
  • 57. CFCP / Module I / AKN / 57 Basic Types • int: an integer, reflects the natural size of integers in the host machine – Find the size of storage & hence the range of vaules in your machine • float: a single precision floating-point number. – Find the storage size. • double: a double-precision floating-point number – with more precision and perhaps greater range than float – Find the storage size • Qualifiers: may be used with basic types to increase/decrease the normal storage size. – long, short (int, double), signed, unsigned (char, int).
  • 58. CFCP / Module I / AKN / 58 Constants (Integer) • A constant is just an immediate, absolute value found in an expression. • Decimal integer constants: 0, 1, 2, 123 . • Octal integer:(0 used as prefix) 0144 • Hexadecimal integer const: (0x prefix) • the constants 100, 0144, and 0x64 all represent the same number. • However, The compiler doesn't care; it always converts everything into binary internally, anyway. • A constant can be forced to be of type long int by suffixing it with the letter L (upper or lower case)
  • 59. CFCP / Module I / AKN / 59 Constants (Floating Point) • Floating point constant: – 3.14, 10., .01, 123e4, 123.456e7 . – The e indicates multiplication by a power of 10; 123.456e7 is 123.456 times 10 to the 7th, or 1,234,560,000. – Floating-point constants are of type double by default.
  • 60. CFCP / Module I / AKN / 60 Constants (char & String) • Character constants: represented in single quote – ‘A’, ‘f’, ‘t’ – May contain multiple symbols with escape char: ‘n’, ‘t’ etc. • String constants. a string is a set of zero or more characters; – enclosed in double quotes: "apple", "hello, world", "this is a test" – a string containing one character is distinct from a lone character?. – A string is represented in C as a sequence or array of characters.
  • 61. CFCP / Module I / AKN / 61 Variable (I) • A variable is a place you can store a value. So that you can refer to it unambiguously, a variable needs a name. • A declaration tells the compiler, the name and type of a variable you'll be using in your program. • In its simplest form, a declaration consists of – the type, the name of the variable, and a terminating semicolon (;) – char c; int i; float f; • Several variables of the same type may be declared with separating coma in one declaration – int age, rollNumber;
  • 62. CFCP / Module I / AKN / 62 Variable (II) • A declaration for a variable can also contain an initial value. – This initializer consists of an equal sign and an expression, which is usually a single constant: – int i = 1; int a = i*3, i2 = func(20); • Declarations may also contain qualifiers, storage classes, and that we can declare arrays, functions, pointers, and other kinds of data structures.
  • 63. CFCP / Module I / AKN / 63 Variable (III) • You can't place variable declarations just anywhere! They must either be placed – at the beginning of a function, or – at the beginning of a brace-enclosed block of statements or – outside of any function. • Within limits, you can give your variables and functions any names (identifiers) you want but first 32 characters are significant. – Names may contain alphabets, numbers, and underscores – The names abc & ABC are different – keywords can not be used as the names of variables or functions (or as identifiers of any kind).
  • 64. CFCP / Module I / AKN / 64 Operators • Used to operate upon operand (s) • Arithmetic – Binary: +, -, *, /, % Unary: - • When applied to integers, the division operator / discards any remainder, – so 1 / 2 is 0 and 7 / 4 is 1. – And 1 / 2.0 is 0.5, and 7.0 / 4.0 is 1.75. • The modulus operator % gives you the remainder when two integers are divided: 1 % 2 is 1; 7 % 4 is 3. – 2.5 % 5.0 ?
  • 65. CFCP / Module I / AKN / 65 Assignment Operators • The assignment operator „=„ assigns a value to a variable. For example,  x = 1 sets x to 1, and a = b sets a to whatever b's value is. • Groups from right to left  c = a = b is equivalent to c = (a = b) • Other Assignment Operators  +=, i.e. a += b; is equivalent to a = a + b;  Similarly, -=, *=, /+, %=, . . .
  • 66. CFCP / Module I / AKN / 66 Increment/decrement Operators • ++: increments the value of operand by one • -- : decrements the value of operand by one • Use  Postfix: i++; j-- is equivalent to i = i+1; & j = j-1;  Prefix: ++i; --j  ++5 ? • Difference  t = 5; x = t++;  k =3; y = -- k
  • 67. CFCP / Module I / AKN / 67 Operators (IV) • Relational Operators  < , >, <=, >=, ==, != • Logical Operators  &&, ||, ! • Numeric value of a relational or logical expression is 1 for true and 0 for false. • Bit wise operators  &, |, ^ (ex-or), << (left-shift), >> (right shift), ~ (one’s complement)
  • 68. CFCP / Module I / AKN / 68 Operators (V) • sizeof: size of variables/data types – Use: sizeof(int) • & -> address of • * -> pointer • ?: -> conditional expression (ternary operator) – Use: y = (x = = 1) ? 10 : 20; • , -> series opeartor
  • 69. CFCP / Module I / AKN / 69 Precedence & Associativity • Precedence – Which operator executes first – i.e. 3 + 4 * 5 ? • Their associativity indicates in what order operators of equal precedence in an expression are applied
  • 70. CFCP / Module I / AKN / 70 Control Statements • The statement which controls the flow of execution of a program is called control statement Control Statement Branching Statement Looping Statement One Way Branching (if) Two Way Branching (if else) Multi Way Branching If .. else .. if, switch .. case Entry Control (while, for) Exit Control (do … while)
  • 71. CFCP / Module I / AKN / 71 if Statement (I) • Simple Syntax of an if statement is: if ( expression ) statement ; • If you have a series of statements, then enclose them in braces: if( expression ) { statement1 statement2 statement3 } Example: if (x > max) max = x;
  • 72. CFCP / Module I / AKN / 72 if Statement (II) • Syntax of an if statement is: if ( expression ) {statement (s);} elseif ( expression ) {statement (s);} else {statement (s);} Example: if ( a < b ) printf ("a is less thann") ; else if ( a == b ) printf (" a is equal to bn") ; else printf ("a is larger than bn") ;
  • 73. CFCP / Module I / AKN / 73 Assignments • Write a program to print the size of all datatypes discussed • Write a program to find largest of 3 integers, using if statement • Write the outputs for the following & explain a=10; if (a) b=25 else b=12
  • 74. CFCP / Module I / AKN / 74 Readings • Pradeep K Sinha, Priti Sinha, Computer Fundamentals • Computer Fundamentals & C Programming by Balagurusamy