SlideShare une entreprise Scribd logo
1  sur  47
Test Program
• #include<conio.h>
• #include <stdio.h>
•
•
•
•

void main()
{
Printf(“Allah is Great“);
}
Copy this code
CSC141 Introduction to Computer
Programming
Preprocessing directives
•Performed by a program called the preprocessor
•Modifies the source code (in RAM) according to
preprocessor
directives
(preprocessor
commands) embedded in the source code
•Strips comments and white space from the code.
•The source code as stored on disk is not
modified.
•Examples:
# include, macros #if

CSC141 Introduction to Computer
Programming
Compilation
•Performed by a program called the compiler
•Translates the preprocessor-modified source code
into object code (machine code)
•Checks for syntax errors and warnings
•Saves the object code to a disk file, if instructed to
do so.
•If any compiler errors are received, no object code
file will be generated.
•An object code file will be generated if only
warnings, not errors, are received.
CSC141 Introduction to Computer
Programming
Object code

•It is machine language code containing various
calls specific to operating system .e.g. the objec
code written by compiler is not only hardware
dependent
but
also
operating
system
dependent.

•So if you have Linux and Windows operating
systems then object file compiled by one
Operating System (OS) will not get executed on
the other OS.
CSC141 Introduction to Computer
Programming
Linking
•Combines the program object code with other
object code files to produce the executable file.
•The other object code files can come from the
Run-Time Library, other libraries, or object files
that you have created.
•Saves the executable code to a disk file
•If any linker errors are received, no executable file
will be generated.
CSC141 Introduction to Computer
Programming
Program Development
•Editor produces Source File

program.c
•Preprocessor Modified Source Code
not saved on disk
•Compiler produces Object Code File
program.obj
•Linker links Object Code File and Other Object
Code Files (if any) and produces Executable File
program.exe
CSC141 Introduction to Computer
Programming
Basic Structure of C programs
#include <stdio.h>
#include <conio.h>

void main ( )
{
printf( “Hello World”);
}

void main ( )
•Every C program consists of one or more functions.
• main ( ) is a function. It is the first function to
which control is passed from OS when a program is
executed
•void indicates that the main function does not
return any value

CSC141 Introduction to Computer
Programming
Basic Structure of C programs
#include <stdio.h>
#include <conio.h>
void main ( )
{

Syntax
•printf() is a statement.
•A statement in C is terminated by a semi colon (
;).

printf(“Hello World”);
}

CSC141 Introduction to Computer
Programming
Basic Structure of C programs
#include <iostream>
Using namespace std;
void main ( )
{
cout << “Hello World”;
}

Delimiters
•Opening and Closing brace or Curly
brackets marks the start and end of the
code
•The start and end of a block ( such as loop,
if else statement, switch statement) is also
identified by the start and end of curly
brackets.

CSC141 Introduction to Computer
Programming
Basic Structure of C programs
Syntax
•C pays no attention to the “whitespace”
characters encountered in a program like
space, carriage return(newline) and tab
•You can put as many whitespaces in your
program as you like; they will be invisible to
the compiler

#include <iostream>
Using namespace std;
void main ( )
{
cout << “Hello World”;
}

CSC141 Introduction to Computer
Programming
Basic Structure of C programs
#include <iostream>
Using namespace std;
void main ( )
{
cout << “Hello World”;
}

Indentation
•Stretching the code vertically makes it
more readable
•Indentation of blocks of code enclosed in
braces is an important aspect for making
the programs readable
•e.g. the printf statement that is indented in
the previous example
•The same program below will execute
perfectly

#include <iostream> using namespace std; void main ( ){cout << “Hello World” ;}

CSC141 Introduction to Computer
Programming
Variable Names:
Name assigned to a memory location
•Variables are like containers in your computer's memory
•you can store values in them and retrieve or modify them
when necessary.

CSC141 Introduction to Computer
Programming
Variable Value:
L-value
•Variables are also known as l-values.
•L-values are values that can be on the left
side of an assignment statement.
•When we do an assignment, the left hand
side of the assignment operator must be an lvalue.
CSC141 Introduction to Computer
Programming
R-values (Opposite of L-values)
•An r-value refers to any value that can be
assigned to an l-value.
•R-values are always evaluated to produce a
single value.
Examples
•Single numbers (7, which evaluates to numeric
value 7)
•Variables (x, which evaluates to whatever
number was last assigned to it)
•Expressions (x + 5, which evaluates to the last
value of x plus 5).
CSC141 Introduction to Computer
Programming
Assignment Operator
3=5
X+1=y+2
x+5=Y
Z=y+4

In correct
In correct
Incorrect
Correct

When l-value has assigned a r-value to it,
the current l-value is overwritten with rvalue

