Sunday, 22 February 2015

INTRODUCTION TO C


‘C’ & unix was introduced by Dennis Ritchie at Bell Telphone Laboratories in the year 1970’s.  C is a middle level language

Assembly Language  -      Low Level Language
Machine Language    -      Low Level Language
‘C’                             -      Middle Level Language
‘C++’, ‘VB’               -      High Level Language
‘C’ is look like a Character Set.

Data Types:

Integer   -      2 Bytes
Float      -      4 Bytes
Char      -      1 Byte
Double  -      8 Bytes
Long int       -      4 Bytes

Int:
Eg:
78, 34, 678, 1000 -------------

Float:
Eg:
67.003, 23.468, 78.3876 -------------

Char:
Eg:
‘A’, ‘B’, ‘C’, -------------------

String:
‘Table’, ‘Chair’, ‘Pen’, ‘Pencil’ -----------

Double:
Eg:
45.000000000000, 12.0123456789000 ------------
Printf     -      Output Process
Scanf     -      Input Process

Header Files: (PreProcesor)

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#include<malloc.h> etc…
Stdio.h = Standard Input Output . HeaderFiles
Conio.h = Console Input Output . HeaderFiles

Scanf:
Int         =     %d
Float      =     %f
Char      =     %c (or)  %s
Double  =     %f
Long int       =     %ld

There are 32 Keywords
1.    Auto
2.    If    -
3.    Double -
4.    Struct
5.    Do -
6.    Long     -
7.    Signed
8.    Extern
9.    Typedef
10.                   Return  
11.                   Char      -
12.                   Register
13.                   Short
14.                   Sizeof    -
15.                   Break    -
16.                   Continue
17.                   Volatile
18.                   Goto     
19.                   Static
20.                   While    -
21.                   Float      -
22.                   Union   
23.                   Else              -
24.                   Switch   -     
25.                   Case      -
26.                   Enum
27.                   Const
28.                   Void      -
29.                   For -     
30.                   Unsigned
31.                   Default  -
32.                   Int  -

Syntax:

Main()
{
Variable Declarations;
Program Statements;
}
Command Line:
//   /*            */
--------------------------
/*-------------------------
//--------------------------
//--------------------------
--------------------------*/
--------------------------
\n    =     New Line
\t     =     Horizontal Tab

ShortCut Keys:

F1          =     Help
F2          =     Save
F3          =     Open
F5          =     Zoom
F6          =     To see the previous program
Alt F9   =     To compile the program
Ctrl F9  =     To run the program
Alt F5 (or) Getch() = To see the output answer
Alt X     =     Quit
Alt F     =     To open the File menu

Prog:

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“Enter a values:”);
scanf(“%d”,&a);
printf(“Enter b values:”);
scanf(“%d”,&b);
c=a+b;
printf(“Answer=%d”, c);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int n;
float p,r,si;
clrscr();
printf(“Enter principal amount:”);
scanf(“%f”,&p);
printf(“Enter no. of years:”);
scanf(“%d”,&n);
printf(“Enter rate of interest:”);
scanf(“%f,&r);
si=p*n*r/100;
printf(“Simple Interest=%f”, si);
getch();
}

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n;
float p,r,ci,d;
clrscr();
printf(“Enter principal amount:”);
scanf(“%f”,&p);
printf(“Enter no. of years:”);
scanf(“%d”,&n);
printf(“Enter rate of interest:”);
scanf(“%f,&r);
d=1+r/100;
ci=p*pow(d,n)-p;
printf(“Compound Interest=%f”, ci);
getch();
}

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n;
float p,r,ci,d,si;
clrscr();
printf(“Enter p,n,r values:”);
scanf(“%f%d%f”,&p,&n,&r);
si=p*n*r/100;
d=1+r/100;
ci=p*pow(d,n)-p;
printf(“Simple Interest=%f\n”, si);
printf(“Compound Interest=%f”, ci);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
float f,c;
clrscr();
printf(“Enter Celsius value:”);
scanf(“%f”,&c);
f=9.0/5*c+32;
printf(“Farenheit = %f”, f);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
float f,c;
clrscr();
printf(“Enter Farenheit value:”);
scanf(“%f”,&f);
c=5/9.0*f-32;
printf(“Celsius = %f”, c);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
float r,area;
clrscr();
printf(“Enter radius value:”);
scanf(“%f”,&r);
area=3.14*r*r;
printf(“Area=%f”,area);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
float bpay, da, hra, pf, wcharge, gpay, dedu, npay;
clrscr();
printf(“Enter basic pay:”);
scanf(“%f”,&bpay);
hra=0.15*bpay;
da=0.9*bpay;
gpay=bpay+hra+da;
pf=0.1*bpay;
wcharge=200;
 dedu= pf+wcharge;
npay=gpay-dedu;
printf(“Gross pay=%f\n”,gpay);
printf(“Deductions=%f\n”,dedu);
printf(“Netpay=%f\n”,npay);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int m1,m2,m3,i;
char name[20];
clrscr();
i=1;
while(i<=10)
{
printf(“Enter name:”);
scanf(“%s”,&name);
printf(“Enter three subject marks:\n”);
scanf(“%d%d%d”,&m1,&m2,&m3);
if(m1>=35 && m2>=35 && m3>=35)
printf(“Result=pass\n”);
else
printf(“Result=fail\n”);
i++;
}
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int age,wait;
char name[10];
clrscr();
printf(“Enter name:”);
scanf(“%s”,&name);
printf(“Enter age:”);
scanf(“%d”,&age);
if(age>=18)
printf(“%s you are eligible to vote”,name);
else
{
wait = 18 – age;
printf(“%s you have to wait for %d years”,name,wait);
}
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=254;i++)
printf(“%c\t”,i);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int nch;
char word1[20],word2[20],word3[20];
clrscr();
printf(“Enter a first word:\n”);
gets(word1);
printf(“Enter a second word:\n”);
gets(word2);
printf(“\n Contents of the word3 after string copy=”);
strcpy(word3,word1);
puts(word3);
printf(“\t Contents of the word1 after string concatenation=”);
strcat(word1,word2);
puts(word1);
printf(“Length of the concanate string length=”);
nch=strlen(word1);
printf(“%d”,nch);
getch();
}



 There are two types of functions.

1.    Getchar() function
2.    Putchar() function

Getchar() function:

Syntax:
Char c;
--------------
--------------
--------------
c=getchar();

Eg:
#include<stdio.h>
#include<conio.h>
void main()
{
char c;
clrscr();
printf(“Enter character:”);
c=getchar();
if (c!=EOF)
printf(“End of file”);
getch();
}

Putchar() function:

Syntax:
Char c;
---------------
---------------
---------------
putchar( c );
}

Eg:
#include<stdio.h>
#include<conio.h>
void main()
{
char c;
clrscr();
printf(“Enter upper case character:”);
c=getchar();
printf(“Lower case=”);
putchar(c+32);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
char c;
clrscr();
printf(“Enter lower case character:”);
c=getchar();
printf(“Upper case=”);
putchar(c-32);
getch();
}

No comments:

Post a Comment