Declared array values

This is a discussion on Declared array values within the C Programming forums, part of the General Programming Boards category; I am completely new to C and am working my way through a book (The C Programming Language). I have ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    2

    Declared array values

    I am completely new to C and am working my way through a book (The C Programming Language). I have was messing with changing some of the example code and came across some "strange" default values for arrays.

    When I declare an array, I'm expecting all the values to zero. However, when I loop through, some have a negative value, I am curious as to why.

    So the code is:

    Code:
    #include <stdio.h>
    
    main()
    {
    	int i, ndigit[10];
    	
    	printf("A: \n0: %d\n", ndigit[0]);
    	printf("1: %d\n", ndigit[1]);
    	printf("2d: %d\n", ndigit[2]);
    	printf("2f: %f\n", ndigit[2]);
    	printf("3: %d\n", ndigit[3]);
    	printf("4: %d\n", ndigit[4]);
    	printf("5: %d\n", ndigit[5]);
    	printf("6: %d\n", ndigit[6]);
    	printf("7: %d\n", ndigit[7]);
    	printf("8: %d\n", ndigit[8]);
    	printf("9: %d\n", ndigit[9]);
    
    	for (i = 0; i < 10; ++i)
    	{
    		ndigit[i] = 0;
    	}
    	
    	printf("B: \n0: %d\n", ndigit[0]);
    	printf("1: %d\n", ndigit[1]);
    	printf("2: %d\n", ndigit[2]);
    	printf("3: %d\n", ndigit[3]);
    	printf("4: %d\n", ndigit[4]);
    	printf("5: %d\n", ndigit[5]);
    	printf("6: %d\n", ndigit[6]);
    	printf("7: %d\n", ndigit[7]);
    	printf("8: %d\n", ndigit[8]);
    	printf("9: %d\n", ndigit[9]);
    }
    which outputs:

    Code:
    A: 
    0: 0
    1: 0
    2d: -1881143876
    2f: -0.000000
    3: 0
    4: 0
    5: 0
    6: 0
    7: 0
    8: 0
    9: -1881139893
    B: 
    0: 0
    1: 0
    2: 0
    3: 0
    4: 0
    5: 0
    6: 0
    7: 0
    8: 0
    9: 0
    I am obviously expecting the B output to all be 0 as I explicitly zero every value. However, why is A2 -1881143876 output as an integer, but -0.000000 as a float. And why is A9 a different value?

    This is compiled on Mac OSX.

    Thanks.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    The C standard makes no guarantees that data will be initialized to 0 at the time of creating them. They simply inhabit whatever happens to be in memory at that time.
    Also read: http://cpwiki.sourceforge.net/Implicit_main
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Values which are not initialized have an indeterminate value when they are created (except for a few exceptions). To initialize all your array elements to all bits zero, do this:
    Code:
    int array[10] = { 0 } ;

  4. #4
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    983
    However, why is A2 -1881143876 output as an integer, but -0.000000 as a float.
    First, floating point and integral representations aren't the same; that is, the bits that represent a particular number as an integer don't represent the same number when interpreted as a floating point value. The following code is not correct C, but it might show the difference. It assumes that floats and ints are the same size.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int a;
    
      a = 1178658486;
      printf("%d\n%f\n", a, *(float *)&a);
    
      return 0;
    }
    Second, %f expects a double; you're passing an int. It's likely that on your system ints and doubles are different sizes, thus printf() will read more than you've given it. On a typical desktop system, it will take 4 bytes from your int, and 4 bytes from the memory adjacent to your int. Who knows what's there?

    In reality, passing an int to %f is undefined behavior regardless of the sizes of the various types, but you can make assumptions about what will happen on most systems. Just don't write real code that relies on such assumptions!

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    2
    Righto, thanks for the explanations

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Inputted values into an array
    By Panther in forum C Programming
    Replies: 6
    Last Post: 04-11-2003, 10:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21