By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,639 Members | 2,340 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,639 IT Pros & Developers. It's quick & easy.

difference between variables and constants.

P: 25
i have readed the guid.l and now i'm ready for some action.

what is a constant? i've readed in a manual what it is but i dont understand.
is that a number or something?
what is the difference between variables and constants?


thank you.
Aug 15 '07 #1
Share this Question
Share on Google+
15 Replies


Expert 10K+
P: 11,448
i have readed the guid.l and now i'm ready for some action.

what is a constant? i've readed in a manual what it is but i dont understand.
is that a number or something?
what is the difference between variables and constants?


thank you.
Oh please. What do those very names suggest? A constant is a value that
can not be changed. A variable can change values.

kind regards,

Jos
Aug 15 '07 #2

P: 25
can you please give somthing for example; a constant, cause i see that a constant is a number and it beggins with 0X (16 numbers) , 0(8 numbers), and ends with F, L, U.

actually , a constant is very difficul to understand cause of all that ^^^^^^




thanks for answering, i repete again: i've readed the guidl.
Aug 15 '07 #3

P: 25
oops, i forgotn something to say, you said to me that a variable can change, but a constant not, what do you mean with changing?; please give a program for example that we can change and we cannot change.
Aug 15 '07 #4

sicarie
Expert Mod 2.5K+
P: 4,677
oops, i forgotn something to say, you said to me that a variable can change, but a constant not, what do you mean with changing?; please give a program for example that we can change and we cannot change.
These are really basic questions - things that aren't really in the scope of this site. Read a tutorial, take a class. If you're having issues with these types of things, we're not going to be able to do much to help you.

http://www.cplusplus.com/doc/tutorial/

Go through that. Play with ALL the examples. Look up the functions. Play with the examples they give. Then come back and ask questions.
Aug 15 '07 #5

P: 25
thanks , but to be honest , i do know how templa.te classes works.
Aug 15 '07 #6

sicarie
Expert Mod 2.5K+
P: 4,677
thanks , but to be honest , i do know how templa.te classes works.
You know how template classes work, but you don't know the difference between a var and a const?
Aug 15 '07 #7

Banfa
Expert Mod 5K+
P: 8,916
can you please give somthing for example; a constant, cause i see that a constant is a number and it beggins with 0X (16 numbers) , 0(8 numbers), and ends with F, L, U.

actually , a constant is very difficul to understand cause of all that ^^^^^^
You are confusing constants and literals (sorry I may have added to this confusion with an earlier post). A constant is actually a type modifier on a variable that indicates the value of the variable doesn't change.

A literal is were you type a value directly into your code for instance 5 is an integer literal.

Expand|Select|Wrap|Line Numbers
  1. int main(int argc, char **argp)
  2. {
  3.     int i1;             // This is an integer variable
  4.     101;                // This is an integer literal
  5.  
  6.     const int i2 = 100; // This is an integer constant initialised by an integer literal
  7.  
  8.     i1 = 200;           // This is OK
  9.  
  10.     i2 = 300;           // This is NOT OK and causes a compiler error because i2 
  11.                         // is declared constant and there for can not be changed
  12.  
  13.     return 0;
  14. }
  15.  
Aug 15 '07 #8

Banfa
Expert Mod 5K+
P: 8,916
You know how template classes work, but you don't know the difference between a var and a const?
and that's the beauty of C++ you can learn the complex stuff while completely by-passing the basics :D
Aug 15 '07 #9

Expert 10K+
P: 11,448
and that's the beauty of C++ you can learn the complex stuff while completely by-passing the basics :D
Isn't that how most of the programming community operates nowadays? Build
on top of things you don't understand but just deliver. What a great scientific exercise.

kind regards,

Jos ;-)
Aug 15 '07 #10

P: 25
i'll give a simple example of variable that can change and a constant that cannot change; ( must be good, if it isn't say it to me)

Expand|Select|Wrap|Line Numbers
  1. #include < iostream >
  2.  
  3. int main()
  4. {
  5.  int number = 5 ;
  6. cin.get() ;
  7.  
  8. number = number + 1 ;  // you can do also this  number++
  9. cin.get() ;
  10. }
  11.  
that was a variable , it can change!


here is a constant :

WAIT : constant can't change!

is everything good what i just said????
Aug 15 '07 #11

P: 25
please answer!


i need to be professional, cause i am , haha
but just please answer.


thanks


kind regards ,


dorkz
Aug 15 '07 #12

P: 25
hello?
can someone whos very nice answer?
Aug 15 '07 #13

TRScheel
Expert 100+
P: 638
hello?
can someone whos very nice answer?
Amigo, they have answered your question, even going above and beyond to explain literals. I don't know what more you want.

Constant = Cannot change / Bad practice to even attempt to
Variable = Changes at will
Literal = A value (10, 543, 4934030, 0x24, 'c', etc)
Aug 15 '07 #14

Banfa
Expert Mod 5K+
P: 8,916
Isn't that how most of the programming community operates nowadays? Build
on top of things you don't understand but just deliver. What a great scientific exercise.
Unfortunately you are correct, I am constantly surprised at how few C/C++ programmers know how a makefile works despite using them daily to build there projects.
Aug 16 '07 #15

Banfa
Expert Mod 5K+
P: 8,916
that was a variable , it can change!
and that was basically correct, however
here is a constant :

WAIT : constant can't change!
what on earth are you talking about.

Oh and by the way asking for replies twice in 32 minutes is bad form and is likely to irritate the mods and experts. We get to see you replies when we log onto the network but it just so happens that we are not necessarily logged into the site at the same time you are.
Aug 16 '07 #16

Post your reply

Sign in to post your reply or Sign up for a free account.