As in other c languages, data can be of two types:
i) Constant and ii) Variables
Constant
This does not change it's value in the program
Types of Constant
i) Integer, eg: 8, 100, -3 //whole numbers both of negative and positive
ii) Float, eg: 0.34, 0.9, 5.9 //numbers with fractional path
iii) Character, eg: 'z', 'w', 'T', '*' //any character enclosed in a quotation marks
iv) String, eg: "abc", "A100", "I" //one more characters enclosed with double quotation marks
Variables
This can vary its values in the program. It is defined by giving a data type and a name.
/*program number 1*/
1. main()
2. {
3. int v1;
4. float v2 ;
5. char v3;
6. v1 = 65;
7. v2 = -18.23;
8. v3 = 'a';
9. }
The line on the left hand side are for reference only are not in the program. This will let us to read easly our program.
main() is where the program will start to execute and it will appear once in the program.
main() must contain open and close braces { } from line 2 and line 9. Within braces there are statements which are end with ; (semicolon).
Lines 3, 4, and 5 in this program defines variables which are v1, v2 and v3. You can give a variable any name you wish, but bare in mind that each naming variable has got its own rules.
RULES FOR NAMING VARIABLES:
1. A variable name ca only be constructed using letters(a-z, A-Z), numerals(0-9) or underscores (_).
2. A variable name must start with a letter or an underscore.
3. A variable name can not be a C keywords. A keyword is a word that has a special meaning.
4. A variable name can contain any number of characters, but only the first thirty-one characters are significant to the C compiler.
Some examples of variable names:
variable names:
month_1_sales // This is a valid variable name.
month1sales //valid, but not as readable as month_1_sales.
1st_month_sales //invalid, the name does not start with a letter or underscore.
month 1 sales% //Invalid, spaces and % are not allowed
xyz //valid, but variable names should be meaningful
SalesForThisweek //Valid and meaningful.
int //Invalid, this is a keyword.
Line 3 to 5 from program 1 above define v1 as integer variable, v2 as a floating-point (can hold decimals) variable, and v3 as acharacter variable. Note that variable names are case sensitive that is the variable V1 is different from the variable v1.
Lines 6 to 8 of the program assign values to the variables. The value assigned to each variable is stored in the computer's memory.
No comments:
Post a Comment