![]() |
| | #1 |
| Registered User Join Date: Jul 2008
Posts: 2
| using char to store an integer in the range [128,127] This is a simple code i've attempted. Code: void main()
{
char n1 = 10;
printf("n1 is %d.",n1);
}
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 |
| nikhil22 is offline | |
| | #2 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| 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. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #3 | |||
| Registered User Join Date: Jun 2005
Posts: 1,343
| 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:
Quote:
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:
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. | |||
| grumpy is offline | |
| | #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 Quote:
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? | |
| nikhil22 is offline | |
| | #5 |
| Registered User Join Date: Dec 2006
Posts: 1,780
| 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. |
| cyberfish is online now | |
| | #6 |
| Registered User Join Date: Apr 2006
Posts: 1,193
| 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.
__________________ 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. Last edited by King Mir; 07-26-2008 at 09:46 AM. |
| King Mir is offline | |
![]() |
| Tags |
| char, memory, minimum, printf, store |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Selecting bits with Unions.... Not working; please help!! | dan56965 | C Programming | 12 | 08-11-2008 11:02 PM |
| Looking for constructive criticism | wd_kendrick | C Programming | 16 | 05-28-2008 09:42 AM |
| Converting a circulating mouse pointer to use a Potentionmeter | phoenix23 | C Programming | 16 | 10-29-2006 05:04 AM |
| newbie needs help with code | compudude86 | C Programming | 6 | 07-23-2006 08:54 PM |
| Strings are V important... | NANO | C++ Programming | 15 | 04-14-2002 11:57 AM |