eHow launches Android app: Get the best of eHow on the go.

How To

How to Initialize Variables in C

Contributor
By eHow Contributing Writer
(5 Ratings)

C does not initialize variables automatically, so if you do not initialize them properly, you can get unexpected results. Fortunately, C makes it easy to initialize variables when you declare them.

Difficulty: Easy
Instructions

    Initialize Variables at Declaration

  1. Step 1

    Initialize a variable in C to assign it a starting value. Without this, you will get whatever happened to be in memory at that moment, which leads to inconsistent behavior and irreproducible bugs that can be exceedingly difficult to track down.

  2. Step 2

    Add an initialization to the declaration. Just tack on an assignment right to the end of the declaration, like so:

    int x = 5;

  3. Step 3

    Know that initializing arrays works similarly, save that you must put multiple comma-separated values inside curly brackets. When doing this, you can leave off the array's size, and it will be filled in automatically:

    int month_lengths[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

  4. Step 4

    Take advantage of character strings. Character strings, which are really arrays of characters, also support a simpler format for initialization:

    char title[] = "My Program";

  5. Step 5

    Express either kind of array initialization in pointer format (since arrays are really pointers):

    int *month_lengths = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    char *title = "My Program";

  6. Step 6

    Remember that structures in C are initialized in the same way as arrays:

    struct role = { "Hamlet", 7, FALSE, "Prince of Denmark", "Kenneth Branagh"};

  7. Initialize Variables Manually

  8. Step 1

    Wait to initialize a variable at another place in the program if this will be clearer. For instance, a variable that will be the index of a for loop is usually best initialized in the for loop. This makes it easier for another programmer to read, since the initialization is near where it will be used.

  9. Step 2

    Initialize the data structure at the right time. If a data structure is going to be dynamically allocated with malloc() or a similar function, you can't initialize it until after it's allocated. However, in this case, what you're declaring is actually a pointer, which should still be initialized to NULL as a matter of course.

Tips & Warnings
  • It's good to get into the habit of initializing everything, even if you don't need to. The CPU overhead is trivial, and it'll save you from bugs that are hard to diagnose one day.
  • If you initialize a string variable at declaration and then modify it later, be sure not to overrun the original string's length. It's generally better to just declare a string and then initialize it manually with strcpy() if you plan to modify it during the program.
Subscribe

Post a Comment

Post a Comment

Related Ads

  • Have you done this? Click here to let us know.
I Did This
Tags
Get Free Internet Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy.   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.

Demand Media
eHow_eHow Technology and Electronics