An Example from Lafore book

Hello! Please explain why in listing below if variables are not initialized the compiler notificate an error? I mean these variables:int chCount=0;int wdCount=0;char ch='a'; I cant get why if they are filled by garbage(number in memory of each variable) the compiler doesn't initializes them with 0 by itself? Please explain if you don't might :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "stdafx.h"
#include "stdlib.h"
#include <iostream>
#include <iomanip>
#include <conio.h>
using namespace std;
 
int main()
{
  int chCount=0;
  int wdCount=0;
  char ch='a';
  while(ch!='\r'){
  ch=getche();
  if (ch==' ') wdCount++;
  else chCount++;
  }
  cout<<"\n spaces are:"<<wdCount<<endl;
  cout<<"\n symbols are:"<<chCount<<endl;
    system("pause");
    return 0;
}
Last edited on
First off, you've made an error in the #include 's. You've not included the #include <iostream> as the first #include, which is a mistake.

I also can't understand what you are asking here, the code seems to work well enough. Are you asking why the compilers don't automatically make char's and int automatically a null? because it does. In this particular exctract of code, you could have quite easily done something like:

1
2
3
4
5
6
7
8
9

int main()
{
  int chCount;
  int wdCount;
  char ch;
  while(ch!='\r'){
  ch=getchar();
     /*...*/


There are a few exceptions to rules, where defining them is the only solution, which would be things like: global variables, enums... you get the general idea.
@badboy: The compiler doesn't initialize the variables automatically in many cases. This is the same as it works in C and the reason is probably efficiency.

First off, you've made an error in the #include 's. You've not included the #include <iostream> as the first #include, which is a mistake.

It's not necessary to include <iostream> first. If the order of the #include's matter there is something wrong with the headers.

Are you asking why the compilers don't automatically make char's and int automatically a null? because it does.
No it doesn't. You shouldn't use the variables if they are uninitialized.
Last edited on
1
2
3
4
5
6
7
8
int main()
{
  int chCount;
  int wdCount;
  char ch;
  while(ch!='\r'){
  ch=getchar();
     /*...*/


@Ben Duncan if i'll make these variables uninitialized there will be an error message from compiler - "variable 'ch' is being used without being initialized."
because it's the first variable that it meets. So all variables must be initialized before compilation. I want to understand why the complier doesn't initializing them to null automatically. I guess it made by design for some purposes, and I interest for what?
Last edited on
@Peter87 sorry, not noticed your message. Thank you very much for answer and explaination.
Last edited on
@Ben Duncan if i'll make these variables uninitialized there will be an error message from compiler - "variable 'ch' is being used without being initialized."
because it's the first variable that it meets. So all variables must be initialized before compilation.


There should NOT be an error from the compiler, because it is NOT an error. Many compilers will give a warning, and you can choose to make a warning an error if you like, but that's up to you.

In C (and C++) there is a guiding principle that you should not have to pay for what you don't use. There are many cases where variables will not be initialised; why should I have to pay the extra processor cycles to set them to some default value? If I want them set to some initial value, I can do that.
@Moschops Thanks. Now I fully understand this issue.
Topic archived. No new replies allowed.