Home > Back-end >  C at the beginning of introduction, solve the following program! Thank you!!!!!!
C at the beginning of introduction, solve the following program! Thank you!!!!!!

Time:10-10

#include
using namespace std;
Int main () {
Char c1;
C1='a';
CoutChar c2 [10].
C2 [10]="a";
Coutreturn 0;
}

CodePudding user response:

C2 [10]="a";
============
C2 [0]='a';

CodePudding user response:

A single character can't be string

CodePudding user response:

If you want to initialize so write char c2 [10]={' a ', 'b'};
If you want to a one assignment is a [0]='a', a [1]='b'
Must be a single quotes, double quotes are used to make the string
And not as a [10]='a', so write already crossed, because there are hidden character '\ 0', '\ 0' is the terminator

CodePudding user response:

Individual we called character, string is the result of superposition of multiple characters, and character array is the superposition of multiple characters, so we usually think that character array=string
About why wrong, explain here
Must first know a character variable is a byte of memory, is a string enclosed in double quotation marks, single quotation marks are characters
And will the end of the string with a null character

You can try
Sizeof function is used to return parameter of memory
We can test: coutThen you will be surprised to find that the result is 4, it is said above, the string will automatically add a null character (' \ 0 '), the null character also takes up one byte
So c2 [10] is a character array of variables, the system only it provides one byte of memory, and you will be "a" to it, the "a" for the 2 bytes, nature is not achieve

So if you want to give it an initial value, a character array has the following several methods:
1. The c2="string";//here understand that array name is an address, and is the first item of the array; It is convenient in assignment, every characters in the string starting from 0 subscript assigned to each in turn space
2. C2={' 1 'character,' 2 'characters, characters' 3'}; Must have this method should be//don't have to explain, is the way of general array initialization
[in addition to a point, we are operating character array, such as we read in a string, use a character array to store, but for convenience, you don't want to start the subscript 0 read, do you want to start from 1, this is we can use the array name + do you want to start the subscript, specific see the pointer manipulation]

CodePudding user response:

C2 10 element, the subscript biggest is 9

CodePudding user response:

A single character can be a string, the key is your statement is char instead of string

CodePudding user response:

reference 4 floor User_Ghost response:
single we called character, string is the result of superposition of multiple characters, and character array is the superposition of multiple characters, so we usually think that character array=string
About why wrong, explain here
Must first know a character variable is a byte of memory, is a string enclosed in double quotation marks, single quotation marks are characters
And will the end of the string with a null character

You can try
Sizeof function is used to return parameter of memory
We can test: coutThen you will be surprised to find that the result is 4, it is said above, the string will automatically add a null character (' \ 0 '), the null character also takes up one byte
So c2 [10] is a character array of variables, the system only it provides one byte of memory, and you will be "a" to it, the "a" for the 2 bytes, nature is not achieve

So if you want to give it an initial value, a character array has the following several methods:
1. The c2="string";//here understand that array name is an address, and is the first item of the array; It is convenient in assignment, every characters in the string starting from 0 subscript assigned to each in turn space
2. C2={' 1 'character,' 2 'characters, characters' 3'}; Must have this method should be//don't have to explain, is the way of general array initialization
[in addition to a point, we are operating character array, such as we read in a string, use a character array to store, but for convenience, you don't want to start the subscript 0 read, you want to start from 1, this is we can use the array name + do you want to start the subscript, specific see the pointer manipulation]

"C2 [10] is a character array variable, system for it provides a byte of memory" this sentence is not very good

CodePudding user response:

refer to 7th floor eternity Alisa describes response:
Quote: refer to 4th floor User_Ghost response:
single we called character, string is the result of superposition of multiple characters, and character array is the superposition of multiple characters, so we usually think that character array=string
About why wrong, explain here
Must first know a character variable is a byte of memory, is a string enclosed in double quotation marks, single quotation marks are characters
And will the end of the string with a null character

You can try
Sizeof function is used to return parameter of memory
We can test: coutThen you will be surprised to find that the result is 4, it is said above, the string will automatically add a null character (' \ 0 '), the null character also takes up one byte
So c2 [10] is a character array of variables, the system only it provides one byte of memory, and you will be "a" to it, the "a" for the 2 bytes, nature is not achieve

So if you want to give it an initial value, a character array has the following several methods:
1. The c2="string";//here understand that array name is an address, and is the first item of the array; It is convenient in assignment, every characters in the string starting from 0 subscript assigned to each in turn space
2. C2={' 1 'character,' 2 'characters, characters' 3'}; Must have this method should be//don't have to explain, is the way of general array initialization
[in addition to a point, we are operating character array, such as we read in a string, use a character array to store, but for convenience, you don't want to start the subscript 0 read, you want to start from 1, this is we can use the array name + do you want to start the subscript, specific see the pointer manipulation]

"C2 [10] is a character array of variables, the system for it provides a byte of memory" this sentence is not very good

Got it! Because my definition is char! Thank you!!!!!!

CodePudding user response:

refer to 6th floor Nonoas response:
single character can be a string, the key is your statement is a char, rather than the string

Thank you for your answer??

CodePudding user response:

C2 [0]='a';

CodePudding user response:

refer to 7th floor eternity Alisa describes response:
Quote: refer to 4th floor User_Ghost response:
single we called character, string is the result of superposition of multiple characters, and character array is the superposition of multiple characters, so we usually think that character array=string
About why wrong, explain here
Must first know a character variable is a byte of memory, is a string enclosed in double quotation marks, single quotation marks are characters
And will the end of the string with a null character

You can try
Sizeof function is used to return parameter of memory
We can test: coutThen you will be surprised to find that the result is 4, it is said above, the string will automatically add a null character (' \ 0 '), the null character also takes up one byte
So c2 [10] is a character array of variables, the system only it provides one byte of memory, and you will be "a" to it, the "a" for the 2 bytes, nature is not achieve

So if you want to give it an initial value, a character array has the following several methods:
1. The c2="string";//here understand that array name is an address, and is the first item of the array; It is convenient in assignment, every characters in the string starting from 0 subscript assigned to each in turn space
2. C2={' 1 'character,' 2 'characters, characters' 3'}; Must have this method should be//don't have to explain, is the way of general array initialization
[in addition to a point, we are operating character array, such as we read in a string, use a character array to store, but for convenience, you don't want to start the subscript 0 read, you want to start from 1, this is we can use the array name + do you want to start the subscript, specific see the pointer manipulation]

"C2 [10] is a character array of variables, the system for it provides a byte of memory" this sentence is not very good

You can go to check and short type (int) for the 2 bytes, long [int]/[long] int variables accounted for 4 bytes, long long (int) account for 8 bytes
You should know the 8 bit a=a byte bytes, so let's see
2 ^ 1 (2 n)=(here I'll results into the binary) 10
2 ^ 2=100
2 ^ 3=1000
2 ^ 4=10000
At this time we found
2 ^ n=1 (with n 0)
So let's see
Short occupies 2 bytes, i.e., 16
Each bit is used to record the 0 or 1
Judging is derived according to our
2 ^ 16=1 (plus 16 0)
Then we will find that it has, in itself, comes with 16 0, is already filled the short memory? , but we found that the front and a 1
So we can know that short range should be 2 ^ 16-1, then you will ask, why is 16 power, but the book is 15 power?
We misses the point, short can be negative, so we need a bit in the 16 to identify positive and negative, and this place we call "the sign bit
"null
  • Related