Академический Документы
Профессиональный Документы
Культура Документы
Chapter 1 Week 1
Chapter 1 Introduction to C
Chapter 1 Week 1
To introduce C language and its history How to use the compiler for this course Writing and running a C program Handling errors
TOPIC Objectives:
2
TOPIC Objectives:
Chapter 1 Week 1
There are many different languages can be used to program a computer. The most basic of these is machine language. This is the natural dialect of the computer. Usually, a computer program will be written in some high-level programming language, whose instruction set is more compatible with human languages. C Most of these are general-purpose languages such as C . A program that is written in a high-level language, however, must be translated into machine language before it can be executed. This is known as compilation or interpretation
interpreter compiler
Chapter 1 Week 1
History of C
History of C
High-level
main() { ... }
High-level language: instruction set is more compatible with human languages General-purpose: C, Pascal, Fortran, BASIC Special-purpose: specifically designed for some particular type of application. LISP, a list processing language that is widely used for artificial intelligence applications.
Chapter 1 Week 1
C was originally developed in the 1970s by Dennis Ritchie, at Bell Laboratories, Inc. and implemented on PDP 11
Dennis Ritchie
By Ken Thompson
Typeless languages
C initially become widely known as the development language of the UNIX operating system. Note: Programmed Data Processor model 11 (PDP-11)
Chapter 1 Week 1
Original C ...
K&R/ANSI C
1980 ...
Note: Because C is a standardized, hardware-independent, widely available language, applications written in C can often be run with little or no modifications on a wide range of different computer systems.
Management and Science University
Chapter 1 Week 1
Advantages of C Language
Advantages of C Language
Advantages
Small language - 32 keywords Native language of UNIX Used for systems programming as well as applications programming Portable Modular Subset of C++ Efficient
Chapter 1 Week 1
ANSI C Keywords
struct switch typedef union unsigned void volatile while
Chapter 1 Week 1
Programming Preparation
Programming Preparation
Source/Object Code
A Compiler translates Source code to Object code that is executable. On MS-DOS systems this compiled code is automatically created in a file with the same name as the .C file, but with the .exe extension replacing the .C extension.
Source Code: C Program main() { } Object Code: Machine Language 10110 .. 01101 .. ...
Mycode.c
Mycode.exe
Chapter 1 Week 1
C Compiler
C Compiler
Editor/Compiler
Write, edit, save, retrieve, ... Translate program to object code/ executable code
c:\tc\bin
tc ENTER
10
Chapter 1 Week 1
System Menu
IDE
Editing Window
Output Window
11
Chapter 1 Week 1
System Menu
Menu A: Menu A:
Menu B: Menu B: Press Alt or Alt+Red Character to walk around the system menu
Press Alt+F
12
Chapter 1 Week 1
Getting Help
Getting Help
Online Help
You can learn about the compiler through the online Help system.
13
Chapter 1 Week 1
Compile Success
Compile Success
No error
14
Chapter 1 Week 1
15
Chapter 1 Week 1
Source/Object
Object Code
10110 .. 01101 .. ...
Source Code
main() { }
C / C++ Compiler
Correct the syntax errors. Correct the logic errors. Edit/Modify to meet system requirements.
16
Chapter 1 Week 1
main() function
Every C program contains main function called: main() Program execution start from this main function.
main() { }
Type using text editor (from your compiler) Save with any name and with .c or .cpp extension. Compile and run
main( ) { ... }
Management and Science University
17
Screen A: Screen A:
Chapter 1 Week 1
Steps of compiling
Program saved with sample.c name.
Editing Window
Screen B: Screen B:
18
Chapter 1 Week 1
C Program
19
Chapter 1 Week 1
Descriptions
Descriptions
Anything written between the /* and */ is a comment and is ignored by the compiler. The line that started with # is
called as preprocessor directive. Here we have processor directive #include which include the stdio.h file into source program. stdio.h is one of compiler components which called as header file or library file. In this example, we include the stdio.h because this file define the printf() function. Indicate the beginning of main program. The printf() is one of the library function that defined in stdio.h header file. printf() is an output function that prints the strings inside the quotations.
Program Descriptions
Management and Science University
Chapter 1 Week 1
Program Format
All your programming exercises should follow the format described below.
/* Name: ABDULLAH BIN ISMAIL Subject Code: DIT 202 Class Code: FT007 Program file name: myprog.cpp Purpose: Print simple message on screen Start Date: August 31, 1999 Complete Date: August 31, 1999 */ # include stdio.h > # include < < stdio.h > main () main () { clrscr(); { p rintf(C my C first programming language. \n); printf (I is love language very much \n); } } /* End of program: myprog.cpp*/
Chapter 1 Week 1
Header Files
Header Files
Header File/ Library Function Header files contain definitions of library functions and variables which
can be incorporated into any C program by using the pre-processor #include statement (see 1.4).
Example:
Header Files
stdio.h conio.h
Library Functions
scanf() printf() :
* Check your compiler on-line help for further details.
22
Chapter 1 Week 1
Standard header files are provided with each compiler, and cover a range of areas, string handling, mathematical, data conversion, printing and reading of variables. Example:
List of Header Files (41)
alloc.h assert.h ctype.h fcntl.h io.h malloc.h share.h stdiostr.h sys\stat.h bcd.h dir.h float.h iomanip.h math.h signal.h stdlib.h sys\timeb.h bios.h dirent.h fstream.h iostream.h mem.h stdarg.h stream.h sys\types.h complex.h dos.h generic.h limits.h process.h stddef.h string.h time.h
conio.h
errno.h graphics.h locale.h setjmp.h
stdio.h
strstrea.h values.h
23
Chapter 1 Week 1
printf
rename sprintf tmpnam vscanf
scanf
_strerror vfprintf
24
Chapter 1 Week 1
To use any of the standard functions, the appropriate header file should be included. This is done at the beginning of the C source file. For example, to use the function printf() in a program, the line #include <stdio.h>
should be at the beginning of the source file, because the definition for printf() is found in the file stdio.h All header files have the extension .h and generally reside in the /include subdirectory.
25