0% found this document useful (0 votes)
4 views

206 Chapter five

Chapter Five of CSC 206 covers the basics of C++, focusing on tokens such as keywords, identifiers, constants, strings, and operators. It details various C++ keywords, their predefined meanings, and provides examples of their usage. Additionally, the chapter discusses data types, including user-defined, built-in, and derived types, along with type modifiers and symbolic constants.

Uploaded by

gaiyaisaac123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

206 Chapter five

Chapter Five of CSC 206 covers the basics of C++, focusing on tokens such as keywords, identifiers, constants, strings, and operators. It details various C++ keywords, their predefined meanings, and provides examples of their usage. Additionally, the chapter discusses data types, including user-defined, built-in, and derived types, along with type modifiers and symbolic constants.

Uploaded by

gaiyaisaac123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

CSC 206 : Chapter Five

Basics of C++
C++ Token
Tokens are the smallest individual units in program.
The following are the different types of Tokens available in C++
i. Keywords
ii. Identifiers
iii. Constants
iv. Strings
v. Operators

1 Keywords:
Keywords are reserved words that have predefined meanings and can not be used for any other purpose,
such as naming a variable or function. They serve as building blocks of the language and form the
foundation on which the code is written.
C++ Keywords
1. Auto
This keyword automatically identifies the data type of a variable based on its initializer
Example:
using namespace std;
int main() {
auto number = 42;
cout << "The number is: " << number << endl;
return 0;
}
Output:
The number is: 42
2. Default
The default keyword is used to specify default behavior, whether it's related to constructors, function
arguments, or switch cases.
Example:
auto number = 42; // automatically deduces the type of `number` as `int`
Output:
Identifies the number value as 42.
3. Break
This keyword exits a loop or switches a case statement.
Example:
#include <iostream>
int main() {
for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
}
}
return 0;
}
Output:
Exits the loop when `i` is equal to 5
4. Case
This keyword marks a particular case in a switch statement.
Example:
#include <iostream>
int main() {
int day = 3;
switch (day) {
case 1:
std::cout << "Monday";
break;
case 2:
std::cout << "Tuesday";=
break;
case 3:
std::cout << "Wednesday";
break;
default:
std::cout << "Invalid day";
break;
}
return 0;
}
Output:
Wednesday
5. Char
This keyword represents a character type (1 byte).
Example:
#include <iostream>
int main() {
char grade = 'A';
return 0;
}
Output:
This assigns the value “A” to the variable “grade”
6. Const
This keyword defines a variable that cannot be changed after initialization.
Example:
#include <iostream>
const int MAX_VALUE = 100;
int main() {
std::cout << "The maximum value is: " << MAX_VALUE << std::endl;
return 0;
}
Output:
The maximum value is: 100
7. Continue
This keyword skips the current iteration of a loop and proceeds to the next one.
Example:
#include <iostream>
int main() {
for (int i = 0; i < 5; i++) {
if (i == 2) {
continue;
}
std::cout << i << " ";
}
std::cout << std::endl;
return 0;
}
Output:
0134
8. Double
This keyword represents a double-precision floating-point number.
Example:
#include <iostream>
int main() {
double pi = 3.14159;
std::cout << "The value of pi is: " << pi << std::endl;
return 0;
}
Output:
The value of pi is: 3.14159
9. Else
This keyword specifies an alternative to the if statement.
Example:
#include <iostream>
int main() {
int age = 20;
if (age >= 18) {
std::cout << "You are an adult." << std::endl;
} else {
std::cout << "You are not yet an adult." << std::endl;
}
return 0;
}
Output:
Since the input age is 20, which is greater than 18. The output for this would read “You are an adult.”
10. Enum
This keyword is used to define an enumerated data type.
Example:
#include <iostream>
enum Color {
RED,
GREEN,
BLUE
};
int main() {
Color selectedColor = GREEN;
if (selectedColor == RED) {
std::cout << "The selected color is RED." << std::endl;
} else if (selectedColor == GREEN) {
std::cout << "The selected color is GREEN." << std::endl;
} else if (selectedColor == BLUE) {
std::cout << "The selected color is BLUE." << std::endl;
} else {
std::cout << "Invalid color selection." << std::endl;
}
return 0;
}
Output:
The selected color is GREEN.
11. Float
This keyword represents a single-precision floating-point number.
Example:
#include <iostream>
int main() {
float price = 9.99f;
std::cout << "The price is: " << price << std::endl;
return 0;
}
Output:
The price is 9.99
12. For
This keyword creates a loop that iterates over a defined range.
Example:
#include <iostream>
int main() {
for (int i = 0; i < 5; i++) {
std::cout << i << " ";
}
return 0;
}
Output:
01234
13. If
This keyword creates a conditional statement that evaluates whether a condition is true or false.
Example:
#include <iostream>
int main() {
int x = 10;
if (x > 0) {
std::cout << "Positive Number" << std::endl;
}
return 0;
}
Output:
Positive Number
14. Int
This keyword represents an integer type (typically 2 or 4 bytes).
Example:
#include <iostream>
int main() {
int age = 25;
std::cout << "Age: " << age << std::endl;
return 0;
}
Output:
Age: 25
15. Long
This keyword represents a long integer type (typically 4 or 8 bytes).
Example:
#include <iostream>
int main() {
long population = 8000000000;
std::cout << "World population: " << population << std::endl;
return 0;
}
Output:
World population: 8000000000
16. Namespace
This keyword creates a namespace that can contain variables and functions.
Example:
#include <iostream>
using namespace std; // imports the standard namespace
int main() {
cout << "Hello, World!";
return 0;
}
Output:
Hello, World!
17. New
This keyword dynamically allocates memory during program execution.
Example:
#include <iostream>
int main() {
int* ptr = new int;
*ptr = 42;
std::cout << "Value: " << *ptr << std::endl;
delete ptr;
return 0;
}
Output:
Value: 42
18. Return
This keyword exits a function and optionally returns a value.
Example:
#include <iostream>
int add(int a, int b) {
return a + b; // returns the sum of `a` and `b`
}
int main() {
int result = add(5, 7);
std::cout << "Result: " << result << std::endl;
return 0;
}
Output:
Result: 12
19. Short
This keyword represents a short integer type (typically 2 bytes).
Example:
#include <iostream>
int main() {
short distance = 100;
std::cout << "Distance: " << distance << std::endl;
return 0;
}
Output:
Distance: 100
20. Signed
This keyword specifies that a variable can hold negative and positive values.
Example:
#include <iostream>
int main() {
signed int temperature = -10;
std::cout << "Temperature: " << temperature << std::endl;
return 0;
}
Output:
Temperature: -10
21. Static
This keyword specifies that a variable or function belongs to the class and is not an instance.
Example:
#include <iostream>
int main()
{
int size = sizeof(int);
std::cout << "Size of int: " << size << " bytes" << std::endl;
return 0;
}
Output:
Size of int: 4 bytes
22. Struct
This keyword defines a composite data type, similar to a class.
Example:
#include <iostream>
#include <string>
struct Person {
std::string name;
int age;
};
int main() {
Person person1;
Person1.name = "John";
person1.age = 25;
std::cout << "Person's Name: " << person1.name << std::endl;
std::cout << "Person's Age: " << person1.age << std::endl;
return 0;
}
Output:
Person’s Name: John
Person’s Age: 25
23. Switch
This keyword creates a switch statement that evaluates an expression against a range of possible values.
Example:
#include <iostream>
int main() {
int day = 3;
switch (day) {
case 1:
std::cout << "Monday";
break;
case 2:
std::cout << "Tuesday";
break;
case 3:
std::cout << "Wednesday";
break;
default:
std::cout << "Invalid day";
}
return 0;
}
Output:
Wednesday
24. Typedef
This keyword creates aliases for data types.
Example:
#include <iostream>
typedef unsigned long long ULL; // Creates an alias for the data type unsigned long long
int main()
{
ULL num = 1234567890ULL; // Uses the typedef alias `ULL` for the variable `num`
std::cout<< "Value of num:" <<num<<std::endl;
return 0;
}
Output:
Value of num: 1234567890
25. Unsigned
This keyword specifies that a variable can hold only positive values.
Example:
#include <iostream>
int main()
{
unsigned int count = 100; // Unsigned integer data type to store non-negative whole numbers
std::cout << "Count:" <<count<<std::endl;
return 0;
}
Output:
Count: 100
26. Sizeof
This keyword returns the size in bytes of a data type or variable.
Example:
#include <iostream>
int main()
{
int size = sizeof(int); // Retrieves the size (in bytes) of the int data type
std::cout << "Size of int: " << size << " bytes" << std::endl;
return 0;
}
Output:
Size of int: 4 bytes
Identifier
Identifiers are the unique names given to variables, classes, functions, or other entities by the
programmer.
For example, in the below statement,
int num = 11;
num is an identifier.
Rules to Name of an Identifier in C++
We can use any word as an identifier as long as it follows the following rules:
1. An identifier can consist of letters (A-Z or a-z), digits (0-9), and underscores (_). Special
characters and spaces are not allowed.
2. An identifier can only begin with a letter or an underscore only.
3. C++ has reserved keywords that cannot be used as identifiers since they have predefined
meanings in the language. For example, int cannot be used as an identifier as it has already some
predefined meaning in C++. Attempting to use these as identifiers will result in a compilation
error.
4. Identifier must be unique in its namespace.
Additionally, C++ is a case-sensitive language so the identifier such as Num and num are treated as
different.
Basic C++ Data Types
C++ provides a variety of data types to handle different kinds of data. Here’s a quick overview of the
fundamental data types in C++:

