Lesson 4 - C Data Types and Operators
Lesson 4 - C Data Types and Operators
Operators in C
C Data Types
Employee Data
Name à String
ID à String
Age à Integer
int Integer %d
float Floating-point %f
char Character %c
void Void
Data Type Modifiers
Modifiers are C keywords that modify the meaning of fundamental data
types. It indicates how much memory will be allocated to a variable.
• long
• short
• signed
• unsigned
Data Type Modifiers
Keyword Format Specifier Range
• Array
• Pointers
• Structure
• Union
C Operators
C Operators
• Arithmetic Operators
• Increment and Decrement Operators
• Assignment Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
Arithmetic Operators
Operators Description
+ addition
- subtraction
* multiplication
/ division
++
Assignment Operators
Operator Example Same as
= a=b a=b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
Relational Operators
Operator Description Example
== Equal to 5 == 3 is evaluated to 0
If c = 5 and d = 2 then,
Logical AND. True only if all
&& expression ((c==5) && (d>5))
operands are true
equals to 0.
If c = 5 and d = 2 then,
Logical OR. True only if
|| expression ((c==5) || (d>5))
either one operand is true
equals to 1.
Logical NOT. True only if the If c = 5 then, expression !(c==5)
!
operand is 0 equals to 0.
Bitwise Operators
Operators Description
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
<< Shift left
>> Shift right
Demonstration
Exercise
Jose, Rose, and their families went hiking in the mountains together and
realized that distances are measured in different units in the Philippines and
the United States. To help them convert between units, please write a
program that reads a decimal number representing a distance in kilometers
and that prints out the corresponding distance in miles with 6 decimal
places.
You may use the fact that one kilometer equals 0.621371 miles.
Example
Input:
4.8
Output:
2.982581
Exercise
When Rose came to the Philippines (to visit Petra to make an APP) he
brought his favorite cookie recipe! When trying to bake the cookies he
realized that ovens in the Philippines show temperature in degrees
Fahrenheit, but the cookie recipe called for a temperature in degrees
Celsius. We need your help!
Please write a C-program that reads a decimal number representing a
temperature in degrees Celsius and prints out the corresponding
temperature in degrees Fahrenheit with 1 decimal place. Here is the
conversion formula:
Temperature (°F) = Temperature (°C) × 9.0 / 5.0 + 32.0
Example
Input: 30.5
Output: 86.9