Download presentation
Presentation is loading. Please wait.
1
User Interaction and Variables
Tarik Booker CS 242 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
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
aadzxvcna thjke456hftg3sd 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 scanf() function
12
Scanf A way to input data into the computer is to use scanf
This can “scan” in characters from the console Format similar to printf int x; scanf(“%d”, &x); This puts the value of %d into x Different types for % (called format specifiers) %d = decimal (integer) %c = character We will talk about others later…
13
Scanf (2) Note the & sign before the x int x; scanf(“%d”, &x);
This is a pointer reference Outside the scope of this class Passes to scanf the memory address of the variable x Keep the same format – don’t worry about pointers
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 Char Character Int Integer
Double Double Precision Floating-Point Void no 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 char uses signed version of char Unsigned char uses unsigned version of char
17
Data Types (3) Short 16-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
Long long integer (at least 32 bits) Long int Signed long Signed long int Unsigned long Unsigned long int
19
Data types (5) Long long 64-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!
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.