C++ Data Types

User Defined Built-in types Derived types


type
int char float double

User-Defined Data Types


i. struct: A user-defined type that groups together different data types.
ii. class: Similar to struct, but with additional features like encapsulation, inheritance, and
polymorphism.
iii. union: A data structure where different members share the same memory location.
iv. enum: A user-defined type that consists of a set of named integer constants.

Built-in Types
 int: Represents integer values. Can be modified with signed, unsigned, short, and long to adjust
the range.
o int (typically 4 bytes)

o short (typically 2 bytes)

o long (typically 4 or 8 bytes depending on the system)

o long long (typically 8 bytes)

 char: Represents single characters. It typically occupies 1 byte.


o char (signed by default, but can be explicitly signed or unsigned)

 float: Represents floating-point numbers with single precision. Typically 4 bytes.


 double: Represents floating-point numbers with double precision. Typically 8 bytes.
 long double: Represents floating-point numbers with extended precision. Size can vary (often 8,
12, or 16 bytes).
void: The type void normally used for:
1) To specify the return type of function when it is not returning any value.
2) To indicate an empty argument list to a function.
Derived Data Types
 Arrays: Collection of elements of the same type.
 Pointers: Variables that store memory addresses of other variables.
 References: Aliases for other variables.
 Functions: Can be used as data types to represent functions themselves.
C++ compilers support all the built in types. With the exception of void the basic datatypes may have
several modifiers preceding them to serve the needs of various situations. The modifiers signed, unsigned,
long and short may applied to character and integer basic data types. However the modifier long may also
be applied to double.

Type Modifiers
 signed: Indicates that the data type can hold both positive and negative values.
 unsigned: Indicates that the data type can only hold non-negative values.
 short: Reduces the size of the data type (usually 2 bytes).
 long: Increases the size of the data type (varies by implementation).
Data types in C++ can be classified under various categories.
TYPES BYTES RANGE
char 1 -128 to -127
unsigned 1 0 to 265
signed cha 1 -128 to 127
int 2 -32768 to 327568
unsigned int 2 0 to 65535
signed int 2 -32768 to 32768
short int 2 -32768 to 32768
long int 4 -2147483648 to 2147483648
signed long int 4 -2147483648 to 2147483648
unsigned long int 4 0 to 4294967295
float 4 3.4E-38 to 3,4E+38
double 8 1.7E-308 to 1.7E+308
long double 10 3.4E-4932 TO 1.1E+4932

Symbolic Constant
In C++, a symbolic constant is a way to give a meaningful name to a constant value, making your code
more readable and maintainable. Symbolic constants are typically used to represent values that do not
change throughout the execution of a program, such as mathematical constants, configuration values, or
other fixed parameters.
In C++, you can create symbolic constants in a couple of different ways. Here are two common methods:
1. Using const Keyword
You can use the const keyword to define a constant variable. This variable’s value cannot be changed
once it is initialized.
 Advantages: const is simple to use and directly associates the constant with a variable type.
 Usage: You can use MAX_SIZE like any other variable, but its value remains unchanged
throughout the program.
2. Using constexpr Keyword
The constexpr keyword indicates that the value of the constant is evaluated at compile time. It’s a more
modern approach compared to const and is often used for values that are known at compile time.
Advantages: constexpr ensures that the value is constant and available at compile time, which can be
beneficial for optimizations. It can also be used in contexts where compile-time constants are required,
like array sizes.
Usage: constexpr can be used similarly to const, but it has additional constraints and advantages
regarding compile-time evaluation.

You might also like