0% found this document useful (0 votes)
54 views

Types of C Variables:: The Following Are Some Types of C Variables On The Basis of Constants Values It Has

The document discusses different types of C variables and rules for constructing variable names. It also covers C keywords, the structure of a basic C program to add two numbers, and compiling and executing a C program. The main types of C instructions - type declaration, arithmetic, and control instructions - are also introduced.

Uploaded by

dangerman
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Types of C Variables:: The Following Are Some Types of C Variables On The Basis of Constants Values It Has

The document discusses different types of C variables and rules for constructing variable names. It also covers C keywords, the structure of a basic C program to add two numbers, and compiling and executing a C program. The main types of C instructions - type declaration, arithmetic, and control instructions - are also introduced.

Uploaded by

dangerman
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

Types of C Variables:

 The following are some types of C


variables on the basis of constants
values it has.
For example:
○ An integer variable can hold only an integer
constant.
○ A real variable can hold a real constant.
&
o A character variable can hold a character
constant.

1
Rules for Constructing Variable
Names:
 A variable name may consist of letters,
digits, and the underscore ( _ )
 The first character of variable name may
be an alphabetic character or an
underscore ( _ )
 The first character of variable name
cannot be a digit.
 Blank spaces are not allowed in a
variable name.

continued on the next slide...........


2
 Special characters such as arithmetic
operators, # , > , cannot be used in a
variable name.
 Reserved C words cannot be used as
variable names.
 The maximum length of a variable name
is up to 31 characters.
 VAR1 & var1 are two different variables.

3
C keywords:
 Keywords are the words whose meaning
has already been explained to the C
compiler.
 The keywords are also called as
‘Reserved words’.
 Keywords cannot be used as variable
names.
 There are 32 keywords known to C.

continued on next slide..........


4
 32 C keywords:

auto double int struct


break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while

5
The first C program:
 For example:
write a program to add two numbers.

6
 #include<stdio.h>
 #include<conio.h>
 Void main ()
 Int a;
 Int b;
 Int s;
 Printf(“enter value for a \n”);
 Scanf(“%d”,&a);
 Printf(“enter value for b:\n”);

7
 Scarf(“%d”,&b);
 S=a+b;
 printf(“in sum :%d”,s);
 getch ()
 }

8
Compilation and execution of C
program:
 To type your C program you need another program
called Editor.
 once the program has been typed it needs to be
converted to machine language (0s and 1s) before
the machine can execute it. To carry out this
conversion we need another program called
Compiler.
 Integrated development environment (IDE):
consists of an Editor as well as the compiler.
E.g. Turbo C++, Microsoft C++ and Borland C+
+

9
Receiving Input :
○ Printf( );
 printf( )outputs the values to the screen whereas
scanf( ) receives them from the keyboard.

○ Scanf( );
 To make the program general the program Itself
should ask the user to supply the values at run time.
This can be achieved using a function called scanf( )
 This function is a counter-part of the printf( ) function.

10
C instructions
 There are basically three types of
instructions in C:
Type Declaration Instruction
Arithmetic Instruction
Control Instruction

11
Type Declaration Instruction
 This instruction is used to declare the type
of variables being used in the program.
 Any variable used in the program must be
declared before using it in any statement.
 The type declaration statement is written at
the beginning of main( ) function.
Ex: int add ;
float ave, per ;
char name, code ;
 There are several variations of the type
declaration instruction. These are discussed
below:
While declaring the type of variable we can also
initialize it as shown below.
○ int i = 10, j = 25 ;
○ float a = 1.5, b = 1.99 + 2.4 * 1.44 ;

 The order in which we define the variables is


sometimes important sometimes not. For
example;
int i = 10, j = 25 ;
is same as
int j = 25, i = 10 ;
However,
float a = 1.5, b = a + 3.1 ;
is alright, but
float b = a + 3.1, a = 1.5 ;
is not. This is because here we are trying to use a even
before defining it.

 The following statements would work int a, b, c, d ;


 a = b = c =d= 10 ;
However, the following statement would not work
 int a = b = c = d = 10 ;

Once again we are trying to use b (to assign to a) before


defining it.
Arithmetic Instruction
 A C arithmetic instruction consists of a variable
name on the left hand side of = and variable
names & constants on the right hand side of =.
The variables and constants appearing on the
right hand side of = are connected by
arithmetic operators like +, -, *, and /.
 For example;
 int a,b,;
 Float c;
 a=2;
 C=3.5;
 b=a+5*c
 Here
 +,* are arthmetic operators.
= is the assignment operator.
2, 5 are integer constants.
3.5 is real constant.
a,b, are integer variables.
C is real variable.

 The variables and constants together are called


‘operands’ that are operated upon by the
‘arithmetic operators’ and the result is assigned,
using the assignment operator, to the variable
on left-hand side.
 A C arithmetic statement could be of three types.
These are as follows:
 Integer mode arithmetic statement - This is an arithmetic
statement in which all operands are either integer variables
or integer constants.
○ Ex. Int a,b,c,sum;
○ a=3, b=7, c=5;
○ Sum=a+b+c;

 Real mode arithmetic statement - This is an


arithmetic statement in which all operands are
either real constants or real variables.
 For example;
○ float a,b,c ;
○ a=3.4, b=2.1;
○ C=a/b;
 Mixed mode arithmetic statement - This is an arithmetic
statement in which some of the operands are integers and
some of the operands are real.
 For example;
 float x,y,z ;
 int a, b, c, avg;
 Z=x*y/100.0 ;
 avg = ( a + b + c ) / 3 ;

 It is very important to understand how the execution of an


