Professional Documents
Culture Documents
FCPC Unit-3
FCPC Unit-3
Constants also called a literals. Constant refer to fixed values that the program may not
alter. constant can be any of the basic data types.
In c have a two type of constant :
1. Numeric constant
2. Character constant
C constant is showing in the figure:
Data type in C
A data type in a programming language is a set of data with values having predefined
characteristics.
In c we write any program that have a variables . that variables have a some values. But
that value will be which type we dont know , so that always we define by the data type.
The definition of a variable will assign storage for the variable and define the type of data
that will be held in the location.
Types of data type: data type types is following given bellow
Primary
defined
data type
type
integer
enumerated
float
typeded
Double
Long float
pointer
data type
structure
datatype
array
string
structure
union
user
data
Bytes
Bits
Range
-128 to 127
Unsigned char
0 to 255
16
Unsigned int
16
0 to 65535
-128 to 127
-32768 to 32767
32
32
Float
32
Double
0 to 255
-2,147,483,648 -> +2,147,483,647
0 to 4,294,967,295
64
Variables:
A variable is a meaningful name of data storage location in computer memory. When using a
variable you refer to memory address of computer.
Types of variables: in c variable are divided in two types
1. Global variable- global variable is a variable defined outside any function block. Global
variable is also called external variable.
2. Local variable- local variable is a variable defined inside a function block. Local variable
is also called automatic variables.
# include<stdio.h>
#include<conio.h>
int x=5;
void main()
{
printf(here x is a global variable \t %d,x);
getch();
}
Example of local variable:
# include<stdio.h>
#include<conio.h>
void main()
{
int x=5;
Arithmetic operator
Relational Operators
Logical Operators
Assignment Operators
Increments and Decrement Operators
Conditional Operators
7. Bitwise Operators
8. Special Operators
__________________________________________________________________________________
Arithmetic Operators:
Arithmetic operators means do the all arithmetic operations like as addition, subtraction,
multiplication , division, modules etc.
All the operators have almost the same meaning as in other languages. Both unary and
binary operations are available in C language. Unary operations operate on a singe operand,
therefore the number 5 when operated by unary will have the value 5.
Arithmetic Operators:
Operator
Meaning
+
Addition or Unary Plus
_________________________________________________________________________________
Relational Operators
Often it is required to compare the relationship between operands and bring out a decision
and program accordingly. This is when
the relational operator come into picture.
C supports the following relational operators.
Operator
Meaning
<
<=
>
>=
==
!=
is less than
is less than or equal to
is greater than
is greater than or equal to
is equal to
is not equal to
1 - a is equal to b\n" );
}
_________________________________________________________________________________
Logical Operators
C has the following logical operators, they compare or evaluate logical and relational
expressions.
Operator
&&
Meanings
Logical AND
||
!
Logical OR
Logical NOT
Void main ()
{
int i = 1, j = -1, k = 0, w,x,y,z;
w = i || j || k;
x = i && j && k;
y = i || j && k;
z = i && j && k;
printf("w=%d x=%d y=%d z=%d", w,x,y,z);
getch();
}
OUTPUT is
1
0
1
0
_________________________________________________________________________________
OUTPUT is
1
1
_________________________________________________________________________________
=
=
=
=
=
a
a
a
a
a
Statement with
shorthand operator
+1
1
* (n+1)
/ (n+1)
%b
a += 1
a -= 1
a *= (n+1)
a /= (n+1)
a %= b
_________________________________________________________________________
Conditional operator:
A ternary operator ?: is called conditional operator in c
Which is defined as
#include<stdio.h>
#include<conio.h>
Void main()
{
int y,m=5;
y=++m;
printf(the value of y is = %d,y);
getch();
clrscr();
}
Output is
6
Another case is:
#include<stdio.h>
#include<conio.h>
Void main()
{
int y,m=5;
y=m++;
printf(the value of m is = %d,m);
getch();
clrscr();
}
Output is
6
1)
if (expression)
statement;
Example:
#include<stdio.h>
#include<conio.h>
Void main()
{
int i=10;
if(i= = 10)
printf(the value of i is matched);
getch();
}
--------------------------------------------------------------------------------------------------------------------------
2)
if (expression)
{
Block of statements;
}
Example:
#include<stdio.h>
#include<conio.h>
Void main()
{
int i=10;
if(i= = 10)
{
printf(the value of i is matched);
printf(the value of i is 10 );
}
getch();
}
______________________________________________________________________________
3)
if (expression)
{
Block of statements;
}
else
{
Block of statements;
}
Example:
#include<stdio.h>
#include<conio.h>
Void main()
{
int i=10;
if(i= = 10)
{
printf(the value of i is matched);
printf(the value of i is 10 );
}
Else
{
Printf(the value of i is not matched );
}
getch();
}
_________________________________________________________________________________
4)
if (expression)
{
Block of statements;
}
else if(expression)
{
Block of statements;
}
else
{
Block of statements;
}
Example:
#include<stdio.h>
#include<conio.h>
Void main()
{
int x;
scanf(%d,&x);
if(x>79)
printf(Honours);
else if(x>59)
printf(First Division);
else if(x>49)
printf(Second Division);
else if(x>39)
printf(Third Division);
else
printf(Fail);
getch();
}
______________________________________________________________________________
switch( expression )
{
case constant-expression1: statements1; break;
case constant-expression2: statements2; break;
case constant-expression3: statements3; break;
default : statements4;
}
WAP a program to display the entered number is even or odd:
#include <stdio.h>
#include <conio.h>
void main() {
clrscr();
int n;
printf("Enter any number:");
scanf("%d", &n);
switch (n%2) {
case 0:
printf("Entered number %d is even \n", n);
break;
case 1:
printf("Entered number %d is odd \n", n);
break;
Default:
Printf(entered number is negative or zero);
}
getch();
}
Looping:
Loops provide a way to repeat commands and control how many times they are repeated. C
provides a number of looping way.
There are three types of loop in c
1) For loop
2) While loop
3) Do-while loop
1)For loop
for( expression1; expression2; expression3)
{
Single statement
or
Block of statements;
}
Here expression 1 is used for loop initialization, expression 2 is used for condition for loop breaking,
expression 3 for increment the value of variable
Example:
WAP to display all even number from 1 to 50
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
for(i=2;i<=50;i=i+2)
{
printf("%d\n",i);
}
getch();
}
2)While loop
while ( expression )
{
Single statement
Or
Block of statements;
}
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
i=2;
while(i<=50)
{
printf("%d\n",i);
i=i+2;
}
getch();
}
3)Do-while loop
do
{
Single statement
or
Block of statements;
}
while(expression);
WAP to show that how do-while loop execute a single false statement
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
i=51;
do
{
printf("%d\n",i);
i=i+2;
}
while(i<=50);
getch();
}
WAP to find out 3rd place number from 1 to 100 and get the sum of these output which is
divisible by 5.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,sum=0;
for(i=1;i<=100;i=i+3)
{
printf("%d\n",i);
if(i%5==0)
{
sum=sum+i;
}
}
printf("The sum of these values is = %d\n\n",sum);
getch();
}
ARRAYS:
Arrays are a data structure which hold multiple variables of the same data type.
There are two things with array
1) Declaration
2) Initialization
Declaration
The general form for declaring a single dimensional array is:
data_type array_name[expression];
where data_type represents data type of the array. That is, integer, char, float etc. array_name is
the name of array and expression which indicates the number of elements in the array.