Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lesson 6 Getting Started in C Programming Hu Junfeng 2007/10/10.

Similar presentations


Presentation on theme: "Lesson 6 Getting Started in C Programming Hu Junfeng 2007/10/10."— Presentation transcript:

1 Lesson 6 Getting Started in C Programming Hu Junfeng 2007/10/10

2 2 index The essential components of a language A general structure of a C program Identifier Data type Arithmetic expression and variable Displaying Numerical Values VC60 IDE and common compiling errors

3 3 What makes a language a language วิธีการหรือเครื่องมือที่ใช้ในการ สื่อสารระหว่างโปรแกรมเมอร์ 语言之所以成为语言之要素为何?

4 4 Symbol – word - sentence Morphology, word formation rules Syntax  a systematic, orderly arrangement Grammar  The system of inflections, syntax, and word formation of a language What makes a language a language

5 5 /* A first program in C */ #include void main() { printf("Hello world, I came!"); printf("I saw!"); printf("I conquered!"); } General Structure of a C Program Hello world, I came! I saw! I conquered! press a key to continue... Library Function header line Executable statements

6 6 The main() Function Sometimes referred to as a driver function General Structure of a C Program

7 7 /* A first program in C */ #include void main() { printf("Hello world, I came!"); printf("I saw!"); printf("I conquered!"); } Identifiers Identifiers in C consist of three types:  Reserved words  Standard identifiers  Programmer-created identifiers Reserved words Standard identifiers Tokens: predefined language units

8 8 Identifiers (continued) Reserved word: word that is predefined by the programming language for a special purpose and can only be used in a specified manner for its intended purpose  Also referred to as keywords in C

9 9 Identifiers (continued) Standard identifiers: words predefined in C Most of the standard identifiers are the names of functions that are provided in the C standard library It is good programming practice to use standard identifiers only for their intended purpose

10 10 Identifiers (continued) Programmer-created identifiers: Also called programmer-created names  Used for naming data and functions  Must conform to the following rules: First character must be a letter or underscore (_) Only letters, digits, or underscores may follow the initial character Blank spaces are not allowed Cannot be a reserved word

11 11 Identifiers (continued) Examples of invalid C programmer-created names:  4ab7  calculate total  While Examples of valid user-created names:  _systemBuffer  LJ113_10am C is a case-sensitive language  Total, total represent different identifiers

12 12 Data Types a set of values  Encoding/decoding  Space allocated a set of operations that can be applied to these values Built-in data type: is provided as an integral part of the language; also known as primitive type

13 13 Data Types (continued) Encoding /decoding  ASCII  integer (short; long)  unsigned  float (double) Storage  byte; word; words Operations: +, -, *, /,mod

14 14 Data Types (continued)

15 15 Integer Data Types int : whole numbers (integers)  For example: 0, -10, 253, -26351  Not allowed: commas, decimal points, special symbols char : stores individual characters (ASCII)  For example: 'A', '$', 'b', '!'

16 16 Data types (bit pattern) —— integer, unsigned 1000000000000001 Integer : -2 15 —— 2 15 -1 Unsigned: 0 —— 2 16 -1 -2 15 2 11 2 7 2 3 2 0 complement The leading 1 represents a negative number, other bit represents a positive number

17 17 Data types (integer type) Data type size ( bit ) range of values short int 16 -32K ~ 32K-1 (- 32768 ~ 32767) int32-2G ~ 2G-1 Unsigned short1664K -1 ~ 0 unsigned 324G-1 ~ 0

18 18 Integer Data Types (continued)

19 19 Integer Data Types (char)

20 20 Integer Data Types (continued)

21 21 char (escape sequence)

22 22 Floating-Point Data Types A floating-point value (real number) can be the number zero or any positive or negative number that contains a decimal point  For example: +10.625, 5., -6.2, 3251.92, +2  Not allowed: commas, special symbols float : single-precision number - one word double : double-precision number - two words

23 23 Exponential Notation (continued)

24 24 Floating-Point Data Types (cont.) float literal is indicated by appending an f or F long double is created by appending an l or L  9.234 indicates a double literal  9.234f indicates a float literal  9.234L indicates a long double literal

25 25 Floating –point encoding Floating-point numbers consist of an ``exponent,'' ``significand'', and ``sign bit'' sign bit 1 Exponent 7significand 23

26 26 Floating-Point Data Types (cont.) Overflow, Underflow, and Roundoff Error

