Prelim Lecture
Prelim Lecture
Week 1 :
• Discussion of Mission Vision
• Discussion of syllabus and house rules
• Discussion of Expected subject output
• Subject requirements
• Introduction to c programming
Activity1 :
1. What is your first choice and why?
2. How can learning C Programming benefit my career?
Week 2:
Objective:
• Familiarize yourself with basic components of the computer system.
• Discuss the different types of programming languages and their capabilities.
• Introduce Flowchart and Algorithm
Discussion
C programming was developed in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to develop the
UNIX operating system. C is the most widely used computer language. It keeps fluctuating at number one scale of
popularity along with Java programming language, which is also equally popular and most widely used among
modern software programmers.
Point to consider: Why do you think that we should learn c programming?
Flowchart Symbols
initialization
process
On page connector
Start/end
decision flowline
Activity 1:
• Create a flowchart that will describe your daily routine as student from the moment you wake up until you
reach school and be there at exactly 7:30 am
• Draw a program that will enable the person to cook a sunny side up
AGaela
MAT09 Instructor
For discussion purposes only
Introduction to C Language
2. Preprocessor Section
All the header files of the program will be declared in the preprocessor section of the program.
Header files help us to access other’s improved code into our code. A copy of these multiple files is
inserted into our program before the process of compilation.
AGaela
MAT09 Instructor
For discussion purposes only
3. Definition
Preprocessors are the programs that process our source code before the process of compilation.
There are multiple steps which are involved in the writing and execution of the program. Preprocessor
directives start with the ‘#’ symbol. The #define preprocessor is used to create a constant throughout the
program. Whenever this name is encountered by the compiler, it is replaced by the actual piece of defined
code.
4. Global Declaration
The global declaration section contains global variables, function declaration, and static variables.
Variables and functions which are declared in this scope can be used anywhere in the program.
5. Main() Function-
Every C program must have a main function. The main() function of the program is written in this
section. Operations like declaration and execution are performed inside the curly braces of the main
program. The return type of the main() function can be int as well as void too. void() main tells the
compiler that the program will not return any value. The int main() tells the compiler that the program will
return an integer value.
6. Sub Programs
User-defined functions are called in this section of the program. The control of the program is
shifted to the called function whenever they are called from the main or outside the main() function. These
are specified as per the requirements of the programmer.
Elements of a C program
#include<stdio.h> // {package/module/library/headerfile}
# def num 20; // definition
int number = 123; // global variable declaration
int main(){ // main program
local variable declaration;
statement/s
return 0;
} // end program
int sum{
local variable declaration;
statement/s
return sum;
}
Printf Statement
In C language, printf () function is used to print formatted output to the standard output stdout (which is
generally the console screen). The printf function is a part of the C standard library < stdio.h> and it can allow
formatting the output in numerous ways. formatted_string: It is a string that specifies the data to be printed.
int printf(const char *format, {variable|value});
Activity 2:
• Write a program that will display the text “HELLO WORLD ” on the screen
• Write a program that will display the text ” BS MATH ” using a single printf statement and without space in
form of a asterisk. The program should display the text in full screen
• Write a program that will display the following text in the center of the screen use gotoxy statement
Note :
Gotoxy(x,y ) // x and y are integer value, the distance between coordinate depend on the device
resolution
Variable in Programming is a named storage location that holds a value or data. These values can change
during the execution of a program, hence the term “variable.” Variables are essential for storing and manipulating
data in computer programs. A variable is the basic building block of a program that can be used in expressions as
a substitute in place of the value it stores.
Scope of a variable:
The scope of a variable in programming refers to the region of the program where the variable can be accessed
or modified. It defines the visibility and lifetime of a variable within a program. There are typically two types of
variable scope:
Local Scope:
• A variable declared inside a function, block, or loop has a local scope.
• It can only be accessed within the specific function, block, or loop where it is declared.
• Its lifetime is limited to the duration of the function, block, or loop.
AGaela
MAT09 Instructor
For discussion purposes only
Global Scope:
• A variable declared outside of any function or block has a global scope.
• It can be accessed from anywhere in the program, including inside functions.
• Its lifetime extends throughout the entire program.
Data Type
Here's a table containing commonly used types in C programming for quick access.
char 1 %c
float 4 %f
double 8 %lf
unsigned long
at least 4 %lu
int
unsigned long
at least 8 %llu
long int
signed char 1 %c
unsigned char 1 %c
The scanf() function takes a format string as its first argument, which specifies the format and data types of the
input that will be read.
The format string can include conversion specifiers, which begin with the percent sign (%) and are followed by
one or more characters that specify the type of data to be read.
AGaela
MAT09 Instructor
For discussion purposes only
The most common conversion specifiers are:
• %d: reads an integer value
• %f: reads a floating-point value
• %c: reads a single character
• %s: reads a string of characters
• %lf: reads a double-precision floating-point value
Activity 3:
Following the variable convention. Write the appropriate declaration and variable name for given formula
B. The Aztec Cell Phone company charges $50 per month plus 15 cents per minute. The Southern Cell
Phone Company charges no monthly fee but 25 cents per minute. Susan is trying to determine which
company’s phone is right for her. She cannot afford to pay more than $60 per month for her phone. Which
should she choose?
C. Tara is a sales representative for a cosmetic company. She is paid $5.15 per hour each week plus a
commission of 10% of the amount of sales over $5000. She works 40 hours one week, and she sells
$7260 worth of cosmetics during that week. She has been offered a job for another cosmetic company
that pays $5.00 per hour for a 40-hour work week plus a commission of 4% of total sales. Which job
would pay more? Should she change jobs?
Operator is a symbol that tells the compiler to perform specific mathematical or logical functions. By definition,
an operator performs a certain operation on operands. An operator needs one or more operands for the
operation to be performed.
Depending on how many operands are required to perform the operation, operands are called as unary, binary or
ternary operators. They need one, two or three operands respectively.
• Unary operators − ++ (increment), -- (decrement), ! (NOT), ~ (compliment), & (address of), *
(dereference)
• Binary operators − arithmetic, logical and relational operators except !
• Ternary operators − The ? operator
C language is rich in built-in operators and provides the following types of operators −
• Arithmetic Operators
AGaela
MAT09 Instructor
For discussion purposes only
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Misc Operators
Arithmetic Operators
Operator Description Example
Relational Operators
These operators are used to compare two operands and return a boolean value (true or false). They are used in a
boolean expression.
(A == B)
Checks if the values of two operands are equal or not. If yes, then the condition
== is not
becomes true.
true.
Checks if the values of two operands are equal or not. If the values are not (A != B)
!=
equal, then the condition becomes true. is true.
Checks if the value of left operand is greater than the value of right operand. If (A > B) is
>
yes, then the condition becomes true. not true.
Checks if the value of left operand is less than the value of right operand. If yes, (A < B) is
<
then the condition becomes true. true.
(A >= B)
Checks if the value of left operand is greater than or equal to the value of right
>= is not
operand. If yes, then the condition becomes true.
true.
AGaela
MAT09 Instructor
For discussion purposes only
Checks if the value of left operand is less than or equal to the value of right (A <= B)
<=
operand. If yes, then the condition becomes true. is true.
Logical Operators
These operators are used to combine two or more boolean expressions.
Called Logical AND operator. If both the operands are non-zero, then the (A && B)
&&
condition becomes true. is false.
Called Logical OR Operator. If any of the two operands is non-zero, then the (A || B) is
||
condition becomes true. true.
Called Logical NOT Operator. It is used to reverse the logical state of its !(A && B)
!
operand. If a condition is true, then Logical NOT operator will make it false. is true.
Binary AND Operator copies a bit to the result if it exists in both (A & B) = 12, i.e.,
&
operands. 0000 1100
(A | B) = 61, i.e.,
| Binary OR Operator copies a bit if it exists in either operand.
0011 1101
Binary XOR Operator copies the bit if it is set in one operand (A ^ B) = 49, i.e.,
^
but not both. 0011 0001
Binary One's Complement Operator is unary and has the effect (~A ) = ~(60), i.e,. -
~
of 'flipping' bits. 0111101
Binary Left Shift Operator. The left operands value is moved left A << 2 = 240 i.e.,
<<
by the number of bits specified by the right operand. 1111 0000
Binary Right Shift Operator. The left operands value is moved A >> 2 = 15 i.e.,
>>
right by the number of bits specified by the right operand. 0000 1111
Assignment Operators
As the name suggests, an assignment operator "assigns" or sets a value to a named variable in C. These
operators are used to assign values to variables. The "=" symbol is defined as assignment operator in C, however
it is not to be confused with its usage in mathematics.
The following table lists the assignment operators supported by the C language −
AGaela
MAT09 Instructor
For discussion purposes only
Operator Description Example
C = A + B will
Simple assignment operator. Assigns values from right side
= assign the value
operands to left side operand
of A + B to C
C += A is
Add AND assignment operator. It adds the right operand to
+= equivalent to C
the left operand and assign the result to the left operand.
=C+A
C %= A is
Modulus AND assignment operator. It takes modulus using
%= equivalent to C
two operands and assigns the result to the left operand.
=C%A
C <<= 2 is same
<<= Left shift AND assignment operator.
as C = C << 2
C >>= 2 is same
>>= Right shift AND assignment operator.
as C = C >> 2
C &= 2 is same
&= Bitwise AND assignment operator.
as C = C & 2
C ^= 2 is same
^= Bitwise exclusive OR and assignment operator.
as C = C ^ 2
C |= 2 is same
|= Bitwise inclusive OR and assignment operator.
as C = C | 2
sizeof() Returns the size of a variable. sizeof(a), where a is integer, will return 4.
AGaela
MAT09 Instructor
For discussion purposes only
&a; returns the actual address of the
& Returns the address of a variable.
variable.
Operator precedence determines the grouping of terms in an expression and decides how an expression is
evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has a
higher precedence than the addition operator.
For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a higher precedence than +, so
it first gets multiplied with 3*2 and then adds into 7.
Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the
bottom. Within an expression, higher precedence operators will be evaluated first.
AGaela
MAT09 Instructor
For discussion purposes only
Comma , Left to right
Other Operators in C
AGaela
MAT09 Instructor
For discussion purposes only