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

FCS 0084 Programming Introduction to C

Chapter 1 Week 1

Chapter 1 Introduction to C

Management and Science University

FCS 0084 Programming 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

Management and Science University

TOPIC Objectives:
2

TOPIC Objectives:

FCS 0084 Programming Introduction to C

Chapter 1 Week 1

Types of programming languages


Types of programming languages

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

Management and Science University

1.1 Introduction to C Language


3

1.1 Introduction to C Language

FCS 0084 Programming Introduction to C

Chapter 1 Week 1

History of C
History of C

High-level

C is high-level language, general-purpose programming language

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.

Find other special-purpose programming language

Management and Science University

FCS 0084 Programming Introduction to C

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

It is an outgrowth of two earlier languages

BCPL By Martin Richards B

Used to create early version of the UNIX Operating System

Typeless languages

C initially become widely known as the development language of the UNIX operating system. Note: Programmed Data Processor model 11 (PDP-11)

Management and Science University

FCS 0084 Programming Introduction to C

Chapter 1 Week 1

Original C ...

Traditional C Classic C Kernighan and Ritchie C (K&R) ANSI C Standard C

K&R/ANSI C

1980 ...

ANSI: American National Standards Institute

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

FCS 0084 Programming Introduction to C

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

Systems programming: e.g., for writing operating systems


Applications programming: e.g., for writing a program to solve a complicated system of mathematical equations, or for writing a program to bill customers

Management and Science University

FCS 0084 Programming Introduction to C

Chapter 1 Week 1

32 ANSI C Standard Keywords


32 ANSI C Standard Keywords

ANSI C Keywords
struct switch typedef union unsigned void volatile while

auto break case char const continue default do

double else enum extern float for goto if

int long register return short signed sizeof static

Note: C and C++ keywords

Management and Science University

FCS 0084 Programming Introduction to C

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

How to run the object code from C prompt

Management and Science University

FCS 0084 Programming Introduction to C

Chapter 1 Week 1

C Compiler
C Compiler

Editor/Compiler

Borland Turbo C/C++ integrate the Editor and the Compiler

Write, edit, save, retrieve, ... Translate program to object code/ executable code

How to run the compiler ?


1. Change directory to 2. Type 3. Press

c:\tc\bin

tc ENTER

Management and Science University

10

FCS 0084 Programming Introduction to C

Chapter 1 Week 1

Borland Turbo C/C++ IDE

System Menu

IDE

Borland Turbo C/C++ IDE

Editing Window

Output Window

Note: IDE : Integrated Development Environment


Management and Science University

11

FCS 0084 Programming Introduction to C

Chapter 1 Week 1

Borland Turbo C/C++ IDE: System Menu


Borland Turbo C/C++ IDE: System Menu

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

Management and Science University

12

FCS 0084 Programming Introduction to C

Chapter 1 Week 1

Getting Help
Getting Help

Online Help

Select to get help

You can learn about the compiler through the online Help system.

Management and Science University

13

FCS 0084 Programming Introduction to C

Chapter 1 Week 1

Compile Success
Compile Success

Select Compile to compile

Note: You can ignore the warnings.

No error

Management and Science University

14

FCS 0084 Programming Introduction to C

Chapter 1 Week 1

Compile With Error


Compile with error

Select Compile to compile

Two syntax errors


Message Window describes the Errors & Warnings

Management and Science University

15

FCS 0084 Programming Introduction to C

Chapter 1 Week 1

How the compiler works


How the compiler works

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.

Make sure to save your edited program before recompile it.


Management and Science University

16

FCS 0084 Programming Introduction to C

Chapter 1 Week 1

Basic C Program Structure


Basic C Program Structure

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() function has two parts:

Note: curly braces {}


Function

main( ) { ... }
Management and Science University

(i) Head (ii) Body

17

FCS 0084 Programming Introduction to C

Screen A: Screen A:

Chapter 1 Week 1

Steps of compiling
Program saved with sample.c name.

Editing Window

Screen B: Screen B:

Is there anything you can see from your output window


Management and Science University

18

FCS 0084 Programming Introduction to C

Chapter 1 Week 1

Program That Generate output

Program That Generate Output

C Program

/* This is simple program that print a message*/


#include <stdio.h> main() { printf(C is my first programming language.\n); }

Output: C is my first programming language.

Management and Science University

19

FCS 0084 Programming Introduction to C

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

/* This is simple program that print a message*/


#include <stdio.h> main() {

printf(C is my first programming language.\n);


}

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

Indicate end of main program.

Note: Any code/statements must be written inside the body. 20

FCS 0084 Programming Introduction to C

Chapter 1 Week 1

What format you should follow ?

What format you should follow ?

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*/

Comment that describe the programmer and the program.

Code with indentation.

Comment to indicate the end of program. 21

Management and Science University

FCS 0084 Programming Introduction to C

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.

getch() clrscr() textcolor() textbackground():

Management and Science University

22

FCS 0084 Programming Introduction to C

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

Note: Based on Borland Turbo C++ Compiler

Management and Science University

23

FCS 0084 Programming Introduction to C

Chapter 1 Week 1

Example: List of library functions from stdio.h header file.

Library Functions from stdio.h


clearerr fflush flushall fread fwrite fclose fgetc fopen freopen getc putc rewind sscanf ungetc vsprintf fcloseall fgetchar fprintf fscanf getchar putchar rmtmp strerror unlink vsscanf fdopen fgetpos fputc fseek gets puts feof fgets fputchar fsetpos getw putw setbuf tempnam vfscanf ferror fileno fputs ftell perror remove setvbuf tmpfile vprintf

printf
rename sprintf tmpnam vscanf

scanf
_strerror vfprintf

Note: printf() - output function scanf() - input function


Management and Science University

24

FCS 0084 Programming Introduction to C

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.

Management and Science University

25

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