-
This program is the application of modularity, the first two subroutines getline function, copy function as the modulated function, its function is to read a line into s and return its length and copy from to to, the main function by calling these two functions to achieve the purpose. After understanding the role of the modulated function, first analyze the main tonal function, while((len = getline(line,maxline)) 0) is to read the input line into the line and assign the length returned to len, when there is an input that is len>0, then judge whether the length is the maximum value, mark the line if it is satisfied, and copy the line to longest, and so on until the output line with the largest length is no longer input.
-
getline function reads the string Get its length copy function Compares the length of the newly entered string with the longest previously saved string Keep the longest one and finally output this string.
-
There are enough annotations, what else can't be understood? Tangled...
-
A string constant, for which storage space is allocated in memory.
int a="string“;A is the address of the integer array, that is, the address of the first element in the integer array a, and the storage space pointed to is 4 bytes.
The address of the string represents the address of the first character in the string, and the storage space pointed to is 1 byte.
Therefore, int a="string" is wrong.
As for saying, int c[8]; scanf("%s",c);Why it is correct, the analysis is as follows:
You can store a character in an integer memory cell, but you can use the lower bytes of that space. In this case, of course, it is also possible to enter a string from the keyboard and store it as an int array, and the same is true.
-
char d = "string"Yes, int a cannot be initialized with a string. Only by curly braces:
int a = ;
int c[8];
scanf("%s", c);It's just syntax, but the runtime result may not be what you want.
Because scanf %s is the memory that c points to, then one character per byte is stored. For example, input"hellow"
What we expect is c[0] to exist'h', c[1] exists'e', but actually'hell'4 bytes of c[0].
-
The correct option is b;
The error between a and d is because the one-dimensional length of the definition of a two-dimensional array cannot be omitted, i.e., it must be a[n], where n can be any positive integer.
c is wrong because when initializing an array with characters, it should be prefixed with curly braces{}; i.e. a[2][3]=; If you initialize the array with a string, you can leave it unwrapped, e.g. a[2][3]="ab";
As to why is b right?
b The name definition of item B conforms to the variable naming requirements (starting with a letter and an underscore, and is composed of any combination of letters, numbers, and underscores);
The array definition of item b conforms to the C language requirement, i.e., its one-dimensional length cannot be omitted.
The array initialization of item b meets the requirements of the C language, and the array is initialized with characters and curly braces. Moreover, because the two-dimensional length is omitted, the total length of the array can be changed arbitrarily when initialized. That is, the system will allocate at least 6 elements to the array, i.e. a[2][3].
-
char a="12345"The system sets the length for a based on the actual length of the string, so the length is 6.
and char a[10]="12345" sets the length of the array to 10 at the beginning, and then initializes the data in the array, which will not change the length of the array itself.
In short, the length of the array is specified at the time of declaration, and the specified length is obtained at sizeof. If no length is specified during the declaration, the actual length of the array is determined based on the length of the initialization data list.
-
After reading your questions and follow-up questions, I will solve your doubts:
sizeof calculates the length of the character array, which is a basis, that is, to see how much is written in the character array when defined, write n, and calculate it is n (in fact, there is no calculation, just take it; integers and other multi-byte element arrays). You may not believe it and think that char a="12345"I didn't write it! In fact, the compiler has calculated the constant after = at the time of compilation, and filled in the number ......The number that is not filled in the definition of the array is actually the compiler syntax rather than the C C++ syntax, and the length of the compiled array is constant!
-
The length of an array variable is defined as the length of the string if it is random, and if you predefine the size of the array, then it is the length of the array.
In the whole main function, only this one variable, whether it is the first for or the second for, is the same i, the second for will have i=-1, in the case of i= -1, and printf("%d ",a[i]);Statement. So fear not! When there is no second for the case: >>>More
Use the ITOA function.
Prototype: extern char *itoa(int i); >>>More
Bubbling sorting, fractions into an array, names into a two-dimensional string, the first dimension of the string corresponds to the subscript of the array, and the sorting is exchanged once, and the exchange can be achieved by a buffered string t, using strcpy (similar to the assignment of variables), for example. >>>More
a1.For the literal constant 4, the compiler thinks that it is int and double, so although the value of 4 3 is equal to, when it is converted to an integer, it is 1, and for that expression, its result is double, so the decimal part can be preserved. The knowledge involved in this question is an implicit conversion of types. >>>More
In the C language. An integer constant starting with 0 refers to octal. 027 is 2x8+7, so the answer is c.