Presentation is loading. Please wait.

Presentation is loading. Please wait.

USER INTERACTION AND VARIABLES Tarik Booker CS 290 California State University, Los Angeles.

Similar presentations


Presentation on theme: "USER INTERACTION AND VARIABLES Tarik Booker CS 290 California State University, Los Angeles."— Presentation transcript:

1 USER INTERACTION AND VARIABLES Tarik Booker CS 290 California State University, Los Angeles

2 What we will cover… Review Data Types Standard Input

3 Variables Can store values to be used (later) in a program Values can be changed Remember algebra: x + y = 34 y = 5 x =? Similar Concept in Programming

4 Variables (2) You must declare a variable in a program before you use it Format: datatype variableName; A variable can be of any name Follow identifier rules (later…) Descriptive names are best

5 Variables (3) Datatype variableName; Datatype variableName1, variableName2, …,variableName3; Datatype Specific classification of variables Different types / different memory sizes int, double (main ones) Discuss later

6 Variables (4) Variables are Case-Sensitive in C Uppercase and Lowercase Not the Same “Variable1” is not the same as “variable1”

7 Identifiers To name variables or functions Called identifiers Should obey rules: Consists of letters (upper/lower), digits, underscores Must start with a letter (alphabetic character) Must NOT start with a digit!!! Cannot be a reserved word (int, return, main, etc.) Can be up to 31 characters

8 Identifiers (2) Which are legal? helloworld aadzxvcna343546390thjke456hftg3sd HappyHappy 34joy56 $Dollarsand return mains #love Why / Why not?

9 Char There is another type of variable other than integers or floating-point Char Allows you to store a single character Uses ASCII American Standard Code for Information Interchange Each character is assigned to a particular number ASCII Table ‘A’ is 65 ‘a’ is 97 Lowercase is higher than Uppercase Add 32 to change an uppercase letter to lowercase Subtract 32 to change a lowercase letter to uppercase

10 ASCII Table

11 Reading in a Value from Standard In Before, we used a function to send information to Standard out What was it? Now we can get information from Standard in Let’s get a character from standard in Use the getchar() function Stores the result as an integer (the value of the character in ASCII)

12 getchar() How do we use getchar()? Declare a variable of character type Set the value of the character to the result of getchar() Ex: # include int main () { char c; printf("Enter character: "); c = getchar(); }

13 getchar() How do we display the character back? Use putchar() Ex: #include int main () { char c; printf("Enter character: "); c = getchar(); printf("Character entered: "); putchar(c); return(0); }

14 Data Types Char is considered a data type A data type is just the way information is stored in the program char, int, float, double, void (will discuss) are called primitive data types or primitives Other types of primitive data types

15 Data Types CharCharacter IntInteger DoubleDouble Precision Floating-Point Voidno type (or empty) Void Use this data type when no type is expected Primarily used for functions to specify not a return type Look at main

16 Data Types (2) C has many many different types of basic (or primitive) data types Different versions of char, int, long, double Char Signed charuses signed version of char Unsigned charuses unsigned version of char

17 Data Types (3) Short16-bit integer type Short int Signed short Signed short int Unsigned short Unsigned short int

18 Data types (4) Int Signed int Unsigned Unsigned int Longlong integer (at least 32 bits) Long int Signed long Signed long int Unsigned long Unsigned long int

19 Data types (5) Long long64-bit integer Long long int Signed long long Signed long long int

20 Data Types (6) Float Double Long double There are also other types: Boolean (next week) True/False

21 We will do in class work today! Let’s do some problems!


Download ppt "USER INTERACTION AND VARIABLES Tarik Booker CS 290 California State University, Los Angeles."

Similar presentations


Ads by Google