Introduction to C programming/Variables

From Wikiversity
Jump to navigation Jump to search

variables are used to store values in a program. Just like in algebra. There are 3 types of variables in C-programming, character, integer, and float. A character is, well simply a character such as 'v' or 'a' or 'h'. An integer is a whole number, such as '5' or '10' or '19086'. A float is a decimal number, such as '4.26' or '3.14' or '5.679'.

Before you can store a value in a variable you have to declare it. By declaring a variable your telling the compiler to "take this variable, and make room for a value in the computers memory". then you can define it.

Lets say I wanted to make variable a to be an integer with a value of five. I would do this:

int a; a = 5;


the first line of code (int a;) declares a as an integer. Then a=5 makes the variable a have a value of 5. As you may have noticed, there is an int before a. This is use to declare an integer. To declare a character, you would use char before a and make sure the variable is being declared as a character. You would use float before a to make it a float.