Muhammad Rizwan
Muhammad Rizwan
Muhammad Rizwan
A Compiler translates the source code to target code. The target code may be the machine
language for a particular platform or embedded device.
As source code is an input to the compiler by a programmer writer.A compiler just compiles the
source code and gives us the output. The difference is that source code is the input through
programmer while compiled code is the output by the compiler.
Q3: What tool does a programmer use to produce C++ source code?
The tools used by the programmers to produce C++ source code are: 1. Reprocessor 2. Compiler
3.Linkers
Q4: What tool(s) does a programmer use to convert C++ source code into executable machine
code?
The tool used to convert C++ source code into executable machine code is a Compiler.
A program called a linker combines the programmer’s compiled code and the library code.
Q6: Does the linker deal with files containing source code or machine language code?
Yes,It deals with the files containing source code or machine language code.
Preprocessor adds or modifies the contents of source file before the compiler begins processing
the code.
Q8: List several advantages of developing software in a higher-level language has over
developing software in machine language?
High level languages are programmer friendly and it provides higher level of abstraction. It is
machine independent language, very easy to learn, less error prone, easy to find and debug
errors.
Q10: Name a popular C++ IDE is used by programmers developing for Microsoft Windows?
Q 11:Name a popular C++ IDE is used by programmers developing for Apple macOS?
Q1: What preprocessor directive is necessary to use statements with the std::cout printing
stream object?
The std:: before cout is required when we use names that we've brought into the program by
the preprocessor directive #include.
Q2: What statement allows the short name cout to be used instead of std::cout?
The statement Using allows us to use a shorter name for the std::cout printing object. We can
omit the std:: prefix and use the shorter name, cout.
The name std stands for “standard” and the std prefix indicates that cout is a part of collection
of names called the standard namespace.
The symbols << make up the insertion operator. You can think of the message to be printed as
being “inserted” into the cout object.
Q7: Write a C++ program that prints your name in the console window.
#include <iostream>
int main () {
}
Q8: Write a C++ program that prints your first and last name in the console window. Your first
name should appear on one line, and your last name appear on the next line?
#include <iostream>
int main () {
Q9: What other files must you distribute with your executable file so that your program will run
on a Windows PC without Visual Studio installed?
C++ programmers have two options for C++ development environments. One option involves a
command-line environment with a collection of independent tools and the other is IDE.
Q10: Can a single statement in C++ span multiple lines in the source code?
Q1: Will the following lines of code print the same thing? Explain why or why not.
Yes, they will print the same thing ,since 6 has no character type assigned to it in ASCII system.
Q2: Will the following lines of code print the same thing? Explain why or why not.
C++ provides integer-like types that exclude negative numbers. These types include the word
unsigned in their names, meaning they do not allow a negative sign.
Q8: What happens if you attempt to use a variable within a program, and that variable is not
declared?
If you failed to declare variable, then an ERROR will be thrown by the compiler. If you failed to
define a value to variable, then compiler will throw a warning mess. If
you are using any variable inside code, then declaring a variable is must.
Q9: What is wrong with the following statement that attempts to assign the value ten to
variable x?
10 = x;
An integer can’t be written before a variable, so an error will be generated in this case.
Q10: Once a variable has been properly declared and initialized can its value be changed?
Once declared, a particular variable cannot be redeclared in the same context. A variable may
not change its type during its lifetime.
Q10: What is another way to write the following declaration and initialization?
int x = 10;
#include <iostream>
int main() {
int x{10};
}
This alternate form is not commonly used for simple variables, but it necessary for initializing
more complicated kinds of variables called objects.
Q12: In C++ can you declare more than one variable in the same declaration statement? If so,
how?
Variables can be reassigned different values as needed, such as,
#include <iostream>
int main() {
int x;
x = 10;
std::cout << x << '\n';
x = 20;
std::cout << x << '\n';
x = 30;
std::cout << x << '\n';
}
int a;
int b;
int a;
int b;
does not mean a and b refer to the same box (memory location).
Q14: Classify each of the following as either a legal or illegal C++ identifier:
fred Legal
if Illegal
2x Illegal
-4 Illegal
sum_total Legal
sumTotal Legal
sum-total Illegal
sum total Illegal
sumtotal Legal
While Legal
x2 Legal
Private Legal
public Illegal
$16 Illegal
xTwo Legal
_static Illegal
_4 Illegal
___ Illegal
10% Illegal
a27834 Legal
wilma's Illegal
Q15: What can you do if a variable name you would like to use is the same as a reserved word?
When variables must be declared, the compiler can catch typographical errors that dynamically-
typed languages cannot detect. For example, consider the following section of code:
char While;
While = 1;
Q16: Why does C++ require programmers to declare a variable before using it? What are the
advantages of declaring variables?
Before they are used, all variables have to be declared. Declaring a variable means defining its
type, and optionally, setting an initial value (initializing the variable). Variables do not have to
be initialized (assigned a value) when they are declared, but it is often useful.
The Decimal, Double, and Float variable types are different in the way that they store the
values. Precision is the main difference where float is a single precision (32bit) floating point
data type, double is a double precision (64 bit) floating point data type and decimal is a 128-
bit floating point data type.
Q18: How can a programmer force a floating-point literal to be a float instead of a double?
To make a literal floating-point value a float, you must append an f or F to the number, as in
3.14f (The f or F suffix is used with literal values only; you cannot change a double variable into
a float variable by appending an f. Attempting to do so would change the name of the variable!)
It is expressed as 2.45e-5.
Q20: How can you ensure that a variable’s value can never be changed after its initialization?
Constants are declared like variables with the addition of the const keyword:
const double PI = 3.14159;
Once declared and initialized, a constant can be used like a variable in all but one way—a
constant may not be reassigned. It is illegal for a constant to appear on the left side of the
assignment operator (=) outside its
declaration statement. A subsequent statement like
PI = 2.5;
would cause the compiler to issue an error message.
Q21: How can you extend the range of int on some systems?
We can increase the range of int by adding longh and long long before int like:
long int;
Q22: How can you extend the range and precision of double on some systems?
We can extend the range and precision of double by adding long before double like:
long double;
It is a string literal.
It is a character literal.
If the numeric value is in between 0 to 127, then we can assign char value to the int value
according to ASCII system.
If the character is from the ASCII system, then we can assign int value to char value according to
the ASCII system.
int x;
x = 'A';
std::cout << x << '\n';
Q29: What is the difference between the character 'n' and the character '\n'?
Q31: Create an unscoped enumeration type that represents the days of the week.
#include <iostream>
using namespace std;
enum Days
{
monday,
tuesday,
wednesday,
thursday,
friday,
saturday,
sunday
};
}