Variables & Constants
In C++, variables and constants are used to manage data in your programs. A variable is a label for a memory location that holds a value, which can change during the program’s execution. In contrast, a constant represents a value that remains fixed and cannot be modified after it is defined.
Variables
Section titled “Variables”In this course, you’ll work with several fundamental data types to store different kinds of values:
int: Used for integers (positive and negative whole numbers)double: Used for real numbers; it provides more precision and takes up more spacechar: Used to store a single character (any ASCII character) and is initialized with single quotes (‘a’)bool: Used for logical values (true or false)
Declaring Variables
Section titled “Declaring Variables”When declaring a variable in C++, you tell the compiler that variable_name will hold values of a specific
type (DataType). The variable name must be a valid identifier, meaning it should follow naming rules, such
as starting with a letter and containing no special characters except underscores. At this stage, no value
is assigned to the variable, so it contains garbage data until initialized.
int user_score;You can also declare multiple variables in one line using a comma (,) to separate each:
int first_var, second_var, third_var;Style Requirement
Section titled “Style Requirement”The styling convention for variables is lowercase lettering and an underscore to separate each word (snake_case).
int my_var;int myVar;cpplint driver.ccDone processing driver.ccInitializing Variables
Section titled “Initializing Variables”Initialize means assigning an initial value to a variable before using it. Initializing variables properly ensures your program avoids using uninitialized values, which can lead to unpredictable behavior.
int current_month = 11;The assignment operator (=) assigns a value from right to left, meaning the variable on the left-hand side
(LHS) is updated with the value on the right-hand side (RHS). You can use cascading assignments to assign
the same value to multiple variables in a single line. For example:
// Copyright 2024 CSCE2024#include <iostream>
int main() { int x, y, z; x = y = z = 10;
std::cout << x << " " << y << " " << z << std::endl;
return 0;}g++ driver.cc./a.out10 10 10Constants
Section titled “Constants”In C++, placing the const qualifier in front of a variable’s declaration ensures that
its value cannot be changed during program execution. Constants are particularly useful
for defining fixed values like mathematical constants or configuration settings.
Style Requirement
Section titled “Style Requirement”The naming convention for constants is starting with a lowercase “k”, followed by CamelCasing the rest of the name (kCamelCase);
const double kPie = 3.14;const double PIE = 3.14;cpplint driver.ccDone processing driver.ccInitializing With Input
Section titled “Initializing With Input”The cin object is used to take input from the user in C++. Just like cout, it is part of
the std namespace and requires the inclusion of the <iostream> header file at the top of
your program. The cin object works alongside the stream extraction operator (>>) to store
user input in variables, making it a key tool for interactive programs.
// Copyright 2024 CSCE2024#include <iostream>
using std::cin;using std::cout;using std::endl;
int main() { cout << "What is your favorite number: "; int fave_number; cin >> fave_number; cout << "Favorite Number: " << fave_number << endl;
return 0;}g++ driver.cc./a.outWhat is your favorite number: 10Favorite Number: 10Parsing Input
Section titled “Parsing Input”The cin object in C++ allows you to parse structured input by using different data types. The following shows how you can use variables of different data types to parse a date:
// Copyright 2024 CSCE2024#include <iostream>
using std::cin;using std::cout;using std::endl;
int main() { cout << "Enter current date (mm/dd/yyyy):" << endl; int month = 0, day = 0, year = 0; char first_delimiter = '-'; char second_delimiter = first_delimiter;
cin >> month >> first_delimiter >> day >> second_delimiter >> year; cout << month << first_delimiter << day << second_delimiter << year << endl;
return 0;}g++ driver.cc./a.outEnter current date (mm/dd/yyyy):11/20/202411/20/2024