Beginning C Lecture 2 Lecturer: Dr. Zhao Qinpei

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Data types and variables
Chapter 2 Data Types, Declarations, and Displays
String Escape Sequences
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Chapter 2: Using Data.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
The Fundamentals of C++ Chapter 2: Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Primitive Variables.
Copyright Curt Hill Variables What are they? Why do we need them?
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.
Copyright © – Curt Hill Types What they do.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
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.
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.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Today Variable declaration Mathematical Operators Input and Output Lab
A variable is a name for a value stored in memory.
Chapter 2 Variables.
Chapter 2: Introduction to C++
Elementary Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
ECE Application Programming
Tokens in C Keywords Identifiers Constants
ITEC113 Algorithms and Programming Techniques
Revision Lecture
By: Syed Shahrukh Haider
Fundamental Data Types
Chapter 2: Introduction to C++
Chapter 2: Introduction to C++
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
2.1 Parts of a C++ Program.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Numerical Data Types.
Chapter 2 Variables.
Chapter 2: Java Fundamentals
Lectures on Numerical Methods
Chapter 2: Introduction to C++.
Fundamental Data Types
Primitive Types and Expressions
Unit 3: Variables in Java
Module 2 Variables, Data Types and Arithmetic
Chapter 2 Variables.
Session 1 – Introduction to Computer and Algorithm (Part 2)‏
Chapter 2 Primitive Data Types and Operations
Variables in C Topics Naming Variables Declaring Variables
Variables and Constants
Programming Fundamental-1
Presentation transcript:

Beginning C Lecture 2 Lecturer: Dr. Zhao Qinpei Email: qinpeizhao@tongji.edu.cn http://sse.tongji.edu.cn/zhaoqinpei/Courses/

C basics memory Variable, constant Integer Floating-point numbers Expressions Type conversion Other data types Beiginning C / Qinpei Zhao 2018/11/28

Memory 1 kilobyte (or 1KB) is 1,024 bytes. 1 megabyte (or 1MB) is 1,024 kilobytes. 1 gigabyte (or 1GB) is 1,024 megabytes. Why 1,024, not 1,000? Beiginning C / Qinpei Zhao 2018/11/28

Memory distribution Code segment Constant segment Global data segment 代码区 存放程序的二进制代码 Binary code of the program 所有常量均存放在常量区,程序结束后由OS释放 All constants, released by OS when the program is over Constant segment 常量区 Global data segment 全局数据区 全局变量和静态变量,即使是函数内部的静态局部变量,程序结束后由OS释放 Global and static variables Heap segment 堆区 由程序员分配和释放,若程序员不释放,程序结束后可能由OS释放。new, malloc Allocated and released by the programmer Buffer segment 缓冲区 Stack segment 栈区 由编译器自动分配和释放,存放:函数内部的局部变量和函数的参数值 Allocated and released by compiler System Kernel 系统内核 Beginning C / Qinpei Zhao 2018/11/28

Variable 变量 #include <stdio.h> int main(void) { printf(“My salary is $1000 per month\n"); return 0; } #include <stdio.h> int main(void) { int salary = 1000; printf(“My salary is $%d per month\n“, salary); return 0; } Beginning C / Qinpei Zhao 2018/11/28

How to use variables? int salary; salary = 1000; printf(“My salary is $%d per month\n“, salary); two parameters separated by comma ‘,’ para1: control string / format string para2: variable “salary” %d indicates integer What if the variable is not initialized? Beginning C / Qinpei Zhao 2018/11/28

Variable related Variable 全局变量 declare and define e.g., int a; types global variable local variable naming convention numbers underscore characters 下划线 letters no keywords concept changeable during the running of the program declaration and reference basic operators data types integer float character + - * / % declare and define e.g., int a; assignment e.g., a = 10; 局部变量 addition, subtraction multiplication, division modulus Beginning C / Qinpei Zhao 2018/11/28

Declaring variables Must declare variables before use Variable declaration int n; float phi; int a, b, c; int – integer data type float – floating point data type Many other types (later) Beginning C / Qinpei Zhao 2018/11/28

Naming variables A variable name is a sequence of one or more uppercase or lowercase letters, digits, and underscore characters (_) that begins with a letter (_ counts as a letter). e.g., hunter, R, Dip64, _king, 8billion, 4_digits, include, Mary-Lou, Hash! Case sensitive, e.g., computer, Computer Variable names are defined with some flexibility. It’s worth calling them something that gives you a clue to what they indicate. variable name length should be upper to 31 characters, but better not be too long. Beginning C / Qinpei Zhao 2018/11/28

Using variables Beginning C / Qinpei Zhao 2018/11/28

