Thread: length of char data type.

  1. #1
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516

    length of char data type.

    i executed this bit of code

    [code]

    #include<stdio.h>
    int main()
    {
    char variable1;
    int variable2;
    printf("%x\n%x",&variable1,&variable2);
    return 0;
    }

    [\code]

    i got the out put for this as

    fff5
    fff2


    i use the borland 3.0 compiler and os is win98.
    what my question is that char is supposed to take 1 byte of memory space and int takes up 2.then the out put should have been fff5 fff3.

    why is it coming up as fff2 for variable2??

  2. #2
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    The compiler is showing their adress in memory since you didn't initialize them. If you want to see their size then use the sizeof() function with the %d format specifier (you were using %x wich shows numbers in hex with lower-case letters) :

    Code:
    printf("%d\n%d", sizeof(variable1), sizeof(variable2));

    *EDIT* %d is for decimal btw (just in case) , and you can use the following line directly if you want to check how big are 'char's and 'int's on your machine :

    Code:
    printf("%d\n%d", sizeof(char), sizeof(int));
    Last edited by Brain Cell; 01-09-2005 at 12:33 AM.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    Quote Originally Posted by Brain Cell
    The compiler is showing their adress in memory since you didn't initialize them. If you want to see their size then use the sizeof() function with the %d format specifier (you were using %x wich shows numbers in hex with lower-case letters) :

    Code:
    printf("%d\n%d", sizeof(variable1), sizeof(variable2));

    *EDIT* %d is for decimal btw (just in case) , and you can use the following line directly if you want to check how big are 'char's and 'int's on your machine :

    Code:
    printf("%d\n%d", sizeof(char), sizeof(int));

    even if u initialize it,it will do the same.tried it out.and what i want to know is why is that extra 1 byte being used for and for what.

  4. #4
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    sorry i misunderstood your question. I thought you wanted to know why were you getting that output instead of the variable's size (rule no.1 : never answer threads when you're so sleepy)


    The output in my machine is :
    12ff7c
    12ff78

    the size of 'int' is 4 here , so its working fine for me.
    Last edited by Brain Cell; 01-09-2005 at 12:43 AM.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  5. #5
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    it is workin properly for u mate.dont know what is happenin with my compiler.which compiler do u use by the way??

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    alignment is pretty much up to the implementation, so there are no hard and fast rules to determine that.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    i re-compiled it on Dev C++ and got :

    240ff5f
    240ff58

    So im guessing that variables dosen't necessarily get contiguous locations in memory.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  8. #8
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    Quote Originally Posted by Sebastiani
    alignment is pretty much up to the implementation, so there are no hard and fast rules to determine that.
    dont get what you mean to say.

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    well, they're contiguous but potentially padded with extra bytes.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    when the compiler sets up the stack frame in a function it may for instance choose to align all data on four byte boundaries to speed up CPU accesses, causing a single char to take up an extra three bytes of space. it's totally up to the implementation.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  11. #11
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    does that mean that char can sometimes take up 2 bytes??is the compiler padding up some space after the first char??i tried this with a structure as well.and found that there is one extra byte in it,even though the sizeof() func returns the size as less than what the number of bytes are used.please comment.

  12. #12
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159
    PING try this code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    
    printf("\01\32 sizeof ( int ) = %i  \n \02\32 sizeof ( char ) = %i\n",sizeof(int),sizeof(char));
    
    printf("\01\32 sizeof ( int* ) = %i  \n \02\32 sizeof ( char* ) = %i\n",sizeof(int*),sizeof(char*));
    
    return 0;
    }

    i think that is clear...

  13. #13
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    Quote Originally Posted by enjoy
    PING try this code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    
    printf("\01\32 sizeof ( int ) = %i  \n \02\32 sizeof ( char ) = %i\n",sizeof(int),sizeof(char));
    
    printf("\01\32 sizeof ( int* ) = %i  \n \02\32 sizeof ( char* ) = %i\n",sizeof(int*),sizeof(char*));
    
    return 0;
    }
    very stupid .... bt made me smile tho ..neways,i think this is due to memory alignment .so will dig into that on google.

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >does that mean that char can sometimes take up 2 bytes??
    sizeof(char) will always be 1, but yes, the actual size can potentially be more than one byte.

    >i tried this with a structure as well.
    What you're doing is testing your implementation. None of your results are guaranteed to be the same for another implementation.

    >what my question is that char is supposed to take 1 byte of memory space and int takes up 2.
    Okay, but both &variable1 and &variable2 are pointers. What's the size of a pointer to char and a pointer to int? Maybe then your results will make more sense.
    My best code is written with the delete key.

  15. #15
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    i don't think that anyone said it very clearly so i'll take a stab at it....

    this line from the original post:

    printf("%x\n%x",&variable1,&variable2);

    says show me the address of variable1 and variable2 in hexidecimal format.. %x specifies the format. preceding a variable with '&' makes it return its memory address; not it's value or size.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  2. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  3. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM