4 Replies - 306 Views - Last Post: 13 February 2011 - 08:18 AM Rate Topic: -----

#1 thomastan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-February 11

What is Variable and Constant ?

Posted 12 February 2011 - 08:09 PM

I am new in programming. I got problems on Variable and Constant? I want to know about the range and why there must be a range for such as int, char,and etc....I hope i can get answers here. Thank for helping.
Is This A Good Question/Topic? 0
  • +

Replies To: What is Variable and Constant ?

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1618
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: What is Variable and Constant ?

Posted 12 February 2011 - 08:47 PM

what? ill try to help but im not sure what you asking

a variable is place to store data of almost any kind. a constant is data which doesn't change there are constants that are declared like variables also.

"this is a constant"
10 //this is a constant
const int x = -30; //this is a constant that can be used like a variable 



int a;
char b;
short c;
float i;
double j;
char* k;
unsigned int x;
unsigned char y;
unsigned short z;



these are all variables and there values can change. depending on there size they can hold different ranges of information. an int is typically 4 bytes and a byte is typically 8 bits so it can store 2^32 different sets of data. now the numerical range is based on interpretation of this data. in the case of int the highest order bit is seen as the sign and 0 is all bits false. this gives int a range of -2^31 to 2^31-1. char is almost always a byte and as a byte is typically 8 bits a char has 2^8 different values. it follows the same rules as int for it's range so it has a range of -2^7 to 2^7-1 or -128-127. for unsigned values it's even simpler, they dont have a sign so there range is simply 0 to 2 to the power of there size in bits. take unsigned int for example it has a range of 0 to 2^32-1. floats and double are kinda wiredreally wired. you can look up some stuff on them on google if you need to. if you need to know there range look at this
Was This Post Helpful? 0
  • +
  • -

#3 Jeff H  Icon User is offline

  • D.I.C Regular

Reputation: 112
  • View blog
  • Posts: 307
  • Joined: 30-January 11

Re: What is Variable and Constant ?

Posted 12 February 2011 - 09:09 PM

Also to add about the range. The variable will be stored in memory and by knowing the maxium size the computer knows how much memory space it needs to allocate for it.
Was This Post Helpful? 0
  • +
  • -

#4 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 854
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: What is Variable and Constant ?

Posted 13 February 2011 - 04:39 AM

View Postthomastan, on 13 February 2011 - 04:09 AM, said:

I am new in programming. I got problems on Variable and Constant?
What problems are you having exactly?

View Postthomastan, on 13 February 2011 - 04:09 AM, said:

I want to know about the range and why there must be a range for such as int, char
Everything in the known physical universe has a hard limit somewhere - including your computer, which has a finite amount of memory, and therefore has a limit to the amount of data that it can store. of course, it would be a bit silly if a single number occupied your entire memory, when chances are you're rarely going to need massive numbers which occupy more than a handful of bytes in memory - so some sensible people decided that programming languages would have numeric types that could store large numbers, but not excessively large numbers.

if you do happen to need numbers which have thousands or millions of digits, then there are ways around it, but I very much doubt this will be a problem for you while you're still learning.
Was This Post Helpful? 0
  • +
  • -

#5 thomastan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-February 11

Re: What is Variable and Constant ?

Posted 13 February 2011 - 08:18 AM

Thanks alot. You guys help me a lot. Now i understand. And do you know which website that i can learn c++ instead of buying a programming reference book. Because those reference books in Malaysia are so expensive.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1