Home > Back-end >  2 C
2 C

Time:10-14

The first question: why is the first picture with strange symbols, and the second is not


The second question:
Books if a character array of deposit has a number of different length of string, you should make the longest length is greater than the length of the string array, why char [0] can also


 
#include
#include

Int main ()
{
Char c []={" c program "};
Char [] d={} "Hello";
Char [] e={' C ', ', 'p', 'r', 'o', 'g', 'r', 'a', 'm'};
int i;
for(i=0; i<=4; I++)
C=d [I] [I];
Puts (c);

Puts (e);

return 0;
}


 
#include

Int main ()
{
Char [] e={' C ', ', 'p', 'r', 'o', 'g', 'r', 'a', 'm'};
//for (I=0; i<=8; I++)
Puts (e);
}


 
#include

Int main ()
{
Char a, [0].

//to save "abcd" in a
Strcpy (a, "abcde");
//output string in a
Puts (a);
//will be "1234" save in a
Strcpy (a, "1234");
//output string in a
Puts (a);
}

CodePudding user response:

 

#include
#include

Int main ()
{
Char c []={" c program "};
Char [] d={} "Hello";
Char [] e={' C ', ', 'p', 'r', 'o', 'g', 'r', 'a', 'm'};
int i;
for(i=0; i<=4; I++)
C=d [I] [I];
Puts (c);

Puts (e);

return 0;
}






 
#include

Int main ()
{
Char [] e={' C ', ', 'p', 'r', 'o', 'g', 'r', 'a', 'm'};
//for (I=0; i<=8; I++)
Puts (e);
}





 
#include

Int main ()
{
Char a, [0].

//to save "abcd" in a
Strcpy (a, "abcde");
//output string in a
Puts (a);
//will be "1234" save in a
Strcpy (a, "1234");
//output string in a
Puts (a);
}


CodePudding user response:


Why is beyond 5 bytes or not warning?

 
#include

Int main ()
{
Char STR [5].
Gets (STR);
Puts (STR);
return 0;
}

CodePudding user response:

Occupy the static data partition, because there is no defined end, puts the actual cross-border access may occur, this show at random

CodePudding user response:

In addition, the usage gets here is not credible, can inject illegal string, produce coredump memory

CodePudding user response:


Changed to printf is stil

 
#include
#include

Int main ()
{
Char c []={" c program "};
Char [] d={} "Hello";
Char [] e={' C ', ', 'p', 'r', 'o', 'g', 'r', 'a', 'm'};
int i;
for(i=0; i<=4; I++)
C=d [I] [I];
Printf (" % s \ n ", c);

Printf (" % s \ n ", e);

return 0;
}

CodePudding user response:



Why is that? Why don't the second warning?

CodePudding user response:

refer to 6th floor AKK188888881 response:


Why is that? Why don't the second warning?

The first compiler know the back of the string is too long.
The second run the compiler doesn't know when the string is too long,

The second cross-border read the string, not without warning, because of this warning has not included in standard C, so some compiler warnings, some without warning,

CodePudding user response:

Puts is met '\ 0' stop output, e '\ 0', so there will be a cross-border,

A [0], strcpy, will certainly crossed,