c programming language with applications - · pdf file1 p i. lin - lect 2. intro to c language...

14
1 P I. Lin - Lect 2. Intro to C Language 1 C Programming Language with Applications Lecture Note 2 Intro to C Programming Language Professor Paul I. Lin, [email protected] http://www.etcs.ipfw.edu/~lin P I. Lin - Lect 2. Intro to C Language 2 Lecture 2: Introduction to C Programming Language Review C programming Environments: MS Visual Studio.NET and Visual C++ Write a C program under Windows Environment Characteristics of C Basic Elements of a C Programming Data Types Example 3-1: Adding Two Integers Example 3-2: Running Adding Two Integers As DOS Command Reserved Keywords

Upload: doandan

Post on 18-Mar-2018

238 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: C Programming Language with Applications - · PDF file1 P I. Lin - Lect 2. Intro to C Language 1 C Programming Language with Applications Lecture Note 2 Intro to C Programming Language

1

P I. Lin - Lect 2. Intro to C Language 1

C Programming Language with Applications

Lecture Note 2

Intro to C Programming Language

Professor Paul I. Lin, [email protected]://www.etcs.ipfw.edu/~lin

P I. Lin - Lect 2. Intro to C Language 2

Lecture 2: Introduction to C Programming Language

Review

• C programming Environments: MS Visual Studio.NET and Visual C++

• Write a C program under Windows Environment

Characteristics of C

Basic Elements of a C Programming

Data Types• Example 3-1: Adding Two Integers• Example 3-2: Running Adding Two Integers As

DOS Command Reserved Keywords

Page 2: C Programming Language with Applications - · PDF file1 P I. Lin - Lect 2. Intro to C Language 1 C Programming Language with Applications Lecture Note 2 Intro to C Programming Language

2

P I. Lin - Lect 2. Intro to C Language 3

C Programming Environments

C Programming Environments

• Linux/Unix Console Applications (Character-oriented) using ANSI C libraries and API (Application Program Interface) functions

• Windows 32 Console Applications (Character-oriented) using ANSI C standard libraries

• Windows Applications using API functions

P I. Lin - Lect 2. Intro to C Language 4

Writing C Programs Using MS Visual C++

Edit a new program Use the mouse to click on the File menu and

then choose File New Project (to create a new project) Win32 Console Applications: Application type -

> Console Application; Application Option -> Empty project

Edit a C program (Example - welcome.c) Project -> Add New Item -> C++ File -> welcome.c

Save the program Save the program (from the File menu) File Save or Save As File Name: welcome.c

Page 3: C Programming Language with Applications - · PDF file1 P I. Lin - Lect 2. Intro to C Language 1 C Programming Language with Applications Lecture Note 2 Intro to C Programming Language

3

P I. Lin - Lect 2. Intro to C Language 5

Writing C Programs Using MS Visual C++(continue)

Compile and build the program From Build menu, choose

Build Build Solution

When syntax error occurs, click on the error line and correct errors

Run and test the program Debug Start without Debugging, to run program

Or run the executable code directly under DOS environment

P I. Lin - Lect 2. Intro to C Language 6

Characteristics of C

Basic C Characteristics

• A General Purpose Programming Language

• Highly Portable

• Generate Fast Codes

• Small Size Language

• Relative Low-level:

Memory access (& address), register

• C Standard library functions

Page 4: C Programming Language with Applications - · PDF file1 P I. Lin - Lect 2. Intro to C Language 1 C Programming Language with Applications Lecture Note 2 Intro to C Programming Language

4

P I. Lin - Lect 2. Intro to C Language 7

Characteristics of C (continue)

Modern Control Structures:- if, if-else, for, while, do-while, switch, case, ……

Bit-wise operations:& (AND)| (OR)~ (NOT)^ (EXOR)<< (Left Shift)>> (Right Shift)

Logical operations:&& Logical AND| | Logical OR

P I. Lin - Lect 2. Intro to C Language 8

Characteristics of C (continue)

Pointer Implementation Data Types:

• char (signed or unsigned, small integer)• int (signed or unsigned, short 16-bit, long)• float and double• Arrays: char arrays (strings), int arrays, float

arrays, etc• Pointers: char, int, float, struct, functions,

