Basic course of C language (ii) data types, variables, and operators (9)

Source: Internet
Author: User
Tags arrays define local expression
4.5 Initialization and assignment of variables
Initialization of a variable
The initialization of a variable means that the variable is assigned an initial value at the same time as it is described. In Turbo C2.0, external variables and static whole variables are initialized at the beginning of the program, and local variables, including static local variables, are initialized when they enter a function or compound statement that defines them. All the whole variables are automatically zeroed out when there is no explicit initialization, and the values of local and register variables are undefined before they are assigned a value.
For external and static variables, the initial value must be a constant expression, and an automatic variable and a register variable can be any expression, which can include constants and previously described variables and functions.
1. Initialization of a single variable
For example:
Float F0, f1=0.2;/* defines the whole variable, F0 is cleared at initialization, F1 is assigned 0.2*/
Main ()
{
static int i=10, J; /* Define static local variables, initialization I was given, J is not accurate
Fixed * *
int k=i*5; /* Define local variables, when initialized K is assigned 10*5=50*/
Char c= ' y '; /* Define the character-variable and initialize it.
.
.
.
}
2. Initialization of array variables
For example:
Main ()
{
int P[2][3]={{2,-9, 0}, {8, 2,-5}}; /* Define array P and initialize/*
int m[2][4]={{27,-5, 19, 3}, {1, 8,-14, -2}};/* defined array m and initial
Initiation of the * *
Char *f[]={' A ', ' B ', ' C '}; /* Define array F and initialize/*
.
.
.
}
As you can see from the example above, the following rules are used to initialize an array in Turbo C2.0:
(1) Each row of the array initialization assignment with "{}" and "," separated, the total plus a pair of "{}" enclosed, and finally ";" Indicates the end.
(2) The storage of multidimensional arrays is the principle of the fastest change in the variables of the most right dimension.
(3) Multidimensional array storage is continuous, so a one-dimensional array initialization method can be used to initialize multidimensional arrays.
For example:
int X[2][3]={1, 2, 3, 4, 5, 6};/* initialize two-dimensional array with one-dimensional array * *
(4) When an array is initialized, the insufficient array elements are filled with 0来 if the number of data in the initial table is less than that of the element.
(5) The array of pointer variables can not specify the dimension, and when the assignment is initialized, the dimension of the array is continuously assigned from 0.
For example:
Char *f[]={' A ', ' B ', ' C '};
The initialization will assign a value of 3 character pointers, namely: *f[0]= ' a ', *f[1]= ' B ', *f[2]= ' C '.

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.