Sie sind auf Seite 1von 15

C constants:

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

int - data type


int is used to define integer numbers.
float - data type
float is used to define floating point numbers.
double - data type
double is used to define BIG floating point numbers.
char - data type
char defines characters.
Type

Bytes

Bits

Range

Char or signed char

-128 to 127

Unsigned char

0 to 255

Int or signed int

16

Unsigned int

16

0 to 65535
-128 to 127

-32768 to 32767

Signed short int

Unsigned short int

Signed long int

32

Unsigned long int

32

Float

32

Double

0 to 255
-2,147,483,648 -> +2,147,483,647
0 to 4,294,967,295

64

3.4 e-38 to 3.4 e+38


1.7 e-308 to 1.7 e+308

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.

Example of global variable:

# include<stdio.h>
#include<conio.h>
int x=5;

//here x is a global variable

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;

//here x is a local variable

printf(here x is a local variable \t %d,x);


getch();
}
_____________________________________________________________________________________
OperatorsAn operator is a symbol which helps the user give the command to computer, and computer
to do a certain mathematical or logical manipulations.
Operators are used in C language program to operate on data and variables.
C has a rich set of operators which can be classified as:
1.
2.
3.
4.
5.
6.

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

Subtraction or Unary Minus


*
Multiplication
/
Division
%
Modulus Operator
Example of Arithmetic Operators:
#include<stdio.h>
//include header file stdio.h
#include<conio.h>
void main()
//tell the compiler the start of the program
{
int num1, num2, sum, sub, mul, div, mod;
//declaration of variables
scanf (%d %d, &num1, &num2);
//inputs the operands
sum = num1+num2;
//addition of numbers and storing in sum
printf(\n Thu sum is = %d, sum);
//display the output
sub = num1-num2;
//subtraction of numbers and storing in sub.
printf(\n Thu difference is = %d, sub); //display the output
mul = num1*num2;
//multiplication of numbers and storing in mul.
printf(\n Thu product is = %d, mul);
//display the output
div = num1/num2;
//division of numbers and storing in div.
printf(\n Thu division is = %d, div);
//display the output
mod = num1%num2;
//modulus of numbers and storing in mod.
printf(\n Thu modulus is = %d, mod);
//display the output
getch();
}

_________________________________________________________________________________

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

Example of Relational Operators:


#include<stdio.h>
#include<conio.h>
Void main()
{
int a = 21;
int b = 10;
int c ;
if( a == b )
{
printf("Line
}
else
{
printf("Line
}
if ( a < b )
{
printf("Line
}
else
{
printf("Line
}
if ( a > b )
{
printf("Line
}
else
{
printf("Line
}
getch();

1 - a is equal to b\n" );

1 - a is not equal to b\n" );

2 - a is less than b\n" );

2 - a is not less than b\n" );

3 - a is greater than b\n" );

3 - a is not greater than 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

Logical AND (&&) :


This operator is used to evaluate 2 conditions or expressions with relational operators
simultaneously. If both the expressions to the left and to the right of the logical operator is
true then the whole compound expression is true.
Logical OR (||) :
The logical OR is used to combine 2 expressions or the condition evaluates to true if any one
of the 2 expressions is true.
Logical NOT (!) :
The logical not operator takes single expression and evaluates to true if the expression is
false and evaluates to false if the expression is true. In other words it just reverses the
value of the expression.
Example of Logical Operators in C program:
#include<stdio.h>
#include<conio.h>

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
_________________________________________________________________________________

Another Example of Logical Operators in C program:


#include <stdio.h>
#include <conio.h>
void main()
{
int no1=2, no2=5;
clrscr();
printf("\n\n %d",(no1 && no2));
printf("\n\n %d",(no1 || no2));
getch();
}

OUTPUT is
1
1
_________________________________________________________________________________

Shorthand assignment operators:

Statement with simple


assignment operator
a
a
a
a
a

=
=
=
=
=

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

Exp1 ? Exp2 : Exp3


The operator ?: works as follows >> exp1 is evaluated first. If it is true, then expression exp2 is
evaluated and become the value of the expression. If exp1 is false then exp3 is calculated and it
becomes the value of the expression
For example:
a=10;
b=15;
x=(a>b)?a:b;
the meaning of above expression is
if (a>b)
x=a;
else
x=b;
Increment and decrement operator :
C allows ++ for increment and - - for decrement
For example

#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

Managing Input and Output Operations


Specifier Meaning
%c Print a character
%d Print a Integer
%i Print a Integer

%e Print float value in exponential form.


%f Print float value
%g Print using
%e or %f whichever is smaller
%o Print actual value
%s Print a string
%x Print a hexadecimal integer (Unsigned) using lower case a F
%X Print a hexadecimal integer (Unsigned) using upper case A F
%a Print a unsigned integer.
%p Print aError! Hyperlink reference not valid.value%
hx hex short
______________________________________________________________________________

Decision Making (Branching)


Branching:
Branching is so called because the program chooses to follow one branch or another.
if statement
This is the most simple form of the branching statements.
It takes an expression in parenthesis and an statement or block of statements. if the expression is
true then the statement or block of statements gets executed otherwise these statements are
skipped.
We can use if statement in 5 ways:

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();
}

______________________________________________________________________________

5) Nested if-else statement


Switch statement
The switch case statements allows to control complex conditional and branching
operations. It include any number of case instances, but should not have the same
value. The break statement ends the processing of particular case otherwise, the
program continues to the next case and executing all the statements until it reach
at the end of the statement. The default statement shows the error message. You
can see in the given example, the program prompts the user to enter any number.
The expression defined in the switch statement finds whether the number is odd or
even. If the number found is even, it will show the first case otherwise it will show
the second case.

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.

For example, consider the following C declaration:


int a[100];
It declares an array of 100 integers.

Das könnte Ihnen auch gefallen