SlideShare a Scribd company logo
1 of 37
1
OUTCOMES: 
Explain constants and variables 
Describe data types 
Identify operators in programming 
Explain different between formula and 
expression 
2
VARIABLES 
a meaningful name of data storage location in 
computer 
memory 
Variable’s characteristics: 
• has a name 
• has a type 
• holds a value that you assign to the variable 
3
CONSTANTS 
are values that do not change during program 
execution 
Types of constants: 
• Integer constant 
• Floating point constant 
• String constant 
• Character constant 
4
RULES OF DECLARING VARIABLE & CONSTANT 
• Variable names can be short as a single letter or 
as long as 31 character 
• Names must begin with a letter of alphabet 
• Variables only contain letters, numbers and 
underscore (_) character 
• Variables cannot have same name as C 
command or function 
• Variables can be formed by freely combining 
the letters, digits and underscore 
5
EXAMPLE: 
Valid Invalid Comment 
c “c” Illegal character 
Totalx2 2totalx Illegal first 
character 
Monthly_rate Monthly-rate Illegal character (-) 
AGE AGE@ Illegal character @ 
SALARYPAY SALARY 
PAY 
Illegal blank 
6
DECLARE CONSTANT & VARIABLES 
Variables hold different types of data: 
• Numeric (integer, floating point, double floating point) 
• Character 
• String 
Variables can be defined in 2 places: 
a) After the opening brace of a block of code 
b) Before a function name 
7
DECLARATION FORM: 
Type 
variable_list 
Data types Variable; 
Example: 
int no1,no2; int mark; 
char name[20]; float amount, rate; 
8
#include <stdio.h> 
int main() 
{ 
int a, b; 
int result; 
a=5; 
b=2; 
a=a+1; 
result=a-b; 
printf("nntt%d",result); 
return 0; 
} 
4 
Output: 
9
Variables may be initialized by assigning 
constants to 
them either at the time of their declaration or at a 
later 
tiSmyentax to declare a 
constant: 
data type variable value 
int days_of_week = 
7; 
10
Example: 
int m, n =10; 
char response = ‘n’; 
char color [6] = ‘green’; 
float rate, total = 0.0; 
11
#include <stdio.h> 
main() 
{ 
char name[50], matrix[10]; 
int date, month, year, age; 
int yearnow=2013; 
printf("What is your name?n"); 
scanf( "%s", &name ); 
printf("What is your matrix number?n"); 
scanf( "%s", &matrix ); 
printf("What is your date of birth?n"); 
scanf( "%d", &date); 
printf("What is your month of birth?n"); 
scanf( "%d", &month); 
printf("What is your year of birth?n"); 
scanf( "%d",&year); 
age = yearnow-year; 
printf("nn Your name is %s",name); 
printf (" with matrix number %s ", matrix); 
printf (“n Your age is %d”, age); 
return 0; 
} 
Output: 
What is your name? 
Ali 
What is your matrix number? 
1035 
What is your date of birth? 
10 
What is your month of birth? 
3 
What is your year of birth? 
1990 
Your name is Ali with matrix number 1035 
Your age is 23 
12
IDENTIFY THE SCOPE OF CONSTANT & VARIABLES 
Integer constant 
are the whole numbers that do not contain decimal 
point 
Example; 
int number = 10; 
0, -708 
13
Floating point constant 
a decimal number that contains the decimal point (.) 
Example; 
float number = 
0.7564; 
-12.0, 65.4 
14
String constant 
• always enclosed in double quotation marks (“ and “) 
• a single space, a word, or a group of words between 
double quotation marks is a C string constant 
Example; 
char name[6]= 
“AHMAD”; 
“2.0”, “X” 
15
Character constant 
• should be enclosed within single quotation marks (‘ 
and ‘) 
• all the alphabetic, numeric and special character can 
be character constant 
Example; 
char numeric= ‘C’; 
‘X’, ‘0’ 
16
KEYWORDS IN C PROGRAMME 
• every word in C language is a keyword or an 
identifier 
• keyword cannot be used as a variable name 
Definition Name Type 
char Character 
unsigned char Unsigned character 
signed char Signed character (same as char) 
int Integer 
unsigned int Unsigned integer 
signed int Signed integer 
short int Short integer 
unsigned short int Unsigned short integer 
signed short int Signed short integer (same as short 
int) 
long Long integer 
long int Long integer (same as long) 
signed long int Signed long integer (same as long 
int) 
unsigned long int Unsigned long integer 
float Floating-point 
double Double floating-point 
17
DATA TYPES 
Basic data types in C: 
• Integer 
• Character 
• Floating point 
• Double floating point 
18
INTEGER 
hold whole numbers 
Keyword : int 
Syntax: int variable_name; 
Example: 
int number; 
int a, b, c; 
19
CHARACTER 
hold only a single character 
Keyword : char 
Syntax: char variable_name; 
Example: 
char name[30]; 
char huruf; 
20
FLOATING POINT 
contain decimal points 
Keyword : float 
Syntax: float variable_name; 
Example: 
float value; 
Float gross_pay; 
21
DOUBLE FLOATING POINT 
will ensure a maximum accuracy in decimal point 
Keyword : double 
Syntax: double variable_name; 
Example: 
double tax; 
double value; 
22
UNDERSTAND OPERATORS & EXPRESSION 
• C math operators are symbols for multiplying, 
dividing, 
• adding and subtracting and as well as for other 
• operations 
• are not always mathematical, but most of it is 
• mathematical 
• expression – combining operators, variables 
and 
constants 
23
Arithmetic Operator 
Most C programs perform arithmetic 
calculations. 
OPERATO 
R 
ACTION ALGEBRAIC 
EXPRESSION 
C 
EXPRESSI 
ON 
+ Addition f + 7 f + 7 
- Subtraction p – c p – c 
* Multiplication bm b * m 
/ Division x/y or or x÷y x / y 
% Remainder or 
Modulus 
r mod s r % s 
24
Typical operator results. With a=7 and b=2, 
the expression on the values on the right 
EXPRESSION VALUE 
a – b 5 
a + b 9 
a * b 14 
a / b 3 
a % b 1 
25
Parentheses are used to group terms in C 
expressions in much the same manner as 
in algebraic expressions. 
For example, to multiply a times the 
quantity B+C, we write: 
A*(B+C) 
26
Example: 
ALGEBRA C 
m= (a+b+c+d+e)/5; 
y=(m*x) + b; 
a b c d e 
5 
m 
    
 
y  mx  b 
27
Relational Operators 
Operators for data comparisons are 
available 
It’s called relational operators, and the task 
is to compare data. 
28
Standard 
algebraic equality 
operator or 
relational operator 
C equality 
or relational 
operator 
Example of 
C condition Meaning of C 
condition 
Equality operators 
= == X == Y X is equal to Y 
≠ != X != Y X is not equal to Y 
Relational operators 
> > X > Y X is greater than Y 
< < X < Y X is less than Y 
≥ >= X >= Y X is greater than or 
equal to Y 
≤ <= X <= Y X is less than or 
equal to Y 
29
Example: 
if a = 7 and b = 5, 
then a < b yields 0 and a != b yields 1. 
30
Logical operators 
There may be times when you need to 
test more than one set of variables. 
You can combine more than one 
relational test in a compound relational 
test by using C’s logical operators 
31
OPERATO 
R 
MEANING 
&& AND 
|| OR 
! NOT 
32
The first two logical operators, && and ||, 
never appear by themselves. 
They typically go between two or more 
relational tests. 
33
a b 
a | | b 
either a or b 
must be true 
a&&b 
Both a and b 
Must be true 
! a 
Produces the 
opposite 
relation 
0 0 0 0 1 
0 1 1 0 1 
1 0 1 0 0 
1 1 1 1 0 
34
Example: 
The true and false on each side of the 
operators 
represent a relational if test. 
The following statements, for example, are 
valid if 
test that use logical operators (sometimes 
called 
compound relational operators). 
35
if ((a<b) && (c>d)) 
{ 
printf(“Results are invalid”); 
} 
If a is less than b, and c is greater than d, 
print results are invalid to the screen. 
36
if ((sales > 5000) || (hrsWorked > 81)) 
{ 
bonus = 500; 
} 
The sales must be more than 5000, or the 
hrsWorked must 
be more than 81, before the assignment executes. 
37

