Thread: Default Char Value - Simple Question

  1. #1
    Registered User jmpeer's Avatar
    Join Date
    Jan 2010
    Posts
    5

    Default Char Value - Simple Question

    Yo, look at this simple code:

    Code:
    #include <stdio.h>
    
    int main()
    {
        unsigned char bytes[2][100];
        int i = 0;
        for(i = 0; i < 200; i++)
        {
            printf("Byte[0][%-3d]: %-5d \t Byte[1][%-3d]: %-5d \n",i,bytes[0][i],i,bytes[1][i]);
        }
        return 0;
    }
    In C, does the datatype char have a default value?
    This gives me a random series of values.
    And even though the array only goes to 100, it prints up to 200 like I specified without error... and more random values.
    Why is that?
    Where are these numbers coming from?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jmpeer
    In C, does the datatype char have a default value?
    Not really. You might consider the null character as a default, but then there is no "default construction".

    Quote Originally Posted by jmpeer
    This gives me a random series of values.
    And even though the array only goes to 100, it prints up to 200 like I specified without error... and more random values.
    Why is that?
    Basically, your array is left uninitialised.

    Quote Originally Posted by jmpeer
    Where are these numbers coming from?
    Presumably whatever was present in memory.

    To answer your unasked question, you probably want to initialise, e.g.,
    Code:
    unsigned char bytes[2][100] = {""};
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    What you did was request that the compiler set up two blocks of memory each 100 chars in size. In your for loop you pretty much did a common mistake of overstepping your array bounds. Since you did not initialize your two chunks of memory, what the compiler gave you was two blocks of available memory that has not be locked or in use by another process. What ever is there was what has been given up by a prior process called from free() or what ever other memory utility that will free memory up.

  4. #4
    Registered User jmpeer's Avatar
    Join Date
    Jan 2010
    Posts
    5
    Wow. C doesn't really take care of anything for you.

    Alright.

    That's all I need to know.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    That's why it's fast. There is nothing "done for you" to waste time when you didn't need it done.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sending a simple email in C++?
    By Coukapecker in forum C++ Programming
    Replies: 6
    Last Post: 04-09-2010, 12:36 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  4. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM