CL-1002 Programming Fundamentals: Basic Program Structure, Data Types, Math.h Library Functions
CL-1002 Programming Fundamentals: Basic Program Structure, Data Types, Math.h Library Functions
LAB - 03
CL-1002 Basic Program structure, data
Programming types, Math.h Library functions
Fundamentals
Lab 04: Basic Program Structure, Data types Operators, Math.h library functions
Compilation process
Variables (Overview):
A variable is a storage container for data that is capable of holding different values that may change or
update as programs execute. Your program can read the contents of a variable, update the contents of a
variable, and display the value of a variable on the screen. Computer programs can use variables in
order to remember useful information that the programs can then use later in the code.
The first step to using a variable in C is to let your program know that you want the variable to exist. This
step is called the variable's declaration (also known as initialization). In C, this is done by first specifying
the variable's type, which tells the program what kind of information will be stored inside of the
variable, and then by specifying the variable's name (followed by a semicolon to end the programming
statement).
For instance, in line 1 to the left, we've declared a new variable of type int to be named count. An int is
a data type which stores an integer, which could be positive whole numbers, negative whole numbers,
or zero (but not fractions or decimals). Currently, no value has been assigned to count: we've just told
the program to create a space within which values can be stored later.
Once a variable has been declared, it can be manipulated in various ways. Line 2 takes the variable
count and assigns its value to be 2. Now, the number 2 is stored inside of the variable count. Optionally,
we could have combined lines 1 and 2 into a single programming statement to declare a variable and set
its value at the same time, via a line of code such as: int count = 2;.
After a variable has been given a value, its value can be updated. Line 3 updates the value of count
again, this time to be 8. Now, count forgets the number 2 and remembers the number 8 instead.
The value of a variable can be accessed just by using its name. For instance, line 4 declares a new
variable (also of type int) this time named x, and initially sets its value to be count. This tells your
program to go to the count variable, see what value is inside, and set the value of x to be that value.
Since the current value of count is 8, the value of x is set to also be 8.
Lab 04: Basic Program Structure, Data types Operators, Math.h library functions
Note: You should always try to give meaningful names to variables. For
example: firstNameis a better variable name than fn
Example 01:
C Arithmetic Operators:
There are many operators in C for manipulating data which include arithmetic Operators,
Relational Operators, Logical operators and many more which will be discussed
accordingly. Some of the fundamental operators are:
Example 02
(Variables) CL-1002
Example 03
Lab 04: Basic Program Structure, Data types Operators, Math.h library functions
The sizeof operator
The sizeof is a unary operator that returns the size of data (constants, variables, array, structure,
etc).
Example 04
(Variables) CL-1002
Operators Precedence in C
Example 05
Lab 04: Basic Program Structure, Data types Operators, Math.h library functions
Math library functions
Math library functions allow you to perform certain common mathematical calculations.
Functions are normally used in a program by writing the name of the function followed by a left
parenthesis followed by the argument (or a comma-separated list of arguments) of the function
followed by a right parenthesis. For example, to calculate and print the square root of 900.0
you might write When this statement executes, printf( "%.2f", sqrt( 900.0 ) );
The math library function sqrt is called to calculate the square root of the number contained in
the parentheses (900.0). The number 900.0 is the argument of the sqrt function. The preceding
statement would print 30.00. The sqrt function takes an argument of type double and returns a
result of type double. All functions in the math library that return floating-point values return
the
data type double. Note that double values, like float values, can be output using the %f
conversion specification.
Error-Prevention Tip Include the math header by using the preprocessor directive #include
when
using functions in the math library
(Variables) CL-1002
Example 06:
Lab 04: Basic Program Structure, Data types Operators, Math.h library functions