LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-29-2010, 02:29 PM   #1
darkstarbyte
Member
 
Registered: May 2010
Location: 3 planets away from the sun.
Distribution: Slackware and Fedora
Posts: 234

Rep: Reputation: 2
Post Most efficient way to use constants in C.


What is the best way to use constants in C? I have heard that constants can save ram but my question is do you use it on immediate values, variables that don't change, or codes that repeat them selves a lot?

What is the best way to use constants to save ram?
 
Old 10-29-2010, 03:31 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,235

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Well, this is how you use them:
Code:
#DEFINE MAX 1
...
count = MAX;
The C code is then compiled in two steps. First, the preprocessor does a search-and-replace and replaces every instance of "MAX" with "1". Then the code is compiled as normal.

As you've guessed by now, you use constants to represent named constants. Another example would be:
Code:
#define PI 3.14
Their usual use is to make code more readable, by giving human-readable labels to machine-readable values.

Now please tell us where you learned about using constants to save RAM. That's not what they're for. The amount they save is absolutely negligible unless you're deploying to an embedded controller, and probably is even then.

Last edited by dugan; 10-29-2010 at 03:38 PM.
 
Old 10-29-2010, 05:15 PM   #3
darkstarbyte
Member
 
Registered: May 2010
Location: 3 planets away from the sun.
Distribution: Slackware and Fedora
Posts: 234

Original Poster
Rep: Reputation: 2
My programming book said they used it to save ram in the 80's then he said its not a big deal anymore but I thought I would ask.
 
Old 10-29-2010, 06:49 PM   #4
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Rep: Reputation: 120Reputation: 120
It's not a big deal because everyone has so much RAM and because compilers can do a hell of a lot of optimization these days! It's always worth sticking to principles though.
 
Old 10-29-2010, 08:50 PM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,235

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Do keep in mind that the compiler needs to allocate a temporary variable for the value of the constant anyway.

Quote:
Originally Posted by devnull10 View Post
It's always worth sticking to principles though.
If you were worried about this then you wouldn't be using functions either. Pushing function addresses and their parameters onto the stack takes memory too.

Of course, in the 80s the speed difference between compiled C code and assembler was still tangible, so that might actually have been the case.

Today's principle is to code for maintainability, and wait until you know what needs to be optimized before you start optimizing.

Last edited by dugan; 10-29-2010 at 09:06 PM.
 
Old 10-30-2010, 12:41 AM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Without using named constant it's impossible to effectively maintain code, especially, code written by somebody else.

Look up:

DRY (Don't Repeat Yourself) principle
single point of change
.
 
1 members found this post helpful.
Old 10-31-2010, 01:08 PM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by devnull10 View Post
It's not a big deal because everyone has so much RAM
Only slightly off-topic:

This is a general programming forum, so it isn't necessarily the case that RAM is virtually unlimited (pun intended), for all of us. There are still small processors and micro-controllers that have limited memory, and that also have distinct types of memory, such as ROM/EPROM and RAM. In that context, the term 'constant' takes on additional meaning, because a value stored in ROM cannot be written (in fact, the CPU instruction set may have no instruction to do so). If the CPU boots from ROM, then some 'constants', like literal strings, may need to be copied from ROM to RAM by the startup code. Keeping track of what is writable, and what is not then becomes important.

Plus, what Sergei said...

--- rod.
 
1 members found this post helpful.
  


Reply

Tags
programing, programmer, programming language, random



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C - Constants in printf marcojrfurtado Programming 2 11-26-2009 09:14 AM
Newbie C Question on character constants. paulclark Programming 1 10-20-2009 07:21 PM
c: define constants kpachopoulos Programming 5 08-19-2006 03:31 PM
LXer: Variables Won't Constants Aren't: LXer Syndicated Linux News 0 07-26-2006 04:33 PM
CSS - Constants? dushkinup Programming 1 05-02-2004 01:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration