2.4 BASIC COMPONENTS OF PROGRAMMING
The basic component of C includes token, keywords, variables, datatypes, operators etc. Let us see some details of these basic components of C programming language.
2.4.1 Tokens and Keywords
Token
In C language tokens are the smallest element. A token may be a single character or a sequence of characters to form a single item for c program. Tokens can be numeric constants, character constants, keywords, names (lable or identifiers), punctuation or operators used for different kind of operations including arithmetic and numeric operations.
Keywords
In any programming language, some words are reserved for some special use.These words are not allowed to be used by programmer for their own purpose.These words are called keywords and sometimes called reserved words also.C language like any other programming language has defined some keywords . Every keyword have a fixed meaning to the compiler and can not change the meaning. For example int is used for representing integer. C keywords are given below:
auto default float register struct
break do for return switch
case double goto short typedef
char else if signed union
const enum int sizeof unsigned
continue extern volatile long while
static void
Now let us learn about data types and variables in C.
2.4.2 Data Type and Variables in C
Data Types in C
Most of the time in calculation, data are used for example in finding the sum of two numbers first of all these two numbers need to be stored in computer memory then result obtained after adding these two numbers is again stored in the memory. Now a question arises what size of memory should be used for storing these numbers? To answer this question one need to know what the type of number is .A number might be an integer (whole number such as 20) or real number (floating point number such as 10.25).
An integer needs 2 bytes for storage. One byte equals eight bits. There are three basic data type which are:
1) Character
2) Integer
3) Real numbers
In addition to these three two other basic data types enum and void which are out of the scope of discussion here. For real numbers float and double types are used. The size of three basic data type in c is given in table given below.
Variables
Once you have data to store, you need to associate with a specific memory location. These memory locations are given some names and these names are called variable names. The value stored in these locations can change or vary throughout the program’s lifetime. These data are referred with variable name. Each Variable has a specific type, which is known as variable data type. This data type tells the computer how much memory is taken by the variable to store the data.
Name of a variable must start with letters and contain letters, digits, and underscores. For example x, y, z , sum, number_of_points, mars12 are valid variable names. A variable must be declared in a C program before being used. Variable declaration tells the compiler about variable name and their type.
For example: int i ; means a variable named i of integer type is declared. Now i is available as a variable to store integer value. In C you can declare more than one variable in single statement(line). A declaration begins with the type of the variable(s), followed by the name of one or more variables.
float i,j,k,sum; means, four variables named i,j,k, and sum of float type are declared. When you declare your variable it is better to have some meaningful name of a variable.
While you declare variables you may give explanatory comment for more clarification. C Programming Language
You can initialize a variable at the time of declaration. This is done by adding an equal’s sign and the required value after the declaration. For example:
int high = 25; /* Maximum Length */
int low = 10; /* Minimum Length */
two variables high and low are declared and initialized. Remember that , you cannot use any of C's keywords like main, while, switch etc as variable names.
Names or Identifiers
Sometimes called identifiers or labels.These are the names of functions , variables etc. It can be of anything length, but it should not be too long or too short. Names are case sensitive xyz is different from XYZ. Any name must begin with a letter and the rest can be letters, digits, and underscores.
Different operators perform different kind of operations such as arithmetical operations, logical operations etc .In next section we will learn about different operators in C.
2.4. 3 Operators and Punctuations in C
Operators
An operator is a kind of function which is applied to values to give a result. For example for finding sum of two numbers ‘Addition’ operator is required. C is having a wide range of useful operators. You are familiar with operators such as +, , / , which are used for performing addition, subtraction, and division .
The common type of operators in C are:
i) Arithmetic Operator
ii) Assignment Operator
iii) Comparison Operator
iv) Logical Operator
v) Binary Operator
Arithmetic operators are the most commonly used operator . Comparison operators are used for comparison of values, logical operators are used for deciding combination of logical states, assignment operator is used assigning value to a variable, and binary operators are used for manipulation of individual binary digits of data. Here we will not cover binary. Now let us see in detail about each operator type mentioned above.
i) Arithmetic Operators
Commonly used arithmetic operators in C are
+ Addition
Example
4+6 will give 10
Subtraction
Example
10 6
will give 4
* Multiplication
Example
2*5 will give 10
/ Division
Example
8/2 will give 4
% Modulos (give remainder from integer division)
Example
5%2 will give 1
Assignment and Expressions
If you combine operators and values expressions are formed. The values produced by these expressions can be stored in variables. For example: 2 + 4 * 3 is a expression and after evaluating this expression you will get 14.
A simple expression is an assignment statement. An expression is evaluated and the result is saved in a variable. For example expression: a = (i * j) + k , the result will be saved after evaluating : (i*j)+k in variable a. The result will be saved after evaluation.
In any expression, operators *, / and % will be performed before + or .
If you want to have different order of evaluation the you have to use brackets. Any expression in brackets will evaluate first. For example, if you write expression: 2 + 4 * 3 as : (2+4)*3 then you will get 18.
One important point to note about / operator is that when division is performed between two integers, the result will be an integer, and remainder is discarded. For example if expression: 15/4 is evaluated it will give 3 as result. You can use modulus operator(%) between integers only. Never divide a number by zero in your program, this will cause an error, and usually due to this your program will crash (will terminate abnormally).
Some examples of operators, according to precedence.
1 + 5 * 2 → 1 + 10 → 11
(1 + 4) * 2 → 5 * 2 → 10
You can use symbols for storing value. In this case, symbols are evaluated to their values
before being combined
int x=2;
int y=3;
y + x*x → y + 2 * 2 → y + 4 → 3 + 4 → 7
ii) Assignment Operator
Assignment operator ( = ) is used for assigning value to a variable. Now, let us take a problem in which speed of a car is given to 50 km/h, distance to be covered is 200km and you have to find the time it will take to cover this distance. Here, you can use assignment operator for assigning values:
int speed, distance, time; \*are three variables *\
speed = 50;
distance = 200;
time = distance/time; \* time will have value 4 , that is 200/50 *\
Problem: Write a program in C to find a+b , ab , a*b, a/b, and a%b. The value of a is 10 and the value of b is 5.
C Program for showing use of arithmetic operators
/* C program for arithmetic operations */
#include <stdio.h>
int main( void )
{
int a= 10; /* a is initialized with value 10 */
int b = 5 ; /* a is initialized with value 10 */
printf(“a+b = %d\n”,a+b) ;
printf(“ab
= %d\n”,ab)
;
printf(“a*b = %d\n”,a*b) ;
printf(“a/b = %d\n”,a/b) ;
printf(“a%b = %d\n”,a%b) ;
return 0 ;
}
When you will run this program following output will come:
a+b = 15
ab
= 5
a*b = 50
a/b = 2
a%b = 0
You must have noticed that prinf function is having following format :
printf(“message = %d\n”, value to print) ;
Here message is a sting of character, %d for indicating that value for printing is of integer type. Similarly for character value printing %c and for float value printing %f are used. In the above program fixed value to a and b are assigned but if you need to take value as input from your computer for that in C scanf( ) function is used. As prinf( ) used for printing output on your computer screen, scanf( ) is used for reading input from your computer screen.The syntax for using scanf( ) is:
scanf(“%d”,&variable);
Here, %d indicate that the variable in for which data to be read is of integer and & sign is added(prefixed) to the variable name.
The above program can be modified as follows for giving input from screen instead of assigning fixed value.
/* C program for arithmetic operations */
#include <stdio.h>
int main( void )
{
int a, b /* a and b are declared */
printf(“Give vale of a: ”);
scanf(“%d”,&a);
printf(“Give vale of b: ”);
scanf(“%d”,&b);
printf(“a+b = %d\n”,a+b) ;
printf(“ab
= %d\n”,ab)
;
printf(“a*b = %d\n”,a*b) ;
printf(“a/b = %d\n”,a/b) ;
printf(“a%b = %d\n”,a%b) ;
return 0 ;
}
When you will run this program following output will come:
Give vale of a: 20
Give vale of b: 4
a+b = 24
ab
= 16
a*b = 80
a/b = 5
a%b = 0
Special Operators ++ and In
C some operators allow abbreviation of certain types of arithmetic assignment
statements. These operators are ++ and .
i++ or ++i is equivalent to i = i+1;
ior
i
is equivalent to i = i1;
/* Arithmetic operators */
#include <stdio.h>
Now let us see an example program which shows how to use ++ and :
\* C program to show the use of ++ and *\
int main(void)
{
int a,c;
int b=10;
a=5;
c= a+b; \* C is assigned 10+5 *\
printf(“Sum: %d + %d >
%d\n”,a,b,c);
a++;
b;
prinf(“Now a=%d \n”,a);
prinf(“Now b=%d\n”,b);
c= a+b; \* C is assigned 9+4 *\
printf(“Now Sum is: %d + %d >
%d\n”,a,b,c);
return 0;
}
iii) Comparison Operator
Many time in programming requirement arise to compare two values. C is not having special data type to represent logical values ( 0 or 1). In C by using char and int, with a value of 0 means false and any other value represent true. Comparison operators are used to compare values. When two numeric values are compared using comparison operators, they compare values and produce a logical result.
There are six comparison operators:
Operator Meaning
== Equal to
> Greater than
< Less than
>= Greater than equal to
<= Less than equal to C Programming Language
!= Not equal to
These comparison operators are used in expression like:
I == J
Marks> 10
Marks !=100
Example of use of comparison operator
int x= 5;
(x < 10) (4 < 10) true
(x = = 5) (5 = = 5) false
More detailed discussion about using comparisons operators will be taken up when we will discuss about control statements such as if statement or a for or a while statement in subsequent section of this unit.
iv) Logical Operators
AND, OR and NOT are the commonly used logical operators in C.
Operator Meaning
| | OR
&& AND
! NOT
a || b will be true if either of a or b is having value true. It will be false only if both a and b are having false value, a && b will be true if both a and b are having true value, otherwise false. If a is true then !a will be false.
Logical operators are frequently used to combine comparison operators, for example if a person is eligible for a job if his age is between 18 and 30 years then it can be written as following expression:
age >=18 && age <=30
A person will be eligible only if this expression is evaluated to true. Let us check this
expression for a person having age 25.
25 >=18 && 25<=30 true && true true
We can come to conclusion that false && anything is always false, true || anything is always true.
Punctuation
In C programming language Semicolons(;), colons(:), commas( , ), apostrophes( ‘),
quotation marks(“), braces( [ ]) , brackets( { } ), and parentheses( ( ) ) ; : , ‘ “ [ ]
{ } ( ) are punctuation symbol.
2.4.4 Constants
A constant can be seen as a type of variable which do not change its value in program.
There are many types of constants in c such as numeric constant, character constant , string constant etc
Character constants are typically just the character enclosed in single quotes; 'x', 'y', 'z'.
Some characters can't be represented in single quotes. For them a two character sequence is used. For example:
new line ‘\
n’
single quote – ‘\’’
back slash – ‘\\’
Numeric constants are an continuous sequence of digits (and may contain a period). A numeric constant never contains a comma.
Examples of numeric constants:123, 55.45, 100

Comments
Post a Comment