CSC 215 Lecture Variables Types and Expressions Outline

CSC 215 Lecture Variables, Types and Expressions

Outline ❖ Variables ❖ Datatypes ○ ○ Basic data types Derived data types ❖ Expressions ○ ○ Operators: arithmetic, relational, logical, assignment, inc-/dec- rement, bitwise Evaluation ❖ Formatted input/output ❖ Macros

Variables ❏ A variable is nothing but a name given to a storage area that our programs can manipulate. ❏ Each variable in C has a specific type, which determines: • The size and layout of the variables in memory; • The range of values that can be store within the memory; • The set of operations that can be applied to the variable.

Variables ❏ Named values ○ Naming rules: ■ Made up of letters, digits and the underscore character ‘_’ ■ Must not begin with a digit ■ Must not be a special keyword ■ Must begin with either a letter or an underscore auto default ❏ Upper and lowercase letters are distinct because C is case-sensitive. float register struct volatile break char continue do else extern for if long return signed static switch union void while case const double enum goto int short sizeof typedef unsigned

Variables ❏ Variable declaration: ○ ○ ○ Must declare variables before use Variable declaration: int n; float phi; ■ int - integer data type ■ float - floating-point data type Many other types ❏ Variable initialization: ○ ○ Uninitialized variable assumes a default value ○ ○ Can also be initialized at declaration: float phi = 1. 6180339887; Can declare/initialize multiple variables at once: int a, b, c = 0, d = 4; Variables initialized via assignment operator: n = 3;

Basic Data Types ❏ Four types: char int float double ❏ Modifiers: signed unsigned short long ❏Combinations: What about boolean? Strings?

Boolean? ● No special boolean type ● Evaluating boolean and logical expressions: ○ ○ results in integer 1 if the logic is true Results in 0 if the logic is false ● Interpretation of integer as boolean: ○ ○ 0 is perceived as false any non-zero value is perceived as true

Strings ? ● Strings are stored as character array ● Null-terminated (last character in array is ’ ’: null character) char course[7] = {'C', 'S', 'C', '2', '1', '5', '