C: Input, Variables, and Data Types - Variables
(Page 2 of 4 )
Variables are objects used to store data. The easiest way to think of a variable is like a box that holds a book. You can store a book in the box, take the book out and read it, modify the book and put it back, replace the book with an entirely new book, or take it out and leave the box empty for later use. The data in a variable is just that, variable. We worked with variables in the above example to store data from the scanf_s() function.
All variables in C must be assigned a data type (you must tell the program what type of data the variable will hold). Before we can use a variable, we must declare it. We do so like this:
int my_number;
This basically tells the system, "hey, I am going to need space for a variable named my_number."
You can also declare more than one variable on a line:
int my_number, yournumber, yomamasnumber;
And lastly, we can assign a value to a variable at the same time we declare it:
int my_number =22;
This stores the value 22 in the variable named my_number.
If we wanted to store a variable in another variable, we could do that also:
my_number = yournumber;
And if you want to get weird, you can assign multiple variables the same value like so:
my_number = yournumber = yomamanumber = 1900;
Naming Conventions
Variable names can consist of letters (both upper and lowercase), numbers, and the underscore(_). They cannot begin with a number. Here are some sample names for your perusal:
employees
employee_number
employeeNumber
_employees
Boss
BoSs
hero1902
Next: Data Types >>
More BrainDump Articles
More By James Payne