pointers• struct (record)• bit-field struct and union

Page 5: C Programming Language with Applications - · PDF file1 P I. Lin - Lect 2. Intro to C Language 1 C Programming Language with Applications Lecture Note 2 Intro to C Programming Language

5

P I. Lin - Lect 2. Intro to C Language 9

Commonly used Terms in Programming Languages

Statements• Describe some program actions

Example: printf(“Welcome to C \n”);• Execute for its effect

Type of Statements• Declaration statement• Expression statement (arithmetic operations,

assignment, function calls) • Compound statements (a series of statement

to be treated as a single statement)• Selection statement (if, else, switch)• Iteration statement (while, do, for)• Jump statement (break, continue, return)

P I. Lin - Lect 2. Intro to C Language 10

C Identifiers (Variable names)

A sequence of letters, digits, underscores Must begin with a letter (not a digit) Cannot be a key word Can be any length, but only the first 31

characters are recognized Case sensitive (lower and upper -case

letters are treated differently) Examples:

• x x1 name• Number accountNumber value

Page 6: C Programming Language with Applications - · PDF file1 P I. Lin - Lect 2. Intro to C Language 1 C Programming Language with Applications Lecture Note 2 Intro to C Programming Language

6

P I. Lin - Lect 2. Intro to C Language 11

Escape Sequence

The backslash (\) is called an escape character. When encountering a backslash in a string, the complier looks ahead for the next character and combines it with the backslash to form an escape sequence.

Example: Printf(“Welcome to C \n”);

Escape Sequence Description\n New Line\t Horizontal Tab\a Alert – Sound the system bell\\ Insert a backslash in a string\” Insert a double quote in a string\000 octal number\xhh hexadecimal number

P I. Lin - Lect 2. Intro to C Language 12

lvalues & rvalues

lvalues means left values, variable names are said to be lvalues because they can be used on the left side of an assignment operator

rvalues means right values, constants are said to be rvalues because they can be used only on the right side of an assignment operator

A lvalue is an expression referring to an object that may be altered

An object is a region of memory that can be examined and stored into

Page 7: C Programming Language with Applications - · PDF file1 P I. Lin - Lect 2. Intro to C Language 1 C Programming Language with Applications Lecture Note 2 Intro to C Programming Language

7

P I. Lin - Lect 2. Intro to C Language 13

Basic Elements of C

Basic Elements include: • Character set

• Constants

• Variables

• Data types

• Reserved Words

• Expressions

• Preprocessor

• Functions

• Operators

P I. Lin - Lect 2. Intro to C Language 14

Basic Elements of C (continue)

Character set

• A, … Z, a, . z, 0, 1, 2, …, 9, and special symbols

Constants

• Integer constants:

Octal: 0, 1, 2, 3, 4, 5, 6, 7

Decimal: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

hexadecimal: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f or (A, B, C, D, E, F)

Example: decimal 31 can be written as octal 037, or as hexadecimal 0x1f

Page 8: C Programming Language with Applications - · PDF file1 P I. Lin - Lect 2. Intro to C Language 1 C Programming Language with Applications Lecture Note 2 Intro to C Programming Language

8

P I. Lin - Lect 2. Intro to C Language 15

Basic Elements of C (continue)

• ASCII character constants:

‘a’, ‘A’, ..’ z’, ‘Z’, ‘0’, ‘1’, ‘2’, …’9’

• String constants:

“W” or “The C programming Language”

• Floating-point number constants

123.5 0.0 0.1E-6

Variables

Data types

• char, int, long, float, constant, etc.

P I. Lin - Lect 2. Intro to C Language 16

Data Types

Integer types – include char, int, short, and longType Descriptionchar byte (8-bit) or 32-bit integer (-128 ..

0..127)int integer (32-bit) – integer 16-bit (-32,768

.. 0 .. 32,767)short 16-bit integer – signedlong long integer (double size of integer) unsigned charunsigned intunsigned shortunsigned long

Page 9: C Programming Language with Applications - · PDF file1 P I. Lin - Lect 2. Intro to C Language 1 C Programming Language with Applications Lecture Note 2 Intro to C Programming Language

9

P I. Lin - Lect 2. Intro to C Language 17

Data Types (continue)