Arithmetic statements arithmetic operators: + - * / % Modulus (%): calculates the remainder after dividing the value of the expression on the left of the operator by the value of the expression on the right (remainder operator), e.g., 12 % 5 = 2 unary minus (-): negative becomes positive, and vice versa arithmetic expression (算术表达式): 1 + 2 x + y / z – 3 45 / 7 Unary operator: 一元运算符 Beginning C / Qinpei Zhao 2018/11/28

Variables and Memory the amount of memory occupied by variables of a given type will always be the same on a particular machine a variable of a given type on one computer may occupy more memory than it does on another the amount of memory occupied by variable of these types depends on the particular compiler you’re using Beginning C / Qinpei Zhao 2018/11/28

unsigned variables unsigned integer types can be used when values that cannot be negative are used, e.g., the number of person Using unsigned type doesn’t provide more values than the corresponding signed type, but it allow numbers in a twice large range Beginning C / Qinpei Zhao 2018/11/28

Specify integer constants long Big_Number = 1287600L; long below_sea_level = -100000L; long long really_big_number = 123456789LL; unsigned int count = 100U; unsigned long value = 999999999UL; hexadecimal form: 0x99 or 0X99 Beginning C / Qinpei Zhao 2018/11/28

Floating-point values Float-point variables hold values that are written with a decimal point. 0.0000000000000005 or 5e-16 Beginning C / Qinpei Zhao 2018/11/28

Floating-point values (cont.) float ht = 1.003; printf(“The tree is %f height.”, ht); Control the number of decimal places printf(“The tree is %.2f height.”, ht);  1.00 general form of the format specifier for floating-point values: %[width][.precision][modifier]f width: the total number of characters in the output precision: the number of decimal places that are to appear after the decimal point modifier: L when the value is type long double %-10.4f 左对齐,字段宽度10个字符,小数点后面4位数 Beginning C / Qinpei Zhao 2018/11/28

More complicated expressions Preprocessing directive 预编译指令 Beginning C / Qinpei Zhao 2018/11/28

More complicated expressions Define as a variable, but to tell the compiler that the value is fixed and must not be changed. Beginning C / Qinpei Zhao 2018/11/28

Knowing the limits 64-bit operation system Beginning C / Qinpei Zhao 2018/11/28

Knowing the limits Beginning C / Qinpei Zhao 2018/11/28

sizeof operator How many bytes are occupied by a given type by using the sizeof operator. Beginning C / Qinpei Zhao 2018/11/28

Choosing the correct type Beginning C / Qinpei Zhao 2018/11/28

type conversion Explicit conversion (显式类型转换) Automatic conversion(自动转换类型) Implicit conversion (隐式类型转换) Rules for implicit conventions Beginning C / Qinpei Zhao 2018/11/28

More numeric data type - character char = 1 byte, for unsigned type, ranges from [0, 255]; for signed type, ranges from [-128, +127] char letter = ‘A’; char digit = ‘9’; char exclamation = ‘!’; char newline = ‘\n’; char letter = ‘C’; (ASCII = 67) Character input char ch = 0; scanf(“%c”, &ch); Character output Printf(“The character is %c and the code value is %d”, ch, ch); See Appendix B American Standard Code for Information Interchange (ASCII). Beginning C / Qinpei Zhao 2018/11/28

More numeric data type - enumeration define a new integer type where variables of the type have a fixed range of possible values. enum Weekday {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }; declare a new variable of type Weekday and initialize it: enum Weekday today = Wednesday; enum Weekday {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday} today, tomorrow; enum Weekday {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday} today = Monday, tomorrow = today +1; choosing numerator values The enumerators that follow an enumerator with an explicit value will be assigned successive integer values (Monday = 1, Tuesday = 2, …) Beginning C / Qinpei Zhao 2018/11/28

More numeric data type – boolean (布尔型) represents true (1) or false (0) In C, it’s _Bool, not bool (occupied by C++) #include <stdbool.h> to use the data type Or define as follows: #define bool _Bool #define true 1 #define false 0 Beginning C / Qinpei Zhao 2018/11/28

Data types in C Beginning C / Qinpei Zhao 2018/11/28

Mathematical functions Beginning C / Qinpei Zhao 2018/11/28

Designing a program problem The height of a tree is of great interest to many people. For one thing, if a tree is being cut down, knowing its height tells you how far away safe is. This is very important to those with a nervous disposition. Your problem is to find out the height of a tree without using a very long ladder, which itself would introduce risk to life and limb. To find the height of a tree, you’re allowed the help of a friend-preferably a short friend. You should assume that the tree you’re measuring is taller than both you and your friend. Trees that are shorter than you present little risk, unless they’re of the spiky kind. problem Beginning C / Qinpei Zhao 2018/11/28