0% found this document useful (0 votes)
19 views11 pages

Prelim Lecture

ayan

Uploaded by

tamorillo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views11 pages

Prelim Lecture

ayan

Uploaded by

tamorillo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Mat09 – Fundamentals of Computing 1

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

Off page connector input/output

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

Structure of the C Program


The basic structure of a C program is divided into 6 parts which makes it easy to read, modify, document, and
understand in a particular format. C program must follow the below-mentioned outline to successfully compile and
execute. Debugging is easier in a well-structured C program.

Sections of the C Program


There are 6 basic sections responsible for the proper execution of a program. Sections are mentioned below:
1. Documentation
This section consists of the description of the program, the name of the program, and the creation
date and time of the program. It is specified at the start of the program in the form of comments

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

/* write a program that will display hello world


Created by A. Gaela
Date Created : date today */ // documentation

#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});

printf("Characters: %c %c \n", 'a', 65);


printf("Decimals: %d %ld\n", 1977, 650000L);
printf("Preceding with blanks: %10d \n", 1977);
AGaela
MAT09 Instructor
For discussion purposes only
printf("Preceding with zeros: %010d \n", 1977);
printf("Some different radices: %d %x %o %#x %#o \n", 100, 100, 100, 100, 100);
printf("floats: %4.2f %+.0e %E \n", 3.1416, 3.1416, 3.1416);
printf("Width trick: %*d \n", 5, 10);
printf("%s \n", "A string");

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

I think that I shall never see


A poem lovely as a tree.
A tree whose hungry mouth is prest
Against the earth's sweet flowing breast;
A tree that looks at God all day,
And lifts her leafy arms to pray;
A tree that may in summer wear
A nest of robins in her hair;
Upon whose bosom snow has lain;
Who intimately lives with rain.
Poems are made by fools like me,
But only God can make a tree.
Variables

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.

The general rules for naming variables are:


• Names can contain letters, digits and underscores
• Names must begin with a letter or an underscore (_)
• Names are case-sensitive (myVar and myvar are different variables)
• Names cannot contain whitespaces or special characters like !, #, %, etc.
• Reserved words (such as int) cannot be used as names

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.

Type Size (bytes) Format Specifier

Int at least 2, usually 4 %d, %i

char 1 %c

float 4 %f

double 8 %lf

short int 2 usually %hd

unsigned int at least 2, usually 4 %u

long int at least 4, usually 8 %ld, %li

long long int at least 8 %lld, %lli

unsigned long
at least 4 %lu
int

unsigned long
at least 8 %llu
long int

signed char 1 %c

unsigned char 1 %c

long double at least 10, usually 12 or 16 %Lf

How to Use Conversion Specifiers to Read Input

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

A. Given the formula for interest that is compounded is

▪ A represents the amount of money after a certain amount of time


▪ P represents the principle or the amount of money you start with
▪ r represents the interest rate and is always represented as a decimal
▪ t represents the amount of time in years
▪ n is the number of times interest is compounded in one year, for example:
if interest is compounded annually then n = 1
if interest is compounded quarterly then n = 4
if interest is compounded monthly then n = 12

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

+ Adds two operands. A + B = 30

− Subtracts second operand from the first. A − B = -10

* Multiplies both operands. A * B = 200

/ Divides numerator by de-numerator. B/A=2

% Modulus Operator and remainder of after an integer division. B%A=0

++ Increment operator increases the integer value by one. A++ = 11

-- Decrement operator decreases the integer value by one. A-- = 9

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.

Operator Description Example

(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.

Operator Description Example

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.

Operator Description Example

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

Subtract AND assignment operator. It subtracts the right C -= A is


-= operand from the left operand and assigns the result to the equivalent to C
left operand. =C-A

Multiply AND assignment operator. It multiplies the right C *= A is


*= operand with the left operand and assigns the result to the equivalent to C
left operand. =C*A

Divide AND assignment operator. It divides the left operand C /= A is


/= with the right operand and assigns the result to the left equivalent to C
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

Misc Operators ↦ sizeof & ternary

Operator Description Example

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.

* Pointer to a variable. *a;

If Condition is true ? then value X :


?: Conditional Expression.
otherwise value Y
Operators Precedence in C

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.

Category Operator Associativity

Postfix () [] -> . ++ - - Left to right

Unary + - ! ~ ++ - - (type)* & sizeof Right to left

Multiplicative */% Left to right

Additive +- Left to right

Shift << >> Left to right

Relational < <= > >= Left to right

Equality == != Left to right

Bitwise AND & Left to right

Bitwise XOR ^ Left to right

Bitwise OR | Left to right

Logical AND && Left to right

Logical OR || Left to right

Conditional ?: Right to left

Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left

AGaela
MAT09 Instructor
For discussion purposes only
Comma , Left to right
Other Operators in C

AGaela
MAT09 Instructor
For discussion purposes only

You might also like