Floating-point types – represent floating-point (real) numbers

Type Descriptionfloat single precision floating point number double double precision floating point numberlong double

Constants types – includes literal constants, Symbolic constants

Type Descriptionliteral actual number or charactersSymbolic named associated with literal – case

sensitive

P I. Lin - Lect 2. Intro to C Language 18

Example 2-1: Adding Two Integers

• Adding two integers• Using scanf() function to obtain two integer

typed numbers entered by a user at the keyboard

• Calculating the sum of both values• Printing the result using printf() function

• Run the program as a command under • MS DOS Command window (Run ->

command)• Window 32 Command window (Run -> cmd)

Page 10: C Programming Language with Applications - · PDF file1 P I. Lin - Lect 2. Intro to C Language 1 C Programming Language with Applications Lecture Note 2 Intro to C Programming Language

10

P I. Lin - Lect 2. Intro to C Language 19

Example 2-2: Running Compiled Adding Two Integers Program as DOS Command

P I. Lin - Lect 2. Intro to C Language 20

Example 2-2: Running Compiled Adding Two Integers Program as DOS Command

Page 11: C Programming Language with Applications - · PDF file1 P I. Lin - Lect 2. Intro to C Language 1 C Programming Language with Applications Lecture Note 2 Intro to C Programming Language

11

P I. Lin - Lect 2. Intro to C Language 21

Reserved Keywords

C’s Keywords

auto double int struct

break else long switch

case enum register typedef

char extern return union

const float short unsigned

continue for signed void

default goto sizeof volatile

do if static while

P I. Lin - Lect 2. Intro to C Language 22

Reserved Keywords for Visibility and Life Time Controls

Auto -- dynamic, local to a program or function module; short term variables on stack memory

Extern -- to be available at the program linking time

Static -- long term variables on data memoryVolatile -- advise an optimizing compiler that the

variable is important and do not attempt to remove it if it not used

Const -- longest life, cannot be altered after the definition; read only

Page 12: C Programming Language with Applications - · PDF file1 P I. Lin - Lect 2. Intro to C Language 1 C Programming Language with Applications Lecture Note 2 Intro to C Programming Language

12

P I. Lin - Lect 2. Intro to C Language 23

Expressions

Expression is a statement or a part of a statement.Simple expression

x + y Simple decision making

If … else Multiple decision making

switchdefaultcasebreak

P I. Lin - Lect 2. Intro to C Language 24

Iteration Control

Iteration uses a repetition structure

Examples:

for - for loop iteration

while - while loop

do/while - do/while

continue - exit the inner loop and restart from the outer loop

break - exit a block

Page 13: C Programming Language with Applications - · PDF file1 P I. Lin - Lect 2. Intro to C Language 1 C Programming Language with Applications Lecture Note 2 Intro to C Programming Language

13

P I. Lin - Lect 2. Intro to C Language 25

PreprocessorsPreprocessing occurs before a program is compiled.

Its actions are the inclusion of other files, definition of symbolic constants and macros, conditional compilation of program code and conditional execution of preprocessor directives.

#include preprocessor directive #define preprocessor directive

• Symbolic constants• Macros

Conditional compilation #error and #program preprocessor directives # and ## Operators Line Numbers Predefined Symbolic Constants Assertions

P I. Lin - Lect 2. Intro to C Language 26

Functions

Functions allow the programmer to modularize a program. All variables defined in function definitions are local variables – they are known only in the function in which they are defined.

return -- return from a function and go back to caller

void -- no values associated with parameter or arguments in a function

A list of parameters as local variables Examples:

• int main()• printf(“Welcome to C \n”);• scanf(“Enter Input %d “, &intetger1);

Page 14: C Programming Language with Applications - · PDF file1 P I. Lin - Lect 2. Intro to C Language 1 C Programming Language with Applications Lecture Note 2 Intro to C Programming Language

14

P I. Lin - Lect 2. Intro to C Language 27

Summary

Review • C programming Environments• Write C programs under Windows

Environment Characteristics of C Basic Elements of the C Programming Data Types Reserved Keywords Write a Simple Addition Program in C Next

• Constants and Variables• Operators of the C Language

P I. Lin - Lect 2. Intro to C Language 28

Question?