Variable type in C / C ++

A variable provides named storage so that we can manipulate. Each variable in C / C ++ has a specific type, which determines: the size and memory layout of the variable; The range of values ​​can be stored inside that memory; and the set of activities can be applied to that variable.

Variable names may include characters, digits, and underscores. It must start with either a character or an underscore. Uppercase and lowercase letters are different because C / C ++ is a case-sensitive language. Variables can be in various value types such as char, int, float, .

Definition of variables in C / C ++

The variable definition in C / C ++ means telling the compiler where and how much memory to create to store that variable. A variable definition defines a data type, and contains a list of one or more variables of that type, as follows:

 kieu_gia_tri danh_sach_bien ; 

Here, kieu_gia_tri must be a valid data type in C / C ++, including char, w_char, int, float, double, bool or any other user-defined object, . and list_sach_bien may contain one or multiple Identifier names separated by commas. Here are some valid declarations in C / C ++:

 int j , q , k ; char v , viet ; float x , diemthi ; double luong ; 

Lines int j, q, k; just declared and defined variables j, q, k, but instructed the compiler to create variables with the name j, q, k with int.

Variables can be initialized (initialized) in declarations. Initializer consists of an equal symbol followed by a Constant Expression (constant expression), as follows:

 kieu_gia_tri ten_bien = giaTri ; 

For example:

 extern int d = 3 , f = 5 ; // khai bao bien d va f. int d = 3 , f = 5 ; // dinh nghia va khoi tao bien d va f. byte z = 22 ; // dinh nghia va khoi tao bien z. char x = 'QTM' ; // bien x co gia tri la 'QTM'. 

With the definition without initialization: variables are initialized with NULL (all bytes are 0); The initial value of all other variables is not defined.

Declare variables in C / C ++

Declaring the variable in C / C ++ to be certain to the compiler that there is an existing variable with a given type and name, so that the compiler continues the compiler without knowing the full details of that variable. A variable declaration only makes sense at compile time, the compiler needs to declare the actual variable at the time of connecting the program.

Variable declarations are useful when you are using multiple files, and you define your variables in one of those files that will be available at the time of program connection. You will use extern keyword to declare a variable anywhere. Although, you can declare a variable multiple times in a C / C ++ program, but it can only be defined once in a file, a function, or a block of code.

For example

In the following example, a variable is declared at the beginning of the program, but it has been defined inside the main function.

 #include using namespace std ; // Phan khai bao bien: extern int a , b ; extern int c ; extern float f ; int main () { // Phan dinh ngia bien: int a , b ; int c ; float f ; // Phan khoi tao bien a = 10 ; b = 20 ; c = a + b ; cout << c << endl ; f = 70.0 / 3.0 ; cout << f << endl ; return 0 ; } 

When the above code is compiled and executed, it gives the following result:

 30 23.3333 

Like the concept applied on the function declaration, you provide the function name at the time of the declaration and the actual definition of that function can be provided anywhere. For example:

 // phan khai bao ham int func (); int main () { // phan goi ham int i = func (); } // phan dinh nghia ham int func () { return 0 ; } 

Lvalue and Rvalue in C / C ++

There are two types of Expression in C / C ++:

lvalue : This is an expression related to a device location. Any lvalue may appear on the left or right of an assignment.

rvalue : This concept relates to a data value that is stored at some addresses in the device. An rvalue is an expression that cannot have a value assigned to it, ie: rvalue may appear on the right but cannot appear on the left of an assignment.

Variables are lvalue that can appear on the left of the assignment. The numeric literal rvalue cannot be assigned and cannot appear on the left side of the assignment. Here is a valid command in C / C ++:

 int t = 10 ; 

But the following command is invalid and will give a compile-time error (error at compile time):

 16 = 30 ; 

According to Tutorialspoint

Previous article: Data type in C / C ++

Next article: Variable scope in C ++

4.5 ★ | 2 Vote

May be interested

  • Array (Array) in JavaScriptArray (Array) in JavaScript
    array object - array helps you store multiple values ​​in a single variable. it stores a set of fixed-size ranges of elements in the same type (type). an array is used to store a data set, but it is often more useful to think of an array as a collection of variables in the same type.
  • How to add Python to the Windows PATH variableHow to add Python to the Windows PATH variable
    to help you through the hassle of adding python to your windows path after installation, look at the options and a few related steps.
  • Data type in C #Data type in C #
    value type variables can be assigned a value directly. they are inherited from the system.valuetype class.
  • What is Windows PATH?What is Windows PATH?
    if you are trying to run something that is not part of windows, you will need to add it to the path variable. that tells the system where to look for executable files when you request them.
  • How to set and list environment variables in LinuxHow to set and list environment variables in Linux
    if you want to do something that requires using the command line, you will still need to know about environment variables. this may seem like a complicated term, but actually environment variables are easy to understand.
  • errno.h in Cerrno.h in C
    the file header named errno.h in standard c library defines the integer variable errno, which is set by system call and some library functions for errors to indicate that an error is occurring. this macro is an extension of the type of lvalue type int which can be modified, so it can be read and modified by a program.
  • What is USB Type C? Type C cable models are widely usedWhat is USB Type C? Type C cable models are widely used
    what is usb type c? usb type c is currently used very commonly in many technology devices. therefore, the type c port plays a very important role.
  • Scope rules in programming CScope rules in programming C
    a scope in any program is an area in the program where a defined variable exists and outside of that range the variable cannot be accessed.
  • What is Type C charging port? Should I use it or not?What is Type C charging port? Should I use it or not?
    type c charging port is the most popular connection device because of its fast charging feature and superior data transmission ability. discover with hacom right here.
  • How to Code an Alert with a Variable Using JavascriptHow to Code an Alert with a Variable Using Javascript
    alerts with variables are among the most useful things in javascript coding. you can use them to refer to someone by their name, to make a mini madlibs game, or even for showing quiz results. this article will show you how to make a...