arithmetic statement takes place. Firstly, the right hand
side is evaluated using constants and the numerical
values stored in the variable names. This value is then
assigned to the variable on the left-hand side.
 Though Arithmetic instructions look simple to
use one often commits mistakes in writing them.
Let us take a closer look at these statements.
Note the following points carefully.

C allows only one variable on left-hand side of =. That


is, z = k * l is legal, whereas k * l = z is illegal.
In addition to the division operator C also provides a
modular division operator. This operator returns the
remainder on dividing one integer with another. Thus
the expression 10 / 2 yields 5, whereas, 10 % 2 yields
0.
note that the modulus operator cannot be applied on
float.
 An arithmetic instruction is often used for
storing character constants in character
variables. char a, b, d ;
a = 'F' ;
b = 'G' ;
d = '+' ;
When we do this the ASCII values of the
characters are stored in the variables. ASCII
values are used to represent any character in
memory. The ASCII values of ‘F’ and ‘G’ are 70
and 71 (refer the ASCII Table in Appendix E).
 Arithmetic operations can be performed on ints,
floats and chars. Thus the statements,
char x, y ;
int z ;
x = 'a' ;
y = 'b' ;
z = x + y ;
are perfectly valid, since the addition is performed on
the ASCII values of the characters and not on
characters themselves. The ASCII values of ‘a’ and ‘b’
are 97 and 98, and hence can definitely be added.
Integer and Float
Conversions
 In order to effectively develop C programs, it will
be necessary to understand the rules that are
used for the implicit conversion of floating point
and integer values in C. These are mentioned
below.

An arithmetic operation between an integer and


integer always yields an integer result.

An operation between a real and real always yields a


real result.
An operation between an integer and real always
yields a real result. In this operation the integer is
first promoted to a real and then the operation is
performed. Hence the result is real.
 Following are the few practical examples;
Operation Result Operation Result
5/2 2 2/5 0
5.0/2 2.500000 2.0/5 0.400000
5/2.0 2.500000 2/5.0 0.400000
5.0/2.0 2.500000 2.0/5.0 0.400000
Type Conversion in Assignments
 It may so happen that the type of the expression and
the type of the variable on the left-hand side of the
assignment operator may not be same.

 In such a case the value of the expression is


promoted or demoted depending on the type of the
variable on left-hand side of =.

 For example, consider the following assignment


statements.
 int i ;
 float b ;
 i = 3.5 ;
 b = 30 ;
 Here in the first assignment statement
though the expression’s value is a float (3.5)
it cannot be stored in i since it is an int. In
such a case the float is demoted to an int
and then its value is stored. Hence what gets
stored in i is 3.
 Exactly opposite happens in the next
statement. Here, 30 is promoted to
30.000000 and then stored in b, since b
being a float variable cannot hold anything
except a float value.
Hierarchy of Operations
 While executing an arithmetic statement, which has
two or more operators, we may have some problems
as to how exactly does it get executed. For example,
does the expression 2 * x - 3 * y correspond to (2x)-
(3y) or to 2(x-3y)? Similarly, does A / B * C
correspond to A / (B * C) or to (A / B) * C? To
answer these questions satisfactorily one has to
understand the ‘hierarchy’ of operations. The
priority or precedence in which the operations in an
arithmetic statement are performed is called the
hierarchy of operations. It is also known as order of
precedence of the operators.
 The hierarchy of commonly used operators is
shown below;
Priority Operators Description

1st * / % Multiplication, division, modular division

2nd + - Addition, subtraction

3rd = Assignment

Note: We must always remember to use pair of


parenthesis. If there are more than one set of
parenthesis, the operations within the innermost
parentheses would be performed first, followed by
the operations within the second innermost pair and
so on.
Associativity of Operators
 Associativity refers to the order in which
operators having the same precedence
are evaluated. The operators having the
same precedence order are evaluated
either from right-to-left or from left-to-
right.
Operators Associativity
() ++(postfix) --(postfix) Left-to-right
+(unary) -(unary) ++(prefix) --(prefix) Left-to-right
* / % Left-to-right
+ - Left-to-right
= += *= /= Right-to-left
 For example; in the following expression, the +
and – operators have the same precedence.
Thus the associative rule is used to determine
how the expression is evaluated.
1+2+3-4+5
The associativity is from left-to-right and the
expression is evaluated from left to right as;
i. 3+3-4+5
ii. 6-4+5
iii. 2+5
iv. 7
 The following table gives the equivalent
expressions to indicate the precedence order
and associativity of operators.
Expression Equivalent expression
1 7 + c * d / e (7 + ((c*d) /3))
2 b*b - 4*a*c ( (b*b)- ( (4*a) * c) )
3 a / b/ c ( (a/b) /c)
4 2 + a / b * 5 (2+( ( a/b)* 5))
5 2 * 2.4 / 1.2 * 5 ( ( ( 2*2.4)/1.2) *5)
6 6 + 12 / 6 * 2 - 1 ( ( 6+(*12/6) * 2))-1)
7 a * b % c + 1 ( ( ( a*b) % c) +1)
Control Instructions in C
 As the name suggests the ‘Control
Instructions’ enable us to specify the order in
which the various instructions in a program are
to be executed by the computer.
 In other words the control instructions
determine the ‘flow of control’ in a program.
 There are four types of control instructions in
C language which are discussed below;
 Sequence Control Instruction :The Sequence
control instruction ensures that the instructions are
executed in the same order in which they appear in
the program.
Selection or Decision Control Instruction :Decision
control instructions allow the computer to take a
decision as to which instruction is to be executed
next.

Repetition or Loop Control Instruction : The Loop


control instruction helps computer to execute a group
of statements .

Case Control Instruction : The case control


instruction is used to select option from a set of many
options.

You might also like