Home > Net >  adding other random characters to the compiled code
adding other random characters to the compiled code

Time:11-21

enter image description here

here is the code in C but after compiling and seeing the results, some random characters are added to the characters that the program should display compilation result as above picture why is this happening why those characters that are not declared in the code are added

here is my code:

#include <iostream>
using namespace std;

int main()
{

        char big_characters[26] = {'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X', 'C', 'V', 'B', 'N', 'M'};
            cout << big_characters;
    cout << endl << endl;
        char small_characters[26] = {'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm'};
            cout << small_characters;
    cout << endl << endl;
        char digits[10] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
            cout << digits;
    cout << endl << endl;   
        char special_characters[32] = {'`', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', ' ', '[', ']', '{', '}', ';', ':', '|', ',', '<', '.', '>', '/', '?', char(34), char(39), char(92)};
            cout << special_characters;
    cout << endl << endl;
    
    return 0;
}

as I mentioned earlier, some random characters are added to my code as a result of compilation. I don't know why this happening

I think the solution to the problem is to use string instead of single characters (char) I want to know why this is happening in this case compilation result as above

CodePudding user response:

So if you want to output your arrays with no random characters outputed(undefine behaviour) you need to use a simple for loop and then you will get the correct output, like this:

#include <iostream>

using namespace std;

int main() {
    char big_characters[26] = {'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O',
                               'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K',
                               'L', 'Z', 'X', 'C', 'V', 'B', 'N', 'M'};
    for (int i = 0; i < 26; i  ) {
        cout << big_characters[i];
    }
    cout << endl << endl;
    char small_characters[26] = {'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o',
                                 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k',
                                 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm'};
    for (int i = 0; i < 26; i  ) {
        cout << small_characters[i];
    }
    cout << endl << endl;
    char digits[10] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
    for (int i = 0; i < 10; i  ) {
        cout << digits[i];
    }
    cout << endl << endl;
    char special_characters[32] = {
        '`', '~', '!', '@', '#', '$', '%', '^',      '&',      '*',     '(',
        ')', '-', '_', '=', ' ', '[', ']', '{',      '}',      ';',     ':',
        '|', ',', '<', '.', '>', '/', '?', char(34), char(39), char(92)};
    for (int i = 0; i < 32; i  ) {
        cout << special_characters[i];
    }
    cout << endl << endl;

    return 0;
}

You can see it live here. It has the output:

QWERTYUIOPASDFGHJKLZXCVBNM

qwertyuiopasdfghjklzxcvbnm

1234567890

`~!@#$%^&*()-_= []{};:|,<.>/?"'\

with no random characters on the end.

Or instead of the hard coded for loops you could use the std::cout member function write():

cout.write(special_characters, 32);

So you would do cout.write( then the name of the array for example special_characters, then the size for example 32 and then );. They both have the same output, it is just two ways of getting the same thing. But the second one avoids the hard coded: for(...), loop.

  •  Tags:  
  • c
  • Related