Вы находитесь на странице: 1из 43

Computer Programming

Engr. Muhammad Usman Assistant Professor, CSE UET Mardan Campus engrosman@yahoo.com
1

Tentative Course Contents


Introduction C Programming Basics: Data Types, Variables, Operators, and Expressions Control Flow Statements
If-then-else Loops

Program structure and functions Pointers & Arrays Dynamic Memory Allocation Structures and Unions Input and Output
File handling
2

Introduction to C++ & Object Oriented Programming

Suggested Textbooks
The C Programming Language (2nd edition - ANSI C), Brian W. Kernighan and Dennis M. Ritchie, Prentice Hall Let us C, Yashwant Kanitkar Practical C Programming, 3rd Edition, Steve Oualline

Learning
Is best done by doing For a skill like programming, swimming or riding a bicycling you can not learn by listening or watching If you help someone, NEVER take over the keyboard. Try to give the smallest of hints.

Course Objectives
Objectives of this course are three fold
To appreciate the need for a programming language To introduce the concept and usability of the structured programming methodology To develop proficiency in making useful software using the C language

At the end of the day... computers just manipulate 0s and 1s

But binary is hard (for humans) to work with

Machine language
Computers understand only machine language.
Every processor has its own machine language. Basically looks like a sequence of 1s and 0s. Very inconvenient to work with and non intuitive.

All other computer languages were created for human convenience


The computer does not understand C. Must be converted into machine language.

Computer languages (getting closer to human languages)


Assembly machine language with some text codes (still inconvenient). High Level Languages
Interpreted languages Java, Perl.
The program is translated into machine language line by line during execution.

Compiled languages C, Pascal, Fortran.


The program is translated into machine language before execution

High level languages VS machine languages


Actually, binary instructions.

10

Towards a higher level of abstraction

11

There are always trade-offs


High-level languages
simpler to write & understand more library support, data structures

Low-level languages
closer to hardware more efficient (but also dangerous!)

12

Basic Computer Model

13

Data representation inside the computer


Bit a binary digit: 0 or 1.
Bits are always grouped!

Byte a group of 8 bits.


Therefore, a byte can represent up to 28=256 values. The values can range from 0 to 255 or from -128 to 127. The fundamental data unit of a computer.

Word a group of (usually) 4 (or 8) bytes.


4 bytes = 32 bits. Value range: 0 to 232 -1 (4,294,967,295 ). Or, more often: -231 to 231-1 (-2,147,483,648 to +2,147,483,647).

14

Program
A precise sequence of steps/instruction to solve a particular problem

The instructions are executed sequentially. No instruction is executed before the previous has been completed. Each instruction has a numerical code.
15

Examples of instructions
Load data (from an address in the memory). Store data (on an address in memory). Add two numbers. If two numbers are equal, jump to another part of the program. Instructions are numbers!

16

Why C?
Selection of a programming language is an old debate in computing.
For application development, it is a very hot debate. But for system programming very few experts argue for language other than C Reason is simple: C is closest to hardware. C tends to execute faster than code written in other languages.

Historically, Linux kernel is developed in C Unix OS itself in C Device drivers and a great deal of software are almost always written in C C still good for efficient applications that need to use low-level features of C (data compression, graphics, compilers, operating systems, systems programming)

17

History of C
1972: The C programming language developed by Dennis Ritchie. He wanted to design a better Operating System.
MULTICS a better alternative for Punched Cards UNIX (first written in Assembly)

In 60s, there were two types of languages


Low level assembly language: more control on hardware High level language: not to worry about the messy low level details

Joint committee from University of London Computer Unit and Mathematical laboratory at Cambridge decided to design a language that could be
High (wouldnt be tied to a particular computer) as well as low level (manipulation of specific bits) The result was Combined Programming Language (CPL) Further attempts were BCPL, B (by Thompson), and finally C (by Ritchie)

18

1973: C used to rewrite the Unix operating system. 1978: Kernighan and Ritchie set the first standard of C: K&R standard 1989: the American National Standards Institute adopted the ANSI C standard 1999: New ANSI standard (also known as C99)

