Unformatted text preview:

VariableWhy Need VariableHow – DeclarationDeclaration of VariablesBasic Types of VariablesVariable NamesVariable Names – cont.int: integer VariablesOperations for int typeDisplay Integer Variables – IDisplay Integer Variables – IIint: integer Variables – Special Case Iint: integer Variables – Special Case IIExample - IExample – IIfloat: single-precision Variablesfloat Variables - IIfloat Variables - IIIOperations for float typeExample – Idouble: double-precision Variableschar: character VariablesSlide 23_Bool: Boolean TypeFinish the CodeSpecial variable typesSummary of Data TypeSummary of Data Type – cont.Assignment OperatorsUnary OperatorsArithmetic OperatorsOperator PrecedenceVariable•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 memoryage20Why 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 memoryv1 50v2 30sum 80How – 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 declarationDeclaration 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;}Basic Types of Variables •int –integer•float –single-precision floating point •double–double-precision floating point •char –one byte character •_Bool–BooleanVariable Names•Contain one or more characters, –A-Z, a-z –0-9 –_•Must start with a non-digit character–A-Z, a-z–_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•4yiint: integer Variables•For integral values only10, -5, 1000–no fraction10.2 –no commas12,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 othersOperations for int type•Declarationint 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 truncatedDisplay Integer Variables – I#include <stdio.h> Preprocessor: interact with input/output of your computer Start point of the program int main() Start and finish of function{ } Printing results int value1, value2, sum;Finish and return value 0 return 0; value1 = 50; value2 = 30; sum = value1 + value2; printf(“The sum of 50 and 30 is %i\n“, sum); Declear VariablesDefine ValuesSummation Print the value of an integer variableDisplay 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);int: integer Variables – Special Case IStarting with digit “0”–Octal notation. Base 8, not 10•0,1,2,3,4,5,6,70177 = 1*64 + 7*8 + 7 = 1270256 = ?–Display•%i – print out the decimal value •%o – print out the octal value •%#o – print out the octal value, starting with 0int: integer Variables – Special Case IIStarting with digit “0x”–Hexadecimal notation. Based 16, not 10•0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F0x177 = 1*256 + 7*16 + 7 = 3750xAF = ?0x2AF = ?–Display•%i – print out the decimal value •%x – print out the hexadecimal value •%#x – print out the hexadecimal value, starting with 0Example - 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);}Example – II#include <stdio.h>int main(){ int a, b, c, f; a = 10; b = 20; c = 0177; 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);}float: single-precision Variables•For values containing decimal3., 125.8, -0.1–Scientific notation2.25e-3 = 2.25 * 10-3 = 0.00225•Use e or E for exponent–no commasfloat Variables - II•Ranges –IEEE floating-point standard•e = 8, f = 23•±3.4×1038float 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 formatOperations for float type•Declarationfloat 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 truncatedExample – 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);}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 floatchar: character Variables•For single character–Enclosing the character within a pair of ‘ ’•‘a’•‘;’•‘P’•‘\n’•‘1’•Display–%cExample – 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);}_Bool: Boolean Type•For boolean–0/1 –Normally means false/true •Display–%iFinish the Code#include <stdio.h>int main(void){ int integerVar = 100; float floatingVar = 331.79; double doubleVar = 8.44e+11;


View Full Document

UF CGS 3460 - Variable

Download Variable
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Variable and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Variable 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?