i am using dev c++ and i need to figure out how to declare variables.

Declaring variables is to create them which brings them into existence. You have to specify the type of variable that you are creating.

There are different types because there are different types of data to be represented. For example, there are integers which are simply whole numbers (like 1,2,3, 86, 1443, etc). Integers cannot represent fractional parts, like 2.5 or pi (3.141592653589). These kinds of numbers with fractional parts are called floating point numbers in computing. These floating point numbers (numbers with fractional parts) have to be represented in special ways inside the computer. Integers (only straight whole numbers) can be directly represented inside the computer. For example, if there is a number 8, then 8 is what is sitting inside the computer (inside it's memory or CPU register) in binary form (ones and zeros 8 = 1000, 10 = 1010, 46 = 101110). But if the number is fractional, then how do you represent 2.543 using ones and zeros? You can't directly, it has be encoded somehow. Obviously, because some kind of encoding scheme is used for floating point numbers they can't be directly read the way integers can be. They have to be decoded first and thus require more computing power.

Since there is a difference between integers and these floating point types, the compiler has to know this so it can handle each one respectively. That is why you have to declare the type when you create the variable. Sometimes you may only want to use whole numbers ONLY (integers) while other times you may need to use floating point numbers because you have fraction parts (like when dealing with money). Integers are perfectly accurate: 2 + 2 = EXACTLY 4. But .333333 + .333333 + .333333 does not exactly equal 1, it equals .999999. There is no way to add 1/3 + 1/3 + 1/3 and get the exact answer of 1 with floating point types in a computer because there is a limit to the number of digits used. There are two sizes of floating point numbers used, one is called a "float" and the other is called a "double" (meaning double the precision and size of a regular "float" type). Many times, a float type will be satisfactory, but if you need more digits of precision or a really large number, then you can use type double.

There is one other main aspect involved in these different types of data. And that is of size. Each data type has a size associated with it, and in this sense size matters. For example, if you type a character on the keyboard, it only needs one byte to represent it. One byte is a unit of 8 ones or zeros, (i.e, 8 bits) and takes up one whole memory location. And there is a data type called "char". It is only 8 bits and typically holds a character typed on the keyboard. On the other hand, an integer is typically 4 bytes in size (32 bits) and can hold a large number - up to about 4 billion. A type float is also typically 4 bytes in size, and type double is 8 bytes (64 bits) in size (I say typically because it is machine dependent).

OK, here is why size matters: A small type (like char) can't hold the entire contents of a larger type, like an integer. If you try to take the value of an integer that holds 50 and try to transfer that into a type char (which can hold up to 255) then there is no problem. But if you try to transfer an integer vaule of 1,000 into a type char, then it won't all fit and the overflow will just simply be forgotten about. Sometimes that may be what you intended, other times you may have just forgotten that one variable was a type char. But most compilers will warn you with a message like "possible loss of data" just in case it was unintentional. Another reason size matters is because of the processor itself, the way it was designed. If you are moving one byte of data, say to the screen or another memory location, then that requires one kind of instruction. If you are moving a variable that is 4 bytes in size (integer or float) and thus takes up 4 memory locations, then a different instruction is needed. And if you are moving a variable that takes up 8 memory locations (bytes), like a type "double" float then that requires a different instruction still. Since it is the compiler that has to translate your source code into instructions that the processor uses, it has to know the size of the data to write the proper instruction for the processor. The data type provides the size information for the compiler.

So, how do you declare, that is, bring into existence variables? Well, first you have to decide which type(s) you need. Do you only need whole numbers no larger than 4 billion? Then use an integer. Will you be using some typed characters from the keyboard? Then use type char. Or do you need to handle fractional numbers, like money values or scientific numbers? The use a type float, or if necessary, use type "double" (double float) if you need more precision than a regular float or if you just need a really large number!

When you declare variables you need to state the type and give it a name:

char a;
int b;
float c;
double d;

However, it is always good programming practice to always assign (i.e., initialize) your variables when you declare them, like int x = 0; or float income = 2278.25;

Also know that variables by default are generally signed. That means that they can be either positive or negative numbers. Since an integer can hold a little over 4 billion values, a signed integer could go up to 2 billion positive values and 2 billion negative values. If you don't want your variable to be signed, then use the unsigned keyword first:

int x = 0; // x can hold any number in value up to about 2 billion positive or negitive;
unsigned int x = 0; // this integer can hold any value up to about 4 billion positive only.

You can use any name for your variables, except for the reserved keywords.
- I'm outta here ..... bye!

commented: Nice and complete! +14
commented: Far more detailed answer than I would have bothered with for such a question. +5
commented: Sure is a lot of effort here :) +8
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.