C Language Review Problems

Content from various types of blog and Baidu. Knowledge points more scattered.

Using arrays in C language must be defined. One-dimensional array is defined as:
    type specifier array name [constant expression];
constant expression in square brackets represents the number of data elements, also referred to as length of the array.
Constant expressions: the expression is there only a constant value. Such as 10 + 5 + 3 / 1.0, "abcd" is a constant expression. a + 10/7 is not constant expressions.
 The assigned to all the elements, the description in the array, the number of array elements may not be given. For example:
int A [. 5] = {1,2,3,4,5};
can be written as:
int A [] = {1,2,3,4,5};
Array of characters:
the "" string enclosed will automatically add '\ 0' at the end. For example, "abc123" from the surface appear to contain only six characters, it is not true, C language will be added at the end implicitly a '\ 0', this process is carried out quietly in the background, so we feel.
The following illustration shows a "C program" is stored in the memory of the case:
C Program \ 0
Note that, character by character to the array assignment does not automatically add '\ 0', for example:
char str [] = { 'A', 'B', 'C'};
length of the array str is 3, and not four, because finally there is no '\ 0'.
When the string with the character array is stored, paying particular attention '\ 0' To '\ 0' left position; this means a length, the length of the array of characters at least one greater than the string. Consider the following example:
char str [7] = "abc123";
"abc123" appear to contain only six characters, we shift is defined as the length of str 7, it is to be able to accommodate the last '\ 0'. If the length is defined as str 6, it can not receive '\ 0' up.
String "abcd" length of 4, because the string length refers to the number of valid characters in the string does not contain C-style terminator \ 0,
and the string array a [] = "abcd" length of 5, because this is the length of the array count, that is, the storage space occupied, of course, to include \ 0 terminator (it should also take up storage space).
the length of a [10] = "abcd" is 10, since the display character array specifies the length, of course, is occupied by the memory 10, its memory space is contiguous,
that is to say, 10 character spaces, respectively, front a, b, c, d and the end character \ 0, 0 are all behind (to be initialized), a total of 0 5, with the front and the end character abcd \ 0, a total of 10, their storage is continuous of.
a [] = { 'a', 'b', 'c', 'd'}, no double quotes in accordance with the definition, so is not C-style string, just an array of characters. But did not name the size, its size is equal to the length of initialization, this is 4, sizeof also equal to 4, there is no terminator \ 0 a.
By the "" string enclosed automatically added at the end of '\ 0'
character by character and will not be automatically added to the array assignment '\ 0'
Variables inside a function definition, arrays, structures, unions and so called local data. In many compilers, the initial value of the local data are random, meaningless, and the value is not what we usually think of as "zero."
char str [30] = {0 }; // all the elements are initialized to 0, or '\ 0'
if only the initialization portion of the array, then the remaining array elements automatically initialized to a value "zero", so we just 0th element to str assignment to 0, the remaining elements are on a 0.
It can be nested function calls, but can not be nested definitions.
External function: extern default when omitted defined function calls available to other documents.
The C language is passed by value, even if the pointer as an argument.
strlen is a function, which is used to calculate the length of the specified string str, but not including the end character (i.e., a null character).
Sizeof keyword is a unary operator and not a function. Strlen with different functions, its argument can be an array, a pointer, type, object, functions, etc.
S * = char "0123456789";
the sizeof (S); // Results 4 === "s is a pointer to a string constant character pointer
sizeof (* s); // Results 1 ===" * s is the first character
strlen (s); // === result 10 "has 10 characters, strlen internal function is implemented with a cycle to calculate \ 0. before
strlen (* s); // === result 10" error

S char [] = "0123456789";
the sizeof (S); // Results 11 === "s is the array, to calculate \ 0 position, therefore. 1 + 10
strlen (S); // === result 10" 10 characters, strlen internal function is implemented by a calculation cycle is to \ reaches zero before
sizeof (* s); // results 1 === "* s is the first character
S char [100] = "0123456789";
the sizeof (S); // result is 100 === "s in-memory representation size. 1 × 100
strlen (S); // result is 10 ===" strlen is internal function before implementation is to calculate \ 0 is a loop until the
S int [100] = "0123456789";
the sizeof (S); // Results 400 === "s represents a size of the memory again. 4 × 100
strlen (S); // Error ===" strlen parameters only char * and it must be based on a '\ 0' at the end of
char q[]="abc";
char p[]="a\n";
sizeof(q),sizeof(p),strlen(q),strlen(p);\\结果是 4 3 3 2
char P [] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};
char Q [] = { 'A', ' B ',' C ',' D, '\ 0', 'E', 'F', 'G'};
the sizeof (P); // result is 8 === "p represents the size of the memory 8 . 1 ×
strlen (P); // a random value, and the results related to the compiler, the compiler different results are generally different
sizeof (q); // result is 8 === "p represents the size of memory in the 8 × 1
strlen (q); // === 4 result "there is no '\ 0' encountered '\ 0' calculated stopped.
---------------------
Original: https: //blog.csdn.net/magic_world_wow/article/details/80500473
struct {
  
   char a;   
   int b;
   char c;
   double d;
   } e
Why sizeof (e) the size of it is 24
(memory alignment)
to achieve byte alignment relevant details and compilers:
1) the first address variable structure can be divisible its broadest size basic types of members;
2) each member of the structure of the first offset address relative to the structure (offset) are multiples of the size of the member;
total size 3) structure to the structure type of the widest base of an integer multiple of the size of members
B for its first address should be divisible by 4, it should be followed by a filling three bytes, and d in order to make their first addresses is divisible by 8, then it should be filled with the preceding seven bytes c;
thus: a accounted 4, b representing 4, c accounting 8, d itself 8 total: 24
str family of functions: https: //blog.csdn.net/We_are_family678/article/details/79671956
The same type can not be two pointer variables adder, but it can subtraction.

Guess you like

Origin www.cnblogs.com/960571726y/p/11116870.html