Thread: Why does C assign values to arrays like this?

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    3

    Why does C assign values to arrays like this?

    Hi all,
    I am a java programmer learning C and was quite shocked when I wrote this simple array assignment program. C seems to assign values to arrays???

    Program:
    Code:
    #include <stdio.h>
    
    int main(void) {
     
     int values[10];
     int index;
     
     values[0] = 197;
     values[1] = 0;
     values[2] = -100;
     values[5] = 350;
     values[3] = values[0] + values[5];
     values[9] =
     values[5] / 10;
     --values[2];
     
     for (index = 0; index < 10; ++index)
    printf ("values[%i] = %i\n" , index , values[index]);
    
     getch();
     return 0;   
        
        
        
    }
    Output:

    values[0] = 197
    values[1] = 0
    values[2] = -101
    values[3] = 547
    values[4] = 0
    values[5] = 350
    values[6] = 2293616
    values[7] = 212
    values[8] = 204
    values[9] = 35


    I am using DevShed as IDE.

    Thanks for any feedback.

    /Can

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by FromJavatoC
    I am a java programmer learning C and was quite shocked when I wrote this simple array assignment program. C seems to assign values to arrays???
    What made you conclude that? Also, what does "assign values to arrays" actually mean to you?
    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
    Join Date
    Aug 2011
    Posts
    3
    Actually I figured it out. You shouldn't assume any value for uninitialized arrays.
    Assign value = initialize.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by FromJavatoC
    You shouldn't assume any value for uninitialized arrays.
    Ah, yes. In general, you should not access the value of a variable until the variable has been given an initial value.

    Quote Originally Posted by FromJavatoC
    Assign value = initialize.
    Not quite. Array initialisation syntax would be something like:
    Code:
    int values[10] = {197, 0, -100};
    The remaining elements would be zero initialised.
    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

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    3
    Yes , I think I get it now.

    thanks.

    /Can

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by FromJavatoC View Post
    Yes , I think I get it now.

    thanks.

    /Can
    What you were seeing in your original test run were the junk values in the memory where the array was created ( in this case on your program's stack).

    C is not Java... it does not babysit... there are no initializers, there is no garbage collector, there is no memory management, there is no type checking or bounds checking. If you want these things you have to do them yourself.

    One thing I can promise you is that C will make you into a very careful programmer... a skill seldom learned in managed code environments.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by CommonTater View Post
    C is not Java... it does not babysit... there are no initializers,
    C does initialise in some circumstances but not in others. Apart from that, you are correct. It is generally better to assume any variable you have not initialised needs to be initialised before you use it.

    Quote Originally Posted by CommonTater View Post
    One thing I can promise you is that C will make you into a very careful programmer... a skill seldom learned in managed code environments.
    I wish that was true.

    I would suggest the majority of C programmers I have seen are careless. There are also too many programmers who program to one particular compiler and have kittens when their code doesn't work with another compiler.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by grumpy View Post
    C does initialise in some circumstances but not in others. Apart from that, you are correct. It is generally better to assume any variable you have not initialised needs to be initialised before you use it.
    That's kind of like assuming the gun is empty.... I always intialize everything...

    I would suggest the majority of C programmers I have seen are careless. There are also too many programmers who program to one particular compiler and have kittens when their code doesn't work with another compiler.
    If you take a look at the Pelles C forums, you'll recognize one of the authors of addins and libraries... But I don't ever assume my stuff will work anywhere but on Pelles C...

    Portability is not generally an issue for me... If you've got source code for one of my bigger projects, then you're the thief who broke into my house and raided my safe... (i.e. I am no fan of "open source")

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    That's kind of like assuming the gun is empty.... I always intialize everything...
    I assume all guns are loaded, but with unknown ammunition. It may be too big to fit through the barrel, and blow up in my hand. I'd better check it myself.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by FromJavatoC View Post
    I am a java programmer learning C and was quite shocked when I wrote this simple array assignment program
    Java: Oh hai guyz!
    C#: OMG HI DUDE!
    C++: Hi kids.
    C: All of you get off my lawn!


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Java: Oh hai guyz!
    C#: OMG HI DUDE!
    C++: Hi kids.
    Pascal: Iligitimi non carborundum!
    C: All of you get off my lawn!

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I was going to reply with something ASM said, but there were two dialects, neither of which I could understand.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    I was going to reply with something ASM said, but there were two dialects, neither of which I could understand.
    Quzah.
    Java: Oh hai guyz!

    C#: OMG HI DUDE!

    C++: Hi kids.

    Pascal: Iligitimi non carborundum!

    ASM : Well, first I became aware of a secondary presence within my field of concern, then I incremented my attention pointer to achieve a value just short of bit level overflow, noticing that my accumulator was xor ed with my stack pointer as the experience mounted so I called a long jump to a subroutine designed to initialize my oratory mechanism, next I loaded a string from memory and looped across it causing an audible greeting of some inane kind then finally I settled back initializing my auditory input and preparing to record a timpanic greeting from the being in my newly expanded field of experience.

    C: All of you get off my lawn!
    Last edited by CommonTater; 08-12-2011 at 05:03 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assign values to a set of array of structures
    By glucosonte in forum C Programming
    Replies: 1
    Last Post: 08-26-2009, 08:10 AM
  2. Assign an arrays values to another array
    By laczfinador in forum C Programming
    Replies: 3
    Last Post: 05-06-2009, 07:46 AM
  3. assign values to string arrays
    By paulur in forum C Programming
    Replies: 3
    Last Post: 03-22-2006, 01:37 AM
  4. 3-d array assign string values
    By WaterNut in forum C++ Programming
    Replies: 8
    Last Post: 07-01-2004, 12:02 AM
  5. assign values to charechter variables
    By simhap in forum C++ Programming
    Replies: 5
    Last Post: 10-07-2001, 07:55 PM