More Related Content

What's hot

What's hot (18)

Variables, Data Types, Operator & Expression in c in detail
Variables, Data Types, Operator & Expression in c in detailVariables, Data Types, Operator & Expression in c in detail
Variables, Data Types, Operator & Expression in c in detail
 
Constant, variables, data types
Constant, variables, data typesConstant, variables, data types
Constant, variables, data types
 
Unit ii ppt
Unit ii pptUnit ii ppt
Unit ii ppt
 
C language (more)
C language (more)C language (more)
C language (more)
 
2. operator
2. operator2. operator
2. operator
 
Token and operators
Token and operatorsToken and operators
Token and operators
 
Basic Input and Output
Basic Input and OutputBasic Input and Output
Basic Input and Output
 
Module 4- Arrays and Strings
Module 4- Arrays and StringsModule 4- Arrays and Strings
Module 4- Arrays and Strings
 
Constants Variables Datatypes by Mrs. Sowmya Jyothi
Constants Variables Datatypes by Mrs. Sowmya JyothiConstants Variables Datatypes by Mrs. Sowmya Jyothi
Constants Variables Datatypes by Mrs. Sowmya Jyothi
 
datatypes and variables in c language
 datatypes and variables in c language datatypes and variables in c language
datatypes and variables in c language
 
