Thursday, 16 January 2014

Explain comma operator with example.

It is used to separate 2 or more expressions.
It has lowest priority among all operators.

Write a program to show comma operator

#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
printf("Addition =%d \n Subtraction=5d",3+2,3-2);
}
output
Addition=5
Subtraction=1


Tuesday, 14 January 2014

Explain Logical operator with example.

It check logical relation between two expressions.If relation is true it returns 1 otherwise 0.
The operators are logical AND(&&),logical OR(||),logical NOT(!=).

Write a program to print logic 1 if input character is capital otherwise 0.

#include<stdio.h>
#include<conio.h>
main()
{
char x;
int y;
clrscr();
printf(" \n Enter a character ");
scanf(" %c ",&x);
y=( x>=65 && x<=90 ? 1:0 );
printf(" Y : %d ",y );


}

Output
Enter a character : A
Y : 1
Enter a character : a
Y : 0

Write a program to display 1 if inputted number is between 1-100 otherwise 0.Use logical AND(&&) operator.

#include<stdio.h>
#include<conio.h>
main()
{
char x;
int y;
clrscr();
printf(" \n Enter a number:");
scanf(" %c ",&x);
y=( x>=1 && x<=100 ? 1:0 );
printf(" Y : %d ",y );
}

output
Enter a number:5
Y= 1


Write a program to display 1 if inputted number is either 1  or 100 otherwise 0.Use logical OR(||) operator.

#include<stdio.h>
#include<conio.h>
main()
{
char x;
int y;
clrscr();
printf(" \n Enter a number:");
scanf(" %c ",&x);
y=( x==1 || x==100 ? 1:0 );
printf(" Y : %d ",y );
}

output
Enter a number:5
Y=0
Enter a number:100
Y=1

Write a program to display 1 if inputted number is between 1-100 otherwise 0.Use logical AND(&&) operator.

#include<stdio.h>
#include<conio.h>
main()
{
char x;
int y;
clrscr();
printf(" \n Enter a number");
scanf(" %c ",&x);
y=( x>=1 && x<=100 ? 1:0 );
printf(" Y : %d ",y );
}

output
Enter a number:5
Y= 1

Write a program to display 1 if inputted number is not 100 otherwise 0.Use logical NOT(!=) operator.

#include<stdio.h>
#include<conio.h>
main()
{
char x;
int y;
clrscr();
printf(" \n Enter a number");
scanf(" %c ",&x);
y=( x!=100 ? 1:0 );
printf(" Y : %d ",y );
}

output
Enter a number:100
Y= 0

Explain Relational operator with example.

It check relation between two constant values.If relation is true it returns 1 otherwise 0.
The operators are >=,<=,<,>,==,!=.


Write a program to use all relational operator and display thier return values.

#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
printf("  \n Condition : Return value  ");
printf("  \n 10!=10 :%5d  ",10!=10);  /* not equal to */
printf("  \n 10<=10 :%5d  ",10<=10);  /* less than or equal too */
}

Output

Condition : Return Value
   10!=10 : 0
   10<=10 : 1

Explain Input and Output function in C



I/O function

Input function-A program takes data through input function.
Output function-A program displays result on screen through output function

2 Types

a)  Formatted b) Unformatted function

Formatted function-It read and write all types of data types.

2 Types

a) printf( ) b) scanf( )

printf() statement-It prints data values to the console.It need conversion symbols to print  variable values.Here escape sequence \n is used for new line.

scanf() statement-It reads data values.It need conversion symbols to read variable values.
The '&' address operator is used to indicate memory location of variable,so that value read should be placed in that location.

Unformatted function-It work with only character data type.
 Types
(a) getchar( ) (b) putchar( ) (c) getch( ) (d) getche ( ) (e) gets( ) (f) puts( )

Friday, 10 January 2014

Explain arithematic operator in C

binary arithmetic operator-This is used for calculation between two constant values.They are +,-,*,/,%.
unary arithmetic operator-This is used for calculation with one value.They are unary minus(-),increment(++),decrement(--),address operator(&),sizeof.

Write a program to show effect of increment operator as a prefix and suffix.

#include<stdio.h>
#include<conio.h>
main()
{
int a,b,x=10,y=20;
clrscr();
a=x * ++y;  /* increment as prefix,here  value of y is increased  and then used for multiplication*/
c=x*y;
b=x * y++; /*increment as suffix,here  after multiplication y is increased */
d=x*y;
printf(" \n %d %d ",a,c,b,d);
}

output


210 210 200 210

address (&) operator-It prints address of variable in memory.
sizeof() operator-It gives bytes occupied by variable.

Write a program to use address (&) operator and sizeof() operator.


#include<stdio.h>
#include<conio.h>
main()
{
int x=2;
float y=2;
clrscr();
printf("\n sizeof(x)=%d bytes,sizeof(y)=%d bytes ",sizeof(x),sizeof(y));
printf("\n Address of x=%u,Address of y=%u",&x,&y);
}


output
sizeof(x)=2,sizeof(y)=4
Address of x=4066,Address of y=25096

Explain variable in C

Variable
A variable is a data name used for storing a data value.
Its value may change during execution.
It should not be a C keyword.
It may be a combination of uppercase and lowercase characters.
Ex-
sUm and SUM both are different.
It should start with a character not a digit
Declare variable
Its declaration should be done in declaration part of C program because it helps in allocating memory.
Syntax-
datatype variablename;
ex-
int age;
int a,b,c;
Initialize variable
Initialization and declaration of variable can be done in same line.
syntax-
datatype varaible = constant;
ex-
int y=2;
Constant variable
To make value of variable constant use keyword const before declaration
ex-
const int m=1;

Monday, 6 January 2014

Explain data types in C

Data types 
They are used to define  variables before its use.As per need different data types are used in C.
For example
a)int is used to define integer numbers
{
int count;
count=4;
}
b)float is used to define floating point numbers
{
float miles;
miles=5.6;
}
c)double is used to define big floating point numbers,it reserves twice the storage for numbers.
{
double atoms;
atoms=25000;
}
d)char defines character
{
char letter;
letter='c';
These are common data types.Others are short,long,signed,unsigned.
short int<int<long int
float<double<long double
signed and unsigned
when a variable is declared as unsigned then negative range of data type is declared as positive.
example
unsigned long int c;
Memory size of different data types
short integer occupies memory of 2 bytes
long integer occupies memory of 4 bytes
signed and unsigned integer occupies memory of 2 bytes
float occupies memory of 4 bytes
double occupies memory of 8 bytes
character occupies memory of 1 byte