CSC141 Introduction to Computer
Programming
Variable names are called identifiers
But
all the identifiers are not variable names!!!
Identifiers Refer to the Names of:
•variables
•data types
•Constants
•functions
CSC141 Introduction to Computer
Programming
Identifier: Variable Names
Any combination of letters, numbers,
underscore (_)
•Case sensitive
"sum" is different than "Sum“ or “SUM”

and

•Cannot begin with a digit
1stName ------ illegal
devideby10 ---- legal
•Usually, variables beginning with underscore are
used only in special library routines.
_validsystemcall ---- legal
CSC141 Introduction to Computer
Programming
Identifier: Variable Names
•Usually only the first 32 characters are significant
aReally_longName_moreThan31chars
aReally_longName_moreThan31characters
(both of the above identifiers are legal and the
same)
•There can be no embedded blanks or special character
gross salary ( is illegal because it contains space )
%rate

( is illegal because it contains special
character )

•Keywords or Reserve words cannot be used as
identifiers
CSC141 Introduction to Computer
float
( is illegal because it contains a
Programming
Keywords / Reserved Words
•Some words may not be used as identifiers
•These words have special meaning in C
Keywords
auto
break
case
char
const
continue
default
do
double
else

enum
extern
float
for
goto
if
int
long
register
return
short

signed
sizeof
static
struct
switch
typedef
union
unsigned
void
volatile
while
CSC141 Introduction to Computer
Programming
Data types in C/ C++
•Integer
declared as
•float
declared as
float
•double
declared as
•Character declared as
char
•Void
declared as
void

CSC141 Introduction to Computer
Programming

int
double
Size and Types of Data Types
•Int

Integer stores an integer value e.g. 125
int (at least 16 bits, commonly 32 bits)
long (at least 32 bits)
short (at least 8 bits)
Signed vs. unsigned integers:
Default is 2’s complement signed integers

Use “unsigned” keyword for unsigned
numbers
CSC141 Introduction to Computer
Programming
Size and Types of Data Types
•float floating point (at least 32 bits)
stores decimal value (e.g.
125.1234567)
•double

floating point (commonly 64 bits )
stores decimal value
(e.g. 125.123456789012345)
double the precision of a float

CSC141 Introduction to Computer
Programming
Size and Types of Data Types
•char character (at least 8 bits)
stores an integer representing a character
(e.g. ‘A’)
different codes of 8 bit and 15 bit
ANSCII – American National Standard Code
for Information Interchange (8 bits)
EBCDIC – Extended Binary Coded Decimal
interchange Code (8 bits)
UNICODE – 15 bit code
CSC141 Introduction to Computer
Programming
Size and Types of Data Types
• void means nothing ( zero bytes)
used for the function return nothing.
e.g. void main ( void)

CSC141 Introduction to Computer
Programming
Size of Data Types
Exact size can vary, depending on processor
•int is supposed to be "natural" integer size;
for older processors, it's 16 bits
for most modern processors it’s 32 bits
How can you know the size?
•Use function called sizeof.
e.g. sizeof(int), it returns answer in bytes
CSC141 Introduction to Computer
Programming
Additional to Data Type
Like constants but is preprocessor directive
Literal
•Unnamed constants that appear literally in source
code.
Constants
•Variables whose values do not change during the
execution of the program
•This done by appending ‘const” before the data
type
Symbols
•Are values defined by using #define
•Values stay constant during single execution of the
CSC141 Introduction to Computer
Programming
program
Literals
Integer
•125
•-125
•0x125
Character
•‘s‘
•'t'
•'xC'

( positive decimal )
( negative decimal )
( treated as octal)
(character s )
( tab )
(ASCII 12 (0xC) )

Floating point
•6.023
( real value)
•6.023e23
( 6.023 x 1023 )
•5E12
( 5.0 x 1012 )
CSC141 Introduction to Computer
Programming
Constants and Symbol
#define RADIUS 15.0
int main( )
{
const double pi = 3.14159;
double area;
double circum;
area = pi * RADIUS * RADIUS;
circum = 2 * pi * RADIUS;
}

symbol
constant
literal

CSC141 Introduction to Computer
Programming
Operators
An operator has:
•Function
What it does?
Example: + , -, *, / or others
•Precedence
In which order it will execute
Example: "a * b + c * d"
will be executed as : "(a * b) + (c * d)"
because multiply (*) has a higher
precedence
than addition (+)
CSC141 Introduction to Computer
Programming
Operators
•Associativity

Order In which the operators of the
same precedence combined?
Example: "a - b - c"
will be executed in the order "(a - b) - c"
because add/sub associate left-to-right

CSC141 Introduction to Computer
Programming
Assignment Operator
• l-value equals to r-value
x = x + 5;
Evaluate right-hand side (x + 5 ) called r-value
Set l-value ( of left-hand side variable) to r-value.

CSC141 Introduction to Computer
Programming
Assignment Operator (contd..)
•All expressions evaluate to a value with the
assignment operator
Example:
y = x = 5;
the result is the value assigned
•Assignment is associated from right to left.
y = x = 5;
y gets the value 5,
because (x = 5) evaluates to the value 5
CSC141 Introduction to Computer
Programming
Arithmetic Operators
Symbol
•*
•/
•%
•+
•-

Operation
multiply
divide
modulo
addition
subtraction

*/%

have same precedence
whichever comes from left to right
have higher precedence than + have same precedence
whichever comes from left to right
have lower precedence than + -

+ -

Example
x*y
x/y
x%y
x+y
x–y

CSC141 Introduction to Computer
Programming

Association
Left to Right
Left to Right
Left to Right
Left to Right
Left to Right
Arithmetic Operators
• Addition, subtraction, and multiplication work as
you would expect.
• Division (/) returns the whole part of the division
(the quotient)
12 / 3 = 4
15 / 2 = 7
• Modulus (%) returns the remainder
12 % 3 = 0
15 % 2 = 1
• Example of precedence
2+3*4
equals
14
where as (2 + 3) * 4 equals

CSC141 Introduction to Computer
Programming

20
Arithmetic Expressions
•If mixed types, smaller type is "promoted" to larger
Example: x + 4.3
if x is int, converted to double and result is double
•Integer division -- fraction is dropped
Example: x / 3
if x is int and x=5, result is 1 (not 1.666666...)
• Parentheses ( ) can be used to force a different order of
evaluation:
12 - 5 * 2 = 2
But (12 - 5) * 2 = 14

CSC141 Introduction to Computer
Programming
Arithmetic Operator Precedence
•Precedence rules specify the order in which operators are
evaluated
•Associativity determines from Left-to- Right order
•Remember for arithmetic operators precedence
PMDAS
Parentheses, Multiplication, Division, Addition, Subtraction

CSC141 Introduction to Computer
Programming
Bitwise Operators
~
<<
>>

bitwise NOT
shift left
shift right

&
^
|

bitwise AND
bitwise XOR
bitwise OR

~x
x << y
x >> y
x&y
x^y
x|y

Will be taught in future

CSC141 Introduction to Computer
Programming
Logical Operators
Symbol

Operation

Example

Association

!
&&
||

Logical NOT !x
logical AND x && y
logical OR
x || y

Right to Left
Right to Left
Right to Left

Treats entire variable (or value) as:
TRUE (non-zero), or FALSE (zero)
Result is 1 (TRUE) or 0 (FALSE)

CSC141 Introduction to Computer
Programming
Relational Operators
Symbol
>
>=
<
L
<=
==
!=

Operation

greater than
greater than or equal
less than
less than or equal
equal
not equal

Example

Association

x > y
x >= y
x < y

R-to-L
R-to-L

x <= y
x == y
x != y

R-to-L
R-to-L
R-to-L

Result is 1 (TRUE) or 0 (FALSE)

CSC141 Introduction to Computer
Programming

R-to-
Assignment ( =) vs. Equality ( ==)
Be Careful using equality (==) and assignment (=)
operators:
int x = 5;
int y = 7;
if (x == y) {
printf(“ will not be printedn”);
}
if (x = y) {
printf(“x = %d y = %d”, x, y);
}
Result: “x = 7 y = 7” is printed.
Explain?
CSC141 Introduction to Computer
Programming
Special Operators: ++ and -Increment and decrement the value of variable before or after
its value is used in an expression.
Symbol
++
-++
--

Example
x++
x-++x
--x

Operation
Increment after (Post increment)
decrement after (Post decrement)
Increment before (Pre increment)
decrement before (Pre decrement)

CSC141 Introduction to Computer
Programming
Examples Using ++ and -Example-1:
int x = 4;
y = x++;
printf ( “X = %d t “Y = %d “, x, y )
will print
X=5 Y=4
as x is incremented after assignment operation.
Example-2:
x = 4;
y = ++x;
printf ( “X = %d t “Y = %d “, x, y )
will print
X=5 Y=5
as x is incremented before assignment operation.

CSC141 Introduction to Computer
Programming
Special Operators: +=, *=, etc.
Arithmetic operators and bitwise operators can be
combined with assignment operator
Equivalent assignment statement:
x += y; Equivalent to
x = x + y;
x -= y;
Equivalent to
x = x - y;
x *= y;
Equivalent to
x = x * y;
x /= y;
Equivalent to
x = x / y;
x %= y; Equivalent to
x = x % y;
x &= y; Equivalent to
x = x & y;
x |= y;
Equivalent to
x = x | y;
x ^= y; Equivalent to
x = x ^ y;
x <<= y; Equivalent to
x = x << y;
x >>= y; Equivalent to
x = x >> y;
CSC141 Introduction to Computer
Programming
Special Operator: Conditional
Symbol
?:

Operation
conditional

Example
x?y:z

Explanation: x ? y : z
If x is non-zero,(true) then result of expression is y
If x is zero, (false) then result of expression is z

CSC141 Introduction to Computer
Programming
Practice Quiz
Question -1: Solve the following expressions
a).
3-8/4
b). 3 * 4 + 18 / 2
Solution:
•/ has the highest precedence, so we
compute 8 / 4 first, then subtract the result
from 3 the answer is 1
•3 * 4 + 18 / 2
12 + 9
The answer is 21

