C language variables and sample code _c language

Source: Internet
Author: User

C variables

The variable is simply the name of the store that the program can manipulate. Each variable in C has a specific type, which determines the size and layout of the variable's storage, and the values within that range can be stored in memory and the operator can be applied to the variable.

The name of a variable can consist of letters, numbers, and underscore characters. It must begin with a letter or an underscore. Uppercase and lowercase letters are different, because C is case sensitive. Based on the basic types explained in the previous chapter, there are several basic variable types:

type Description
Char is usually a eight-bit byte (one byte). This is an integer type.
Int For machines, the most natural size of integers.
Float A single-precision floating-point value.
Double A double-precision floating-point value.
void Represents a missing type.

The C language also allows you to define a variety of other types of variables, such as enumerations, pointers, arrays, structs, shared bodies, and so on, which will be explained in subsequent chapters, and we'll explain the basic variable types first.

Definition of variables in C

A variable definition is to tell the compiler where to create a variable's storage and how to create a stored variable. A variable definition specifies a data type and contains a list of one or more variables of that type, as follows:

Type variable_list;

In this case, type must be a valid C data type, can be char, w_char, int, float, double, bool, or any user-defined object, Variable_list can consist of one or more identifier names. Multiple identifiers are separated by commas. Several valid declarations are listed below:

int  I, j, K;
char  c, ch;
float f, salary;
Double D;

row int I, j, K; Declares and defines variables I, j, and K, which instructs the compiler to create variables named I, J, and K with the type int.

Variables can be initialized at the time of declaration (specifying an initial value). The initializer consists of an equal sign followed by a constant expression, as follows:

Type variable_name = value;

Several examples are listed below:

extern int d = 3, F = 5;  D and F of the Declaration, which is simply declared
int d = 3, F = 5;      Define and Initialize D and f
byte z =;        Defines and initializes a z
char x = ' x ';        The value of the variable x is ' x '

Without initialization definition: A variable with a static storage duration is implicitly initialized to NULL (all bytes have a value of 0), and the initial value of all other variables is undefined.

Variable declarations in C

A variable declaration assures the compiler that the variable exists with the specified type and name, so that the compiler can continue to compile further without knowing the full details of the variable. A variable declaration has its meaning only at compile time, and the compiler needs the actual variable declaration when the program connects.

There are two things about the declaration of a variable:

1, one is the need to build storage space. For example, int A has established storage space when it is declared.

2, the other is no need to create storage space, by using the extern keyword declaration variable name without defining it. For example: extern int A, where variable a can be defined in another file.

3, unless the extern keyword, otherwise is the definition of variables.

extern int i; Declaration, not a definition
int i; Declaration, also defined

Instance

Try the following instance where the variable is declared on the head, but defined and initialized in the main function:

#include <stdio.h>

//variable declaration
extern int A, b;
extern int C;
extern float F;

int main ()
{/
 * variable definition */
 int A, b;
 int C;
 float F;
 
 /* Initialize
 /a = ten;
 b =;
 
 c = a + B;
 printf ("Value of C:%d \ n", c);

 f = 70.0/3.0;
 printf ("Value of f:%f \ n", f);
 
 return 0;
}

When the above code is compiled and executed, it produces the following results:

Value of c:30
value of f:23.333334

Left value (lvalues) and right value (Rvalues) in C

There are two types of expressions in C:

1. Left value (lvalue) : An expression that points to a memory location is called a left-valued (lvalue) expression. The left value can appear to the left or right of the assignment number.

2. Right value (rvalue) : The term Right value (rvalue) refers to the number of addresses stored in memory. The right value is an expression that cannot be assigned to it, that is, the right value can appear to the right of the assignment number, but not to the left of the assignment number.

A variable is a left value, so it can appear to the left of the assignment number. Numeric literals are the right values, so they cannot be assigned and cannot appear to the left of the assignment number. The following is a valid statement:

int g = 20;

But the following is not a valid statement that generates a compile-time error:

10 = 20;

The above is the C language variable data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.