C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-26-2008, 01:34 AM   #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
nikhil22 is offline   Reply With Quote
Old 07-26-2008, 02:21 AM   #2
and the hat of vanishing
 
Salem's Avatar
 
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   Reply With Quote
Old 07-26-2008, 04:47 AM   #3
Registered User
 
Join Date: Jun 2005
Posts: 1,343
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.
grumpy is offline   Reply With Quote
Old 07-26-2008, 05:10 AM   #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:
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?
nikhil22 is offline   Reply With Quote
Old 07-26-2008, 07:20 AM   #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   Reply With Quote
Old 07-26-2008, 09:44 AM   #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   Reply With Quote
Reply

Tags
char, memory, minimum, printf, store

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 02:37 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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