C is a programmers language written by a programmer for programmers.


19

Dennis MacAlistair Ritchie (September 9, 1941)


Creator of the C programming language and a key developer of the Unix operating system nickname "dmr" (his Bell Labs email address) in technical discussion groups. American computer scientist notable for developing C and for having influence on other programming languages, as well as operating systems such as Multics and Unix. Born in Bronxville, New York, Ritchie graduated from Harvard University with degrees in physics and applied mathematics. In 1967, he began working at the Bell Labs Computing Sciences Research Center. In 1983, Ritchie and Ken Thompson jointly received the Turing Award for their development of generic operating systems theory and specifically for the implementation of the UNIX operating system.

20

DMR (Contd)
On April 21, 1999, Thompson and Ritchie jointly received the 1998 National Medal of Technology from President Bill Clinton for co-inventing the UNIX operating system and the C programming language In 2010 Dennis Ritchie, along with Ken Thompson, was awarded the Japan Prize for Information and Communications for the pioneering work in the development of Unix operating system Ritchie was the head of Lucent Technologies System Software Research Department when he retired in 2007
21

Brian Kernighan (January 1942 Toronto)


Professor at the Computer Science Department of Princeton University Bachelor's degree in Engineering Physics from the University of Toronto. He received his PhD in electrical engineering from Princeton University Canadian computer scientist who worked at Bell Labs alongside Unix creators Ken Thompson and Dennis Ritchie and contributed to the development of Unix. Co-author of K&R book Kernighan coined the term UNIX in the 1970s
The original term he coined was Unics (for Uniplexed Information and Computing Service, a play on Multics), which was later changed to Unix.

Hello world, a program originally written by Brian Kernighan of Bell Labs in "A Tutorial Introduction to the Language B

22

Strengths of C
Efficiency Portability Power Flexibility
not just for systems programming!

Standard Library Integration with UNIX

23

C is a procedural language
It enables the user to create new instructions (procedures) from existing ones. Instead of re-writing the same code over and over again, write it once and call it when needed.

24

Why different languages?


Many languages were developed with specific applications in mind:
Data processing Web applications Mathematical calculations Artificial intelligence

25

How do we compile?
A special program the compiler translates computer language to machine language There are many compilers in the market
For MS Windows
Microsoft Visual C++ 6.0 or 2008 Express Edition Microsoft Visual Studio 2008 or 2010 Professional Edition Turbo C

For Unix/Linux
GCC or CC

26

Getting Started
Alphabets Words Sentences Paragraph

English Language

Alphabets, Digits, SpecialSymbols

Constants, Variables, Keywords

Instructions

Program/ Code
27

C Language

28

29

30

31

32

33

34

35

36

37

38

Let's look at the whole process


Write a program
Choose your favorite text editor

Compile + link the program


C compiler will do one of two things:
print error messages and abort (most probably) produce an executable program

Run the program

39

Our first C program


/* HelloWorld An example program */ #include <stdio.h> int main(void) { printf(Hello, world!\n); return 0; } This that all C statements Noteis aathe compiler the compiler terminates function This another C statement. This one/*a insert thethe Yet is an instruction starts end with to semicolon (;). statement. This tells Ccomment towe are about toand endsawith a */. define This is of the file stdio.h to the program end of a with a and prior to Curly braces indicate contents This statement calls athe operating system to a human program and informs the beginning printf, which causes named main. are used tofunction the programthat it has Comments explain called compilation. block special function it is where the instructions. text toofand are ignored by the compiler. program ended successfully. main is abe printed on thescreen. reader, Specifically in this case a about the This running. startsfile contains information function. printf fuction.

40

Now, lets get to work!!!


41

Exercise
Write, compile and run a program that prints the name of your department in two lines (one word in each separate line).

42

A name printing program


/* This program prints a string on the screen in two lines. */ #include <stdio.h> int main(void) { printf(Telecommunication\nEngineering\n); return 0; }
43

Вам также может понравиться