C Language Basics Tutorial (my c tour started) [10]_c language

Source: Internet
Author: User
Tags arrays constant

27. Expression (Expression)

An expression is made up of operators and operands. A single operand is the simplest expression. Take a look at the following examples:

9
-4
+5
3 + 6
A * (B/C-D)
E = 9/3
f = ++e% 3

An expression in an expression is called a subexpression. For example: B/C is a subexpression of a * (B/C-D).

Each expression has a value, which is an important attribute of the C language. Obviously, the value of 9 and-4 is 9 and -4,3 + 6, which is 9. The value of E = 9/3 is 3, which is the value assigned to the variable E to the left of the = number. Let's take a look at the following expression:

8-(A = 2 * 3)

Think about it, how much is it worth? Yes, that's 2. However, this expression is not recommended because of poor readability.

28. Array base (top)

1. Introduction to Arrays

An arrayis made up of elements of a series of the same data types . The compiler can know the number of elements in an array from the array declaration, and the data type of those elements. For example:

Double dbl[20]; / * An array containing 20 double types of elements * *
int c[12]; / * An array containing 12 int elements * *
Char ch[40]; / * An array containing 40 char elements * *

The square brackets [] indicate that they are arrays, and the numbers in [] indicate the number of elements the array contains.

The elements in the array are adjacent , and the value of the element may be random before initialization. The image below shows the adjacent relationship.

You can access a specific element by using an array name and subscript (subscript number or index). The subscript begins at 0 and ends at n-1. For example: C[0] is the first element of array C, and c[11 is its last element, the 12th element.


2. Initialize

int c[12] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};

As shown above, we initialize the array with a comma-delimited range of values in the curly braces. We call this the initialization list . Curly braces are a must! Spaces between commas and values are optional. After initialization, the value of the first element of array C C[0] is 0, and so on.

The following applet outputs the values of all elements in the array Iarr.

#include <stdio.h>

int main (void)
{
int iarr[4] = {0, 1, 2, 3};
int i = 2;

printf ("%d\n", iarr[0]); / * Output 0 * *
printf ("%d\n", iarr[1]); / * Output 1 * *
printf ("%d\n", Iarr[i]); / * Output 2 * *
printf ("%d\n", iarr[1+2]); / * Output 3 * *

return 0;
}

As shown above, when accessing an array element, [] can be a constant, can be a variable, or it can be an expression. [] can also be a function call with a return value of integral type. In short, as long as the value in [] is an integer type .

Note that the above program, if the int iarr[4] = {0, 1, 2, 3}; to an int iarr[4]; (that is, no initialization), the value of the element inside it is random, that is, the value that exists in that memory space. If changed to int iarr[4]; Before placing it in the int main (void), the value of the element inside it is 0. Specific reasons I will explain in the following tutorials.

If the number of values in the initialization list is less than the number of elements in the array, the remaining elements are initialized to 0. For example:

int iarr[4] = {0, 1};