27 27 Arithmetic Operations Arithmetic operators: operators used for arithmetic operations:  Addition +  Subtraction -  Multiplication *  Division /  Modulus Division % Binary operators require two operands Arithmetic expression

28 28 Arithmetic operators (int) Operator Symbol Form Operation Addition + x + yx plus y Subtraction - x - yx minus y Multiplication * x * yx times y division / x/yx divided by y modulus % x%yremainder of x divided by y

29 29 Integer Division 15/2 = 7  Integers cannot contain a fractional part  Remainder is truncated % is the modulus or remainder operator  9 % 4 is 1  17 % 3 is 2  14 % 2 is 0

30 30 Data Types and operations mod assignment

31 31 Arithmetic expression A simple binary arithmetic expression consists of a binary arithmetic operator connecting two operand in the form:  literalValue operator literalValue 3 + 7 12.62 - 9.8.08 * 12.2 12.6 / 2. An operand can be either a literal value or an identifier that has a value associated with it Arithmetic expression

32 32 Variables & Variable declaration Variables are: names  storage of data. The term "variable" is used because the value stored in the variable can be changed. In C variables must be declared before it be used. Act as an place holder in expressions Arithmetic expression

33 33 data type variableName1 [, variableName2]; int i, j=90; short si; char c1='a'; float balance, profit, loss; Variable declaration : Const of char Const of integer initialization

34 34 Declaration Statements (cont.)

35 35 Declaration Statements (cont.)

36 36 Declaration Statements (cont.)

37 37 Declaration Statements (cont.) ?

38 38 Variable in expression Identifiers const of float

39 39 Operator precedence and associatively —— complex arithmetic expression Two binary arithmetic operator symbols must never be placed side by side Parentheses may be used to form groupings  Expressions in parentheses are evaluated first Parentheses may be enclosed by other parentheses

40 40 Operator Precedence and Associativity (cont.) Levels of precedence: 1. negations done first 2. Multiplication, division, and modulus operations are the next; 3. Addition and subtraction are computed last; 4. Same level expressions are evaluated from left to right ( except the negation operator )

41 41 Left to right association : Given that the following declarations: int m = 3, n = 4; float x = 2.5, y = 1.0; Expression Equivalent expression Result m + n + x +y (((m+n)+x)+y) 10.5 m + n * x + y ((m+(x*n))+y) 14.5 x / y + m / n (x/y) + (m/n) 2.5 x - y * m + y/n (x - (y*m)) + (y/n) -0.25 x / 0 undefined

42 42 mixed-mode expression with Assignment Operators

43 43 Mixed-mode expression (type casting)

44 44 Mixed-mode expression (automatic type conversion)

45 45 Displaying Numerical Values: —— the printf() Function Function arguments

46 46 Displaying Numerical Values (cont.) printf(“conversion control sequence” , argument list)

47 47 Displaying Numerical Values(cont.) #include void main() { int i=65; printf("the value is %d",i); } declare space& encoding method decoding & display mode printf(“the value is %c",i);

48 48 Displaying Numerical Values(cont.) Invoking or calling the printf() function display as an integer Escape sequence

49 49 Escape sequences:

50 50 Common Compiler Errors

51 51 Figure G.1: The Integrated Development Environment (IDE)

52 52 Figure G.2: The File Submenu

53 53 Figure G.3: The New Dialog Box

54 54 Figure G.4: Selecting the type of console application

55 55 Figure G.5: The application ’ s information dialog

56 56 Figure G.7: Creating a C++ source code file

57 57 Figure G.9: An expanded FileView hierarchy tree

58 58

59 59 Summary Simple C programs consist of the single function named main() All executable C statements must be terminated by a semicolon The two basic numerical data types used almost exclusively in current C programs are integers and double-precision numbers An expression is a sequence of one or more operands separated by operators

60 60 Summary (continued) Expressions are evaluated according to the precedence and associativity of the operators used Declaration statements inform the compiler of a function’s valid variable names Every variable in a C program must be  Declared with a data type  Used after it is declared

61 61 The assignment Read the material recommended in Chapter two. Programming exercise: Exercise: 2.1 2 Exercise:2.4 4 Exercise 2.5 7 Hand in before : 10/OCT via email in zip


Download ppt "Lesson 6 Getting Started in C Programming Hu Junfeng 2007/10/10."

Similar presentations


Ads by Google