Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variable Symbol represents a place to store information

Similar presentations


Presentation on theme: "Variable Symbol represents a place to store information"— Presentation transcript:

1 Variable Symbol represents a place to store information
name value memory space Example: somebody’s age An integer variable age; age = 20; Contrasted with constants No change in constants Computer memory age 20

2 Why Need Variable Remember a value
Requests a value from the user Results from calculation Example: int v1, v2, sum; v1 = 50; v2 = 30; sum = v1 + v2; * = means assign the value as Provide a way to access your computer's memory Computer memory v1 50 v2 30 sum 80

3 How – Declaration A variable must be declared before it can be used.
Tell the computer that you need to store a number in a variable Mostly declared at the start of each function. Declaration format: type name = initial_value; type name1 = initial_value1, name2 = initial_value2, …; It is not required to put initial value in declaration

4 Declaration of Variables
type name = initial_value; type name1 = initial_value1, name2 = initial_value2, …; { int v1, v2, sum; v1 = 50; v2 = 30; sum = v1 + v2; } { int v1= 50, v2 = 30, sum; sum = v1 + v2; }

5 Basic Types of Variables
int  integer float  single-precision floating point double double-precision floating point char  one byte character _Bool Boolean

6 Variable Names Contain one or more characters,
A-Z, a-z 0-9 _ Must start with a non-digit character

7 Variable Names – cont. Cannot be C's keywords Limitation
int, main, while, etc Limitation Maximum of 63 characters for a variable name Sometimes 31 or 8 in very old C Case sensitive: upper and lower case characters are different floating int Int main3 4yi

8 int: integer Variables
For integral values only 10, -5, 1000 no fraction 10.2 no commas 12,000 Ranges Machine dependent Minimum: 16 digits 32 bits of storage on grove.ufl.edu (usual) Signed: −2,147,483,648 to +2,147,483,647 Unsigned: 0 to +4,294,967,295 Maybe 64 on others

9 Operations for int type
Declaration int x, y, z; Assignment y = 10; z = 5; Calculation Plus: + x = y + z; Minus: - x = y – z; Muliply: * x = y * z; Divide: / x = y / z; Modulus x = y % z; result of y/z will be truncated

10 Display Integer Variables – I
Preprocessor: interact with input/output of your computer #include <stdio.h> int main() Start point of the program { } Start and finish of function int value1, value2, sum; Declear Variables value1 = 50; value2 = 30; Define Values sum = value1 + value2; Summation printf(“The sum of 50 and 30 is %i\n“, sum); Print the value of an integer variable Printing results return 0; Finish and return value 0

11 Display Integer Variables – II
#include <stdio.h> int main() { int value1, value2, sum; value1 = 50; value2 = 30; sum = value1 + value2; return 0; } printf(“The sum of %i and %i is %i\n“, value1, value2, sum);

12 int: integer Variables – Special Case I
Starting with digit “0” Octal notation. Base 8, not 10 0,1,2,3,4,5,6,7 0177 = 1*64 + 7*8 + 7 = 127 0256 = ? Display %i – print out the decimal value %o – print out the octal value %#o – print out the octal value, starting with 0

13 int: integer Variables – Special Case II
Starting with digit “0x” Hexadecimal notation. Based 16, not 10 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F 0x177 = 1* * = 375 0xAF = ? 0x2AF = ? Display %i – print out the decimal value %x – print out the hexadecimal value %#x – print out the hexadecimal value, starting with 0

14 Example - I #include <stdio.h> int main() { int a, b, c, d;
d = 0xAF; printf(“The four numbers are %i, %i, %#o, %#x\n”, a, b, c, d); printf(“The four decimal numbers are %i, %i, %i, %i\n”, a, b, c, d); }

15 Example – II #include <stdio.h> int main() { int a, b, c, f;
f = a/b; printf(“%i / %i = %i\n”, a, b, f); f = b/a; printf(“%i / %i = %i\n”, b, a, f); f = c/a; printf(“%i / %i = %i\n”, c, a, f); }

16 float: single-precision Variables
For values containing decimal 3., 125.8, -0.1 Scientific notation 2.25e-3 = 2.25 * 10-3 = Use e or E for exponent no commas

