learncpp Ch 1-6 & bit manipulation Flashcards
basics, functions, files, debugging, data types, operators, bit manipulation, scope, duration, and linkage
Statement
type of instruction that causes the program to perform some action. Statements are often terminated by a semicolon.
function
collection of statements that execute sequentially. Every C++ program must include a special function named main. When you run your program, execution starts at the top of the main function.
syntax
he rules that govern how elements of the C++ language are constructed
syntax error
occurs when you violate the grammatical rules of the language.
comments
allow the programmer to leave notes in the code. C++ supports two types of comments. Line comments start with a // and run to the end of the line. Block comments start with a /* and go to the paired */ symbol. Don’t nest block comments.
You can use comments to temporarily disable lines or sections of code. This is called commenting out your code.
data
any information that can be moved, processed, or stored by a computer.
value
A single piece of data, stored somewhere in memory
variable
named piece of memory that we can use to store values.
identifier
variable’s name
definition statement
In order to create a variable, we use a statement
instantiated
When the program is run, each defined variable is ___________, which means it is assigned a memory address.
data type
tells the compiler how to interpret a piece of data into a meaningful value.
integer
number that can be written without a fractional component, such as 4, 27, 0, -2, or -12.
copy assignment
(via operator=) can be used to assign an already created variable a value.
initialization
can be used to give a variable a value at the point of creation. C++ supports 3 types of initialization:
Copy initialization.
Direct initialization (also called parenthesis initialization).
List initialization (also called uniform initialization or brace initialization).
You should prefer brace initialization over the other initialization forms, and prefer initialization over assignment.
Although you can define multiple variables in a single statement, it’s better to define and initialize each variable on its own line, in a separate statement.
std::cout «
allow us to output an expression to the console as text.
std::endl
outputs a newline character, forcing the console cursor to move to the next line.
std::cin»_space;
allow us to get a value from the keyboard.
uninitializaed variable
A variable that has not been given a value
Trying to get the value of an uninitialized variable will result in _____ ______, which can manifest in any number of ways.
undefined behavior
keywords
These have special meaning within the language and may not be used as variable names.
literal constant
fixed value inserted directly into the source code. Examples are 5 and “Hello world!”.
operation
mathematical process involving zero or more input values, called operands.
The specific operation to be performed is denoted by the provided ______. The result of an operation produces an output value.
operator
unary
operators take one operand.
binary
operators take two operands, often called left and right.
Ternary
operators take three operands
Nullary
operators take zero operands.
expression
combination of literals, variables, operators, and function calls that are evaluated to produce a single output value.
An expression is a combination of literals, variables, operators, and function calls that are evaluated to produce a single output value. The calculation of this output value is called
evaluation
An expression is a combination of literals, variables, operators, and function calls that are evaluated to produce a single output value. The calculation of this output value is called evaluation. The value produced is the ______ of the expression.
result
expression statement
an expression that has been turned into a statement by placing a semicolon at the end of the expression.
When writing programs, add a few lines or a function, compile, resolve any errors, and make sure it works. Don’t wait until you’ve written an entire program before compiling it for the first time!
Focus on getting your code working. Once you are sure you are going to keep some bit of code, then you can spend time removing (or commenting out) temporary/debugging code, adding comments, handling error cases, formatting your code, ensuring best practices are followed, removing redundant logic, etc…
First-draft programs are often messy and imperfect. Most code requires cleanup and refinement to get to great!
What is the difference between initialization and assignment?
Initialization provides a variable with an initial value (at the point of creation). Assignment gives a variable a new value after the variable has already been defined.
When does undefined behavior occur? What are the consequences of undefined behavior?
Undefined behavior occurs when the programmer does something that is ill-specified by the C++ language. The consequences could be almost anything, from crashing to producing the wrong answer to working correctly anyway.
Write a program that asks the user to enter a number, and then enter a second number. The program should tell the user what the result of adding and subtracting the two numbers is.
The output of the program should match the following (assuming inputs of 6 and 4):
Enter an integer: 6
Enter another integer: 4
6 + 4 is 10.
6 - 4 is 2.
include <iostream></iostream>
int main()
{
std::cout «_space;“Enter an integer: “;
int x{};
std::cin»_space; x;
std::cout << "Enter another integer: "; int y{}; std::cin >> y; std::cout << x << " + " << y << " is " << x + y << ".\n"; std::cout << x << " - " << y << " is " << x - y << ".\n"; return 0; }
function
a reusable sequence of statements designed to do a particular job
user-defined functions
Functions you write yourself
function call
an expression that tells the CPU to execute a function.
caller
The function initiating the function call
calle or called function
function being called
Do not forget to include parenthesis when making a function call.
The curly braces and statements in a function definition are called the
function body
value-returning function
A function that returns a value
return type
indicates the type of value that the function will return.
return statement
determines the specific return value that is returned to the caller.
A return value is copied from the function back to the caller – this process is called return by value. Failure to return a value from a non-void function will result in
undefined behavior
status code
The return value from function main. It tells the operating system (and any other programs that called yours) whether your program executed successfully or not. By consensus a return value of 0 means success, and a non-zero return value means failure.
DRY programming
“don’t repeat yourself”. Make use of variables and functions to remove redundant code.
Functions with a return type of void do not return a value to the _____
caller
void function or non-value returning function
A function that does not return a value.
Void functions can’t be called where a value is required.
early return
A return statement that is not the last statement in a function. Such a statement causes the function to return to the caller immediately.
function parameter
variable used in a function where the value is provided by the caller of the function
argument
the specific value passed from the caller to the function.
pass by value
When an argument is copied into the parameter
local variables
Function parameters and variables defined inside the function body
lifetime
The time in which a variable exists
runtime
Variables are created and destroyed at, _______. which is when the program is running.
A variable’s scope determines where it can be seen and used. When a variable can be seen an used, we say it is in scope.
When it can not be seen, it can not be used, and we say it is out of scope.
Scope is a ____-____ property, meaning it is enforced at compile time.
compile-time
forward declaration
allows us to tell the compiler about the existence of an identifier before actually defining the identifier.
To write a forward declaration for a function, we use a function prototype
which includes the function’s return type, name, and parameters, but no function body, followed by a semicolon.
definition
actually implements (for functions and types) or instantiates (for variables) an identifier.
declaration
is a statement that tells the compiler about the existence of the identifier. In C++, all definitions serve as declarations.
pure declarations
declarations that are not also definitions (such as function prototypes).
Most non-trivial programs contain ____ files.
multiple
When two identifiers are introduced into the same program in a way that the compiler or linker can’t tell them apart, the compiler or linker will error due to a _____ _____
naming collision
namespace
guarantees that all identifiers within the namespace are unique. The std namespace is one such namespace.
proprocessor
process that runs on the code before it is compiled
directives
special instructions to the preprocessor. Directives start with a # symbol and end with a newline.
macro
a rule that defines how input text is converted to a replacement output text.
header files
files designed to propagate declarations to code files. When using the #include directive, the #include directive is replaced by the contents of the included file. When including headers, use angled brackets when including system headers (e.g. those in the C++ standard library), and use double quotes when including user-defined headers (the ones you write). When including system headers, include the versions with no .h extension if they exist.
header guards
prevent the contents of a header from being included more than once into a given code file. They do not prevent the contents of a header from being included into multiple different code files.
syntax error
error that occurs when you write a statement that is not valid according to the grammar of the C++ language. The compiler will catch these.