CSC141 Introduction to Computer
Programming
Practice Quiz
Question - 2: If a=2, b=4, c=6, d=8, then what will be
the result of the following expression?
x = a * b + c * d / 4;
Will be solved as:
x = (a * b) + ((c * d) / 4);
x = ( 2 * 4) + ((6 * 8) / 4);
x = ( 8 ) + (12 )
x=
20

CSC141 Introduction to Computer
Programming
Practice Quiz
Question - 3: Put the parentheses in the order of
execution of the following expression.
5-3+4+2-1+7
•+ and - have equal precedence, so this
expression is evaluated left to right:
(((((5 - 3) + 4) + 2) - 1) + 7)
•Innermost parentheses are evaluated first

CSC141 Introduction to Computer
Programming

Contenu connexe

Tendances

Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C ProgrammingMOHAMAD NOH AHMAD
 
User_Defined_Functions_ppt_slideshare.
User_Defined_Functions_ppt_slideshare.User_Defined_Functions_ppt_slideshare.
User_Defined_Functions_ppt_slideshare.NabeelaNousheen
 
Complete C programming Language Course
Complete C programming Language CourseComplete C programming Language Course
Complete C programming Language CourseVivek chan
 
C Programming Language Step by Step Part 1
C Programming Language Step by Step Part 1C Programming Language Step by Step Part 1
C Programming Language Step by Step Part 1Rumman Ansari
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programminggajendra singh
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programmingRutvik Pensionwar
 
