Search
Left Quote    Knowledge speaks, but wisdom listens.
- Jimi Hendrix    
Right Quote
 
[login] | [Register]
 

Beginning Visual C++ 6.0

by: bs0d
Page: 2 of 10
(View All)

Declaring your variables


If you are experienced in other programming laugages like visual basic or javascript, you will find out that they have made you life quite easy when it comes to coding. The number one difference you will find opposed to other various langauges is that you are required to declare each variable and its type before it is used. In programming before, you could just say:




This is no longer the case. You will get errors (in the linker at the bottom of Visual C++) like, "Unidentified identifier" or "undeclaredvariable" --something like that. The good thing is, that it will give you an idea of where the error is. Try not to pay too much attention to the exact line, because that may not always be the case. Anyways, If you wanted to do the calcuation above in Visual C++, it would look like this:




Don't worry about your variable ans being = to zero at first. This is just the initial value that needs to be set.

DATA TYPES


Int stands for integer. And their are other types of integers (short, long, and int).

There are also Floating-point numbers. These are numbers that contain a decimal. And there are other types of float numbers (float, double and long double). To declare a floating point number, it would look like this:




quite similar to int, just remember it has a decimal, so when you declare it use a decimal.

char is another type. This is short for character. A char is one character enclosed in single quotation marks. Some programmers pronounce it as "care", others say "char" like in charcoal- at any rate, you would declare a char like this:




String is a data type you will use. This is zero or more characters enclosed in double quotation marks. When you use a string, this is what the code should look like:




Finally their are boolean values. If you do not know, boolean is either True or False. Instead of using the full word boolean like in Visual Basic, you will use bool. Take a look below:





Declaring Variables


You want to make it a habit to declare your variables correctly, and not to mix them back and forth, this will cause tedious errors in your programs and can produce weird results because they are stored in a special memory location depending on the type. It can get quite technical. I'll explain a bit.

The computer stores numeric data in binary code, and character data in ASCII codes. 0 (zero) in AScII = 48; 0 (zero) in binary = 00110000. C++ will store it according to the data type -- so say you have the number 9. Well, if you assign that the type int: its memory location will read as the binary number 1001 (one 1 + one 8). If the data type is char: its memory location will will be stored as a character using ASCII code 57, which is represented in internal memory as 00111001 (one 1 + one 8 + one 16 + one 32). Catching on?


I will show you in a code sample, and you can later on execute the code to see the results for yourself.



the results will read for var1: 65, and for var2: A. Eventhough they equal the same thing, the results are different based on the datatype, because 01000001 is interpreted for a character as the ASCII code 65, which is equivalent to the letter, A. However, when you declare the type as int, 01000001 is interpreted as the binary code for decimal number 65.




1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |  10  |  
Next »


Comments:

  Mussawar
  Subject: "Nice Tutorial" Date: Nov 24 2013 at 3:35 am    
I'm beginner and this is probably the best tutorial ever I seen. Quite easy to understand. Appreciation and thanks for writing it.
You Must be logged in or a member to comment.


Tutorial Stats

Tutorial Stats

96,118 Views
1 Total Comments
4 Rating of 5 (2 Votes)

Options

Tutorial Options

· Login to Rate This Article
· Login to Post a Comment
· Read more by this author
Digg This Article! Del.icio.us: Bookmark This Article Reddit: Bookmark This Article BlinkList: Blink This Article! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Article Spurl: Mark This Article

Articles

Related    

· C Tutorial
Your Article Here



"AllSyntax.com" Copyright © 2002-2017; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank