Alphabets:: Constants, Variables and Keywords in C
Alphabets:: Constants, Variables and Keywords in C
It was designed and written by a man named Dennis Ritchie. C is reliable, simple and easy to use. C has survived for more than 3 decades. C language is a base to learn different programming language. If you want to learn C++ or JAVA, without the knowledge of C it becomes very difficult to learn these programming languages. Many major components of popular operating systems like Windows, UNIX, LINUX is still written in C. Nothing beats C in terms of speed of execution. Before starting with the programming, lets have a look at the C Character set. Any alphabet, digit or special symbol can be termed as a character. Below table shows list of valid alphabets, digits and symbols allowed in C. Alphabets: A, B, C, D, ,X, Y, Z a, b, c, d, ,x, y, z Digits : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Special Symbols : ~!@#%^&*()_-+=|\{} []:;"'<>,.?/ Following are the basic data types: 1) Numeric data type: deals with numeric data such as integer or float data type 2) Non-numeric data type: deals with non numeric data such as characters. 3) Integer data type: deals with integer or whole number 4) Real data type: deals with numeric data that includes fractions 5) Enumerated data type: These data types are defined by the user. Constants, Variables and Keywords in C A constant, Variable or a keyword can be formed from any combination of Alphabets, Digits and Special Symbols. A constant is an entity whose value does not change throughout the program execution.
A variable is an entity whose value keeps on changing throughout the program execution. However, its not a rule that the value of the variable will change. A variable value might remain same throughout the program execution. However the main difference between variable and constant is that we cant change the value of constant in between the program, but we can change the value of the variable during program execution.
Let us take an example: We define a variable named x in a program. Let us say x=3. After some time during execution we change the value of x to 6. Now, x=6. Here value of x is getting changed. But the value of 3 and 6 will never change. Hence, x is called a variable and 3 is called a constant.
Keywords are the words whose meaning has already been explained to the C compiler. The keyword cannot be used as the variable name. If we try to do so we are trying to assign new meaning to the keyword. The keywords are also known as Reserved words. E.g.: for, if, static, while, do, break etc. Rules for constructing Constants in C Language There are rules to be followed in order to create a constant. Let us discuss the rules to create constants. Rules for constructing Integer constant 1) An integer constant must have at least one digit. 2) It must not have a decimal point. 3) It can either be positive or negative. 4) No commas or blanks are allowed within an integer constant. 5) If no sign precedes an integer constant, it is assumed to be positive. 6) The allowable range for integer constants is -32768 to 32767
Actually, the range of the integer constant depends on the compiler. The above mentioned range is for 16bit compiler. However, for a 32-bit compiler the range would be even greater.
The integer constants Whole Numbers E.g. 25, 35, -25, -46 Computer allocates only 2 bytes in memory. 16th bit is sign bit. (if 0 then positive value, if 1 then negative value)
Decimal Integer constant: 0 to 9 E.g.: 49, 58, -62 (40000 cannot come because it is > 32767)
Octal Integer constant: 0 to 7 Add 0 before the value. E.g.: 045, 056, 067
Hexadecimal Integer constant: 0 to 9 and A to F Add 0x before the value E.g.: 0x42, 0x56, 0x67
Rules for constructing Real constants Rules for constructing Real constants (Fractional Form) 1) A real constant must have at least one digit 2) It must have a decimal point 3) It could be either positive or negative 4) If no sign precedes an integer constant, it is assumed to be positive. 5) No commas or blanks are allowed within a real constant. E.g.: +867.9, -26.9876, 654.0
Rules for constructing Real constants (Exponential Form) 1) The mantissa part and the exponential part should be separated by the letter e 2) The mantissa may have a positive or negative sign(default sign is positive) 3) The exponent must have at least one digit 4) The exponent must be a positive or negative integer(default sign is positive) 5) The range of real constants in exponential form is -3.4e38 and +3.4e38 E.g.: +3.2e-4, 4.1e8, -0.2e+4, -3.2e-4
Rules for constructing Character constant 1) A character constant is an alphabet, a single digit or a single special symbol enclosed within inverted commas. Both the inverted commas should point to the left. For example, S is a valid character constant whereas S is not. 2) The maximum length of a character constant can be 1 character. Allots 1 byte of memory
E.g.: B, l, # Rules for Constructing Variable Names in C Language A variable is an entity whose value keeps on changing throughout the program execution. As we all know, data is stored in the memory of the computer. Actually, data is not stored in the variable. A variable is the name given to the memory location. A variable name is an entity that points to a particular memory location. Variable can be of different types. Rules for constructing variable names 1) A Variable name consists of any combination of alphabets, digits and underscores. Some compiler allows variable names whole length could be up to 247 characters. Still it would be safer to stick to the rule of 31 characters. Please avoid creating long variable name as it adds to your typing effort. 2) The first character of the variable name must either be alphabet or underscore. It should not start with the digit. 3) No commas and blanks are allowed in the variable name. 4) No special symbols other than underscore are allowed in the variable name. We need to declare the type of the variable name before making use of that name in the program. Type declaration can be done as follows: To declare a variable as integer, follow the below syntax: int variable_name; Here int is the type of the variable named variable_name. int denotes integer type. Following are the examples of type declaration statements: E.g.: int p, n; float r;
Data Types in C Data types determine the following: Range of the data Type of the data stored Number of bytes it occupies in memory
C supports following data types: int occupies 2 (16-bit compiler) or 4 bytes (32-bit compiler) of memory float - occupies 4 byes of memory double occupies 8 bytes of memory char occupies 1 byte of memory
int is used to define integer numbers. It occupies 2 bytes of memory (16-bit compiler) { int amount; amount = 5; }
float is used to define floating type numbers. It occupies 4 bytes of memory (16-bit compiler) { float amount; amount = 1.200000; }
double is used to store big floating point numbers. It reserves 8 bytes of memory (16-bit compiler) { double amount; amount = 1.200000000000; }
char is used to store characters. It occupies 1 byte of memory (16-bit compiler) { char alp; alp = A; }
The modifiers define the amount of storage allocated to the variable. int, float and double have the following four modifiers. short long signed unsigned
long is used to store long int short is used to store short int long double is used to store long double Using signed int is redundant as int is signed by default. Modifier long can be applied to double data type.
Following are the valid combinations: long int or long (For 16 bit compiler: 4 byte) short int or short (For 16 bit compiler: 2 byte) signed int or int (For 16 bit compiler: 2 byte) unsigned int (For 16 bit compiler: 2 byte) short unsigned int long unsigned int char (Occupies 1 byte of memory) signed char or char (Range: -128 to +127, Occupies 1 byte of memory) unsigned char (Range: 0 to 255, Occupies 1 byte of memory) long double (Occupies 10 bytes of memory)
Following rule applies: short int <= int <= long int float <= double <=long double
Summary:
Data Type short int short unsigned int short signed int signed int unsigned int long int long unsigned int long signed int float double long double char signed char unsigned char Understanding the program in C 1) /* 2) Program: 3) Addition of two numbers 4) */ 5) //Here it starts 6) #include<stdio.h> 7) #include<conio.h>
Bytes 2 2 2 2 2 4 4 4 4 8 10 1 1 1
8) Void main() 9) { 10) int a, b, c; 11) /*Assign the values to the variable a and b */ 12) a = 5; 13) b = 4; 14) /*Perform addition of a and b */ 15) c = a + b; 16) printf(\nAddition of a and b is %d, c); 17) }
Let us learn the above program in detail. Line 1 thru 4: This is the comments in the program. It is called multiline comment. The text written within /*...*/ are ignored by the compiler. Line 5: This is another way to comment a text or statement. It is called single line comment.The entire line following the tag // is ignored by the compiler. Line 6 and 7: #include is known as pre- processor directives. It tell the compiler to include text from another file, stuffing it right into your source code. is a file name enclosed within angle brackets. The whole statement #include tells the compiler to take text from the file stdio.h and stick it into your source code before the source code is compiled. Line 8: This is the start of the main function. The program execution starts from this function. main() is the name given to a set of statements. This name has to be main(). The statement that belongs to main() are enclosed within a pair of braces {}. Line 10: Any variable that we need to use must be declared before using it. Line 12 thru 15: Here the values are getting assigned to the variables. Line 16:
Once the value of a+b is assigned to c, it needs to be displayed on the screen. We can use readymade library function to display the value on the screen. One such function is printf(). The general form of the printf() function is: printf(,); can contain, %d for printing integer values %f for printing real values %c for printing character values \n is a newline character. It takes the cursor to the new line. It is one of the several escape sequences available in C. Line 9 and 17: The function body should be enclosed in opening and closing braces.
Note: Though comments are not necessary, it is a good programming practice to include comments in the program. Adding comments in the program increases the readability of the program. WE can include as many comments as possible. A comment can be split over more than one line as shown in the above program (Line 1 thru 4). Any C statement always ends with a ;
Receiving Input In the above program, we have assigned the value 5 and 4 to a and b respectively. But, every time we cant do so. A situation might arise wherein we need to take input from the user. Here we will learn how to receive input from the user. scanf() It is a standard library function in C. This function will accept the input from the user and stores it in a particular user defined variable. The syntax of this function is: scanf( , &); can contain, %d for printing integer values %f for printing real values
& before the variable name is a must. & is the address of operator. It gives the address used by the variable in memory.
Let us consider an example scanf(%d,&a); When we execute this statement, it will ask for input. Suppose the input given is 5. 5 will be stored as integer at the address of variable a. we can print the value stored at that address using simple printf() statement as shown below. printf(%d, a); Example: 1) /* 2) Program to accept three numbers and prints the sum of these numbers 3) */ 4) #include<stdio.h> 5) #include<conio.h> 6) Void main() 7) { 8) int a, b, c; 9) /*Assign the values to the variable a and b */ 10) scanf(%d %d, &a, &b); 11) /*Perform addition of a and b */ 12) c = a + b; 13) printf(\nAddition of a and b is %d, c); 14) } Instructions in C Programming Language There are three types of instructions in C. They are as follows: 1) Type Declaration Instruction 2) Arithmetic Instruction 3) Control instruction Type Declaration instruction is used to declare the type of variables used in C. Any variable we want to use in the program must be declared before using it. This declaration is done using Type declaration
instruction. This declaration is done at the beginning of the main() function. E.g.: int pr, rt; float amt; Arithmetic instructions are used to perform arithmetic operations on variables and constants. Here we will learn some new terms. These are operands and operators. The variables and constants on which arithmetic operation is done by arithmetic operators are called operands. Example 1: int a = 5, b = 10, c; c = a + b; Here, a, b and c are called operands =, + are called as operators Example 2: int a; float b, c, d; a = 10; b = 0.05; c = 1.5; d = a +b * c; Here, a is integer variable. b, c and d are real variables. = is the assignment operator. + and * are arithmetic operators. 10 is integer constant. 0.05 and 1.5 are real constants.