Module 3
Module 3
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.
III.DISCUSSION:
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
The data type specifies the size and type of information the variable will store.
Engineering-Computing Academy of
Science and Technology
BSIT
them:
The character must be surrounded by single quotes, like 'A' or 'c', and we use
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
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),
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,
safer to use double for most calculations - but note that it takes up twice as
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
You have probably already noticed that if you print a floating point number, the
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
Type Conversion
Sometimes, you have to convert the value of one data type to another type. This
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),
To get the right result, you need to know how type conversion works.
Implicit Conversion
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
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
Explicit Conversion
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
Operators
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
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Bitwise operators
Engineering-Computing Academy of
Science and Technology
BSIT
Arithmetic Operators
Assignment Operators
In the example below, we use the assignment operator (=) to assign the
Comparison Operators
Comparison operators are used to compare two values (or variables). This is
decisions.
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:
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,
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.
Booleans
Very often, in programming, you will need a data type that can only have one of
YES / NO
ON / OFF
TRUE / FALSE
For this, C has a bool data type, which is known as booleans. Booleans
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
Before trying to print the boolean variables, you should know that boolean values
0 represents false
Therefore, you must use the %d format specifier to print a boolean value:
Engineering-Computing Academy of
Science and Technology
BSIT
For example, you can use a comparison operator, such as the greater
In the example below, we use the equal to (==) operator to compare different values:
Engineering-Computing Academy of
Science and Technology
BSIT
You have already learned that C supports the usual logical conditions from
mathematics:
Equal to a == b
You can use these conditions to perform different actions for different decisions.
is true
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
The if Statement
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
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
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.). https://www.tutorialspoint.com/cprogramming/c_basic_syntax.htm
basic-syntax/
S, H. S. (2024, July 23). The One-Stop Solution To Learn Everything You Need To Know About
variables-in-c#:~:text=In%20C%20programming%20language%2C%20a,times%20during
%20the%20program%20execution.
What is an IDE? - Integrated Development Environment Explained - AWS. (n.d.). Amazon Web
10! = 10x9 = 90
8! =