Complete C++ programming Language Course
Complete C++ programming Language CourseComplete C++ programming Language Course
Complete C++ programming Language CourseVivek chan
 
Brief introduction to the c programming language
Brief introduction to the c programming languageBrief introduction to the c programming language
Brief introduction to the c programming languageKumar Gaurav
 
C the basic concepts
C the basic conceptsC the basic concepts
C the basic conceptsAbhinav Vatsa
 
OpenGurukul : Language : C Programming
OpenGurukul : Language : C ProgrammingOpenGurukul : Language : C Programming
OpenGurukul : Language : C ProgrammingOpen Gurukul
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial javaTpoint s
 

Tendances (20)

Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
C programming
C programmingC programming
C programming
 
Learning the C Language
Learning the C LanguageLearning the C Language
Learning the C Language
 
User_Defined_Functions_ppt_slideshare.
User_Defined_Functions_ppt_slideshare.User_Defined_Functions_ppt_slideshare.
User_Defined_Functions_ppt_slideshare.
 
C programming language
C programming languageC programming language
C programming language
 
Complete C programming Language Course
Complete C programming Language CourseComplete C programming Language Course
Complete C programming Language Course
 
C program
C programC program
C program
 
C Programming Language Step by Step Part 1
C Programming Language Step by Step Part 1C Programming Language Step by Step Part 1
C Programming Language Step by Step Part 1
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
C basics
C   basicsC   basics
C basics
 
Deep C
Deep CDeep C
Deep C
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Complete C++ programming Language Course
Complete C++ programming Language CourseComplete C++ programming Language Course
Complete C++ programming Language Course
 
Brief introduction to the c programming language
Brief introduction to the c programming languageBrief introduction to the c programming language
Brief introduction to the c programming language
 
C the basic concepts
C the basic conceptsC the basic concepts
C the basic concepts
 
OpenGurukul : Language : C Programming
OpenGurukul : Language : C ProgrammingOpenGurukul : Language : C Programming
OpenGurukul : Language : C Programming
 
Intro to C++ - language
Intro to C++ - languageIntro to C++ - language
Intro to C++ - language
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 

Similaire à C Programming

cmp104 lec 8
cmp104 lec 8cmp104 lec 8
cmp104 lec 8kapil078
 
Programming using c++ tool
Programming using c++ toolProgramming using c++ tool
Programming using c++ toolAbdullah Jan
 
l1-introduction_to_computers_and_c_programming.pptx
l1-introduction_to_computers_and_c_programming.pptxl1-introduction_to_computers_and_c_programming.pptx
l1-introduction_to_computers_and_c_programming.pptxssuser6f38e5
 
