Sei sulla pagina 1di 25

Dr.

Aman-ur-Rehman Department of Nuclear Engineering,


Pakistan Institute of Engineering and Applied Sciences, P.O. Nilore, Islamabad.
Dr. Aman-ur-Rehman DNE, PIEAS

COURSE CONTENTS
Basic components of a computer Programming languages Programming in C and its Applications

Dr. Aman-ur-Rehman DNE, PIEAS

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

Dr. Aman-ur-Rehman DNE, PIEAS

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.

Dr. Aman-ur-Rehman DNE, PIEAS

Functions of a Computer and Computer System


A Computer has four functions
accepts data (Input) processes data (Processing) produces output (Output) stores results (Storage) Computer System: All of the components of a computer system can be summarized with the simple equation
COMPUTER SYSTEM = HARDWARE + SOFTWARE+ USER Hardware : All physical parts of the computer Software : Programs (Software gives "intelligence" to the computer) USER : Person, who operates computer Dr. Aman-ur-Rehman DNE, PIEAS

Components of a Computer
INPUT Keyboard Mouse Scanner Camera
Arithmetic Logic Unit (ALU) Control Unit Internal Memory Unit

OUTPUT Screen Printer Plotter Photo

Dr. Aman-ur-Rehman DNE, PIEAS

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.

Dr. Aman-ur-Rehman DNE, PIEAS

Structure of a C Program

Most C Programs contain the following basic elements


Preprocessor statements Global Declaration Function Prototypes Functions
Dr. Aman-ur-Rehman DNE, PIEAS

Structure of a C Program
Preprocessor statements
Global declaration

Function prototypes
main() { }

function1() { }
function2() { }

Dr. Aman-ur-Rehman DNE, PIEAS

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

Creating the Source Code


The first step in creating a C program is to type the C language statements into a file. A text editor is required to accomplish this task. There are number of editors that can be used for this purpose. All the integrated C programming environments also contain a text editor. An integrated environment is a menu driven system for developing C programs that includes and editor, compiler, linker, and debugger. By convention, C program files have names ending in .c. for example, cylinder.c , binomial.c , roots.c One should give meaningful names to the programs so that is reminds what the program does. The second part of the file name (.c) identifies it as a C program
Dr. Aman-ur-Rehman DNE, PIEAS

A SIMPLE C PROGRAM
/**************************************************/ /* hello_world.c */ /* Program prints Hello World! */ /**************************************************/ #include <stdio.h>

int main() { printf(Hello World!\n); return 0; }


Dr. Aman-ur-Rehman DNE, PIEAS

Basic Programming Concepts


Comment To explain what the program is for and why it is the way it is. Preprocessing Done before the program is compiled. To include header files containing information about C libraries Statement A line of code that tells computer to perform some action. Always ended with a semicolon(;). Invoking functions Function A module often consisting of a set of statements. Two functions in the hello world program: main, printf

Dr. Aman-ur-Rehman DNE, PIEAS

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.

+ * / % ++ --

Normal order of operations is followed. Parentheses can be used.

Addition Subtraction Multiplicatio n Division Modulus (remainder) Increment Decrement

Dr. Aman-ur-Rehman DNE, PIEAS

Arithmetic Operators: An Example


1. /* Arithmetic operators */ 2. #include <stdio.h> 3. 4. int main() 5. { 6. printf("7+3=%d\n",7+3); 7. printf("7-3=%d\n",7-3); 8. printf("7*3=%d\n",7*3); 9. printf("7/3=%d\n",7/3); 10. printf("7.0/3.0=%f\n",7.0/3.0); 11. return 0; 12.}
Dr. Aman-ur-Rehman DNE, PIEAS

Output of the Program

Dr. Aman-ur-Rehman DNE, PIEAS

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

Dr. Aman-ur-Rehman DNE, PIEAS

Output of the Program

Dr. Aman-ur-Rehman DNE, PIEAS

Output of the Program

Dr. Aman-ur-Rehman DNE, PIEAS

Output of the Program

Dr. Aman-ur-Rehman DNE, PIEAS

Output of the Program

Dr. Aman-ur-Rehman DNE, PIEAS

Output of the Program

Dr. Aman-ur-Rehman DNE, PIEAS

Potrebbero piacerti anche