Anda di halaman 1dari 24

NWFP University of Engineering and Technology, Peshawar, Pakistan

CSE-102 Computer Programming

Introduction
By; Engr. Muhammad Athar Javed Sethi

Lecture 1

Course Information
Course Course Course Course Code: CSE-102 Name: Computer Programming Credit Hours: 4(3+1) Group Address:

http://www.groups.yahoo.com/group/uetp_crash2010_compr og

Why Programming Language?


Computer only understands machine language Initially all computers were programmed using machine language
difficult and cumbersome Only small programs can be written consists of 1s and 0s 1001011100001000

MEMORY ORGANIZATION
10001000100101
DATA

01001010101010

DATA

CPU
11100101010101
INSTRUCTION

10101000011001

PROGRAM

MEMORY

LOW AND HIGH LEVEL PROGRAMMING Lowest Level: Machine Codes Directly process able, written in Binary: 10001011 01100111 10011011 11000111 Hard to read, slow to create, fast to run.

LOW AND HIGH LEVEL PROGRAMMING Next lowest level: Assembler Mnemonics directly represent machine code, Symbolic, :
mov A,90h inc A jnz loop1

Human readable, slow to create, fast to run, processor specific.

LOW AND HIGH LEVEL PROGRAMMING High level: Pascal, C, Fortran, C++/Java One statement is equivalent of many machine code operations. (How???) Human understandable, fast to write, inefficient (?) in terms of code size and run speed, portable between processors.

High-Level Language
#include <stdio.h> int main() { printf(HelloWorld); return 0; }
10100110 01110110 00100110 00000000 11111010 11111010 01001110 10100110 11100110 10010110 11001110 00101110 10100110 01001110 11111010 01100110 01001110 10000110 etc...

Source code

Executable code

Compilers and linkers translate a high level program into executable machine code.

What is C Language C is general purpose programming language, closely associated with the UNIX system (operating system), where it was developed. 1972: C language developed at Bell Labs.
Dennis Ritchie wrote C for Unix.

C is portable, not tied to any particular operating system or machine. A structured programming language.

What is C Language(CONT)
Programs written in C are efficient, as they are smaller and faster than corresponding programs written in other programming languages. Extremely powerful and wealthy library. C is flexible, used to write applications for a variety of areas.
CAD, games, commercial systems, AI (expert systems, robotics). Process Control and real time systems. System program such as operating systems, compilers, linkers etc.

There is very little error checking etc. it assumes that you know what you are doing.

Why C Language
Structured language.
Sequence, selection, repetition.

Relatively small language, described in a small space and learned quickly. Standard library exists, allowing portability. It allow us write clear, concise programs that are portable and efficient. C is the basis for many other languages (Java,Verilog,C++). Low level activities possible. It can produce clean and efficient code. Preferred language for OS (operating Systems).

Steps in compilation of C Program

Stages of Compilation Stage 1: Preprocessing


Performed by a program called the preprocessor Modifies the source code (in RAM) according to preprocessor directives (preprocessor commands) embedded in the source code Strips comments and white space from the code The source code as stored on disk is not modified.

Stages of Compilation (CONT) Stage 2: Compilation


Performed by a program called the compiler Translates the preprocessor-modified source code into object code (machine code) Checks for syntax errors and warnings Saves the object code to a disk file, If any compiler errors are received, no object code file will be generated. An object code file will be generated if only warnings, not errors, are received.

Stages of Compilation (CONT) Stage 3: Linking


Combines the program object code with other object code to produce the executable file. The other object code can come from the RunTime Library, other libraries, or object files that you have created. Saves the executable code to a disk file. On the Linux system, that file may be called a.out or simply a. If any linker errors are received, no executable file will be generated.

Structure of C Program C program consists of three main parts;


Preprocessor Directives.
Start with #

The main( ) function. C statements. C Program:


#include <stdio.h> int main() { printf(Hello World!); return o; }

Basic Structure of a C Program


Example: Hello World
Algorithm: C Program:
#include <stdio.h> int main() { printf(Hello World!); return 0; }

output Hello World!

Basic Structure of a C Program (cont)

Example: Hello world


C Program:
#include <stdio.h>

Includes standard input/output library of procedures. Read: Hash-include

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

Basic Structure of a C Program (cont)

Example: Hello world


C Program:
#include <stdio.h> int main() { printf(Hello World!); return 0; }

every C program must have a main

Basic Structure of a C Program


Example: Hello World
C Program:
#include <stdio.h> int main() { printf(Hello World); return 0; }

Curly braces mark the beginning and end of a block of instructions.

Basic Structure of a C Program

Example: Hello World


C Program:
#include <stdio.h>

Instruction (function call) to output Hello World

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

Basic Structure of a C Program


Statements (lines of instructions) always end with a semi-colon (;) C Program:
#include <stdio.h> int main() { printf(Hello World); return 0; }

Example: Hello World

C and C++
Traditionally C programs use the file extension .C and C++ programs the extension .CPP C is essentially a subset of C++, so you could use a C++ compiler to run a C program. The two languages are extremely similar. In the labs we will be using a Turbo C++ Version 3 software.

Some Programmer Jargon


Some words that will be used a lot:
Text Editor: Application that helps create/modify C programs. Source code: The stuff you type into the computer. The program you are writing. Compile (build): Taking source code and making a program that the computer can understand. Executable: The compiled program that the computer can run. Library: Added functions for C programming to do certain tasks. Header file: Files ending in .h which are included at the start of source code.

Anda mungkin juga menyukai