Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

CSC203 Slide

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Principle of Programming I

CSC 203

Lecture Slide Prepared by:


Mr. Damilola Popoola
(Course Instructor)
Introduction to C language

• ‘C’ programming language is high level


programming language. It is the upgraded version of
another language (Basic Combined Program
Language). It was designed at Bell laboratoriesin
the early1970’s by Dennis Ritchie. C language can
be used in Mathematical Scientific, Engineering and
Commercial applications.

2
Features of C language

1. Simple, versatile and general purpose language


2. It has various set of operators
3. Program execution are fast and efficient.
4. Can easily manipulates with bits, bytes and
addresses
5. Varieties of data types are available

3
C character set

1. Source character set:This type of character set


includes three types of characters namely;
• Alphabets : A to Z, a to z and Underscore( _ )
• Decimal digits : 0 to 9
• iii. Special symbols: + - * / ^ % = & ! () { } [ ] “ etc

4
C character set…contd
2. Execution character set : This set of characters
are invisible and cannot be printed or displayed
directly. They will have effect only when the program is
being executed. These characters are represented by
a back slash (\) followed by a character.

5
Structure of a C program

1. Pre-processor Statements: They are called pre-


processor directives, which begins with the #
symbol. They direct the C pre-processor to include
header files and also symbolic constants in to C
program.
2. main(): it is the main function of every C program
which indicates the of program execution There
must be one and only one main() function in every
C program.

6
Structure of a C program

3. Braces: Every C program uses a pair of curly


braces { }. The left brace indicates beginning of
main() function. On the other hand, the right brace
indicates end of the main() function. The braces
can also be used to indicate the beginning and end
of user-defined functions and compound
statements.
4. Declarations: It is part of C program where all the
variables, arrays, functions etc., used in the C
program are declared and may be initialized with
their basic data types.
7
Structure of a C program

5. Statements: These are instructions to the specific


operations. They may be input-output statements,
arithmetic statements, control statements and other
statements. They are also including comments.
6. User-defined functions: These are subprograms.
Generally, a subprogram is a function, and they
contain a set of statements to perform a specific
task. These are written by the user; hence the
name is user-defined functions. They may be
written before or after the main() function.

8
Basic data types in C
1. Character data: Any character of the ASCII
character set can be considered as a character
data types and its maximum size can be 1 byte or
8 byte long. ‘Char’is the keyword used to represent
character data type in C.
2. Integer data: The keyword ‘int’ stands for the
integer data type in C and its size is either 16 or 32
bits.
3. Floating point data:- The numbers which are
stored in floating point representation with
mantissa and exponent are called floating point
(real) numbers.
4. Double data : - Double is a keyword in C to
represent double precision floating point numbers. 9
Delimiters in C

• This is symbol that has syntactic meaning and has


got significance. These will not specify any operation
to result in a value. C language delimiters list is
given below

10
Variables / Identifiers

These are the names of the objects, whose values can


be changed during the program execution. Variables
are named with description that transmits the value it
holds.
Rules for naming a variable:-
• It can be of letters, digits and underscore( _ )
• First letter should be a letter or an underscore, but it
should not be a digit.
• Reserve words cannot be used as variable names.

11
Operators
An Operator is a symbol that operates on a certain data
type. The data items that operators act upon are called
operands. Some operators require two operands,
some operators act upon only one operand. Operators
in C program includes;
1. Arithmetic Operators
2. Relational Operators
3. Logical Operator
4. Assignment Operator
5. Increment & Decrement Operator
6. Conditional Operator
12
Arithmetic operators
Arithmetic operators performs arithmetic operations.
The Arithmetic operators can operate on any built in
data type. A list of arithmetic operators and their
meaning are;
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo division

13
Relational operators
Relational Operators are used to compare arithmetic,
logical and character expressions. The Relational
Operators compare their left hand side expression with
their right hand side expression. Then evaluates to an
integer. If the Expression is false it evaluate to “zero”(0)
if the expression is true it evaluate to “one” (1)
Operator Meaning
< Less than
> Greater than
<= Less than or Equal to
>= Greater than or Equal to
== Equal to
14
!= Not Equal to
Logical operators
A logical operator is used to evaluate logical and
relational expressions. The logical operators act upon
operands that are themselves logical expressions.
There are three logical operators.
Operators Expression
&& Logical AND
|| Logical OR
! Logical NOT

15
Assignment operators
• An assignment operator is used to assign a value to
a variable. The most commonly used assignment
operator is =. The general format for assignment
operator is :
<Identifer> = < expression >

16
Increment/Decrement operators
The increment/decrement operator act upon a Single
operand and produce a new value is also called as
“unary operator”. The increment operator ++ adds 1 to
the operand and the Decrement operator - - subtracts 1
from the operand.
Syntax: < operator >< variable name >;

17
Conditional operators
It is called ternary because it uses three expression.
The ternary operator acts like If- Else construction.
Syn :( <Exp –1 > ? <Exp-2> : <Exp-3> );

18
Expressions and its evaluation procedure
An expression can be defined as collection of data object and operators
that can be evaluated to lead a single new data object. A data object is a
constant, variable or another data object.
• Evaluation Procedure: The evaluation of arithmetic expressions is
as per the hierarchy rules governed by the C compiler. The
precedence or hierarchy rules for arithmetic expressions are
• 1. The expression is scanned from left to right.
• 2. While scanning the expression, the evaluation preference for the
operators are
*, /, % - evaluated first
+, - - evaluated next
• 3. To overcome the above precedence rules, user has to make use of
parenthesis. If parenthesis is used, the expression/ expressions with
in parenthesis are evaluated first as per the above hierarchy.

19
Statements
• Data Input & Output
An input/output function can be accessed from
anywhere within a program simply by writing the
function name followed by a list of arguments enclosed
in parentheses. The arguments represent data items
that are sent to the function.

20
Statements…contd
1. getchar()
• Single characters can be entered into the computer
using the C library Function getchar(). It returns a
single character from a standard input device. The
function does not require any arguments.
• Syntax: <Character variable> = getchar();
• Example: char c;
c = getchar();

21
Statements…contd
2. putchar()
• Single characters can be displayed using function
putchar(). It returns a single character to a standard
output device. It must be expressed as an argument
to the function.
• Syntax: putchar(<character variable>);
• Example: char c;
putchar(c);

22
Statements…contd
3. scanf()
• Scanf() function can be used input the data into the
memory from the standard input device. This function
can be used to enter any combination of numerical
Values, single characters and strings. The function
returns number of data items.
• Syntax: -scanf (“control strings”, &arg1,&arg2, &argn);
Where control string reference to a string containing
certain required formatting information and arg1,
arg2,…argn are arguments that represent the individual
input data items.

23
Statements…contd

24
Statements…contd
4. printf()
• The printf() function is used to print the data from the
computer’s memory onto a standard output device.
This function can be used to output any combination
of numerical values, single character and strings.
• Syntax: printf(“control string”, arg-1, arg-2,… arg-n );
Where control string is a string that contains
formatted information, and arg-1, arg-2… are
arguments that represent the output data items.

25
Statements…contd
• Assignment statement
Assignment statement can be defined as the statement
through which the value obtained from an expression
can be stored in a variable.
The general form of assignment statement is
< variable name> = < arithmetic expression> ;

26

You might also like