24
Introduction to Computers and Programming (CSC103) Lecture 03

Introduction to Computer and Programming - Lecture 03

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Introduction to Computer and Programming - Lecture 03

Introduction to Computers and

Programming (CSC103)

Lecture 03

Page 2: Introduction to Computer and Programming - Lecture 03

Introduction to C Language

C is developed at AT&T’s Bell Laboratories of USA in 1972

Designed and written by Dennis Ritchie

Gained popularity and support in late 1970

Most of the Operating Systems are written in C

American National Standards Institute (ANSI) formed an

ANSI C committee X3J11 in 1983

In 1990, first official ANSI standard definition was published

Why C? Why not C++, Java or C#?

First have to learn basic learning elements of a language

2

Page 3: Introduction to Computer and Programming - Lecture 03

Programming

Computers are dumb machines; They are told what to do

e.g. How to add two numbers, how to find the average etc

Basic operations of a computer system form instruction

set

Solving a problem

Express the solution in set of instructions

Program: collection of instructions to solve a specific

problem

Algorithm: the approach or method that is used to solve a

problem.

3

Page 4: Introduction to Computer and Programming - Lecture 03

For example: A program that tests if a number is even or

odd

Program: The set of statements that solves the problem

Algorithm: The method that is used to test if a number is even

or odd

To solve a problem, first express the solution in terms of

an algorithm

Then develop a program that implements that algorithm

4

Page 5: Introduction to Computer and Programming - Lecture 03

Algorithm for solving even and odd number problem:

1) Get the number

2) Divide the number by two (2)

3) If remainder is zero (0), the number is even

4) Otherwise, the number is odd

This algorithm is then implemented in a specific language

like C, C++, Java, Visual Basic etc

5

Page 6: Introduction to Computer and Programming - Lecture 03

Learning English Language vs C Language

6

Page 7: Introduction to Computer and Programming - Lecture 03

C Character Set

Valid alphabets, numbers and special symbols allowed in C

7

Page 8: Introduction to Computer and Programming - Lecture 03

My First C Program – with a problem

main() {

printf("My First C Program");

} Output:

Compilation Error

8

Page 9: Introduction to Computer and Programming - Lecture 03

My First C Program – correction

#include<stdio.h>

void main() {

printf("My First C Program");

} Output

My First C Program

9

Page 10: Introduction to Computer and Programming - Lecture 03

Compilation & Execution Edit

Program is first typed into a file

A text editor like notepad is used

Save the file with a valid name along with .c extension e.g. prog1.c

This program is known as source program

Preprocessor Removes comments

Handles directives for source file inclusions, definitions etc

Compile It examines each program statement in the source program

Checks the syntax and semantics of the language

Any error is notified to the user Correct the error and restart the compilation process

Two types of errors Syntactic errors (when an invalid statement is written in program)

Semantic errors (logical error)

10

Page 11: Introduction to Computer and Programming - Lecture 03

If there is no error, the compiler translates each

statement into assembly language form.

Then the assembly language statements are translated

into machine instructions using another program called

assembler

Sometimes assembler is part of the compiler

The assembler translated each assembly language

statement into a binary format known as object code

Object code is written into another file having extension of

”obj” e.g. prog1.obj

Now the program is ready to be linked

11

Page 12: Introduction to Computer and Programming - Lecture 03

Link

The purpose of linking is to get the program into final form for execution

It links other programs if the compiler have used before

Programs from library are also linked

The process of compiling and linking is called building

The final linked file, called executable object code is stored in another file

with extension “.exe”

Execute

To subsequently execute the program type the name of the executable file

Loading is the process of placing the program in computer’s memory and initiating it’s execution

12

Page 13: Introduction to Computer and Programming - Lecture 03

Constants, Variables and Keywords

The alphabets, digits and special symbols when properly

combined form constants, variables and keywords

A constant is an entity that doesn’t change

A variable is an entity that may change

In a program, we do different calculations

The results of these calculations are stored in computers

memory

To make the retrieval of these values easy, these memory

locations are given names

Since the values stored in these memory locations may change

The names given to these memory locations are called variables

13

Page 14: Introduction to Computer and Programming - Lecture 03

Example

For example 3 is stored in a memory location

The name of this memory location is x

Then we assign a new value 5 to this memory location

This will overwrite the earlier value 3, as a memory

location can hold one value at a time

Since the memory location x can hold different values at

different times

x is known as a variable

Value 3 or 5 do not change, hence are known as

constants

14

Page 15: Introduction to Computer and Programming - Lecture 03

Types of C Constants

C constants can be divided into two major categories

Primary Constants

Secondary Constants

15

Page 16: Introduction to Computer and Programming - Lecture 03

Rules for Integer Constants

An integer constant must have at least one digit

It must not have a decimal point

It can be either positive or negative

If no sign precedes an integer constant it is assumed to be

positive

No commas or blanks are allowed within an integer

constant

The allowable range for constants is -32768 to 32767

e.g. 430, +635, -4090

16

Page 17: Introduction to Computer and Programming - Lecture 03

Rules for Real or Float Constants

The float constants could be written in two forms

Fractional form

Exponential form

Following are the rules for float constants

A float constant must have at least one digit

It must have a decimal point

It could be either positive or negative

Default sign is positive

No commas or blanks are allowed within a float constant

e.g. +534.63, 834.0, -27.50

17

Page 18: Introduction to Computer and Programming - Lecture 03

Cont…

In exponential form, the float constant is represented in two parts. The part appearing before e is called mantissa, whereas the part after e is called exponent

Following are the rules for exponential form of float constant

The mantissa and exponential parts should be separated by e

The mantissa part may have a positive or negative sign

Default sign of mantissa part is positive

The exponent must have at least one digit, which must be a positive or negative integer. Default sign is positive

Range of float constant in exponential form is -3.4e38 to 3.4e38 (which is 3.4 x 1038)

e.g. 363e5,+4.2e7, 9.4e-4

18

Page 19: Introduction to Computer and Programming - Lecture 03

Rules for Character Constants

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

a single special symbol enclosed within single inverted

commas

Both inverted commas should point to the left

For example, ’A’ is a valid character constant whereas ‘A’

is not

The maximum length of a character constant can be 1

character

e.g. ‘A’, ‘e’, ‘5’

19

Page 20: Introduction to Computer and Programming - Lecture 03

Types of C Variables

Variable names are given to a location in memory

These locations can contain integer, float or character

constants

Types of variables depends on the types of constants that

it can handle

A particular type of variable can hold only the same type

of constant

e.g. an integer variable can hold only an integer constant,

a float variable can hold a float constant and a character

variable can hold a character constant

20

Page 21: Introduction to Computer and Programming - Lecture 03

Constructing a Variable Name

Constructing the variable names of all types the same set

of rules apply. These rules are given below:

A variable name is any combination of 1 to 31 alphabets, digits

or underscores

Some compilers allow variable names of length 247 characters

Do not create unnecessary long variable names

The first character in the variable name must be an alphabet or

underscore

No commas or blanks are allowed within a variable name

No special characters other than underscore can be used

e.g. si_int, age_max, value80

21

Page 22: Introduction to Computer and Programming - Lecture 03

C compiler distinguish between the variable names by

making it compulsory to declare the type of any variable

you want to use in a program

Following are some type declaration examples

int sum;

float salary;

char name;

22

Page 23: Introduction to Computer and Programming - Lecture 03

Addition Example

#include<stdio.h>

void main()

{

int sum;

sum = 30 + 40;

printf("The sum of 30 and 40 is %d\n", sum);

}

23

Page 24: Introduction to Computer and Programming - Lecture 03

C Keywords

Keywords are the words whose meaning has already been explained to the

C compiler

The keywords cannot be used as variable names

The keywords are also called “Reserved words”

There are 32 keywords available in C

24