Which of the Following Requires That Only a Value of the Proper Type Can Be Stored Into a Variable?

C++ Variable Types


A variable provides the states with named storage that our programs can manipulate. Each variable in C++ has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be practical to the variable.

The proper noun of a variable tin exist composed of messages, digits, and the underscore character. It must begin with either a letter or an underscore. Upper and lowercase messages are distinct considering C++ is case-sensitive −

There are following basic types of variable in C++ as explained in last chapter −

Sr.No Type & Description
ane

bool

Stores either value true or false.

two

char

Typically a single octet (one byte). This is an integer type.

3

int

The most natural size of integer for the machine.

four

float

A single-precision floating betoken value.

v

double

A double-precision floating betoken value.

6

void

Represents the absenteeism of type.

7

wchar_t

A wide character type.

C++ also allows to define various other types of variables, which we will cover in subsequent capacity like Enumeration, Arrow, Assortment, Reference, Data structures, and Classes.

Following section will cover how to define, declare and use various types of variables.

Variable Definition in C++

A variable definition tells the compiler where and how much storage to create for the variable. A variable definition specifies a data type, and contains a list of one or more than variables of that type as follows −

blazon variable_list;        

Hither, blazon must be a valid C++ data blazon including char, w_char, int, float, double, bool or any user-divers object, etc., and variable_list may consist of one or more than identifier names separated by commas. Some valid declarations are shown hither −

int    i, j, 1000; char   c, ch; float  f, bacon; double d;        

The line int i, j, k; both declares and defines the variables i, j and thousand; which instructs the compiler to create variables named i, j and k of type int.

Variables can exist initialized (assigned an initial value) in their declaration. The initializer consists of an equal sign followed by a constant expression equally follows −

type variable_name = value;        

Some examples are −

extern int d = three, f = 5;    // announcement of d and f.  int d = iii, f = v;           // definition and initializing d and f.  byte z = 22;                // definition and initializes z.  char x = '10';               // the variable x has the value 'ten'.        

For definition without an initializer: variables with static storage elapsing are implicitly initialized with NULL (all bytes have the value 0); the initial value of all other variables is undefined.

Variable Declaration in C++

A variable annunciation provides balls to the compiler that in that location is one variable existing with the given type and name so that compiler proceed for further compilation without needing complete detail about the variable. A variable declaration has its meaning at the time of compilation only, compiler needs actual variable definition at the time of linking of the program.

A variable declaration is useful when you are using multiple files and you define your variable in 1 of the files which will be available at the time of linking of the program. Y'all will employ extern keyword to declare a variable at any place. Though yous can declare a variable multiple times in your C++ programme, only information technology tin can be defined merely in one case in a file, a role or a block of code.

Example

Try the following example where a variable has been declared at the summit, but information technology has been defined inside the main part −

#include <iostream> using namespace std;  // Variable annunciation: extern int a, b; extern int c; extern float f;    int main () {    // Variable definition:    int a, b;    int c;    float f;      // actual initialization    a = x;    b = 20;    c = a + b;      cout << c << endl ;     f = 70.0/iii.0;    cout << f << endl ;      return 0; }        

When the above code is compiled and executed, it produces the post-obit result −

30 23.3333        

Aforementioned concept applies on function declaration where yous provide a function name at the time of its declaration and its actual definition can exist given anywhere else. For example −

// part declaration int func(); int main() {    // function telephone call    int i = func(); }  // office definition int func() {    return 0; }        

Lvalues and Rvalues

In that location are two kinds of expressions in C++ −

  • lvalue − Expressions that refer to a retentiveness location is chosen "lvalue" expression. An lvalue may announced as either the left-hand or right-manus side of an assignment.

  • rvalue − The term rvalue refers to a data value that is stored at some address in memory. An rvalue is an expression that cannot have a value assigned to it which means an rvalue may appear on the correct- but not left-hand side of an assignment.

Variables are lvalues and so may announced on the left-hand side of an assignment. Numeric literals are rvalues and so may non exist assigned and can not appear on the left-hand side. Following is a valid statement −

int chiliad = 20;        

But the following is not a valid statement and would generate compile-time error −

10 = 20;        

Useful Video Courses


C++ Online Training

Video

Learn C++ Pointers with Visual Studio 2017

Video

Modern OpenGL C++ 3D Game &amp; 3D Rendering

Video

Cocos2d-x v3 C++ - Beginning Game Development

Video

C++ Development - The Complete Coding Guide

Video

Tic-Tac-Toe Clone - The Complete Cocos2d-x C++ Game Course

Video

richforelut.blogspot.com

Source: https://www.tutorialspoint.com/cplusplus/cpp_variable_types.htm

0 Response to "Which of the Following Requires That Only a Value of the Proper Type Can Be Stored Into a Variable?"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel