Variable in C programming
A variable in C is nothing but a name given to storage memory so that the program can be manipulated. Each variable in C has a defined type, to determine the size and layout for that variable memory. The range of values that can be stored in memory, setting of expressions can be applied to variables.
The name of the variable may include letters, numbers and underscores (_), but it must begin with an alphabetic character or an underscore. Uppercase and lowercase letters are usually two distinct objects because C is a case-sensitive language. Based on the basic types explained in the previous chapter, there are the following types of basic variables:
Type DescriptionChar is an integer variable, 1 byte in size. int The type for natural numbers. float The floating point accuracy value is single. double Dual precision floating point value. void Represents a type without type.The C programming language allows the definition of different types of variables, which can be viewed in the following chapters such as enumerated variables, pointer variables, array variables, structure variables , Union variables , .
Variable definition in C language
Defining variables means informing the compiler where and how to create the storage for that variable. A variable definition defines a data type and contains a list of one or more variables of that type as follows:
kieu_du_lieu danh_sach_bien ;
Here, kieu_du_lieu is of data type of C language such as char, w_char, int, float, double, bool or any other type of user defined object . list_list_in may include one or more separated identifiers each other by commas. Some valid declaration examples of variables are as follows:
int i , j ; char ho , ten , c , ch ; float f , luong , diemthi ; double d ;
Line int i, j; just declared and defined for variables i, j, k and instructed the compiler to create variables under the name i, j, k with type int.
The variable can be initialized (assigned initial values) in its declaration. An initialization consists of a "=" followed by a numeric constant expression as follows:
kieu_du_lieu ten_bien = gia_tri ;
Some examples below:
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 = 'hoclaptrinhc' ; // bien x co gia tri la 'hoclaptrinhc'.
With the definition of no initial value, static variables can store with NULL values, (all bytes have a value of 0), the initial value of all variables of all other types is valid Unknown.
Declare the variable in C language:
The variable declaration provides a guarantee for the compiler to recognize that there are no variables with the same type and name as it was previously declared, otherwise a compiler error will occur. A variable declaration is only meaningful at compile time, the compiler needs to declare a specific variable at the time of connection to the program.
A variable declaration is useful when you use multiple files simultaneously and you define your variable in one of those files. You can use extern keyword to declare variables anywhere. So you can declare a variable multiple times in C program, but only define in a file, a function or a block of code.
Usually extern variable is declared in file.h because when you want to use you only need to include the .h file to be able to use the variable.
For example
Try the example below, where the variable is declared at the top, but they are defined and initialized in main function:
#include // phan khai bao bien: extern int a , b ; extern int c ; extern float f ; int main () { /* phan dinh nghia bien: */ int a , b ; int c ; float f ; /* phan khoi tao gia tri thuc su */ a = 15 ; b = 35 ; c = a + b ; printf ( "Gia tri cua c la : %d n" , c ); f = 50.0 / 3.0 ; printf ( "Gia tri cua f la : %f n" , f ); printf ( "===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); return 0 ; }
Compile and run C program to see results:
Some concepts can be applied in function declarations where you provide the function name at the time it declares and defines it anywhere. For example:
// phan khai bao ham int tenham (); int main () { // loi goi ham int i = tenham (); } // phan dinh nghia ham int tenham () { return 0 ; }
Lvalue and Rvalue in C:
There are two types of Expression:
- lvalue : Expression that refers to memory location as " lvalue ". A lvalue may appear either on the left or the right of an assignment.
- rvalue : Concerning data values stored at some addresses in memory. An rvalue is an expression that cannot have a value assigned to it, meaning a rvalue may appear on the right but not the left of an assignment.
Variables are lvalues and often appear on the left side of the assignment. Constants are numbers that are rvalue and cannot be assigned and cannot appear on the left side of the assignment. Here is a valid declaration:
int g = 20 ;
But the following is an invalid declaration and will contain an error message:
10 = 20 ;
According to Tutorialspoint
Previous article: Data type in programming C
Next article: Constant in programming C
You should read it
May be interested
- Variable in PHPthe main way to store information in php programs is to use a variable.
- Set of multiple choice questions about programming with P10 prizethe programming questions below will give you lots of useful information. if you are interested in learning programming languages, the series of programming language topics will be very helpful for you.
- Set of multiple choice questions about programming with P7 prizecurrent programming is no longer strange to us. programming work is becoming hot and more interested. please join the network administrator to learn about programming skills through multiple-choice questions below.
- Set of multiple-choice questions on award-winning programming P5serializing programming tests, in the following article, readers will be able to expand their knowledge with more interesting questions. let's start.
- Set of multiple choice questions about programming with P6the following network administrators will continue to send you interesting questions about programming. if you love this topic, then try your knowledge.
- P13 programming set of multiple choice questionsyou are a fan of programming languages and want to learn more about this topic. to give you an interesting reading about programming, in this article, the network administrator will send you a good quiz about this topic. invite your reference.
- Data type in C / C ++data type in c / c ++ while working with any programming language, you need to use various types of variables to store information. variables, nothing but memory locations are reserved for saving values. that is, when you create a variable, you reserved some space in memory for that variable.
- Set of multiple choice questions on programming with P3 prizeinvite readers to participate in testing knowledge with questions around the topic of the following programming of network administrator. the question set will have 10 questions with 4 answers, please choose the most accurate answer.
- Set of multiple choice questions on programming with P16 prizemultiple choice questions about programming will be the first piece of knowledge to help you get started with programming. invite your reference.
- Set of multiple choice questions for programming with P8 prizesfollowing the series of quizzes around programming topics, below invite readers to test their knowledge with multiple choice questions that we have summarized below.