0% found this document useful (0 votes)
34 views

Lesson 4 - C Data Types and Operators

Here are C programs to convert between kilometers to miles and Celsius to Fahrenheit as requested: #include <stdio.h> int main() { float km, miles; printf("Enter distance in km: "); scanf("%f", &km); miles = km * 0.621371; printf("%.6f", miles); return 0; } #include <stdio.h> int main() { float celsius, fahrenheit; printf("Enter temperature in Celsius: "); scanf("%f", &celsius); fahrenheit = (celsius * 9.0/5.0) + 32.0; printf("%.1f",

Uploaded by

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

Lesson 4 - C Data Types and Operators

Here are C programs to convert between kilometers to miles and Celsius to Fahrenheit as requested: #include <stdio.h> int main() { float km, miles; printf("Enter distance in km: "); scanf("%f", &km); miles = km * 0.621371; printf("%.6f", miles); return 0; } #include <stdio.h> int main() { float celsius, fahrenheit; printf("Enter temperature in Celsius: "); scanf("%f", &celsius); fahrenheit = (celsius * 9.0/5.0) + 32.0; printf("%.1f",

Uploaded by

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

Data Types and

Operators in C
C Data Types
Employee Data
Name à String

ID à String

Age à Integer

Salary à Float or Double

Contact Number à String


Primary Data Type
Keyword Data Type Format Specifier

int Integer %d

float Floating-point %f

double Double %lf

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

int %d -32,767 to 32,767

unsigned int %u 0 to 65,535


-32,767 to 32,767
signed int %d
(same as int)
short int %hd -32,767 to 32,767
-2,147,483,647 to
long int %ld
2,147,483,647
unsigned long int %lu 0 to 4,294,967,295
Derived Data Types
Derived data types are primary data types that are grouped together. You
can group many elements of similar data types.

• 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

% remainder after division (modulo


division)
Increment and Decrement Operators
--

++
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

> Greater than 5 > 3 is evaluated to 1

< Less than 5 < 3 is evaluated to 0

!= Not equal to 5 != 3 is evaluated to 1

>= Greater than or equal to 5 >= 3 is evaluated to 1

<= Less than or equal to 5 <= 3 is evaluated to 0


Logical Operators

Operator Description Example

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

You might also like