CS计算机代考程序代写 Lecture 9-1 C Variables

Lecture 9-1 C Variables

2
Objectives
At the end of lecture 3-1, you will know • How to store data in Cvariables
• The identifiers
• Pre-defined C data types
• Constants in C

Read & watch
• Read 2.1—2.4 in English text book or
• TutorialsPoint
– C variables at
http://www.tutorialspoint.com/cprogramming/c_vari ables.htm
– C constants
http://www.tutorialspoint.com/cprogramming/c_con stants.htm
• Video clip
– Cprogrammingtutorial-7-Variables
https://www.youtube.com/watch?v=k1ur8rX- DQQ&index=7&list=PL6gx4Cwl9DGAKIXv8Yr6nh GJ9Vlcjyymq
3

4
How to store data in C?
• Data to be processed is stored in memory (RAM)
• Program to be executed is store in memory (RAM)
• But how to refer the data in memory?
– use variables in high level languages
– we do not use memory addresses directly

5
C variables
• Attributes of a variable
– A variable has a name, called identifier – A variable has a value
– A variable has a declared data type
• So variables are containers to store data
• The name of a variable is translated into memory address when the program is compiled
– –

at that address the value of the variable is stored
you use the variable to refer that memory location (value)
different types have different internal represenations and requirements on the memory bytes

Understand C variables
• A variable has a name, called identifier
• A variable has a value
• A variable has a declared data type
• A box (container) is used to keep a value; different type of values requires different size. Only one value is stored. Each box has a name to refer to the box
pi 3.14159
6
grade G

7
C Identifiers
• A variable has a name, called identifier
• How to name a variable? (book 2.1 page
35)
– There are rules
• Starts with English letters or ’_’ (understrykningstecken)
• It can contain digits
• C reserved key words can not be used as identifiers
– Name variables meaningfullyeasier to read/understand your programs

8
C Identifier examples
• Valid identifiers
i y5 tal_1 ANTAL hogsta_vadret _value
• Invalid identifiers (why?) 4name +value -abc int
• Note: Upper case and lower case letters are different in C
– name and Name are 2 different identifiers

9
C Identifier questions
• Vilka av nedanstånde är identifierare i C?
nr4 NR4 nr_4 nr-4
_nr4 IDENT ident identi värdet adam&eva identifierare

10
C data types
• When you define a variable to keep some piece of data, you need to choose a data typethe right size of the box (memory) is used to keep the value
• Defined types
– integerskeep whole numbers
– floatkeep real numbers (with fractions) – charkeep a character

11
C data type – integers
• There are following pre-defined integer types int  at least 2 memory bytes used
short int  at least 2 memory bytes used
long intat least 4 memory bytes used
– The more bytes used, the larger the value can be represented
• By default, they are signed integers (that is, they can represent negative integers too)
• By attaching specifier “unsigned” to tell that the data saved is non-negative:
– unsigned int
– unsigned short int – unsigned long int

12
C data type – integers
• Examples int i;
short int tal;
long int big_num; unsigned int a;

13
C data type – char
• char
– Each character is represented by its character code (ASCII code).
– 1 memory byte is used
• Type char can also be thought to be integer type to keep small integers
• Examples
– char ch;
– char grade;

14
C data type for real numbers
• The pre-defined floating types float  at least 4 bytes double at least 8 bytes long double  16 bytes
• Real numbers are not stored exactly as int values.
– read and test the example float.c • Examples
float result; // result is a variable of float double d;
long double maxtal;

15
Store a value to a variable
• You can store a value to a variable via assignment
statement
– The value on the right is assigned to the variable on left tal = 5; // if tal is a variable of integer type
teck = ‘x’; // if teck is a variable of char or integer type result = 3.14159 ; // if result is a variable of floating type
• You can also assign a value when you define the variable int a = 100;
int b = – 4;
char grade = ‘U’;

16
Example variables.c
• Study the program variables.c

Integer overflow
• Each variable is represented by a limited number of bytes (decided by data type)
– data range min — max
– Integer overflow: if the calculated value is out side this range, integer overflow occurs and the result is incorrect.
• Read and try int-overflow.c
contains symbolic names/constants for min/max integer type values
• Ref: read more aout it at Wikipedia 17

Arithmetic underflow/overflow
• The pre-defined floating types float  at least 4 bytes double at least 8 bytes long double  16 bytes
• Example float
– The ranges that are representable by float is – Read more about it at Wikipedia
18

19
Constants
• Constants are also common data objects manipulated in a program
• Integer constants
– 1234anint
– 1234L or 1234l  long int – 1234U  unsigned int
– Suffix: l or L, u or U, ul or UL
• Number systems
– Octal: starts with zero 037 – Hexa-decimal: 0x1F
• Example
– 0xFUL what is the value?
(ell)

20
Constants–char
• A character constant is an integer, written as one character within single quotes, ‘x’
• The value is its ASCII code or code in the character set
• Several representation forms – ‘x’
– ‘\170’ – ‘\x78’

Constants–char
• Escape sequences
– Some special character constants
21

22
Constants—char vs string
• There is no string type in C
• A string is a sequence of characters,
terminated by a null character ‘\0’
– The storage required 1 + string length
• The length of a string s can be calculated by a function strlen(s) implemented in the library string
• Question:
– What is the difference between 7, ‘7’, and “7”? in C?

23
Type conversions
• Automatic conversion is done when types are mixed in an expression
– from floating to integer
the fraction part is discarded
int i = 3.14; // i has value of 3
– double d = i; // d has value of 3.0
• Helpful rules on conversions
– English book page 44
– Swedish book 3.6.1 pages 38-39
• Forced conversion (cast) (type-name) value