Iarr[0] and iarr[1] are initialized to 0, respectively, for 0 and 1;iarr[2] and iarr[3. Note that the number of values in the initialization list can be less than the number of array elements, but it is wrong to exceed the number of elements in the array!

When initializing an array, [] You can leave it blank. For example:

int iarr[] = {1, 2, 3};

The compiler calculates the number of values in the initialization list, and then constructs an array that contains so many elements. As in the example above, the compiler calculates a total of 3 values in the list, and then constructs the Iarr into an array of 3 elements. For example:

#include <stdio.h>

int main (void)
{
int iarr[] = {1, 2, 3};

printf ("%d\n", iarr[0]); / * Output 1 * *
printf ("%d\n", iarr[1]); / * Output 2 * *
printf ("%d\n", iarr[2]); / * Output 3 * *

return 0;
}

We can use the following expression to calculate the number of elements in the Iarr:

sizeof iarr/sizeof Iarr[0]

Among them, sizeof Iarr calculates the amount of memory occupied by Iarr, sizeof Iarr[0] calculates the amount of memory occupied by Iarr[0 (that is, the amount of memory occupied by each element in the array Iarr), and they divide by the number of Iarr elements. sizeof is an operator, as I will say later.

29. Array base (bottom)

1. Assigning an initial value (designated initializers)

assigning an initial value is a C99 addition, which allows us to initialize the specific elements of the array directly. C99 before, if we were to initialize an element in an array, such as a third element, we had to initialize the element before it. For example:

int iarr[10] = {0, 0, 300};

And in C99, we can initialize a particular element like this:

int iarr[10] = {[2] = 300}; / * Assign initialization iarr[2] to

The remaining elements will be initialized to 0. Here we look at a small program.

#include <stdio.h>

int main (void)
{
int iarr[5] = {6, 3, [3] = 1, 5, [1] = 8};

printf ("%d\n", iarr[0]);
printf ("%d\n", iarr[1]);
printf ("%d\n", iarr[2]);
printf ("%d\n", iarr[3]);
printf ("%d\n", iarr[4]);

return 0;
}

The output is:

6
8
0
1
5

You can see two points from this:

A. If there is a value after the initial value is assigned, subsequent values are used to initialize the later elements. In the example above,
IARR[3] is initialized to 1, and its subsequent element iarr[4] is initialized to 5.

B. If the initialization of an element occurs more than once in the initialization list, the last time. In the example above,
IARR[1] is initialized to 3 and then initialized to 8 by [1] = 8 assignment.


2. Assigning values to array elements

We can use the subscript to assign a value to a particular element. For example:

int iarr[5];
IARR[0] = 100; /* Assignment to the first element * *
IARR[4] = 120; /* Assign value to Fifth Element/*
IARR[2] = 180; /* Assign value to the third element * *

C does not allow the use of arrays directly to assign values to other arrays, nor does it allow the use of an initialization list to assign values to a group. For example:

int iarr_1[5] = {1, 2, 3, 4, 5}; /* Correct * *
int iarr_2[5];

iarr_2 = iarr_1; /* ERROR! */
Iarr_2[5] = {3, 4, 5, 6, 7}; /* ERROR! */
IARR_2[5] = iarr_1[5]; /* Out of bounds! */

The last statement has crossed the line! Because both arrays have only 5 elements, the sixth element is accessed using subscript 5!

3. Array bounds (bounds)

When using subscript, we must ensure that the subscript does not cross over. For example:

int iarr[46];

The subscript range for this array is 0 to 45, and it is our responsibility to ensure that the subscript does not exceed this range because the compiler does not detect the subscript out of bounds!

The C standard does not define the consequences of scaling out of bounds, that is, when a subscript crosses the line in the program we write, the program may work properly, or it may exit unexpectedly, and other strange situations may occur.

#include <stdio.h>

int main (void)
{
int var_1 = 20;
int arr[5];
int var_2 = 40;

printf ("var_1:%d, var_2:%d\n", var_1, var_2);

ARR[-1] =-1;
ARR[5] = 5;

printf ("%d%d\n", arr[-1], arr[5]);
printf ("var_1:%d, var_2:%d\n", var_1, var_2);

return 0;
}

The output that the above program uses dev-c++ 4.9.9.2 to compile runs is:

VAR_1:20, var_2:40
-1 5
VAR_1:20, var_2:-1

Visible, subscript crossing may change the value of other variables . This is because GCC (the C compiler used by dev-c++) saves the var_2 to the memory space before the array arr, so assigning a value to arr[-1 changes the var_2 value exactly. Different compiler compilation runs the program may have different output, or it may exit unexpectedly.

The philosophy of C language is to trust programmers, and not to detect Cross-border programs running faster. When the program compiles, some of the underlying values are still unknown, so if you want to detect the subscript out of bounds, the compiler must add extra code to the generated target code to detect whether the subscript is out of bounds when the program runs, which can cause the program to run at a reduced rate. Therefore, in order to run efficiently, C does not detect whether the subscript is out of bounds.


4. Specify the number of array elements

Before C99, when declaring an array, the value in [] must be an integer constant greater than 0 . In C99, when declaring an array, [] can be a variable . This is called a variable-length array (variable-length array, abbreviated VLA). When declaring a VLA, it cannot be initialized. In a subsequent tutorial, I will explain the VLA in detail.

int n = 99;
Double dbl_1[4]; /* Correct * *
Double DBL_2[8/2 + 4]; /* Correct * *
int iar_1[-5]; * * wrong! [] The value must be greater than 0 */
int iar_2[0]; * * wrong! [] The value must be greater than 0 */
int iar_3[9.2]; * * wrong! The value in [] must be an integer type */
Char Ch[n]; /* C99 not supported before! */

Related Article

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.