Vous êtes sur la page 1sur 24

Introduction to Computers and

Programming (CSC103)
Lecture 03

Introduction to C Language
C is developed at AT&Ts 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

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.

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

Algorithm for solving even and odd number problem:

1)
2)
3)
4)

Get the number


Divide the number by two (2)
If remainder is zero (0), the number is even
Otherwise, the number is odd

This algorithm is then implemented in a specific language


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

Learning English Language vs C Language

C Character Set
Valid alphabets, numbers and special symbols allowed in C

My First C Program with a problem


main()
{
printf("My First C Program");
}
Output:
Compilation Error

My First C Program correction


#include<stdio.h>
void main()
{
printf("My First C Program");
}
Output
My First C Program

Compilation & Execution

Edit

Preprocessor

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
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

Two types of errors

10

Correct the error and restart the compilation process


Syntactic errors (when an invalid statement is written in program)
Semantic errors (logical error)

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

The assembler translated each assembly language


statement into a binary format known as object code

Sometimes assembler is part of the compiler

Object code is written into another file having extension of


obj e.g. prog1.obj

Now the program is ready to be linked


11

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

12

To subsequently execute the program type the name of the


executable file
Loading is the process of placing the program in computers memory
and initiating its execution

Constants, Variables and Keywords

The alphabets, digits and special symbols when properly


combined form constants, variables and keywords
A constant is an entity that doesnt change
A variable is an entity that may change
In a program, we do different calculations

13

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

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

Types of C Constants

C constants can be divided into two major categories

15

Primary Constants
Secondary Constants

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

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

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

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

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

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

21

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

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

Addition Example
#include<stdio.h>
void main()
{
int sum;
sum = 30 + 40;
printf("The sum of 30 and 40 is %d\n", sum);
}

23

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

Vous aimerez peut-être aussi