Lab 03. Variables Output Formats
Lab 03. Variables Output Formats
The aim of this lab is to familiarize student with the programming environment and cover
some other basics of C++ programming.
What can you name your variables? In general, variable names can be composed of letters,
numbers, and underscores (_). However, C++ reserves certain keywords which have special
meaning to the language, and you are not allowed to use any of these keywords as variables
names. Some examples of C++ keywords are int, for, else, and class. You can, however, use
keywords in the middle of a variable name, such as "foreign" or "classical".
Variable Types
A variable type is a description of the kind of information a variable will store. Following are
some of the Types that a variable can have:
Declaring Variables
Declaring a variable in C++ is simple. Let's say you want to declare a variable of type int
called myAge. That is to say, the variable myAge will store an integer. In C/C++, this is
NUST Institute of Civil Engineering (NICE-SCEE)
National University of Sciences & Technology (NUST)
written: int myAge;
All this does is tell the computer that you plan to use an integer, and that the integer's name
is myAge. In some languages, variables are initialized to 0 - that is, a variable's initial value
will be 0. This is not true of C++! Sometimes your variables will be initialized to 0, but
sometimes they will be initialized with garbage. As you might anticipate, this can cause some
nasty bugs. Hence, it is always a good idea to initialize your variables with some value. If you
don't know what a variable's initial value should be, initialize it to 0. Initializing a variable is
easy.
Let’s write a program that stores your age in a variable and outputs “My age is 21". The first
line of the main function initializes myAge by assigning it a value immediately.
Example 1:
Run below given program and display output.
To print out the value of some variable, you need to embed a format specifier in your text
string and pass extra arguments to the cout function. An example:
cout<< “My Age is ” <<myAge<< “Years”;
This statement prints out the value of the variable myAge. Note that the value of myAge is
passed to the cout function.
That's all there is to it! By the way, the equals sign ("=") is called an operator which will be
discussed in more detail later. Also, note the way we used cout to output the value of a
variable.
For formatted output operations, cout is used together with the insertion operator, which is
written as “<<” (i.e., two "less than" signs).
The << operator inserts the data that follows it into the stream that precedes it. In the
examples above, it inserted the literal string Output sentence, the number 120, and the value
of variable x into the standard output stream cout. Notice that the sentence in the first
statement is enclosed in double quotes (") because it is a string literal, while in the last one, x
is not. The double quoting is what makes the difference; when the text is enclosed between
them, the text is printed literally; when they are not, the text is interpreted as the identifier
of a variable, and its value is printed instead. For example, these two sentences have very
different results:
cout << "This " << " is a " << "single C++ statement";
This last statement would print the text This is a single C++ statement. Chaining insertions is
especially useful to mix literals and variables in a single statement:
cout <<"I am "<< age <<" years old and my zipcode is "<<zipcode;
Assuming the age variable contains the value 24 and the zipcode variable contains 90064, the
output of the previous statement would be:
What cout does not do automatically is add line breaks at the end, unless instructed to do so.
For example, take the following two statements inserting into cout:
cout << "This is a sentence.";
cout << "This is another sentence.";
The output would be in a single line, without any line breaks in between. Something like:
This is a sentence.This is another sentence.
To insert a line break, a new-line keyword shall be inserted at the exact position the line
should be broken. In C++, the endl manipulator can be used to break lines. For example:
1 cout<<"First sentence."<<endl;
2 cout<<"Second sentence."<<endl;
First sentence.
Second sentence.
NUST Institute of Civil Engineering (NICE-SCEE)
National University of Sciences & Technology (NUST)
Example 2:
Display output of the following code:
C++ Comments
A comment is text that the compiler ignores but that is useful for programmers. Comments
are normally used to annotate code for future reference. The compiler treats them as white
space. You can use comments in testing to make certain lines of code inactive.
The comment characters (/*, */, and //) have no special meaning within a character
constant, string literal, or comment.
Example 3:
Run below given program and display the output
NUST Institute of Civil Engineering (NICE-SCEE)
National University of Sciences & Technology (NUST)
In this example you saw various ways of declaring variables of various data types and how to
write comments in C/C++ programs.
Task 1:
Write a program which takes a number and prints its multiplication table on the screen. E.g.
if the number is 2, the output should be:
Task 2:
Write a program that computes the area of a circle with radius r, using the formula a=πr 2.
Take the value of r =2, π as a named constant for use in the program. Hint: You’ll need to
multiple r by itself to compute r2
Task 3:
Write a program that computes the circumference of a circle with radius r = 4, using the
formula C=2πr.