Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
COURSE CONTENTS
Basic components of a computer Programming languages Programming in C and its Applications
Lab Instructors
1. 2. 3. 4. 5. 6. 7. Dr. Naeem Akhtar, SE Dr. Ghulam Raza, SE Mr. Mazhar Husnain, SE Mr. Kashif Yaqoob, PhD Scholar Mr. Bannat Gul, PhD Scholar Mr. Taskeen Raza, PhD Scholar Ms. Quratul Ain, PhD Scholar
Computer
Computer is an advanced electronic device that takes raw data as input from the user and processes these data under the control of set of instructions (called program) and gives the result (output) and saves output for the future use. It can process both numerical and nonnumerical (arithmetic and logical) calculations.
Components of a Computer
INPUT Keyboard Mouse Scanner Camera
Arithmetic Logic Unit (ALU) Control Unit Internal Memory Unit
Components of a Computer
The major components of the computer are the central processing unit (CPU), main memory, secondary memory, and input and output devices. The CPU is the heart of any computer. It is divided into a control unit, an arithmetic logic unit (ALU), and internal memory.
The control unit of the CPU interprets the instructions of the computer program and it controls all the other parts of the computer. It also fetches data values from input devices or main memory and stores them in the memory registers and sends data values from memory registers to output device or main memory. The ALU performs the actual mathematical calculations. The internal memory within a CPU consists of a series of memory registers used for the temporary storage of intermediate result during calculations.
Dr. Aman-ur-Rehman DNE, PIEAS
Computer Program
A computer program is a sequence instructions that are executed by a CPU. of
Program flow may be influenced by special 'jump' instructions that transfer execution to an instruction other than the following one.
Structure of a C Program
Structure of a C Program
Preprocessor statements
Global declaration
Function prototypes
main() { }
function1() { }
function2() { }
Structure of a C Program
Preprocessor statements represent instructions to the C preprocessor. All preprocessor statements begin with the # symbol. Global declaration statements define Global variables which are accessible to all parts of a C program. Function declaration (also called function prototypes) are statements that provide the compiler information regarding the type of the value returned by a function and also type and number of function arguments. Functions are groups of statements that the computer can execute. C functions are similar to subroutines in other programming languages.
Dr. Aman-ur-Rehman DNE, PIEAS
Creating a C Program
There are several steps that have to be performed to create a C program
1.
2.
3. 4.
Use a text editor to write your program (source code) in C Compile your program using a C compiler. Correct any errors pointed out by the compiler. Steps 1 and 2 are repeated until there are no errors in the program. Link your program with library functions using an linker. Execute and test your program.
Dr. Aman-ur-Rehman DNE, PIEAS
A SIMPLE C PROGRAM
/**************************************************/ /* hello_world.c */ /* Program prints Hello World! */ /**************************************************/ #include <stdio.h>
Escape Sequences
Escape Sequences are used to control printf to do something other than printing characters.
\n: Newline. Position cursor at the beginning of the next line. \t: Tab. Move cursor to the next tab stop. \a: Alert. Sound the system bell.
Exercise
Modify the hello world program to try out various escape sequences.
Dr. Aman-ur-Rehman DNE, PIEAS
Arithmetic Operators
To form expressions
Many statements are merely expressions.
+ * / % ++ --
A SIMPLE C PROGRAM
/***********************************************************/ /* cylinder.c */ /* Computes the volume and surface area of a cylinder */ /***********************************************************/ #include<stdio.h> #define PI 3.141592564 void main(void); void main(void) { float radius,height,volume,surface_area; /* print heading */ printf("\n Cylinder.c"); printf("\n Computes the volume and surface area of a cylinder."); /* read in radius and height */ printf("\n\n Enter radius of cylinder: "); scanf("%f", &radius); printf("\n\n Enter height of cylinder: "); scanf("%f", &height); /* Compute volume and surface area */ volume = PI * radius * radius * height; surface_area = 2 * PI * radius * (radius + height); /* print results */ printf("\n Volume of cylinder is: %10.4f", volume); printf("\n Surface area of cylinder is:%10.4f", surface_area);