Thread: using char to store an integer in the range [128,127]

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

    using char to store an integer in the range [128,127]

    Hi,
    This is a simple code i've attempted.
    Code:
    void main()
    {
    char n1 = 10;
    printf("n1 is %d.",n1);
    }
    Since char occupies 1 byte, n1 occupies 1 byte.
    But in the printf(), n1 is converted into an int (4 bytes). So will the program reserve 4 bytes for n1?
    I'd like to know if there's a way to display a small number stored in a char variable without converting it to int in the printf(), i.e. without using %d.

    I've tried %c, but it doesnt display 10 as required.

    The only point of asking this question is how to code this program so that it occupies minimum memory.....to the extent of saving even 3 bytes.

    Thank you
    Nikhil

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    1. main returns int - see the FAQ

    2. Since you're probably using a 32-bit machine, if you didn't use them, they would be wasted anyway.

    > The only point of asking this question is how to code this program so that it occupies minimum memory
    Not using printf would save a hell of a lot. It is a rather complicated function.
    puts(), putchar() and your own int-to-str conversion.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by nikhil22 View Post
    Hi,
    Since char occupies 1 byte, n1 occupies 1 byte.
    Pedantically speaking, that depends on what a "byte" is on the target machine. However, sizeof(char) is 1, by definition, in the standards. And on most machines sizeof() yields a number of bytes.
    Quote Originally Posted by nikhil22 View Post
    But in the printf(), n1 is converted into an int (4 bytes). So will the program reserve 4 bytes for n1?
    No it will not. However, in C, when passing it as an argument to printf() the value of n1 is converted to an int, which implicitly means a temporary int value is created to hold a copy of the value in n1. C++ is, strictly speaking, a little different from that (some implicit conversions done when passing variables by value to functions differ from C).
    Quote Originally Posted by nikhil22 View Post
    I'd like to know if there's a way to display a small number stored in a char variable without converting it to int in the printf(), i.e. without using %d.

    I've tried %c, but it doesnt display 10 as required.
    That's because the %c format descriptor outputs the character verbatim. A character with value of 10 is (in ANSI character set) a line feed, not the output 1 and 0.

    Take note that the output you are seeking is two characters: getting two character output from one character input involves some form of conversion.
    Quote Originally Posted by nikhil22 View Post
    The only point of asking this question is how to code this program so that it occupies minimum memory.....to the extent of saving even 3 bytes.
    As Salem said, the simpler way to save memory is to avoid using printf() [that will typically save 100s or Ks of bytes on most machines, as printf() is a large function that does a lot of things] and to do your own conversion (char to string or char to output) in another way.

    Have you actually measured the memory usage of your program, and identified a specific need to save those three bytes? Or are you just making an arbitrary decision to save those three bytes? My guess, based on your question, is that you're doing the second: and that is premature optimisation - if you'd done things rigorously, you would have eliminated the printf() call already, and saved an order of magnitude (at least) more memory than three bytes.

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    2
    Thanks Salem and grumpy!
    Like grumpy put it, my approach was definitely the premature optimisation type.

    >>grumpy
    if you'd done things rigorously, you would have eliminated the printf() call already, and saved an order of magnitude (at least) more memory than three bytes.
    Which tools can be used to check memory usage?
    I'm using DevC++ and haven't seen any way to do it....The only crude way i'm aware of is by Ctrl+Alt+Del!!!
    Or should I use a debugger?

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Given your apparent lack of experience in this language, I would say forget about it for now, and put your effort into learning the language basics. That would be a lot more useful.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Char will always be converted to int when passed to functions like printf.

    Function calls only use the memory preallocated for your program stack. This is also where local variables are stored. The Stack is always of fixed size and allocated when your program starts; using more or less Stack memory will not effect how much memory your program has. The only way to change the size of the Stack is with compiler options.

    It is still possible to use too much of the stack -- that's called stack overflow. That usually doesn't happen with well written programs unless lots of recursion is used, you you have a very large array. But unless you are getting stack overflows, it doesn't matter how much of the stack you use. And if you are getting stack overflows, its more likely that you have errors in recursive functions or your arrays are too big.
    Last edited by King Mir; 07-26-2008 at 09:46 AM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM

Tags for this Thread