Symbolic Constants using #define in C

By: Emiley J Emailed: 1649 times Printed: 2119 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

It's bad practice to bury ``magic numbers'' like 300 and 20 in a program; they convey little information to someone who might have to read the program later, and they are hard to change in a systematic way. One way to deal with magic numbers is to give them meaningful names. A #define line defines a symbolic name or symbolic constant to be a particular string of characters:

  #define name replacement list

Thereafter, any occurrence of name (not in quotes and not part of another name) will be replaced by the corresponding replacement text. The name has the same form as a variable name: a sequence of letters and digits that begins with a letter. The replacement text can be any sequence of characters; it is not limited to numbers.

   #include <stdio.h>

   #define LOWER  0     /* lower limit of table */
   #define UPPER  300   /* upper limit */
   #define STEP   20    /* step size */

   /* print Fahrenheit-Celsius table */
   main()
   {
       int fahr;

       for (fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP)
           printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
   }
The quantities LOWER, UPPER and STEP are symbolic constants, not variables, so they do not appear in declarations. Symbolic constant names are conventionally written in upper case so they can ber readily distinguished from lower case variable names. Notice that there is no semicolon at the end of a #define line.

C Home | All C Tutorials | Latest C Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(1)


1. View Comment

can i use comma operator in simbolic constant in c

View Tutorial          By: Madheswaran at 2011-12-05 18:44:22

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Emiley J
Password must include both numeric and alphabetic characters - Magento
What is Hadoop?
Returning multiple values from a web service
Tomcat and httpd configured in port 8080 and 80
Java Webservices using Netbeans and Tomcat
Java WebService connected to Database
How to Deploy a Java Web Service
Call a webservice in Java
Java WebService - Create your first web service in Java
package javax.jws does not exist
Getting Started with Android
HTML5 Location - getCurrentPosition() in HTML5
HTML5 Canvas - Using Canvas in HTML5
HTML5 - Introduction
HTML5 Video - Handling video in HTML5

More Tutorials in C
Sum of the elements of an array in C
Printing a simple histogram in C
Sorting an integer array in C
Find square and square root for a given number in C
Simple arithmetic calculations in C
Command-line arguments in C
Calculator in C
Passing double value to a function in C
Passing pointer to a function in C
Infix to Prefix And Postfix in C
while, do while and for loops in C
Unicode and UTF-8 in C
Formatting with printf in C
if, if...else and switch statements in C with samples
Statements in C

More Latest News
Most Viewed Articles (in C )
Using memset(), memcpy(), and memmove() in C
External Variables and Scope in C
perror() Function - example program in C
Printing a simple histogram in C
goto and labels in C
lseek() sample program in C
assert() Function Example program in C
Passing double value to a function in C
Sum of the elements of an array in C
#if, #elif, #ifndef, #ifdef in C (Conditional Inclusion)
UNIX read and write system calls sample program in C
Listing Files and Directories sample program in C
Bitwise Logical Operators in C
Find square and square root for a given number in C
Symbolic Constants using #define in C
Most Emailed Articles (in C)
Multi-dimensional Arrays in C (Explained using date conversion program)
Open, Creat, Close, Unlink system calls sample program in C
Formatting with printf in C
Unicode and UTF-8 in C
Character Arrays in C
Macro Substitution using #define in C
Pointer Arrays and Pointers to Pointers in C
UNIX read and write system calls sample program in C
Arguments - Call by Value in C
Pointers vs. Multi-dimensional Arrays in C
Using Bit-field in C
lseek() sample program in C
while, do while and for loops in C
Sum of the elements of an array in C
Sorting an integer array in C