All in one forum  - Applications | Games | E-Books | Music, Movies & Videos | Mobile Stuff | Live Discussions | Webmaster Stuff | Many More | Community to Hang Out & Stick to
Mark Forums Read

Go Back   Home > Tutorial Section > Programming > C and C++
Reload this Page SYMBOLIC CONSTANTS (#define)
Forgot Password? Join Us!
C and C++ Post your C and C++ Related Tutorial Here

Notices
Free Image Hosting Solution for all Your link here @ 10$ 10 K Unique Visitors 1 Million Visitors Per Month Your link here @ 10$

Your Ad Here

Rate This Thread - SYMBOLIC CONSTANTS (#define).

Post New Thread Reply
 
LinkBack Thread Tools Display Modes
  (#1 (permalink)) Old
 
KoOL's Avatar
 
User Info
Join Date: Oct 2007
Location: In You Mind
Age: 27
Achievements Posts: 10,862
Casino Cash: $1145330

Total Points: 237,899,711,998.76
Donate

KoOL has disabled reputation

Awards Showcase
Administrator Of the Month Of Jan 
Total Awards: 1
SYMBOLIC CONSTANTS (#define) - 01-08-2008, 04:47 PM

A symbolic constant is a symbol, often similar to a variable name, which represents one and only one value. You can think of them as variables that never change (not while the program is running anyway). Often times in a program you might want to use a single value for something over and over. Well, that's what symbolic constants are for. Before I go on, let's see how to make a symbolic constant.
HTML Code:
#include <stdio.h>

#define PI 3.14

int main()
.
.
First you type #define then a symbol, then the value it represents. Although it is not required, C programmers usually make the symbol all uppercase. Never end #define statements with a semicolon. Symbolic constants should always be defined above the main function. That's why I included that other stuff, so you could see where they go. I usually try to put them right after the #include things. Basically, this tells the compiler to use the value 3.14 every time it sees the symbol PI. Now, you can use PI all through your program instead of 3.14. Why bother? Well, let's say you wrote a big math program, and you use PI a dozen times. Then you decided that you needed more precision; instead of 3.14 you want 3.14159. If you used #define to make a constant, then all you have to do is change the value once. If you actually typed in 3.14 all the time, you will need to change it a dozen times.
In our guessing game in the section on functions we probably should have used a constant for our range. Although we only needed it once anyway, #defines are usually easier to find than hunting through a program. As a rule of thumb, if a value is not going to change throughout your program, it should be a symbolic constant.
Here is the guessing game program revised with a constant.
HTML Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define RANGE 10

//function prototypes//
void seedrnd();
int  rnd( int );

int main()
{
	int guess, num;

	/* function call to rnd which will return a random number between 1 and 10*/

	num = rnd( RANGE );

	printf("I have chosen a number between 1 and 10!\n");
	printf("Enter guess: ");

	scanf("%d", &guess);

	if( guess == num )
		printf("You guessed it! Bully for you!\n");
	else
		printf("I'm sorry, the answer is %d\n", num);

	printf("Please play again\n");

	return 0;
}

// function definitions //

void seedrnd()
{
	// Used to set up the random number generator //
	srand( time(NULL) );
}

int  rnd( int range)
{
	int num;

	/* call to seedrnd */
	seedrnd();

	/* The % thing divides the numbers, but gives us the remainder
	   of the division, instead of the answer */

	num = rand() % range;	//rand() gets a random number
	num++;			//Add one to get rind of zeros

	return num;
}
Fairly simple. When rnd is called, the value ten will be passed to it. If we change the #define line to this:
#define RANGE 100
Then when rnd is called the value 100 will be passed. Actually, we don't need to pass any parameters at all in this program. Symbolic constants are usable by all functions, not just main. So, in the rnd function we could have used the constant RANGE after the modulus operator. Constants are not limited to numeric values. If you want a constant string you can do that too. It would look like this:

#define ADVICE "Stop, drop, and roll"
You might use it like this: puts(ADVICE); or any other place you would use a string.
TECHNICAL NOTES

*Remember, anything with a # in front of it is called a preprocessor directive.
*Before a program is actually compiled, this thing called a preprocessor finds all the symbols you defined and replaces the text in your file with the values they represent.
*The expression "rule of thumb" comes from the ruling of an 1800's judge who said it was ok for a man to beat his wife as long as the stick he used was no wider than his thumb.



For Download Links U need to Press Thanks Button

Please don't PM me For Posts, Request and Not Working Threads. Use Button.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Click here to Donate to remove the Adverts.
Your Ad Here
Post New Thread Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Differential Equations with Symbolic Computation sid003 Ebooks,tutorials & Other Stuff 0 11-22-2007 03:30 PM
Can you define CTR, AdWords, Adsense,... pamela AdSense 0 09-13-2007 07:59 PM


New To AiO Forum? Need Help?

All times are GMT. The time now is 10:40 PM.
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
vBulletin Skin developed by: vBStyles

RapidShare Links PhazeDDL Warez
PhazeDDL Warez