Professional Documents
Culture Documents
Fundamentals
Unit II
int main(void)
printf("Hello world!\n");
return 0;
int total,marks;
Here total and marks are user
defined identifiers.
Variables :
Variable is an identifier that is used to represent some
specified type of information.
Eg: x=3 Here x is variable.
PREETHA V - AP/CSE, SRIT.
Keywords
It is a reserved word, which cannot be used for
anything else. Examples:
auto register continue
double typedef for
int char signed
struct extern void
break return default
else union goto
long const sizeof
switch float do
case short if
enum unsigned
static while
PREETHA V - AP/CSE, SRIT.
Constants
Constants
String constants
String constant are sequence of characters
enclosed with in double quote.
Eg: “Hello” ,”444”,”a” etc,.
PREETHA V - AP/CSE, SRIT.
Data Types
A data type is a classification identifying one of
various types of data, such as real-
valued, integer or Boolean, that determines the
possible values for that type
Operator Description
+ Adds two operands
- Subtracts second operand
from first
* Multiply two operand
/ Divide numerator by
denominator
% Remainder of division
++ Increment operator-
increases integer value by
one
-- Decrement operator-
decreases integer value by
PREETHA V - AP/CSE, SRIT. one
Sample program
#include<stdio.h> // Header File
#include <conio.h>
int b=10; //Global Declaration
void main ( ) /* main is the starting of every c program */
{
int a,c; //Local Declaration
clrscr( );
scanf(“%d”,&a);
printf(“ \n The sum of the two values:”);
c = a+b;
printf(“%d”,c);
getch( );
PREETHA V - AP/CSE, SRIT.
}
Relational operators
The relational operators are
Operator Description
== Check if two operand are
equal
!= Check if two operand are
not equal
> Check if operand on the
left is greater than
operand on the right
< Check if operand on the
left is smaller than
operand on the right
>= Check left operand is
greater than or equal to
right operand
<= Check if operand on left is
PREETHA V - AP/CSE, SRIT. smaller than or equal to
right operand
Sample program
#include<stdio.h>
#include <conio.h>
void main ( )
{
int a=10,b=3,c=5;
clrscr( );
if(a>b) // relational operator
{
printf(" \n a is bigger than b");
}
if((a>b)&&(a>c)) //Logical operator
{
printf(" \n a is biggest");
}
getch( );
}
Output:
a is bigger than b
a is biggest PREETHA V - AP/CSE, SRIT.
Logical operators
C supports following 3 logical operators
Suppose a=1 and b=0
#include<stdio.h>
#include <conio.h>
void main ( )
{
int a=5,b=4,c;
clrscr( );
c = a&b;
printf(" \n value a&b is:%d",c);
getch( );
}
Output:
value a&b is:4
PREETHA V - AP/CSE, SRIT.
Assignment operators
Assignment operators supported by C are
#include<stdio.h>
#include <conio.h>
int b=10;
void main ( )
{
int a=3,b=5;
clrscr( );
a+=b; // a= a+b
printf(" \n The sum of the two values:%d",a);
getch( );
}
Output:
The
PREETHAsum ofSRIT.the two values:8
V - AP/CSE,
Increment or decrement
operator (Unary)
It is used to Increment or decrement an operand.
Output:
The Larger Value is 8
comma operator ( , )
sizeof operator
pointer operator (& , *) etc,.
#include<stdio.h>
#include <conio.h>
void main ( )
{
int c;
clrscr( );
printf(" \n size of int is:%d",sizeof c);
getch( );
}
Output:
size of int is: 2
PREETHA V - AP/CSE, SRIT.
Expression
An expression represents data item such as variable, constant which are
interconnected using operators.
Eg:
Expression C Expression
a+b+c a+b+c
Example:
5 - 20/4 + 3*3 – 1 5 – (20/4) + 3*(3 – 1)
=5-5+9–1 = 5 - 5 + 3*2
=0+9–1 =5-5+6
=9–1 =6
=8
PREETHA V - AP/CSE, SRIT.
Input/output Function
Input/output
Function
Formatted Unformatted
An input can be given in the form of a file or from the command line
C programming provides a set of built-in functions to read the given input and
display the output
char *gets(char *s)reads a line from stdin into the buffer pointed to by s until
either a terminating newline or EOF
int puts(const char *s)writes the string ‘s’ and ‘a’ trailing newline to stdout
Categories:
Sequential structure
Selection structure
Iteration structure etc,.
Sequential structure
In which instructions are executed in sequence.
Selection structure
In which instruction are executed based on the result of some condition.
Iteration structure
In which instruction are executed repeatedly.
Syntax: If
if (condition is true) condition
True
{
Statements
Statements;
False
a is greater than 10
IF…ELSE Statement
Syntax
if (condition)
{ True If False
True statements; Condition
}
else True False
statements statements
{
False statements;
}
If False
Condition
1
False
True
Statements
If
True False
Condition
2
True False
statements statements
if (condition1)
{
if (condition2)
{
True statements;
}
else
{
False statements;
}
}
else
{
False statements;
}
PREETHA V - AP/CSE, SRIT.
IF…ELSE LADDER
Statements Statements
#include<stdio.h>
#include<conio.h>
void main()
{
float Basic;
float Gross_salary,da,hra,pf;
Gross_salary=da=hra=pf=0;
clrscr();
printf("\nEnter the salary:");
scanf("%f",&Basic);
hra=Basic*0.10;
pf=Basic*0.12;
da=Basic*0.35;
Gross_salary=Basic+da+hra-pf;
printf("\n The Gross salary is:%f",Gross_salary);
PREETHA V - AP/CSE, SRIT.
if(Gross_salary>=35000)
{
printf("\nThe Grosssalary is Greater than 35000");
}
else if(Gross_salary>=25000)
{
printf("\nThe Grosssalary is Between 25000 and 35000");
}
else
{
printf(“\nThe Gross salary is less than 25000");
}
getch();
}
Output:
Enter the Basic:12000
The Gross salary is 15960.000000
PREETHA V - AP/CSE, SRIT.
Syntax
.
False
WHILE (condition) condition
{
. True
Body of the loop; Body of The loop
.
}
Syntax
do
Body of The loop
{
True
False
}while (condition);
PREETHA V - AP/CSE, SRIT.
Example
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,n;
printf("\nEnter the Number:");
scanf("%d",&n);
do
{
printf(“hello\n”);
i++;
}while(i<=n);
}
Output:
Enter the Number: 4
Hello
Hello
Hello
PREETHA V - AP/CSE, SRIT.
Hello
For Loop
Syntax Initialization
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
Printf(“enter the value of n”);
Scanf(“%d”,&n);
For(i=0;i<=n;i++)
{
Printf(“Hello\n”);
}
Output:
Enter the Number:3
Hello
Hello
Hello
Hello
PREETHA V - AP/CSE, SRIT.
NESTED FOR LOOP
Syntax:
for (initi; cond; Inc/Dec)
{
}
PREETHA V - AP/CSE, SRIT.
Example
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
for(i=1;i<=3;i++)
{
printf("\n");
Output:
for(j=1;j<=3;j++)
{ 1 2 3
printf("%d\t",j); 1 2 3
}
1 2 3
}
getch();
} PREETHA V - AP/CSE, SRIT.
Example
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
for(i=1;i<=4;i++)
{
printf("\n");
for(j=i;j<=4;j++)
{ Output:
printf("%d\t",j); 1 2 34
}
2 3 4
}
getch(); 3 4
}PREETHA V - AP/CSE, SRIT. 4
SWITCH CASE
Syntax
switch (expression) Switch
{
case constant 1: Case 1
block1;
break;
case constant 2: Case 2
block2;
break;
. Default
. case
default :
default block;
break;
} PREETHA V - AP/CSE, SRIT.
Example 1
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
printf("\nEnter the Number:");
scanf("%d",&n);
switch(n)
{
case 1:
printf("\n Its in case 1");
break;
case 2:
printf("\n Its in case 2");
break;
default:
printf("\n Its in default");
Output:
break; Enter the Number:1
} Its in case 1
getch();
PREETHA V - AP/CSE, SRIT.
}
Example 2
#include<stdio.h> case 2 :
#include<conio.h> c=a-b;
void main() printf("The sub is :%d",c);
{ break;
int n,a,b,c; default:
clrscr(); printf(“\n no
printf("\nenter the two numbers:"); operation”);
scanf("%d\t%d",&a,&b); break;
printf("\nEnter the choice: "); }
scanf("%d",&n); getch();
switch(n) }
{
case 1 :
c=a+b;
Try to find the output youself!
printf("The addition is :%d",c);
break;
PREETHA V - AP/CSE, SRIT.
Break Statement
Itis used to terminate the loop
When a break statement is encountered inside a loop, then
the loop is terminated.
………… …………
………… …………
………… …………
goto label; label:
…………
…………
PREETHA V - AP/CSE, SRIT.
Example programs
solving simple scientific and statistical problems
#include<stdio.h>
#include<conio.h>
void main()
{
int r=0,sum=0,n,a;
printf("\nEnter the number:");
scanf("%d",&n);
a=n;
if(a==sum)
{
printf("\nIt is an Armstrong number");
}
else
{
printf("\nIt is not an Armstrong number");
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int r=0,sum=0,n;
printf("\nEnter the no:");
scanf("%d",&n);
while(n>0)
{
r=n%10;
sum=sum*10+r;
n=n/10;
}
printf("Reverse of the number is:%d",sum);
getch();
}
#include<stdio.h>
#include <conio.h>
void main ( )
{
int a,b,c;
clrscr( );
printf(" \nEnter the value of a:");
scanf("%d",&a);
printf(" \nEnter the value of b:");
scanf("%d",&b);
c=a;
a=b;
b=c;
printf(" \nThe value of a is:%d",a);
printf(" \nThe value of b is:%d",b);
getch( );
}