C language basics
C language basicsC language basics
C language basics
 
Module 5-Structure and Union
Module 5-Structure and UnionModule 5-Structure and Union
Module 5-Structure and Union
 
Data types
Data typesData types
Data types
 
Project in TLE
Project in TLEProject in TLE
Project in TLE
 
Basics of c++
Basics of c++Basics of c++
Basics of c++
 
C –imp points
C –imp pointsC –imp points
C –imp points
 
Report Group 4 Constants and Variables
Report Group 4 Constants and VariablesReport Group 4 Constants and Variables
Report Group 4 Constants and Variables
 
C – operators and expressions
C – operators and expressionsC – operators and expressions
C – operators and expressions
 

Viewers also liked

Software Design 1: Coupling & cohesion
Software Design 1: Coupling & cohesionSoftware Design 1: Coupling & cohesion
Software Design 1: Coupling & cohesionAttila Magyar
 
Chapter 5 software design
Chapter 5 software designChapter 5 software design
Chapter 5 software designPiyush Gogia
 
Image enhancement techniques
Image enhancement techniquesImage enhancement techniques
Image enhancement techniquesSaideep
 
Introduction to Computer graphics
Introduction to Computer graphics Introduction to Computer graphics
Introduction to Computer graphics PrathimaBaliga
 
Design concepts and principles
Design concepts and principlesDesign concepts and principles
Design concepts and principlessaurabhshertukde
 

Viewers also liked (10)

CHAPTER 1
CHAPTER 1CHAPTER 1
CHAPTER 1
 
CHAPTER 6
CHAPTER 6CHAPTER 6
CHAPTER 6
 
CHAPTER 5
CHAPTER 5CHAPTER 5
CHAPTER 5
 
Cohesion and coupling
Cohesion and couplingCohesion and coupling
Cohesion and coupling
 
Software Design 1: Coupling & cohesion
Software Design 1: Coupling & cohesionSoftware Design 1: Coupling & cohesion
Software Design 1: Coupling & cohesion
 
Chapter 5 software design
Chapter 5 software designChapter 5 software design
Chapter 5 software design
 
Image enhancement techniques
Image enhancement techniquesImage enhancement techniques
Image enhancement techniques
 
Introduction to Computer graphics
Introduction to Computer graphics Introduction to Computer graphics
Introduction to Computer graphics
 
Design concepts and principles
Design concepts and principlesDesign concepts and principles
Design concepts and principles
 
Slideshare ppt
Slideshare pptSlideshare ppt
Slideshare ppt
 

Similar to CHAPTER 2

CSharp Language Overview Part 1
CSharp Language Overview Part 1CSharp Language Overview Part 1
CSharp Language Overview Part 1Hossein Zahed
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
presentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxpresentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxKrishanPalSingh39
 
M.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C LanguageM.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C LanguageDr.Florence Dayana
 
INTRODUCTION TO C++.pptx
INTRODUCTION TO C++.pptxINTRODUCTION TO C++.pptx
INTRODUCTION TO C++.pptxMamataAnilgod
 
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)SURBHI SAROHA
 
presentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptxpresentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptxKrishanPalSingh39
 
introductory concepts
introductory conceptsintroductory concepts
introductory conceptsWalepak Ubi
 
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubey
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit DubeyMCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubey
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubeykiranrajat
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C ProgrammingMOHAMAD NOH AHMAD
 
Unit 4 Foc
Unit 4 FocUnit 4 Foc
Unit 4 FocJAYA
 
Chapter-2 is for tokens in C programming
Chapter-2 is for tokens in C programmingChapter-2 is for tokens in C programming
Chapter-2 is for tokens in C programmingz9819898203
 
Constants and variables in c programming
Constants and variables in c programmingConstants and variables in c programming
Constants and variables in c programmingChitrank Dixit
 

