Slide 6 Variable names -Identifiers An identifier is a name for variables constants functions etc It consists of a letter(including the underscore)followed by any sequence of letters digits or underscores letter underscore() Names are case-sensitive. The following are unique identifiers Hello, hello, whoami, whoAMi, WhoAmI Names cannot have special characters in them e.g., X=Y, J-20,#007, etc are invalid identifiers C++ keywords cannot be used as identifiers Choose identifiers that are meaningful and easy to remember
Slide 6 Variable names --Identifiers • An identifier is a name for variables, constants, functions, etc. • It consists of a letter (including the underscore) followed by any sequence of letters, digits or underscores • Names are case-sensitive. The following are unique identifiers: Hello, hello, whoami, whoAMI, WhoAmI • Names cannot have special characters in them e.g., X=Y, J-20, #007, etc. are invalid identifiers. • C++ keywords cannot be used as identifiers. • Choose identifiers that are meaningful and easy to remember
Slide 7 Keywords(reserved words) Reserved words have a special meaning in C++ The list of reserved words asm auto, bool, break, case, catch, char class, const, continue, default, delete do double, else enum, extern, float, for friend, goto, ifr include, inline, int, long, namespace, new, operator, private, protected, public, register, return, short, signed sizeof, static, struct, switch, template this, throw, try, typedef, union, unsigned, using, virtual, void, volatile, while
Slide 7 Keywords (Reserved words) • Reserved words have a special meaning in C++. • The list of reserved words: asm, auto, bool, break, case, catch, char, class, const, continue, default, delete, do, double, else, enum, extern, float, for, friend, goto, if, include, inline, int, long, namespace, new, operator, private, protected, public, register, return, short, signed, sizeof, static, struct, switch, template, this, throw, try, typedef, union, unsigned, using, virtual, void, volatile, while
Slide 8 Variable declarations Variable declaration syntax <type> <identifier Examples int nickel; A variable must be declared before it can be used! int main(t 5;// illegal: x was not declared what is syntax, semantics?
Slide 8 Variable Declarations Variable declaration syntax: <type> <identifier>; Examples: int nickel; int penny; A variable must be declared before it can be used! int main(){ x = 5; // illegal: x was not declared } what is syntax, semantics?
Slide 9 a variable can be initialized in a declaration int x=3 Several variables of the same type can be declared in the same declaration( though it is better to put them on separate lines) int height, width; A variable must have only one type. For example, a variable of the type int can only hold integer values
Slide 9 • A variable can be initialized in a declaration: int x = 3; • Several variables of the same type can be declared in the same declaration (though it is better to put them on separate lines): int height, width; • A variable must have only one type. For example, a variable of the type int can only hold integer values
Slide 10 Variable initialization a variable can be initialized in a declaration int x=3 Always initialize your variables, which are not always automatically initialized Different ways of initialization int x=3 int x(3)i
Slide 10 Variable initialization • A variable can be initialized in a declaration: int x = 3; • Always initialize your variables, which are not always automatically initialized!!! • Different ways of initialization int x = 3; int x(3);