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

Slides:



Advertisements
Similar presentations
Outline 2.1 Introduction 2.2 Basics of C Programs
Advertisements

Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
CMT Programming Software Applications
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Data types and variables
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
JavaScript, Fourth Edition
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
JavaScript, Third Edition
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Basic Elements of C++ Chapter 2.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
Chapter 2 Getting Started in C Programming
A First Book of ANSI C Fourth Edition
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Numeric Types, Expressions, and Output ROBERT REAVES.
Input, Output, and Processing
Chapter 2: Using Data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Lecture #5 Introduction to C++
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
CNG 140 C Programming Prof. Muslim Bozyiğit Dr. Ghalib A. Shah Department of Computer Engineering Mıddle East Technical University, NCC Spring
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Chapter 2 Variables.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Variables Symbol representing a place to store information
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ for Engineers and Scientists Second Edition
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Chapter 2: Basic Elements of C++
Chapter 2 Variables.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Tokens in C Keywords Identifiers Constants
Chapter 2: Problem Solving Using C++
ITEC113 Algorithms and Programming Techniques
Multiple variables can be created in one declaration
Java Programming: From Problem Analysis to Program Design, 4e
C++ for Engineers and Scientists Second Edition
CS111 Computer Programming
Chapter 2 Variables.
Lectures on Numerical Methods
Expressions and Assignment
Chapter 2: Introduction to C++.
Chapter 2 Variables.
Presentation transcript:

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