chapter 1 introduction to c language

32
Chapter 1 Introduction to C Language By C. Shing ITEC Dept Radford University

Upload: tibor

Post on 18-Jan-2016

237 views

Category:

Documents


16 download

DESCRIPTION

Chapter 1 Introduction to C Language. By C. Shing ITEC Dept Radford University. Objectives. Understand brief history of C Describe C components Understand C features Understand Program Structure in C Understand how to run a C program using GNU C compiler and in .NET environment - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 1 Introduction to C Language

Chapter 1 Introduction to C Language

By C. Shing

ITEC Dept

Radford University

Page 2: Chapter 1 Introduction to C Language

Slide 2

Objectives Understand brief history of C Describe C components Understand C features Understand Program Structure in C Understand how to run a C program

using GNU C compiler and in .NET environment Understand how to use a vi editor

Page 3: Chapter 1 Introduction to C Language

Slide 3

C Brief History 1967 Martin Richards

Develop BCPL language for writing OS and compilers 1969 Ken Thompson, Bell Lab

Develop B language for Unix (on DEC PDP-7) machine dependent

1970 Dennis Ritchie, Bell Lab developed C language on DEC PDP-11 (traditional C: cc) machine independent

1978 C++: Bjarne Stroustrup Object-oriented Super-set of C

1989 ANSI C (e.g. GNU C: gcc) 1999 updated ANSI C

Variable-length array Restrict pointer modifier

2002 Microsoft C# for .NET framework

Page 4: Chapter 1 Introduction to C Language

Slide 4

C Components Compiler

traditional ANSI

Preprocessor: code begins with # at column 1 Process code before compiler starts translating

Library Standard Library (/usr/lib/libc.a)

Examine table of contents: ar t /usr/lib/libc.a Math Library (/usr/lib/libm.a) Utility Library

Page 5: Chapter 1 Introduction to C Language

Slide 5

C Features Middle-level language

Can access bit level Machine independent Case sensitive Type language (weak type)

Variable must be declared a type Allow automatic type conversion No boolean data type (0: false, non-zero: true)

No run-time error checking Programmer’s responsibility: array boundary

Page 6: Chapter 1 Introduction to C Language

Slide 6

C Features Small language, very efficient

32 keywords in ANSI C compared to BASIC

(> 100 reserved words) All subprograms are functions Function uses pass-by-value Function cannot be nested within another function

Can manipulate memory System programming tool

Page 7: Chapter 1 Introduction to C Language

Slide 7

Running C Program C source program -> C Compiler

-> Object program -> Link

(to get executable program)

->(feed data) Execution

Page 8: Chapter 1 Introduction to C Language

Slide 8

Program Structures File Extension

.c (or .cpp for C++) Compile & link (Solaris) , then execute

gcc mainfile.c [sub1filename.c sub2filename.c]or in traditional C: (if math library is called in the

program) cc mainfile.c [sub1filename.c sub2filename.c] –lm

then a.outor gcc -o executable mainfile.c

[sub1filename.c sub2filename.c] then executable

Page 9: Chapter 1 Introduction to C Language

Slide 9

Program Structures (Cont.) Compile & link (Solaris) to your library, then execute

gcc mainfile.c [sub1filename.c sub2filename.c]-lyourlibraryname

or in traditional C: (if yourlibrary is called in the program) cc mainfile.c [sub1filename.c sub2filename.c]

–llyourlibrarynamethen a.outor gcc -o executable mainfile.c [sub1filename.c sub2filename.c] –llyourlibraryname then executable

Page 10: Chapter 1 Introduction to C Language

Slide 10

Program Structures (Cont.) Compile only (Solaris)

gcc –c filename.c

or cc -c filename.c (traditional C)

Page 11: Chapter 1 Introduction to C Language

Slide 11

Program Structures (Cont.) mainfile.c/* *********************************** This is a comment ** Block ************************************/// Comment line// Declaration for standard library: in

/usr/include/stdio.h // The following line is required#include <stdio.h>

Page 12: Chapter 1 Introduction to C Language

Slide 12

Program Structures (Cont.)

#include <math.h> //if math library is used

// The following file yourdeclaration.h

// is in your current directory

#include “yourdeclaration.h”

// define macro constants

#define PI 3.14159

#define MY_MESSAGE “Good Morning!”

Page 13: Chapter 1 Introduction to C Language

Slide 13

Program Structures (Cont.)// declare named global constantsconst int number = 4;

// declare typestypedef char myCharacter;

// declare global variablesint a;char b=‘a’, c=‘\n’;myCharacter e;long d = 14000L; // this means long int (default int)float x=-2.5F;double y=2.5;

Page 14: Chapter 1 Introduction to C Language

Slide 14

Program Structures (Cont.)// declare function prototypeint sub1(int);

// define main functionint main (void){ // declare local variables int lb, lc;

lc = 2; // call function lb = sub1(lc); printf(“%d\n”,lb);

return 0; // no error exit}

Page 15: Chapter 1 Introduction to C Language

Slide 15

Program Structures (Cont.)// define functionint sub1 (int l){ // declare local variables int la; // call function la = 5*l;

return la; }

Page 16: Chapter 1 Introduction to C Language

Slide 16

Appendix: Traditional C (not recommended to write)

No const data type (use #define instead) No void data type Main function does not return anything No function prototype Return type for function default is int if not

specified Function formal parameters are defined after ()

Page 17: Chapter 1 Introduction to C Language

Slide 17

Program Structures (Traditional C) mainfile.c/* *********************************** This is a comment ** Block ************************************/// Comment line// Declaration for standard library: in

/usr/include/stdio.h // The following line is required#include <stdio.h>

Page 18: Chapter 1 Introduction to C Language

Slide 18

Program Structures (Cont.)

#include <math.h> //if math library is used

// The following file yourdeclaration.h

// is in your current directory

#include “yourdeclaration.h”

// define macro constants

#define PI 3.14159

#define MY_MESSAGE “Good Morning!”

#define NUMBER 4

Page 19: Chapter 1 Introduction to C Language

Slide 19

Program Structures (Cont.)

// declare types

typedef char myCharacter;

// declare global variables

int a;

char b=‘a’, c=‘\n’;

myCharacter e;

long d = 14000L;

float x=-2.5F;

double y=2.5;

Page 20: Chapter 1 Introduction to C Language

Slide 20

Program Structures (Cont.)// define function sub1 must appear before main functionsub1 ()int l;{ // declare local variables int la; // call function la = 5*l;

return la; }

Page 21: Chapter 1 Introduction to C Language

Slide 21

Program Structures (Cont.)// define main functionmain (){ // declare local variables int lb, lc; lc = 2; // call function lb = sub1(lc); printf(“%d\n”,lb);}

Page 22: Chapter 1 Introduction to C Language

Slide 22

vi/vim editor vi filename.c Please refer to

http://www.chem.brown.edu/instructions/vi.html

Page 23: Chapter 1 Introduction to C Language

Slide 23

Use vi Editor 2 modes:

Editor mode (press <esc>): make correction Insert/open/append mode

(press<i>/<o>/<a>): add text

Page 24: Chapter 1 Introduction to C Language

Slide 24

Use vi Editor (Cont.) Editor mode: (Common keys)

<h>:move left, <l>: move right,

<j>: move down, <k>: move up <0>: beginning of line, <$>: end of line <w>: next word, <b>: previous word <:><n>: go to line n

Page 25: Chapter 1 Introduction to C Language

Slide 25

Use vi Editor (Cont.) Editor mode: (Cont.)

<x>: delete character, <d><w>: delete word <d><d>: delete line (to clip board), <Y>: copy line <p>: paste from clip board after/below cursor <P>: paste from clip board before/above cursor Move cursor to line m,<d><d>, then move cursor to line n, <p>: move line m to below line n Move cursor to line m,<Y>, then move cursor to line n, <p>: copy line m to below line n

Page 26: Chapter 1 Introduction to C Language

Slide 26

Use vi Editor (Cont.) Editor mode: (Cont.) Note: put a number n before an action will

repeat the action n times.For example:10<j>: move cursor down 10 lines10<x> delete 10 characters10<d><d>: delete 10 lines10<Y>: copy 10 lines

Page 27: Chapter 1 Introduction to C Language

Slide 27

Use vi Editor (Cont.) Editor mode: (Cont.)

<r>: replace a character <c><w> type in word<esc>: replace a word <R> type in words<esc>: replace/type over words </>type in word<enter>: search the 1st occurrence of the word <:><w>: save, <:><w><q>: save and quit, <:><w><q>type in filename<enter>: save to filename and quit <:><s></><word1></><word2></><enter>: substitute the 1st occurrence of word1 for word2 <:><1><,><$><s></><word1></><word2></><enter>: substitute word1 for word2 from line 1 to lat line

Page 28: Chapter 1 Introduction to C Language

Slide 28

Use vi Editor (Cont.) Editor mode: (Cont.)

<J>: join the cursor line and the line below into one line Move cursor to position <i><esc>:

split at the cursor position into 2 lines <.>: repeat the previous command <n>: next searched word <G>: go to last line <ctrl><g>: file status

Page 29: Chapter 1 Introduction to C Language

Slide 29

Use vi Editor (Cont.) Insert/open/append mode: (Common keys)

<i>type in words<esc>:insert words before the cursor

<a>type in words<esc>:append words after the cursor

<o>type in words<esc>:open lines after the cursor

and append words <O>type in words<esc>:open lines before the cursor

and append words

Page 30: Chapter 1 Introduction to C Language

Slide 30

Class Example Hands On Example0:

edit the following 2 lines in file .exrc (vi ~/.exrc) :abbr #b /************************ :abbr #e ************************/

Then when you usevi template.c

And after press<i>/<a>/<o> to insert text, you type#b followed by <Enter>, you add a line /******** in.

Similarly, when you type #e followed by <Enter>, you add a line ********/ in

Example 1

Page 31: Chapter 1 Introduction to C Language

Slide 31

Programming Environment in .NET Running C/C++ Instruction (Reference)

Not used in this course

Page 32: Chapter 1 Introduction to C Language

Slide 32

Reference: Brian W. Kernighan & Dennis M. Ritchie:

C Programming Language, ANSI Edition

Prentice Hall Al Kelley & Ira Pohl: A Book on C, 4th ed.

Addison Wesley Deitel & Deitel: C How to Program, 4th ed.,

Chapter 1, Prentice Hall