Similar to CHAPTER 2 (20)

CSharp Language Overview Part 1
CSharp Language Overview Part 1CSharp Language Overview Part 1
CSharp Language Overview Part 1
 
C Building Blocks
C Building Blocks C Building Blocks
C Building Blocks
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
presentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxpresentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptx
 
Unit i intro-operators
Unit   i intro-operatorsUnit   i intro-operators
Unit i intro-operators
 
M.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C LanguageM.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C Language
 
C introduction by thooyavan
C introduction by  thooyavanC introduction by  thooyavan
C introduction by thooyavan
 
INTRODUCTION TO C++.pptx
INTRODUCTION TO C++.pptxINTRODUCTION TO C++.pptx
INTRODUCTION TO C++.pptx
 
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)
 
C intro
C introC intro
C intro
 
presentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptxpresentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptx
 
introductory concepts
introductory conceptsintroductory concepts
introductory concepts
 
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubey
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit DubeyMCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubey
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubey
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
Unit 4 Foc
Unit 4 FocUnit 4 Foc
Unit 4 Foc
 
C programming
C programming C programming
C programming
 
lecture2.ppt
lecture2.pptlecture2.ppt
lecture2.ppt
 
Chapter-2 is for tokens in C programming
Chapter-2 is for tokens in C programmingChapter-2 is for tokens in C programming
Chapter-2 is for tokens in C programming
 
C introduction
C introductionC introduction
C introduction
 
Constants and variables in c programming
Constants and variables in c programmingConstants and variables in c programming
Constants and variables in c programming
 

Recently uploaded

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfSanaAli374401
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...KokoStevan
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 

Recently uploaded (20)

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 

CHAPTER 2

  • 1. 1
  • 2. OUTCOMES: Explain constants and variables Describe data types Identify operators in programming Explain different between formula and expression 2
  • 3. VARIABLES a meaningful name of data storage location in computer memory Variable’s characteristics: • has a name • has a type • holds a value that you assign to the variable 3
  • 4. CONSTANTS are values that do not change during program execution Types of constants: • Integer constant • Floating point constant • String constant • Character constant 4
  • 5. RULES OF DECLARING VARIABLE & CONSTANT • Variable names can be short as a single letter or as long as 31 character • Names must begin with a letter of alphabet • Variables only contain letters, numbers and underscore (_) character • Variables cannot have same name as C command or function • Variables can be formed by freely combining the letters, digits and underscore 5
  • 6. EXAMPLE: Valid Invalid Comment c “c” Illegal character Totalx2 2totalx Illegal first character Monthly_rate Monthly-rate Illegal character (-) AGE AGE@ Illegal character @ SALARYPAY SALARY PAY Illegal blank 6
  • 7. DECLARE CONSTANT & VARIABLES Variables hold different types of data: • Numeric (integer, floating point, double floating point) • Character • String Variables can be defined in 2 places: a) After the opening brace of a block of code b) Before a function name 7
  • 8. DECLARATION FORM: Type variable_list Data types Variable; Example: int no1,no2; int mark; char name[20]; float amount, rate; 8
  • 9. #include <stdio.h> int main() { int a, b; int result; a=5; b=2; a=a+1; result=a-b; printf("nntt%d",result); return 0; } 4 Output: 9
  • 10. Variables may be initialized by assigning constants to them either at the time of their declaration or at a later tiSmyentax to declare a constant: data type variable value int days_of_week = 7; 10
  • 11. Example: int m, n =10; char response = ‘n’; char color [6] = ‘green’; float rate, total = 0.0; 11
  • 12. #include <stdio.h> main() { char name[50], matrix[10]; int date, month, year, age; int yearnow=2013; printf("What is your name?n"); scanf( "%s", &name ); printf("What is your matrix number?n"); scanf( "%s", &matrix ); printf("What is your date of birth?n"); scanf( "%d", &date); printf("What is your month of birth?n"); scanf( "%d", &month); printf("What is your year of birth?n"); scanf( "%d",&year); age = yearnow-year; printf("nn Your name is %s",name); printf (" with matrix number %s ", matrix); printf (“n Your age is %d”, age); return 0; } Output: What is your name? Ali What is your matrix number? 1035 What is your date of birth? 10 What is your month of birth? 3 What is your year of birth? 1990 Your name is Ali with matrix number 1035 Your age is 23 12
  • 13. IDENTIFY THE SCOPE OF CONSTANT & VARIABLES Integer constant are the whole numbers that do not contain decimal point Example; int number = 10; 0, -708 13
  • 14. Floating point constant a decimal number that contains the decimal point (.) Example; float number = 0.7564; -12.0, 65.4 14
  • 15. String constant • always enclosed in double quotation marks (“ and “) • a single space, a word, or a group of words between double quotation marks is a C string constant Example; char name[6]= “AHMAD”; “2.0”, “X” 15
  • 16. Character constant • should be enclosed within single quotation marks (‘ and ‘) • all the alphabetic, numeric and special character can be character constant Example; char numeric= ‘C’; ‘X’, ‘0’ 16
  • 17. KEYWORDS IN C PROGRAMME • every word in C language is a keyword or an identifier • keyword cannot be used as a variable name Definition Name Type char Character unsigned char Unsigned character signed char Signed character (same as char) int Integer unsigned int Unsigned integer signed int Signed integer short int Short integer unsigned short int Unsigned short integer signed short int Signed short integer (same as short int) long Long integer long int Long integer (same as long) signed long int Signed long integer (same as long int) unsigned long int Unsigned long integer float Floating-point double Double floating-point 17
  • 18. DATA TYPES Basic data types in C: • Integer • Character • Floating point • Double floating point 18
  • 19. INTEGER hold whole numbers Keyword : int Syntax: int variable_name; Example: int number; int a, b, c; 19
  • 20. CHARACTER hold only a single character Keyword : char Syntax: char variable_name; Example: char name[30]; char huruf; 20
  • 21. FLOATING POINT contain decimal points Keyword : float Syntax: float variable_name; Example: float value; Float gross_pay; 21
  • 22. DOUBLE FLOATING POINT will ensure a maximum accuracy in decimal point Keyword : double Syntax: double variable_name; Example: double tax; double value; 22
  • 23. UNDERSTAND OPERATORS & EXPRESSION • C math operators are symbols for multiplying, dividing, • adding and subtracting and as well as for other • operations • are not always mathematical, but most of it is • mathematical • expression – combining operators, variables and constants 23
  • 24. Arithmetic Operator Most C programs perform arithmetic calculations. OPERATO R ACTION ALGEBRAIC EXPRESSION C EXPRESSI ON + Addition f + 7 f + 7 - Subtraction p – c p – c * Multiplication bm b * m / Division x/y or or x÷y x / y % Remainder or Modulus r mod s r % s 24
  • 25. Typical operator results. With a=7 and b=2, the expression on the values on the right EXPRESSION VALUE a – b 5 a + b 9 a * b 14 a / b 3 a % b 1 25
  • 26. Parentheses are used to group terms in C expressions in much the same manner as in algebraic expressions. For example, to multiply a times the quantity B+C, we write: A*(B+C) 26
  • 27. Example: ALGEBRA C m= (a+b+c+d+e)/5; y=(m*x) + b; a b c d e 5 m      y  mx  b 27
  • 28. Relational Operators Operators for data comparisons are available It’s called relational operators, and the task is to compare data. 28
  • 29. Standard algebraic equality operator or relational operator C equality or relational operator Example of C condition Meaning of C condition Equality operators = == X == Y X is equal to Y ≠ != X != Y X is not equal to Y Relational operators > > X > Y X is greater than Y < < X < Y X is less than Y ≥ >= X >= Y X is greater than or equal to Y ≤ <= X <= Y X is less than or equal to Y 29
  • 30. Example: if a = 7 and b = 5, then a < b yields 0 and a != b yields 1. 30
  • 31. Logical operators There may be times when you need to test more than one set of variables. You can combine more than one relational test in a compound relational test by using C’s logical operators 31
  • 32. OPERATO R MEANING && AND || OR ! NOT 32
  • 33. The first two logical operators, && and ||, never appear by themselves. They typically go between two or more relational tests. 33
  • 34. a b a | | b either a or b must be true a&&b Both a and b Must be true ! a Produces the opposite relation 0 0 0 0 1 0 1 1 0 1 1 0 1 0 0 1 1 1 1 0 34
  • 35. Example: The true and false on each side of the operators represent a relational if test. The following statements, for example, are valid if test that use logical operators (sometimes called compound relational operators). 35
  • 36. if ((a<b) && (c>d)) { printf(“Results are invalid”); } If a is less than b, and c is greater than d, print results are invalid to the screen. 36
  • 37. if ((sales > 5000) || (hrsWorked > 81)) { bonus = 500; } The sales must be more than 5000, or the hrsWorked must be more than 81, before the assignment executes. 37