You can only initialize a variable when you define it. For instance
char ch = 'a';
defines ch
as a char
and initializes it with the character literal 'a'
. If you had
char ch;
//...
ch = 'a';
then you are no longer initializing ch
but instead you are assigning to it. Until you reach ch = 'a';
ch
has some unspecified value1 and using its value before you reach ch = 'a';
would be undefined behavior. Ideally you should always initialize a variable so it has a known state and since you can declare a variable at any point you can hold off on declaring the variable until you know what you are going to initialize it or when you are going to set it immediately. Example
//some code
// oh, I need to get a char from the user
std::cout << "Yo, user, give me a character: ";
char ch; // no initialization but it is okay as I set it immediately in the next line
cin >> ch; // now ch has a value
// more code
1: There are places will it will be initialized for you. For instance, if ch
was declared in the global scope then it would be zero initialized for you