...

View Full Version : The Difference Between a Literal and Symbolic Constant in C?



ASHWHIT
01-04-2012, 12:47 PM
Hi,

I keep finding myself getting confused, I know there are two types of contstant:

Literal
Symbolic


and that a symbolic is defined before executing the program like so:

#define CONSTNAME value

Its a literal which I am finding confusing, and what makes it different from just a standard variable (other than the fact the value can't be changed).

For instance, you declare a variable like so:


int variablename;

but surely this is also a variable, but I have given it a value:


int variablename = value;

My real question I guess is, can I later change the value of the above or is that a constant, and therefore can't be changed?

I know you can also write a constant like so:


const int variablename;

But from my understanding you can drop the 'const'?

Cheers,

Ashley

Spookster
01-04-2012, 03:11 PM
You are a bit confused.

1. A literal constant is simply a value such as

10

It can be used throughout your program and it's value cannot be changed.

2. Symbolic constants can be created by either defining them with the preprocessor directive (not good practice) or declaring them using the const modifier.

#define MY_SYMBOLIC_CONSTANT 25

const int MY_SYMBOLIC_CONSTANT 25;

Defining constants using the preprocessor directive works a little differently in that before compilation the preprocessor simply does a text replacement throughout your code wherever you used MY_SYMBOLIC_CONSTANT it will replace that with 25. This was the original way that constants were declared in C until they added the const modifier so you can declare constants more naturally as part of your code. A constant declared with const is read only and cannot be changed. Using the preprocessor define directive still has it's uses for defining macros but you should avoid using them to define constants because they have no scope nor is type safe.

watson88
01-11-2012, 09:14 AM
Software programmers use various data types in their code.

A literal is a value that is expressed as itself. For example, the number 25 or the string "Hello World" are both literals.

A constant is a data type that substitutes a literal. Constants are useful in situations where

a specific, unchanging value is to be used at various times during the software program
you want to more easily understand the software code

A variable in a program can change its value during the course of execution of the program. A constant retains the same value throughout the program.

Jacobhine
01-20-2012, 11:18 AM
I think you should check following link it will helps you to get difference between these two

http://stackoverflow.com/questions/4585205/difference-and-definition-of-literal-and-symbolic-constants-in-c



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum