Lesson 6 Getting Started in C Programming Hu Junfeng 2007/10/10
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 What makes a language a language วิธีการหรือเครื่องมือที่ใช้ในการ สื่อสารระหว่างโปรแกรมเมอร์ 语言之所以成为语言之要素为何?
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 /* 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 The main() Function Sometimes referred to as a driver function General Structure of a C Program
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 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 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 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 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 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 Data Types (continued) Encoding /decoding ASCII integer (short; long) unsigned float (double) Storage byte; word; words Operations: +, -, *, /,mod
14 Data Types (continued)
15 Integer Data Types int : whole numbers (integers) For example: 0, -10, 253, Not allowed: commas, decimal points, special symbols char : stores individual characters (ASCII) For example: 'A', '$', 'b', '!'
16 Data types (bit pattern) —— integer, unsigned Integer : —— Unsigned: 0 —— complement The leading 1 represents a negative number, other bit represents a positive number
17 Data types (integer type) Data type size ( bit ) range of values short int K ~ 32K-1 ( ~ 32767) int32-2G ~ 2G-1 Unsigned short1664K -1 ~ 0 unsigned 324G-1 ~ 0
18 Integer Data Types (continued)
19 Integer Data Types (char)
20 Integer Data Types (continued)
21 char (escape sequence)
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: , 5., -6.2, , +2 Not allowed: commas, special symbols float : single-precision number - one word double : double-precision number - two words
23 Exponential Notation (continued)
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 indicates a double literal 9.234f indicates a float literal 9.234L indicates a long double literal
25 Floating –point encoding Floating-point numbers consist of an ``exponent,'' ``significand'', and ``sign bit'' sign bit 1 Exponent 7significand 23
26 Floating-Point Data Types (cont.) Overflow, Underflow, and Roundoff Error
27 Arithmetic Operations Arithmetic operators: operators used for arithmetic operations: Addition + Subtraction - Multiplication * Division / Modulus Division % Binary operators require two operands Arithmetic expression
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 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 Data Types and operations mod assignment
31 Arithmetic expression A simple binary arithmetic expression consists of a binary arithmetic operator connecting two operand in the form: literalValue operator literalValue * / 2. An operand can be either a literal value or an identifier that has a value associated with it Arithmetic expression
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 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 Declaration Statements (cont.)
35 Declaration Statements (cont.)
36 Declaration Statements (cont.)
37 Declaration Statements (cont.) ?
38 Variable in expression Identifiers const of float
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 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 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) x / 0 undefined
42 mixed-mode expression with Assignment Operators
43 Mixed-mode expression (type casting)
44 Mixed-mode expression (automatic type conversion)
45 Displaying Numerical Values: —— the printf() Function Function arguments
46 Displaying Numerical Values (cont.) printf(“conversion control sequence” , argument list)
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 Displaying Numerical Values(cont.) Invoking or calling the printf() function display as an integer Escape sequence
49 Escape sequences:
50 Common Compiler Errors
51 Figure G.1: The Integrated Development Environment (IDE)
52 Figure G.2: The File Submenu
53 Figure G.3: The New Dialog Box
54 Figure G.4: Selecting the type of console application
55 Figure G.5: The application ’ s information dialog
56 Figure G.7: Creating a C++ source code file
57 Figure G.9: An expanded FileView hierarchy tree
58
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 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 The assignment Read the material recommended in Chapter two. Programming exercise: Exercise: Exercise:2.4 4 Exercise Hand in before : 10/OCT via in zip