Thread: Memory related question in C

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    20

    Memory related question in C

    I want to write a c program to display list of data types and their corresponsing memory allocation size

    There is any keyword in C which prints that "int" occupies "16" bits or such a kind of code is available ?

    Please give me some suggestions.

    Thanks
    TheDeveloper

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    the sizeof operator prints the number of bytes.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    20
    thanks for the quick answer

    If there any way to find print minimum and maximum value accupied by different data types ?

    say for example

    "int" ranges between "-32,768 -> +32,767"

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Macros for the integer types are in limits.h. Macros for the floating-point types are in float.h.

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    20
    Quote Originally Posted by tabstop View Post
    Macros for the integer types are in limits.h. Macros for the floating-point types are in float.h.
    can you please explain me with sample code to retrieve that data from limits.h ?

    thanks in advance

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    They're macros / constants -- see a list at http://www.cplusplus.com/reference/clibrary/climits/

    As for using them, checkout the tutorial on this website http://www.cprogramming.com/tutorial/cpreprocessor.html

  7. #7
    HelpingYouHelpUsHelpUsAll
    Join Date
    Dec 2007
    Location
    In your nightmares
    Posts
    223
    Quote Originally Posted by TheDeveloper View Post
    can you please explain me with sample code to retrieve that data from limits.h ?
    As zacs7 noted, they are constants and by the time the program is executed, the pre-processor would have already replaced every instance of "INT_MAX" w/ 1240 (as an example).
    long time no C; //seige
    You miss 100% of the people you don't C;
    Code:
    if (language != LANG_C && language != LANG_CPP)
        drown(language);

  8. #8
    Registered User
    Join Date
    Dec 2008
    Posts
    20
    Quote Originally Posted by zacs7 View Post
    They're macros / constants -- see a list at http://www.cplusplus.com/reference/clibrary/climits/

    As for using them, checkout the tutorial on this website http://www.cprogramming.com/tutorial/cpreprocessor.html
    Any luck that the values can be displayed using bitwise operator?

    thanks in advance

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by TheDeveloper
    Any luck that the values can be displayed using bitwise operator?
    No, since bitwise operators do not display anything. You can use bitwise operators with them, just as you can use bitwise operators with literals.
    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

  10. #10
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by TheDeveloper View Post
    Any luck that the values can be displayed using bitwise operator?

    thanks in advance
    Perhaps something like
    Code:
    unsigned min, max;
    min = 0;
    max = ~min;

  11. #11
    Registered User
    Join Date
    Dec 2008
    Posts
    20
    Quote Originally Posted by itCbitC View Post
    Perhaps something like
    Code:
    unsigned min, max;
    min = 0;
    max = ~min;
    can someone explain what exactly this code does?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by TheDeveloper
    can someone explain what exactly this code does?
    For an unsigned integer, zero must be the minimum value, so it simply sets that as such. It then flips all the bits of zero to give the maximum unsigned value.
    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

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by TheDeveloper View Post
    can someone explain what exactly this code does?
    Yes.

    (If you're asking about bitwise operators, you should at least be able to recognize them when you see them -- ~ is bitwise not, which means max is assigned the bitwise complement of min (it has a 1 where min has a 0 and vice versa.)

  14. #14
    Registered User
    Join Date
    Dec 2008
    Location
    Loch Ness
    Posts
    6
    if u write max = ~min, max will be stored with the 1's complement of min....

    As min is 0 max (in binary) will be 11111........1 31 times...

    Hence max gives the max value an unsigned int can store...

  15. #15
    Registered User
    Join Date
    Dec 2008
    Posts
    20
    Quote Originally Posted by laserlight View Post
    For an unsigned integer, zero must be the minimum value, so it simply sets that as such. It then flips all the bits of zero to give the maximum unsigned value.
    thanks for your answer

    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
     unsigned max,min;
     min = 0;
     clrscr();
     max = ~min;
     printf("%u",max);
     getch();
    }
     
    Output : 65535
    How to find similar way for floating point values ?

    thanks in advance

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char ** and argv memory question
    By simo_mon in forum C Programming
    Replies: 3
    Last Post: 08-17-2008, 08:47 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Question regarding constructors and memory.....
    By INFERNO2K in forum C++ Programming
    Replies: 6
    Last Post: 05-25-2005, 11:30 AM
  4. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM