Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
37 views

Algorithm and Programming Week4

The document discusses the structure of C programs and various concepts in C including identifiers, variables and constants, and operators. It explains that C is a structured programming language where programs are organized into sections like declarations, main code, and procedures. It provides rules for naming identifiers in C and defines variable types. It also describes different types of operators in C like arithmetic, assignment, relational, and logical operators.

Uploaded by

Lailatul
Copyright
© © All Rights Reserved
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Algorithm and Programming Week4

The document discusses the structure of C programs and various concepts in C including identifiers, variables and constants, and operators. It explains that C is a structured programming language where programs are organized into sections like declarations, main code, and procedures. It provides rules for naming identifiers in C and defines variable types. It also describes different types of operators in C like arithmetic, assignment, relational, and logical operators.

Uploaded by

Lailatul
Copyright
© © All Rights Reserved
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Algorithm

and
Programming
VI190207
Subject of the
Lectures
• C structure • Input / Output Operation
• Variable and contanta • Branc and Loop
• Operator and expression • Array, string and pointer

Dwi Oktavianto Wahyu Nugroho, S.T., M.T.


Murry Raditya, S.T., M.T.
Algorithm and
Programming
VI190207

C Structure

Dwi Oktavianto Wahyu Nugroho, S.T., M.T.


Murry Raditya, S.T., M.T.
C Structure
Background

C is a structured programming language.


It is considered a high-level language because it allows the programmer
to concentrate on the problem at hand and not worry about the machine
that the program will be using.
That is another reason why it is used by software developers whose
applications have to run on many different hardware platforms.
C Structure
Structure of a C Program
C Structure
Processor Directives
Structure of a C Program vs Asssembly Program
Asssembly Program
% T I T L E " S aDirectives
mple Header"
.8086
.model smal l ;Select a memory model
Model Directives
.stack stack_size ;Define the stack size
INCLUDE XXXX.inc

Declaration .data
; Va r i a b l e and a r r a y d e c l a r a t i o n s
; Declare v a r i a b l e s a t t h i s l e v e l

Main .code main proc


; W r i t e t h e program main code a t t h i s level
main endp

; O t h e r P r o ce du r es
; Always o r g a n i z e y o u r program i n t o p r o c e d u r e s
end main ; To mark t h e end o f t h e s o u r c e f i l e

Model Directives
C Structure
Structure of a C Program vs Asssembly Program
Asssembly Program

%macro write_string 2
mov eax, 4 ; system call number (sys_write)
mov ebx, 1 ; file descriptor (stdout)
mov ecx, %1 ; message to write -> mov ecx, msg
mov edx, %2 ; message length -> mov edx, len
int 80h
%endmacro
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
write_string msg1, len1
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
msg1 db 'Hello World!',0xA,0xD
len1 equ $ - msg1 ; $ evaluates to the "current address",
;so $ - msg1 means "the current address
;minus the address with the label msg1".
;This calculates the length of the string
;that starts at msg1
C Structure
Structure of a C Program
C Structure
comment of a C Program
Block Comments

Line Comments
Algorithm and
Programming
VI190207

Identifiers

Dwi Oktavianto Wahyu Nugroho, S.T., M.T.


Murry Raditya, S.T., M.T.
Identifiers
• Identifiers allow us to name data and other objects in the program.
• Each identified object in the computer is stored at a unique address.

Rules for Identifiers


1. First character must be alphabetic character or underscore.
2. Must consist only of alphabetic character, digits, or underscores
3. First 63 character of an identifier are significant.
4. Cannot duplicate a keyword
5. An identifier must start with a letter or underscore: it may not have a space or a hyphen.
6. C is a case-sensitive language.
Identifiers
Rules for Identifiers
1. First character must be alphabetic character or underscore.
2. Must consist only of alphabetic character, digits, or underscores
3. First 63 character of an identifier are significant.
4. Cannot duplicate a keyword
5. An identifier must start with a letter or underscore: it may not have a space or a hyphen.
6. C is a case-sensitive language.
Algorithm and
Programming
VI190207

Variable and Constanta

Dwi Oktavianto Wahyu Nugroho, S.T., M.T.


Murry Raditya, S.T., M.T.
Variable and Constanta
Types
A type defines a set of values and a set of operations that can be applied on those values.
Variable and Constanta
Types
A type defines a set of values and a set of operations that can be applied on those values.

sizeof (short) ≤ sizeof (int) ≤ sizeof (long) ≤ sizeof (long long)


Variable and Constanta
Types
A type defines a set of values and a set of operations that can be applied on those values.

sizeof (float) ≤ sizeof (double) ≤ sizeof (long double)


Variable and Constanta
Types
Variable and Constanta
Variable
• Variables are named memory locations that have a type, such as integer or character, which is inherited
from their type.
• The type determines the values that a variable may contain and the operations that may be used with its
values.
Variable’s identifier

 Variable Declaration char code;


 Variable Initialization int i;
long long national_debt;
float payRate;
double pi;

Note Variable’s type

When a variable is defined, it is not initialized.


We must initialize any variable requiring prescribed data when the function starts.
Variable and Constanta
Variable Declarations and Definitions

bool fact;
short maxItems; //Word separator : Capital
long long national_debt; //Word separator : underscore
float payRate; //Word separator : Capital
double tax;
char code, kind; //Poor style – see text
int a, b; //Poor style – see text
Variable and Constanta
Variable Initialization
Program Memory

char code = ‘b’; b code


int i = 14; 14 i
long long national_debt = 1000000000; 1000000000 national_debt
float payRate = 14.25; 14.25 payRate
double pi = 3.1415926536; 3.1415926536 pi

Address value Address name


Variable and Constanta
Constants
• Constants are data values that cannot be changed during the execution of a program.
• Like variables, constants have a type.

 Constant Representation
 Coding Constants

Note
A character constant is enclosed in single quotes.
Variable and Constanta
Constants – control character
Variable and Constanta
Constants – integer
example

Constants –
real example
Variable and Constanta
Constants – string
example

Constants – null character and null string

Note
Use single quotes for character constants.
Use double quotes for string constants.
Algorithm and
Programming
VI190207

Operator and expression

Dwi Oktavianto Wahyu Nugroho, S.T., M.T.


Murry Raditya, S.T., M.T.
Operator and expression
Operators in C:
An operator is a symbol which operates on a value or a variable.
For example: + is an operator to perform addition.

The operators can be classified as:


 Arithmetic Operators
 Increment and Decrement Operators
 Assignment Operators
 Relational Operators
 Logical Operators
 Conditional Operators
 Bitwise Operators
 Special Operators
Operator and expression
Operators in C:
The operators can be classified as:
 Arithmetic Operators Operator Meaning of Operator
 Increment and Decrement Operators + addition or unary plus
 Assignment Operators - subtraction or unary minus
 Relational Operators * multiplication
 Logical Operators / division
 Conditional Operators
% remainder after division( modulo division)
 Bitwise Operators
 Special Operators
Operator and expression
Operators in C:
The operators can be classified as: 1. C programming has two operators increment ++ and
 Arithmetic Operators decrement -- to change the value of an operand (constant or
 Increment and Decrement Operators variable) by 1.
 Assignment Operators 2. Increment ++ increases the value by 1 whereas decrement --
 Relational Operators decreases the value by 1.
 Logical Operators 3. These two operators are unary operators, meaning they only
 Conditional Operators operate on a single operand.
 Bitwise Operators eg. int a=10, b=100
 Special Operators ++a = 11
--b = 99
Operator and expression
Operators in C:
The operators can be classified as: An assignment operator is used for assigning a value to a
 Arithmetic Operators variable. The most common assignment operator is =
 Increment and Decrement Operators
 Assignment Operators Operator Example Same as
 Relational Operators
= a=b a=b
 Logical Operators
 Conditional Operators += a += b a = a+b
 Bitwise Operators -= a -= b a = a-b
 Special Operators *= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
Operator and expression
Operators in C:
The operators can be classified as: A relational operator checks the relationship between two operands.
If the relation is true, it returns 1;
 Arithmetic Operators if the relation is false, it returns value 0.
 Increment and Decrement Operators
 Assignment Operators Relational operators are used in decision making and loops.
 Relational Operators Operator Meaning of Operator Example
== Equal to 5 == 3 returns 0
 Logical Operators > Greater than 5 > 3 returns 1
 Conditional Operators < Less than 5 < 3 returns 0
 Bitwise Operators != Not equal to 5 != 3 returns 1
 Special Operators >= Greater than or equal to 5 >= 3 returns 1
<= Less than or equal to 5 <= 3 return 0

Operator Description Example


== Checks if the values of two operands are equal or not, if yes then condition becomes true. (A == B) is not true.
!= Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. (A != B) is true.

> Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (A > B) is not true.

< Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (A < B) is true.
Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition
>= (A >= B) is not true.
becomes true.
Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition
<= (A <= B) is true.
becomes true.
Operator and expression
Operators in C:
The operators can be classified as: Operator Description
 Arithmetic Operators && Called Logical AND operator. If both the
 Increment and Decrement Operators operands are non-zero, then condition
 Assignment Operators becomes true.
 Relational Operators
Example : (A && B) is false.
 Logical Operators
 Conditional Operators || Called Logical OR Operator. If any of
 Bitwise Operators the two operands is non-zero, then
 Special Operators condition becomes true.
Example : (A || B) is true.
! Called Logical NOT Operator. Use to
reverses the logical state of its operand.
If a condition is true, then Logical NOT
operator will make false.
Example : !(A && B) is true
Operator and expression
Operators in C:
The operators can be classified as: Category Operator Associativity
 Arithmetic Operators Postfix () [] -> . ++ - - Left to right
 Increment and Decrement Operators Unary + - ! ~ ++ - - (type)* & sizeof Right to left
 Assignment Operators Multiplicative * / % Left to right
 Relational Operators Additive +- Left to right
 Logical Operators Shift << >> Left to right
 Conditional Operators Relational < <= > >= Left to right
 Bitwise Operators Equality == != Left to right
 Special Operators Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right Logical OR ||
Left to right Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left Comma
, Left to right
Operator and expression
Operators in C:
The operators can be classified as: Bitwise operator works on bits and performs bit-by-bit
 Arithmetic Operators operation. The truth tables for &, |, and ^ are as follows:
 Increment and Decrement Operators
 Assignment Operators P q p&q p|q p^q
 Relational Operators
0 0 0 0 0
 Logical Operators
 Conditional Operators 0 1 0 1 1
 Bitwise Operators 1 1 1 1 0
 Special Operators 1 0 0 1 1
Operator and expression
Operators in C:
The operators can be classified as: Category Operator Associativity
 Arithmetic Operators Postfix () [] -> . ++ - - Left to right
 Increment and Decrement Operators Unary + - ! ~ ++ - - (type)* & sizeof Right to left
 Assignment Operators Multiplicative * / % Left to right
 Relational Operators Additive +- Left to right
 Logical Operators Shift << >> Left to right
 Conditional Operators Relational < <= > >= Left to right
 Bitwise Operators Equality == != Left to right
 Special Operators Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right Conditional ?:
Right to left Assignment = += -= *= /= %=>>= <<= &= ^= |=
Right to left
Comma , Left to right
The End

You might also like