1 / 26

Variable Basics

Variable Basics. Data Types & Printing. age. 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. 20. v1. v2. sum.

jada-hebert
Download Presentation

Variable Basics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Variable Basics Data Types & Printing

  2. age 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 20

  3. v1 v2 sum Why We Need Variables • 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 50 30 80

  4. Declaring • 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

  5. 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; • }

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

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

  8. Variable Names – cont. • Cannot be C's keywords • 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 Invalid Invalid

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

  10. Print the value of an integer variable 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; Declare Variables value1 = 50; value2 = 30; Define Values sum = value1 + value2; Summation printf(“The sum of 50 and 30 is %i\n“, sum); Printing results return 0; Finish and return value 0 The sum of 50 and 30 is 80

  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); The sum of 50 and 30 is 80

  12. Number Bases • We are used to base 10: 0-9 • 42010 = 4 * 10 2 + 2 * 101 + 0 * 100 • Other bases • Hexadecimal (Base 16): 0-9, A-F • 42010 = 1 * 162 + 10 * 161 + 4 * 160 = 1A416 • Octal (Base 8): 0-7 • 42010 = 6 * 82 + 4 * 81 + 4 * 80 = 6448 • Binary (Base 2): 0-1 • 42010 = 1 1010 01002

  13. 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

  14. 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*256 + 7*16 + 7 = 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

  15. Example - I #include <stdio.h> int main() { int a, b, c, d; a = 10; b = 20; c = 0177; 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); } The four numbers are 10, 20, 0177, 0xaf The four decimal numbers are 10, 20, 127, 175

  16. float: single-precision Variables • For values containing decimal 3., 125.8, -0.1 • Scientific notation 2.25e-3 = 2.25 * 10-3 = 0.00225 • Use e or E for exponent • No commas • Uses 4 bytes on *.cise.ufl.edu

  17. float Variables - II • Ranges • IEEE floating-point standard • e = 8, f = 23 • ±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. 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 • ±1.79769×10308 • Same display method as float • Operation similar as float

  20. char: character Variables • For single character • Enclosing the character within a pair of ‘ ’ • ‘a’ • ‘;’ • ‘P’ • ‘\n’ • ‘1’ • One Byte • Display • %c

  21. 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); } ASCII TABLE The three numbers are 36 , 52, 97 The three characters are $ , 4, a

  22. _Bool: Boolean Type • For boolean • 0/1 • Normally means false/true • 1 Byte long • Display • %i

  23. Finish the Code #include <stdio.h> int main(void) { int integerVar = 100; float floatingVar = 331.79; 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; } %i %f %f %e %c %i

  24. 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

  25. Summary of Data Type

  26. Summary of Data Type – cont.

More Related