Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    The Difference Between a Literal and Symbolic Constant in C?

    Hi,

    I keep finding myself getting confused, I know there are two types of contstant:
    1. Literal
    2. Symbolic


    and that a symbolic is defined before executing the program like so:
    Code:
    #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:

    Code:
    int variablename;
    but surely this is also a variable, but I have given it a value:

    Code:
    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:

    Code:
    const int variablename;
    But from my understanding you can drop the 'const'?

    Cheers,

    Ashley

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    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.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    New to the CF scene
    Join Date
    Nov 2011
    Location
    USA
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think you should check following link it will helps you to get difference between these two

    http://stackoverflow.com/questions/4...constants-in-c


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •