Engineering-Computing Academy of
Science and Technology
BSIT
Course Code CCC 112 A
Course Description Introduction to Computer Programming 1
Module Title Data Types, Operators and Conditionals
I. INTRODUCTION:
Understanding data types and control flow in C is essential for mastering the core
principles of this versatile programming language. Data types define the kind of data
variables can store, enabling programmers to manage memory efficiently and perform
precise operations. C offers a range of data types, from fundamental types like integers
and floating points to more complex structures, ensuring flexibility in handling data.
Complementing these are control flow mechanisms, such as conditional statements and
loops, which allow developers to implement logic and decision-making in their programs.
Together, these concepts form the backbone of C programming, empowering developers
to create robust, dynamic, and optimized solutions across various applications.
II. PRE- DISCUSSION ACTIVITY:
Engineering-Computing Academy of
Science and Technology
BSIT
[Link]:
CHAPTER 3.1 Data Types
Data Types in C
Each variable in C has an associated data type. It specifies the type of data
that the variable can store like integer, character, floating, double, etc. Each
data type requires different amounts of memory and has some specific
operations which can be performed over it.
The data type specifies the size and type of information the variable will store.
Engineering-Computing Academy of
Science and Technology
BSIT
Basic Format Specifiers
There are different format
specifiers for each data
type. Here are some of
them:
The char Type
The char data type is used to store a single character.
The character must be surrounded by single quotes, like 'A' or 'c', and we use
the %c format specifier to print it:
If you try to store more than a single character, it will only print the last character:
Engineering-Computing Academy of
Science and Technology
BSIT
To store multiple characters (or whole words), use strings
Numeric Types
Use int when you need to store a whole number without decimals, like 35 or
1000, and float or double when you need a floating point number (with decimals),
like 9.99 or 3.14515.
Engineering-Computing Academy of
Science and Technology
BSIT
float vs. double
The precision of a floating point value indicates how many digits the value can
have after the decimal point. The precision of float is six or seven decimal digits,
while double variables have a precision of about 15 digits. Therefore, it is often
safer to use double for most calculations - but note that it takes up twice as
much memory as float (8 bytes vs. 4 bytes).
Scientific Numbers
A floating point number can also be a scientific number with an "e" to indicate the
power of 10:
Engineering-Computing Academy of
Science and Technology
BSIT
Set Decimal Precision
You have probably already noticed that if you print a floating point number, the
output will show many digits after the decimal point:
If you want to remove the extra zeros (set decimal precision), you can use a dot
(.) followed by a number that specifies how many digits that should be shown
after the decimal point:
Engineering-Computing Academy of
Science and Technology
BSIT
Type Conversion
Sometimes, you have to convert the value of one data type to another type. This
is known as type conversion.
For example, if you try to divide two integers, 5 by 2, you would expect the result
to be 2.5. But since we are working with integers (and not floating-point values),
the following example will just output 2:
To get the right result, you need to know how type conversion works.
There are two types of conversion in C:
Implicit Conversion (automatically)
Explicit Conversion (manually)
Engineering-Computing Academy of
Science and Technology
BSIT
Implicit Conversion
Implicit conversion is done automatically by the compiler when you assign a
value of one type to another.
For example, if you assign an int value to a float type:
As another example, if you divide two integers: 5 by 2, you know that the sum
is 2.5. And as you know from the beginning of this page, if you store the sum as
an integer, the result will only display the number 2. Therefore, it would be better
to store the sum as a float or a double, right?
Why is the result 2.00000 and not 2.5? Well, it is because 5 and 2 are still
integers in the division. In this case, you need to manually convert the integer
values to floating-point values.
Engineering-Computing Academy of
Science and Technology
BSIT
Explicit Conversion
Explicit conversion is done manually by placing the type in parentheses () in front
of the value.
Considering our problem from the example above, we can now get the right
result:
Constants
If you don't want others (or yourself) to change existing variable values, you can
use the const keyword.
Engineering-Computing Academy of
Science and Technology
BSIT
This will declare the variable as "constant", which
means unchangeable and read-only:
CHAPTER 3.2 Operators
Operators
Operators are used to perform operations on variables and values.
In the example below, we use the + operator to add together two values:
Engineering-Computing Academy of
Science and Technology
BSIT
Although the + operator is often used to add together two values, like in the
example above, it can also be used to add together a variable and a value, or a
variable and another variable:
C divides the operators into the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Bitwise operators
Engineering-Computing Academy of
Science and Technology
BSIT
Arithmetic Operators
Arithmetic operators are used to perform common mathematical operations.
Assignment Operators
Assignment operators are used to assign values to variables.
In the example below, we use the assignment operator (=) to assign the
value 10 to a variable called x:
Engineering-Computing Academy of
Science and Technology
BSIT
Comparison Operators
Comparison operators are used to compare two values (or variables). This is
important in programming, because it helps us to find answers and make
decisions.
The return value of a comparison is either 1 or 0, which means true (1)
or false (0).
Engineering-Computing Academy of
Science and Technology
BSIT
In the following example, we use the greater than operator (>) to find out if 5 is
greater than 3:
A list of all comparison operators:
Logical Operators
You can also test for true or false values with logical operators.
Engineering-Computing Academy of
Science and Technology
BSIT
Logical operators are used to determine the logic between variables or values,
by combining multiple conditions:
Bitwise Operators
A bitwise operator is a character that represents an action taken on data at
the bit level, as opposed to bytes or larger units of data. More simply put, it is an
operator that enables the manipulation of individual bits in a binary pattern.
CHAPTER 3.3 Boolean in C
Engineering-Computing Academy of
Science and Technology
BSIT
Booleans
Very often, in programming, you will need a data type that can only have one of
two values, like:
YES / NO
ON / OFF
TRUE / FALSE
For this, C has a bool data type, which is known as booleans. Booleans
represent values that are either true or false.
Boolean Variables
In C, the bool type is not a built-in data type, like int or char. It was introduced in
C99, and you must import the following header file to use it:
#include <stdbool.h>
A boolean variable is declared with the bool keyword and can take the
values true or false:
Engineering-Computing Academy of
Science and Technology
BSIT
Before trying to print the boolean variables, you should know that boolean values
are returned as integers:
1 (or any other number that is not 0) represents true
0 represents false
Therefore, you must use the %d format specifier to print a boolean value:
Engineering-Computing Academy of
Science and Technology
BSIT
Comparing Values and Variables
Comparing values are useful in programming, because it helps us to find
answers and make decisions.
For example, you can use a comparison operator, such as the greater
than (>) operator, to compare two values:
In the example below, we use the equal to (==) operator to compare different values:
Engineering-Computing Academy of
Science and Technology
BSIT
CHAPTER 3.4 Conditionals in C
Conditions and If Statements
You have already learned that C supports the usual logical conditions from
mathematics:
Less than: a < b
Less than or equal to: a <= b
Greater than: a > b
Greater than or equal to: a >= b
Equal to a == b
Not Equal to: a != b
You can use these conditions to perform different actions for different decisions.
C has the following conditional statements:
Use if to specify a block of code to be executed, if a specified condition
is true
Use else to specify a block of code to be executed, if the same condition
is false
Engineering-Computing Academy of
Science and Technology
BSIT
Use else if to specify a new condition to test, if the first condition is false
Use switch to specify many alternative blocks of code to be executed
The if Statement
Use the if statement to specify a block of code to be executed if a condition
is true.
Note that if is in lowercase letters. Uppercase letters (If or IF) will generate an
error.
Engineering-Computing Academy of
Science and Technology
BSIT
The else Statement
Use the else statement to specify a block of code to be executed if the condition
is false.
Engineering-Computing Academy of
Science and Technology
BSIT
The else if Statement
Use the else if statement to specify a new condition if the first condition is false.
Engineering-Computing Academy of
Science and Technology
BSIT
III. ASSESSMENT:
Reference List:
C - Basic Syntax. (n.d.). [Link]
GeeksforGeeks. (2023, June 16). C Basic Syntax. GeeksforGeeks. [Link]
basic-syntax/
S, H. S. (2024, July 23). The One-Stop Solution To Learn Everything You Need To Know About
Variables In C. [Link]. [Link]
variables-in-c#:~:text=In%20C%20programming%20language%2C%20a,times%20during
%20the%20program%20execution.
[Link]. (n.d.). [Link]
What is an IDE? - Integrated Development Environment Explained - AWS. (n.d.). Amazon Web
Services, Inc. [Link]
Prepared by: Reviewed by: Recommending Approved:
Approval:
Engineering-Computing Academy of
Science and Technology
BSIT
Justin Nichol P. Dr. Jane M. Virgo C. Lopez, PhD Donna Padilla
Pasamonte Fernandez Vice President for Taguiba, PhD
Faculty,E-Coast Dean,E-Coast Academics President
10! = 10x9 = 90
8! =