Basic array question(s)

This is a discussion on Basic array question(s) within the C Programming forums, part of the General Programming Boards category; Code: int ibuf[5]; // or even int ibuf[5] = {1,2,3,4,5}; printf("Size of ibuf: %i\n", sizeof(ibuf)); Output Code: Size of ibuf: ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    111

    Basic array question(s)

    Code:
    int ibuf[5];
    // or even int ibuf[5] = {1,2,3,4,5};
    
    printf("Size of ibuf: %i\n", sizeof(ibuf));
    Output
    Code:
    Size of ibuf: 20
    Why? Is this the size of the array in bytes? How do I get the number of elements?

    Why then, does this return what I expect it to?
    Code:
    char cbuf[5] = "Hello";
    
    printf("Size of cbuf: %i\n", sizeof(cbuf));
    Output:
    Code:
    Size of cbuf: 5

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,705
    Quote Originally Posted by cnewbie1
    Why? Is this the size of the array in bytes?
    Yes.

    Quote Originally Posted by cnewbie1
    How do I get the number of elements?
    With the expression:
    Code:
    sizeof(ibuf) / sizeof(ibuf[0])
    Note that ibuf really has to be the array, not a pointer to an element of the array.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    28
    sizeof(int)=4

    sizeof(char)=1

    thats why your output is 20 with 5 integers.

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    Quote Originally Posted by mrbains View Post
    sizeof(int)=4

    sizeof(char)=1

    thats why your output is 20 with 5 integers.
    Sorry, a bit slow here. That I didn't get :-/

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Each element takes 4 bytes. 4 x 5 elements = 20.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by cnewbie1 View Post
    Sorry, a bit slow here. That I didn't get :-/
    If you're looking suss out the number of elements in an array, take the size of the array and divide it by the size of each element...

    Code:
    int x;  
    int y;
    int z;
    int array[10];   
    
    x = sizeof(int);  // 4 because each int needs 4 bytes.
    y = sizeof(array); // 40 bytes
    z = y / x;  // 10 elements
    
    // or
    
    int ArraySize = sizeof(array) / sizeof(int);
    (I like laserlight's solution better, btw)

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    Ok. An int needs 4 bytes. But a char actually needs only one?

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    28
    type this code and run it.

    Code:
    #include<stdio.h>
    
    main()
    { 
      printf("size of int =%i",sizeof(int));
      printf("\nsize of char =%i",sizeof(char));
    
    }
    your output should be int=4 and char=1.
    i guess you are using amd??
    anyways on different machines you will find different resuilts.Like sizeof(int) on my machine is 2 bytes.
    and so you can do for any data type.

  9. #9
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    No, I'm on a new intel processor.

    Ok, so this is processor-dependent, and I'll have to check this for every processor type? Nice to know :-)

    My previous programming experience is with Java and PHP, so it's taking a while to get used to not having all those abstract functions like count(array), string data type etc. etc. Thanks for the answers :-)

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    28
    Quote Originally Posted by cnewbie1 View Post
    No, I'm on a new intel processor.

    Ok, so this is processor-dependent, and I'll have to check this for every processor type? Nice to know :-)

    My previous programming experience is with Java and PHP, so it's taking a while to get used to not having all those abstract functions like count(array), string data type etc. etc. Thanks for the answers :-)
    Well i dont really know on what this depends even me i was interested to know your processor because i've seen sizeof(int)=4 on amd.
    i am on intel too but my int is 2 bytes.
    so may be it depends on the compiler in the pre-processor directives, or your operating system 64 bit or 32 bit.

  11. #11
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    Yup, it might depend on several factors. My processor is (naturally) 64-bit, but my OS is 32-bit. Furthermore, I'm using Linux and gcc.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by cnewbie1 View Post
    No, I'm on a new intel processor.
    Ok, so this is processor-dependent, and I'll have to check this for every processor type? Nice to know :-)
    No it's not processor dependent... It's compiler dependent. In a given compiler you can safely expect the sizes always remaining the same.

    The reason variables are different sizes is pretty simple... underlying the various types is binary storage. A 1 byte variable can only store values in the range from -127 to +127 ... 2 bytes gives you -32767 to +32767... and so on. So it's a question of capacity. Some variables can be up to 16 bytes (again dependending on the compiler in use).


    BTW... if sizeof(Int) is returning 2 bytes, that's a 16 bit compiler... Unless you are working on microcontrollers, it is seriously outdated. There are tons of perfectly good free 32 and 64 bit compilers out there. Many OS versions won't even run 16 bit code anymore...
    smorgasbordet - Pelles C

    My previous programming experience is with Java and PHP, so it's taking a while to get used to not having all those abstract functions like count(array), string data type etc. etc. Thanks for the answers :-)
    C is an extensible language. That is to say you can write your own libraries of functions and include them into your projects. Many of the so called abstractions like getting the number of elements in an array, flipping the sign of a variable etc. are very easily written and can be built into a static library and called as needed. This may not be standard C, but so long as you are willing to share the library with anyone who has to compile your code it's no problem.

    Someplace in my dusty old archives I believe I still have a string library I wrote years ago (when first learning C) that uses Pascal style strings instead of the C standard... That's part of the joy of working this close to the System... If you don't like it... you can re-create it to your liking.

  13. #13
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    Quote Originally Posted by CommonTater View Post
    BTW... if sizeof(Int) is returning 2 bytes, that's a 16 bit compiler... Unless you are working on microcontrollers, it is seriously outdated. There are tons of perfectly good free 32 and 64 bit compilers out there. Many OS versions won't even run 16 bit code anymore...
    smorgasbordet - Pelles C
    Hmm...I don't remember anyone mentionining 2 bit int size here. It would be seriously strange with a 16-bit version of gcc in a new Linux distro.

    As to the other points you mention, I absolutely agree. That's the main reason I want to learn C, along with the fact that there seems to be fewer and fewer C-programmers out there (at least here in Norway). .NET and Java seems to be the main focus of everyone learning programming nowadays, and the demand for those skills seems to just grow and grow. If I had any economical sense, I'd learn .NET and C#. But, that me :-)

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,583
    Quote Originally Posted by CommonTater View Post
    No it's not processor dependent... It's compiler dependent. In a given compiler you can safely expect the sizes always remaining the same.
    Well, technically it is processor and platform dependent, and the compilers adhere to those requirements.
    int is typically described as a variable whose size is of the native size of the processor. So on 16 bit cpu, it would 16 bits, on 32 bit cpu it would be 32 bits, and so on.
    Obviously, using a 32-bit integer on a 16-bit cpu would harm performance, so the compiler takes advantage of this when deciding what size the types should be.

    Quote Originally Posted by cnewbie1 View Post
    As to the other points you mention, I absolutely agree. That's the main reason I want to learn C, along with the fact that there seems to be fewer and fewer C-programmers out there (at least here in Norway). .NET and Java seems to be the main focus of everyone learning programming nowadays, and the demand for those skills seems to just grow and grow. If I had any economical sense, I'd learn .NET and C#. But, that me :-)
    You may also want to check out C++. It is even more extensible.
    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.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Elysia View Post
    Well, technically it is processor and platform dependent, and the compilers adhere to those requirements.
    int is typically described as a variable whose size is of the native size of the processor. So on 16 bit cpu, it would 16 bits, on 32 bit cpu it would be 32 bits, and so on.
    Obviously, using a 32-bit integer on a 16-bit cpu would harm performance, so the compiler takes advantage of this when deciding what size the types should be.
    Ideally you are right... but in practice it ends up being the compiler. I say this because I use a 32 bit compiler on a 64bit CPU and several people here are using 16bit compilers on 32 and 64 bit systems. Granted some updating is probably the best bet but the compiler does seem to be calling the shots.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Two basic questions about generated assembly
    By George2 in forum Windows Programming
    Replies: 2
    Last Post: 07-16-2008, 03:19 AM
  2. two array questions
    By |Wiz| in forum C++ Programming
    Replies: 4
    Last Post: 04-05-2006, 05:06 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM

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