Initializing variables in C

后端 未结 10 691
长情又很酷
长情又很酷 2020-12-02 12:21

I know that sometimes if you don\'t initialize an int, you will get a random number if you print the integer.

But initializing everything to zero seems

相关标签:
10条回答
  • 2020-12-02 13:03

    If the variable is in the scope of of a function and not a member of a class I always initialize it because otherwise you will get warnings. Even if this variable will be used later I prefer to assign it on declaration.

    As for member variables, you should initialize them in the constructor of your class.

    For pointers, always initialize them to some default, particularly NULL, even if they are to be used later, they are dangerous when uninitialized.

    Also it is recommended to build your code with the highest level of warnings that your compiler supports, it helps to identify bad practices and potential errors.

    0 讨论(0)
  • 2020-12-02 13:05

    Initializing a variable, even if it is not strictly required, is ALWAYS a good practice. The few extra characters (like "= 0") typed during development may save hours of debugging time later, particularly when it is forgotten that some variables remained uninitialized.

    In passing, I feel it is good to declare a variable close to its use.

    The following is bad:

    int a;    // line 30
    ...
    a = 0;    // line 40
    

    The following is good:

    int a = 0;  // line 40
    

    Also, if the variable is to be overwritten right after initialization, like

    int a = 0;
    a = foo();
    

    it is better to write it as

    int a = foo();
    
    0 讨论(0)
  • 2020-12-02 13:06

    A rule that hasn't been mentioned yet is this: when the variable is declared inside a function it is not initialised, and when it is declared in static or global scope it's set to 0:

    int a; // is set to 0
    
    void foo() {
      int b;  // set to whatever happens to be in memory there
    }
    

    However - for readability I would usually initialise everything at declaration time.

    If you're interested in learning this sort of thing in detail, I'd recommend this presentation and this book

    0 讨论(0)
  • 2020-12-02 13:08

    As long as I have not read from a variable before writing to it, I have not had to bother with initializing it.

    Reading before writing can cause serious and hard to catch bugs. I think this class of bugs is notorious enough to gain a mention in the popular SICP lecture videos.

    0 讨论(0)
  • 2020-12-02 13:11

    Static and global variables will be initialized to zero for you so you may skip initialization. Automatic variables (e.g. non-static variables defined in function body) may contain garbage and should probably always be initialized.

    If there is a non-zero specific value you need at initialization then you should always initialize explicitly.

    0 讨论(0)
  • 2020-12-02 13:12

    It's always good practice to initialize your variables, but sometimes it's not strictly necessary. Consider the following:

    int a;
    for (a = 0; a < 10; a++) { } // a is initialized later
    

    or

    void myfunc(int& num) {
      num = 10;
    }
    
    int a;
    myfunc(&a); // myfunc sets, but does not read, the value in a
    

    or

    char a;
    cin >> a; // perhaps the most common example in code of where
              // initialization isn't strictly necessary
    

    These are just a couple of examples where it isn't strictly necessary to initialize a variable, since it's set later (but not accessed between declaration and initialization).

    In general though, it doesn't hurt to always initialize your variables at declaration (and indeed, this is probably best practice).

    0 讨论(0)
提交回复
热议问题