17 float Variables - II Ranges IEEE floating-point standard ±3.4×1038

18 float Variables - III Display %f – print out the decimal value
%e – print out the scientific notation %g – let printf decide the format -4 < value of exponent < 5: %f format Otherwise: %e format

19 Operations for float type
Declaration float x, y, z; Assignment y = 10.00; z = 5.8; Calculation Plus: + x = y + z; Minus: - x = y – z; Muliply: * x = y * z; Divide: / x = y / z; result of y/z will NOT be truncated

20 Example – I #include <stdio.h> int main() { int a, b, c;
float f; a = 10; b = 20; c = a/b; printf(“%i / %i = %i\n”, a, b, c); f = a/b; printf(“%i / %i = %f\n”, a, b, f); }

21 double: double-precision Variables
Similar to float More storage space (IEEE floating-point standard) float variables: e+f+1 = 32 double variables: e+f+1 = 64 Ranges ± ×10308 Same display method as float Operation similar as float

22 char: character Variables
For single character Enclosing the character within a pair of ‘ ’ ‘a’ ‘;’ ‘P’ ‘\n’ ‘1’ Display %c

23 Example – II #include <stdio.h> int main() { int a, b, f;
char c; a = 36; b = 52; c = ‘a’; printf(“The three numbers are %i , %i, %i\n”, a, b, c); printf(“The three characters are %c , %c, %c\n”, a, b, c); }

24 _Bool: Boolean Type For boolean Display 0/1 Normally means false/true

25 Finish the Code #include <stdio.h> int main(void) {
int integerVar = 100; float floatingVar = ; double doubleVar = 8.44e+11; char charVar = ‘W’; _Bool boolVar = 0; printf(“integerVar = %? \n”, integerVar); printf(“floatingVar = %? \n”, floatingVar ); printf(“doubleVar = %? \n”, doubleVar ); printf(“doubleVar = %? \n”, doubleVar ); // scientific notation printf(“charVar = %? \n”, charVar ); printf(“boolVar = %? \n”, boolVar ); return 0; }

26 Special variable types
short usually use less space add “h” after “%” in printf long, long long usually use more space use “l” to indicate add “l” after “%” in printf signed, unsigned specify whether is a signed quantity use “u” to indicate unsigned %u for unsigned

27 Summary of Data Type Type Examples Printf char ‘a’, ‘\n’ %c _Bool 0, 1
%i, %u short int 1,100, -5 %hi, %hx, %ho unsigned short int 1, 39, 100 %hu, %hx, %ho int -1, 5, 0XAF, 0177 %i, %x, %o unsigned int 5u, 0XAFu, 0177U %u, %x, %o long int 0xffffL, 12l %li, %lx, %lo unsigned long int 0xffffUL, 12ul %lu, %lx, %lo

28 Summary of Data Type – cont.
Examples Printf long long int 0xffffLL, 12ll %lli, %llx, %llo unsigned long long int 0xffffULL, 12ull %llu, %llx, %llo float 12.3f, 3.1e-5f, 0x1.5p10 %f, %e, %g %a double 12.3, 3.1e-5, 0x1.5p10 long double 12.3l, 3.1e-5l %Lf, %Le, %Lg

29 Assignment Operators Join the arithmetic operators Examples:
Format: op= Examples: count = count + 10; count += 10; count = count - 5; count -= 5; a /= b + c; a = a / (b + c);

30 Unary Operators M += 1; M = M + 1; ++M; M++; Unary plus / minus
+ / - Example: -a Unary increment/decrement ++ / -- M += 1; M = M + 1; ++M; M++;

31 Arithmetic Operators add: +, minus: -, multiply: *, divide: /, modulus: % Parentheses (grouping): ( ) Unary plus / minus + - Unary increment/decrement ++ --

32 Operator Precedence Precedence Order for
Operators with higher precedence are evaluated first Operators with same precedence are evaluated from left to right In decreasing precedence ( ) unary increment (++), unary decrement (--) unary plus (+), unary minus (-) multiply (*), divide(/), modulus(%) add(+), minus(-) Order for c = -a * b a + b * c / d


Download ppt "Variable Symbol represents a place to store information"

Similar presentations


Ads by Google