Aioforum.com's Free Rapidshare Downloads All in one Premium Account

Your Link here @20$ Free Dedicated Rapidshare Premium Account Your Link here @20$ Join NFO Competition
Go Back   Home > Aio Forums Zone > Tutorial Section > Programming > C and C++
Better than google adsense
Forgot Password? Join Us!

Notices

Your Ad Here


Post New Thread Reply
 
LinkBack Thread Tools Display Modes
Old 01-08-2008, 04:47 PM   #1
 
KoOL's Avatar

 
User Info
Join Date: Oct 2007
Location: In Your Mind
Age: 27
Achievements Posts: 11,154
Casino Cash: $1201280

Total Points: 242,699,873,505.84
Donate

KoOL has disabled reputation

Awards Showcase
Administrator Of the Month Of Jan 
Total Awards: 1
SYMBOLIC CONSTANTS (#define)

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.
KoOL is offline  
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.
Old 11-04-2008, 05:44 AM   #2

 
User Info
Join Date: Nov 2008
Location: NEW DELHI INDIA
Age: 34
Achievements Posts: 1
Casino Cash: $500

Total Points: 145.02
Donate

Reputation: 10
bharat25 is on a distinguished road


Re: SYMBOLIC CONSTANTS (#define)

good one
thanks
bharat25 is offline  
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.
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 08:59 PM


These are the 125 most used thread tags
Tag Cloud
(2008) 0 1 1cd 2 3 3gp 4 7 10 51 2005 2006 2007 2008 2009 ac3 adobe advanced aio antivirus appz audio beta build business cd christmas collection complete converter crack desktop direct download dvd dvdrip dvdscr earth edition eng exclusive final flash format free full game games genuine guide happy hdtv hindi hosts hq incl internet joomla kaspersky keygen link links mac manager media microsoft mobile movie movies mp3 music network office original pack patch pc photo photoshop platinum player portable premium pro professional quality rapidshare reloaded rip s60v3 security serial server smartmovie software songs sp1 speed studio subs subtitles suite tamil telugu template tm tools ultimate update utilities version video videos vista wallpapers web windows working world wwe x264 xp xvid |

New To AiO Forum? Need Help?

Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.

Site Best Viewed with Firefox 3.0 & IE v7.0
RapidShare Links PhazeDDL Warez
Full DownloadsAuto Submitter