424769021-1-First-C-Program-1-ppt (1).ppt
424769021-1-First-C-Program-1-ppt (1).ppt424769021-1-First-C-Program-1-ppt (1).ppt
424769021-1-First-C-Program-1-ppt (1).pptadvRajatSharma
 
490450755-Chapter-2.ppt
490450755-Chapter-2.ppt490450755-Chapter-2.ppt
490450755-Chapter-2.pptManiMala75
 
490450755-Chapter-2.ppt
490450755-Chapter-2.ppt490450755-Chapter-2.ppt
490450755-Chapter-2.pptManiMala75
 
Chapter-2 edited on Programming in Can refer this ppt
Chapter-2 edited on Programming in Can refer this pptChapter-2 edited on Programming in Can refer this ppt
Chapter-2 edited on Programming in Can refer this pptANISHYAPIT
 
270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.pptUdhayaKumar175069
 
Survey of programming language getting started in C
Survey of programming language getting started in CSurvey of programming language getting started in C
Survey of programming language getting started in Cummeafruz
 

Similaire à C Programming (20)

cmp104 lec 8
cmp104 lec 8cmp104 lec 8
cmp104 lec 8
 
Programming using c++ tool
Programming using c++ toolProgramming using c++ tool
Programming using c++ tool
 
C for Engineers
C for EngineersC for Engineers
C for Engineers
 
Introduction Of C++
Introduction Of C++Introduction Of C++
Introduction Of C++
 
College1
College1College1
College1
 
l1-introduction_to_computers_and_c_programming.pptx
l1-introduction_to_computers_and_c_programming.pptxl1-introduction_to_computers_and_c_programming.pptx
l1-introduction_to_computers_and_c_programming.pptx
 
Unit ii
Unit   iiUnit   ii
Unit ii
 
C language unit-1
C language unit-1C language unit-1
C language unit-1
 
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDYC LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
 
C languaGE UNIT-1
C languaGE UNIT-1C languaGE UNIT-1
C languaGE UNIT-1
 
Prog1-L1.pdf
Prog1-L1.pdfProg1-L1.pdf
Prog1-L1.pdf
 
424769021-1-First-C-Program-1-ppt (1).ppt
424769021-1-First-C-Program-1-ppt (1).ppt424769021-1-First-C-Program-1-ppt (1).ppt
424769021-1-First-C-Program-1-ppt (1).ppt
 
490450755-Chapter-2.ppt
490450755-Chapter-2.ppt490450755-Chapter-2.ppt
490450755-Chapter-2.ppt
 
490450755-Chapter-2.ppt
490450755-Chapter-2.ppt490450755-Chapter-2.ppt
490450755-Chapter-2.ppt
 
Chapter-2 edited on Programming in Can refer this ppt
Chapter-2 edited on Programming in Can refer this pptChapter-2 edited on Programming in Can refer this ppt
Chapter-2 edited on Programming in Can refer this ppt
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Presentation c++
Presentation c++Presentation c++
Presentation c++
 
C
CC
C
 
270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt
 
Survey of programming language getting started in C
Survey of programming language getting started in CSurvey of programming language getting started in C
Survey of programming language getting started in C
 

Plus de educationfront

Personality Development in Islam
Personality Development in IslamPersonality Development in Islam
Personality Development in Islameducationfront
 
Introduction to Programming Languages
Introduction to Programming LanguagesIntroduction to Programming Languages
Introduction to Programming Languageseducationfront
 
Fundamentals of Computer
Fundamentals of Computer Fundamentals of Computer
Fundamentals of Computer educationfront
 
Punjab Public Service Commission (PPSC)
Punjab Public Service Commission (PPSC)Punjab Public Service Commission (PPSC)
Punjab Public Service Commission (PPSC)educationfront
 

Plus de educationfront (6)

Personality Development in Islam
Personality Development in IslamPersonality Development in Islam
Personality Development in Islam
 
Introduction to Programming Languages
Introduction to Programming LanguagesIntroduction to Programming Languages
Introduction to Programming Languages
 
Fundamentals of Computer
Fundamentals of Computer Fundamentals of Computer
Fundamentals of Computer
 
Fcp lecture 01
Fcp lecture 01Fcp lecture 01
Fcp lecture 01
 
Punjab Public Service Commission (PPSC)
Punjab Public Service Commission (PPSC)Punjab Public Service Commission (PPSC)
Punjab Public Service Commission (PPSC)
 
Action research
Action researchAction research
Action research
 

Dernier

Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxPoojaSen20
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 

Dernier (20)

Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 

C Programming

  • 1. Test Program • #include<conio.h> • #include <stdio.h> • • • • void main() { Printf(“Allah is Great“); } Copy this code CSC141 Introduction to Computer Programming
  • 2. Preprocessing directives •Performed by a program called the preprocessor •Modifies the source code (in RAM) according to preprocessor directives (preprocessor commands) embedded in the source code •Strips comments and white space from the code. •The source code as stored on disk is not modified. •Examples: # include, macros #if CSC141 Introduction to Computer Programming
  • 3. Compilation •Performed by a program called the compiler •Translates the preprocessor-modified source code into object code (machine code) •Checks for syntax errors and warnings •Saves the object code to a disk file, if instructed to do so. •If any compiler errors are received, no object code file will be generated. •An object code file will be generated if only warnings, not errors, are received. CSC141 Introduction to Computer Programming
  • 4. Object code •It is machine language code containing various calls specific to operating system .e.g. the objec code written by compiler is not only hardware dependent but also operating system dependent. •So if you have Linux and Windows operating systems then object file compiled by one Operating System (OS) will not get executed on the other OS. CSC141 Introduction to Computer Programming
  • 5. Linking •Combines the program object code with other object code files to produce the executable file. •The other object code files can come from the Run-Time Library, other libraries, or object files that you have created. •Saves the executable code to a disk file •If any linker errors are received, no executable file will be generated. CSC141 Introduction to Computer Programming
  • 6. Program Development •Editor produces Source File program.c •Preprocessor Modified Source Code not saved on disk •Compiler produces Object Code File program.obj •Linker links Object Code File and Other Object Code Files (if any) and produces Executable File program.exe CSC141 Introduction to Computer Programming
  • 7. Basic Structure of C programs #include <stdio.h> #include <conio.h> void main ( ) { printf( “Hello World”); } void main ( ) •Every C program consists of one or more functions. • main ( ) is a function. It is the first function to which control is passed from OS when a program is executed •void indicates that the main function does not return any value CSC141 Introduction to Computer Programming
  • 8. Basic Structure of C programs #include <stdio.h> #include <conio.h> void main ( ) { Syntax •printf() is a statement. •A statement in C is terminated by a semi colon ( ;). printf(“Hello World”); } CSC141 Introduction to Computer Programming
  • 9. Basic Structure of C programs #include <iostream> Using namespace std; void main ( ) { cout << “Hello World”; } Delimiters •Opening and Closing brace or Curly brackets marks the start and end of the code •The start and end of a block ( such as loop, if else statement, switch statement) is also identified by the start and end of curly brackets. CSC141 Introduction to Computer Programming
  • 10. Basic Structure of C programs Syntax •C pays no attention to the “whitespace” characters encountered in a program like space, carriage return(newline) and tab •You can put as many whitespaces in your program as you like; they will be invisible to the compiler #include <iostream> Using namespace std; void main ( ) { cout << “Hello World”; } CSC141 Introduction to Computer Programming
  • 11. Basic Structure of C programs #include <iostream> Using namespace std; void main ( ) { cout << “Hello World”; } Indentation •Stretching the code vertically makes it more readable •Indentation of blocks of code enclosed in braces is an important aspect for making the programs readable •e.g. the printf statement that is indented in the previous example •The same program below will execute perfectly #include <iostream> using namespace std; void main ( ){cout << “Hello World” ;} CSC141 Introduction to Computer Programming
  • 12. Variable Names: Name assigned to a memory location •Variables are like containers in your computer's memory •you can store values in them and retrieve or modify them when necessary. CSC141 Introduction to Computer Programming
  • 13. Variable Value: L-value •Variables are also known as l-values. •L-values are values that can be on the left side of an assignment statement. •When we do an assignment, the left hand side of the assignment operator must be an lvalue. CSC141 Introduction to Computer Programming
  • 14. R-values (Opposite of L-values) •An r-value refers to any value that can be assigned to an l-value. •R-values are always evaluated to produce a single value. Examples •Single numbers (7, which evaluates to numeric value 7) •Variables (x, which evaluates to whatever number was last assigned to it) •Expressions (x + 5, which evaluates to the last value of x plus 5). CSC141 Introduction to Computer Programming
  • 15. Assignment Operator 3=5 X+1=y+2 x+5=Y Z=y+4 In correct In correct Incorrect Correct When l-value has assigned a r-value to it, the current l-value is overwritten with rvalue CSC141 Introduction to Computer Programming
  • 16. Variable names are called identifiers But all the identifiers are not variable names!!! Identifiers Refer to the Names of: •variables •data types •Constants •functions CSC141 Introduction to Computer Programming
  • 17. Identifier: Variable Names Any combination of letters, numbers, underscore (_) •Case sensitive "sum" is different than "Sum“ or “SUM” and •Cannot begin with a digit 1stName ------ illegal devideby10 ---- legal •Usually, variables beginning with underscore are used only in special library routines. _validsystemcall ---- legal CSC141 Introduction to Computer Programming
  • 18. Identifier: Variable Names •Usually only the first 32 characters are significant aReally_longName_moreThan31chars aReally_longName_moreThan31characters (both of the above identifiers are legal and the same) •There can be no embedded blanks or special character gross salary ( is illegal because it contains space ) %rate ( is illegal because it contains special character ) •Keywords or Reserve words cannot be used as identifiers CSC141 Introduction to Computer float ( is illegal because it contains a Programming
  • 19. Keywords / Reserved Words •Some words may not be used as identifiers •These words have special meaning in C Keywords auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while CSC141 Introduction to Computer Programming
  • 20. Data types in C/ C++ •Integer declared as •float declared as float •double declared as •Character declared as char •Void declared as void CSC141 Introduction to Computer Programming int double
  • 21. Size and Types of Data Types •Int Integer stores an integer value e.g. 125 int (at least 16 bits, commonly 32 bits) long (at least 32 bits) short (at least 8 bits) Signed vs. unsigned integers: Default is 2’s complement signed integers Use “unsigned” keyword for unsigned numbers CSC141 Introduction to Computer Programming
  • 22. Size and Types of Data Types •float floating point (at least 32 bits) stores decimal value (e.g. 125.1234567) •double floating point (commonly 64 bits ) stores decimal value (e.g. 125.123456789012345) double the precision of a float CSC141 Introduction to Computer Programming
  • 23. Size and Types of Data Types •char character (at least 8 bits) stores an integer representing a character (e.g. ‘A’) different codes of 8 bit and 15 bit ANSCII – American National Standard Code for Information Interchange (8 bits) EBCDIC – Extended Binary Coded Decimal interchange Code (8 bits) UNICODE – 15 bit code CSC141 Introduction to Computer Programming
  • 24. Size and Types of Data Types • void means nothing ( zero bytes) used for the function return nothing. e.g. void main ( void) CSC141 Introduction to Computer Programming
  • 25. Size of Data Types Exact size can vary, depending on processor •int is supposed to be "natural" integer size; for older processors, it's 16 bits for most modern processors it’s 32 bits How can you know the size? •Use function called sizeof. e.g. sizeof(int), it returns answer in bytes CSC141 Introduction to Computer Programming
  • 26. Additional to Data Type Like constants but is preprocessor directive Literal •Unnamed constants that appear literally in source code. Constants •Variables whose values do not change during the execution of the program •This done by appending ‘const” before the data type Symbols •Are values defined by using #define •Values stay constant during single execution of the CSC141 Introduction to Computer Programming program
  • 27. Literals Integer •125 •-125 •0x125 Character •‘s‘ •'t' •'xC' ( positive decimal ) ( negative decimal ) ( treated as octal) (character s ) ( tab ) (ASCII 12 (0xC) ) Floating point •6.023 ( real value) •6.023e23 ( 6.023 x 1023 ) •5E12 ( 5.0 x 1012 ) CSC141 Introduction to Computer Programming
  • 28. Constants and Symbol #define RADIUS 15.0 int main( ) { const double pi = 3.14159; double area; double circum; area = pi * RADIUS * RADIUS; circum = 2 * pi * RADIUS; } symbol constant literal CSC141 Introduction to Computer Programming
  • 29. Operators An operator has: •Function What it does? Example: + , -, *, / or others •Precedence In which order it will execute Example: "a * b + c * d" will be executed as : "(a * b) + (c * d)" because multiply (*) has a higher precedence than addition (+) CSC141 Introduction to Computer Programming
  • 30. Operators •Associativity Order In which the operators of the same precedence combined? Example: "a - b - c" will be executed in the order "(a - b) - c" because add/sub associate left-to-right CSC141 Introduction to Computer Programming
  • 31. Assignment Operator • l-value equals to r-value x = x + 5; Evaluate right-hand side (x + 5 ) called r-value Set l-value ( of left-hand side variable) to r-value. CSC141 Introduction to Computer Programming
  • 32. Assignment Operator (contd..) •All expressions evaluate to a value with the assignment operator Example: y = x = 5; the result is the value assigned •Assignment is associated from right to left. y = x = 5; y gets the value 5, because (x = 5) evaluates to the value 5 CSC141 Introduction to Computer Programming
  • 33. Arithmetic Operators Symbol •* •/ •% •+ •- Operation multiply divide modulo addition subtraction */% have same precedence whichever comes from left to right have higher precedence than + have same precedence whichever comes from left to right have lower precedence than + - + - Example x*y x/y x%y x+y x–y CSC141 Introduction to Computer Programming Association Left to Right Left to Right Left to Right Left to Right Left to Right
  • 34. Arithmetic Operators • Addition, subtraction, and multiplication work as you would expect. • Division (/) returns the whole part of the division (the quotient) 12 / 3 = 4 15 / 2 = 7 • Modulus (%) returns the remainder 12 % 3 = 0 15 % 2 = 1 • Example of precedence 2+3*4 equals 14 where as (2 + 3) * 4 equals CSC141 Introduction to Computer Programming 20
  • 35. Arithmetic Expressions •If mixed types, smaller type is "promoted" to larger Example: x + 4.3 if x is int, converted to double and result is double •Integer division -- fraction is dropped Example: x / 3 if x is int and x=5, result is 1 (not 1.666666...) • Parentheses ( ) can be used to force a different order of evaluation: 12 - 5 * 2 = 2 But (12 - 5) * 2 = 14 CSC141 Introduction to Computer Programming
  • 36. Arithmetic Operator Precedence •Precedence rules specify the order in which operators are evaluated •Associativity determines from Left-to- Right order •Remember for arithmetic operators precedence PMDAS Parentheses, Multiplication, Division, Addition, Subtraction CSC141 Introduction to Computer Programming
  • 37. Bitwise Operators ~ << >> bitwise NOT shift left shift right & ^ | bitwise AND bitwise XOR bitwise OR ~x x << y x >> y x&y x^y x|y Will be taught in future CSC141 Introduction to Computer Programming
  • 38. Logical Operators Symbol Operation Example Association ! && || Logical NOT !x logical AND x && y logical OR x || y Right to Left Right to Left Right to Left Treats entire variable (or value) as: TRUE (non-zero), or FALSE (zero) Result is 1 (TRUE) or 0 (FALSE) CSC141 Introduction to Computer Programming
  • 39. Relational Operators Symbol > >= < L <= == != Operation greater than greater than or equal less than less than or equal equal not equal Example Association x > y x >= y x < y R-to-L R-to-L x <= y x == y x != y R-to-L R-to-L R-to-L Result is 1 (TRUE) or 0 (FALSE) CSC141 Introduction to Computer Programming R-to-
  • 40. Assignment ( =) vs. Equality ( ==) Be Careful using equality (==) and assignment (=) operators: int x = 5; int y = 7; if (x == y) { printf(“ will not be printedn”); } if (x = y) { printf(“x = %d y = %d”, x, y); } Result: “x = 7 y = 7” is printed. Explain? CSC141 Introduction to Computer Programming
  • 41. Special Operators: ++ and -Increment and decrement the value of variable before or after its value is used in an expression. Symbol ++ -++ -- Example x++ x-++x --x Operation Increment after (Post increment) decrement after (Post decrement) Increment before (Pre increment) decrement before (Pre decrement) CSC141 Introduction to Computer Programming
  • 42. Examples Using ++ and -Example-1: int x = 4; y = x++; printf ( “X = %d t “Y = %d “, x, y ) will print X=5 Y=4 as x is incremented after assignment operation. Example-2: x = 4; y = ++x; printf ( “X = %d t “Y = %d “, x, y ) will print X=5 Y=5 as x is incremented before assignment operation. CSC141 Introduction to Computer Programming
  • 43. Special Operators: +=, *=, etc. Arithmetic operators and bitwise operators can be combined with assignment operator Equivalent assignment statement: x += y; Equivalent to x = x + y; x -= y; Equivalent to x = x - y; x *= y; Equivalent to x = x * y; x /= y; Equivalent to x = x / y; x %= y; Equivalent to x = x % y; x &= y; Equivalent to x = x & y; x |= y; Equivalent to x = x | y; x ^= y; Equivalent to x = x ^ y; x <<= y; Equivalent to x = x << y; x >>= y; Equivalent to x = x >> y; CSC141 Introduction to Computer Programming
  • 44. Special Operator: Conditional Symbol ?: Operation conditional Example x?y:z Explanation: x ? y : z If x is non-zero,(true) then result of expression is y If x is zero, (false) then result of expression is z CSC141 Introduction to Computer Programming
  • 45. Practice Quiz Question -1: Solve the following expressions a). 3-8/4 b). 3 * 4 + 18 / 2 Solution: •/ has the highest precedence, so we compute 8 / 4 first, then subtract the result from 3 the answer is 1 •3 * 4 + 18 / 2 12 + 9 The answer is 21 CSC141 Introduction to Computer Programming
  • 46. Practice Quiz Question - 2: If a=2, b=4, c=6, d=8, then what will be the result of the following expression? x = a * b + c * d / 4; Will be solved as: x = (a * b) + ((c * d) / 4); x = ( 2 * 4) + ((6 * 8) / 4); x = ( 8 ) + (12 ) x= 20 CSC141 Introduction to Computer Programming
  • 47. Practice Quiz Question - 3: Put the parentheses in the order of execution of the following expression. 5-3+4+2-1+7 •+ and - have equal precedence, so this expression is evaluated left to right: (((((5 - 3) + 4) + 2) - 1) + 7) •Innermost parentheses are evaluated first CSC141 Introduction to Computer Programming