lecture 1
lecture 1
Koustav Rudra
Introduction to C
30/05/2022
Books
• Programming with C
• Byron Gottfried, Schaum’s Outlines Series, Tata McGraw-Hill
• Programming in ANSI C
• E. Balaguruswamy, Tata McGraw-Hill
About the course
• Class Timings
• Monday [2:00 – 2:50]
• Tuesday [3:00 – 3:50]
• Wednesday [4:00 – 4:50] è Tutorial
• Programming Lab
• Thursday [9:00 – 10:50]
• Replacement
• 08/06/2022(Wednesday)[4-5] 07/06/2022(Tuesday)[2-3]
Relevant Information
• Updates, announcement, course material will be disseminated through google classroom
• Students are advised to join both theory and programming courses
• For beginners:
• Learn C through programming
• Attendance:
• 75% mandatory
• Attendance will be based on serial number
• Faculty is not responsible if students misplaced their seats
Architecture
• Typical system architecture for a desktop PC
Input Output
Memory
Device Device
Transfer of data
ALU CU
Communication of
CPU
control signals
Central Processing Unit (CPU)
• All computations take place here in order for the computer to perform a designated task
• It has a large number of registers which temporarily store data and programs (instructions)
• It has functional units (circuitry) to carry out arithmetic and logic operations
• It retrieves instructions from the memory, interprets (decodes) them, and performs the requested
operation
Main Memory
• Uses semiconductor technology
• Allows direct access
• Memory sizes in the range of 256 MegaBytes to 16 GigaBytes are typical today
• Output Device
• Monitor, Printer
Computer Languages
• A programming language is a language specifically designed to express computations that can be
performed on a computer
• Expressed in binary
• All the commands and data values are expressed using 1s and 0s
• Source code: The stuff you type into the computer. The program you are writing
• Compile(build): Taking source code and making a program that the computer can understand
• Library: Added functions for C programming which are bolted onto do certain tasks
• Header file: Files ending in ‘.h’ which are included at the start of source code
HLL Program
HLL Program
HLL Program
HLL Program
Compiler HLL Program Linker HLL Program
HLL Program Executable Code
Object Code
HLL Program
HLL Program
Libraries
Linker and Loader
• Compiler converts source code written in a programming language (the source language) into
machine language
• If code contains error compiler fails
• Syntax Error
• Logic Error
X1.o
X2.o
Linker X.o
X3.o
• Loader is a special type of program that copies programs from a storage device to main memory,
where they can be executed
• Transparent to user
Operating System
• Makes the computer easy to use
• Basically the computer is very difficult to use
• Understands only machine language
• What is a program?
• Set of instructions for carrying out a specific task
• C is quick running
• C is the basis for many other languages (Java, C++, awk, Perl)
• It may not feel like it but C is one of the easiest languages to learn
Keywords of C [32]
Flow Control (6) if, else, return, switch, case, default
Rare but useful types (7) extern, signed, unsigned, long, short, static, const
• Program
• A translation of the algorithm/flowchart into a form that can be processed by a computer
• Typically written in a high-level language like C, C++, Java
Problem Solving
• Step 1:
• Clearly specify the problem to be solved
• Step 2:
• Draw flowchart or write algorithm
• Step 3:
• Convert flowchart (algorithm) into program code
• Step 4:
• Compile the program into object code
• Step 5:
• Execute the program
Variables and Constants
• Most important concept for problem solving using computers
Address 1
Address 2
Every variable is mapped to a
particular memory address
Address N-2
Address N-1
Variables in Memory
Variable
Instruction Executed
X Y
X = 50 50 ?
Y = 30 50 30
Time
X=Y*5 150 30
Y=X/2 150 75
Flowchart: Basic Symbols
Input/ Output
Decision Box
Start/ Stop
Flowchart: Basic Symbols
Flow of Control
READ A, B, C
S=A+B+C
OUTPUT S
STOP
Types of variable
• We must declare the type of every variable we use in C
• This prevents some bugs caused by spelling errors (misspelling variable names)
• Declarations of types should always be together at the top of main or a function (see later)
• Other types are char, signed, unsigned, long, short and const (see later)
Identifiers
• Names given to various program elements (variables, constants, functions, etc.)
• May consist of letters, digits and the underscore (‘_’) character, with no space between.
• Case sensitive
• ‘area’, ‘AREA’ and ‘Area’ are all different
Valid and Invalid Identifiers
• Valid Identifiers • Invalid Identifiers
• X • 10abc
• Abc • my-name
• simple_interest • “hello”
• _emp_1 • simple interest
• a123 • (area)
• LIST • %rate
• stud_name Empl_3
• Empl_2 avg_empl_salary
Datatypes
• int :- integer quantity
• Typically occupies 4 bytes (32 bits) in memory
• Typical examples: –
• short int
• long int
• unsigned int
• unsigned char
Datatype – Memory [64 bit machine]
Qualifier Data-type Bytes
int 4
short int 2
long int 8
float 4
short float X
long float X
double 8
short double X
long double 16
char 1
short char X
long char X
Datatype – Values [64 bit machine]
Qualifier Data-type Bytes
int -231 to 231-1
unsigned int 0 to 232-1
char -27 to 27-1
unsigned char 0 to 28-1
unsigned float X
unsigned double X
Constants
• Four basic types of constants in C
Constants
e means “10 to
• Two different notations: the power of”
• Decimal notation
• 25.0, 0.0034, .84, -2.234
• Exponential (scientific) notation
• 3.45e23, 0.123e-12, 123E2
Single Character Constants
• Contains a single character enclosed within a pair of single quote marks
• Examples :: ‘2’, ‘+’, ‘Z’
• An escape sequence always begins with a backward slash and is followed by one or more special
characters
• ‘\n’ --- new line
• ‘\t’ --- horizontal tab
• ‘\’’ --- single quote
• ‘\”’ --- double quote
• ‘\\’ --- backslash
• ‘\0’ --- null
Escape Sequence
Character Escape Sequence ASCII value
bell (alert) \a 7
backspace \b 8
horizontal tab \t 9
vertical tab \v 11
newline \n 10
form feed \f 12
carriage return \r 13
double quote(“) \” 34
apostrophe/single quote \’ 39
backslash \\ 92
null \0 0
String Constants
• Sequence of characters enclosed in double quotes
• The characters may be letters, numbers, special characters and blank spaces
• Examples
• “nice”, “Good Morning”, “3+6”, “3”, “C”