C Programming - Fundamentals
Prepared By C.SaravanaKumar
- Ex 1 : My First Basic Program -I
- Ex 2 : My First Basic Program -II
- Ex 3 : My First Basic Program -III
- Ex 4 : Integer & Float Converstion
- Ex 5 : Floating Point Error..
- Ex 6 : Variable Declaration
- Ex 7 : Working With Variable
- Ex 8 : Integer Variable With Scanf-I
- Ex 9 : Integer Variable With Scanf-II
- Ex 10 :Integer Variable With Scanf-III
- Ex 11 :Integer Variable With Scanf-IV
- Ex 12 :Character Variable Declaration
- Ex 13 :String Variable With Scanf Function
- Ex 14 :String Variable With gets Function
- Ex 15 :Combine all Variables
- Ex 16 :Print Employee Information with gotoxy function
Example : 1 Write a Program to print "Hello"
#include<stdio.h>
#include<conio.h>
void main()
{
/*My First Program - Comment Line */
clrscr();
printf("Hello");
getch();
}
#include<conio.h>
void main()
{
/*My First Program - Comment Line */
clrscr();
printf("Hello");
getch();
}
Output :
Hello
Example : 2
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("Hello\nWelcome");
printf("\nGoldenjrsinfo");
printf("-My Web");
getch();
}
#include<conio.h>
void main()
{
clrscr();
printf("Hello\nWelcome");
printf("\nGoldenjrsinfo");
printf("-My Web");
getch();
}
Output :
Hello
Welcome
Goldenjrsinfo-MyWeb
Welcome
Goldenjrsinfo-MyWeb
Example : 3
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("\nHello 12");
printf("\n%d");
printf("\n%d",100);
printf("\nA=%d",100);
printf("\nA=%d+10",100);
printf("\nA=%d",100+10);
getch();
}
#include<conio.h>
void main()
{
clrscr();
printf("\nHello 12");
printf("\n%d");
printf("\n%d",100);
printf("\nA=%d",100);
printf("\nA=%d+10",100);
printf("\nA=%d",100+10);
getch();
}
Output :
Hello 12
0
100
A=100
A=100+10
A=110
0
100
A=100
A=100+10
A=110
Example : 4 Integer & Float Conversions (5/2)
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("\n1-> %d",5/2);
printf("\n2-> 5/2 = %d",5/2);
/* printf("\n5/2 = %f",5/2); This Line is Error */
printf("\n3-> 5.0/2 = %d",5.0/2);
printf("\n4-> 5/0/2 = %f",5.0/2);
printf("\n5-> 5.0/2 = %.2f",5.0/2);
printf("\n6-> 5/2.0 = %.2f",5/2.0);
printf("\n7-> 5.0/2.0=%.2f",5.0/2.0);
getch();
}
#include<conio.h>
void main()
{
clrscr();
printf("\n1-> %d",5/2);
printf("\n2-> 5/2 = %d",5/2);
/* printf("\n5/2 = %f",5/2); This Line is Error */
printf("\n3-> 5.0/2 = %d",5.0/2);
printf("\n4-> 5/0/2 = %f",5.0/2);
printf("\n5-> 5.0/2 = %.2f",5.0/2);
printf("\n6-> 5/2.0 = %.2f",5/2.0);
printf("\n7-> 5.0/2.0=%.2f",5.0/2.0);
getch();
}
Output :
1-> 2
2-> 5/2 = 2
3-> 5.0/2 = 0
4-> 5/0/2 = 2.500000
5-> 5.0/2 = 2.50
6-> 5/2.0 = 2.50
7-> 5.0/2.0=2.50
2-> 5/2 = 2
3-> 5.0/2 = 0
4-> 5/0/2 = 2.500000
5-> 5.0/2 = 2.50
6-> 5/2.0 = 2.50
7-> 5.0/2.0=2.50
Example : 5 Floating Point Error....
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("\n5/2 = %f",5/2);
/*if Error found this program replace %f into %d"*/
getch();
}
#include<conio.h>
void main()
{
clrscr();
printf("\n5/2 = %f",5/2);
/*if Error found this program replace %f into %d"*/
getch();
}
Output :(Press ALT+F9 to compile In Turboc & Press ALT+F5 to see below Message)
printf : floating point formats not linked
Abnormal program termination
Abnormal program termination
Example : 6 Variables.....
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("\n%d",10);
printf("\n%d (Garbage Value)",a);
getch();
}
#include<conio.h>
void main()
{
int a;
clrscr();
printf("\n%d",10);
printf("\n%d (Garbage Value)",a);
getch();
}
Output :
10
-28711 (Garbage Value)
-28711 (Garbage Value)
Example : 7 Variables.....
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
a=10;
printf("\n%d ",a);
printf("\nValue = %d",a);
printf("\nGiven Value = %d",a);
getch();
}
#include<conio.h>
void main()
{
int a;
clrscr();
a=10;
printf("\n%d ",a);
printf("\nValue = %d",a);
printf("\nGiven Value = %d",a);
getch();
}
Output :
10
Value = 10
Given Value=10
Value = 10
Given Value=10
Example : 8 Integer Variable with scanf
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
/* a=10;*/
scanf("%d",a);
printf("\n%d ",a);
printf("\nValue = %d",a);
getch();
}
#include<conio.h>
void main()
{
int a;
clrscr();
/* a=10;*/
scanf("%d",a);
printf("\n%d ",a);
printf("\nValue = %d",a);
getch();
}
Output :if You Type 20 from Keyboard
20
Press Enter...
20
Value = 20
Value = 20
Finally...You can see Output window..
20
20
Value = 20
20
Value = 20
Example : 9 Integer Variable with scanf
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("\nEnter Any Value = ");
scanf("%d",a);
printf("\nEntered Value = %d",a);
getch();
}
#include<conio.h>
void main()
{
int a;
clrscr();
printf("\nEnter Any Value = ");
scanf("%d",a);
printf("\nEntered Value = %d",a);
getch();
}
Output :
Enter Any Value =
if You Type 20 from Keyboard
Enter Any Value = 20
Press Enter...
Entered Value = 20
Finally...You can see Output window..
Enter Any Value =20
Entered Value = 20
Entered Value = 20
Example : 10 Working With scanf Function
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
scanf("%d %d",&a,&b);
printf("\n%d",a);
printf("\n%d",b);
getch();
}
Output : 1
if You type 10 from Keyboard
10
Press Enter > again please type 20 from Keyboard
10
20
Finally...You can see Output window..
10
20
10
20
20
10
20
Output : 2
press 10 from Keyboard > press spacebar (Don't Press Enter) > Press 20 from Spacebar
10 20
Press Enter >
10 20
Finally...You can see Output window..
10 20
10
20
10
20
Example : 11 Working With Scanf ..
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("\nEnter a Value = ");
scanf("%d",&a);
printf("\nEnter b Value = ");
scanf("%d",&b);
printf("\nA Value = %d",a);
printf("\nB Value = %d",b);
printf("\nA+B Value = %d",a+b);
printf("\nA+10 Value = %d",a+10);
getch();
}
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("\nEnter a Value = ");
scanf("%d",&a);
printf("\nEnter b Value = ");
scanf("%d",&b);
printf("\nA Value = %d",a);
printf("\nB Value = %d",b);
printf("\nA+B Value = %d",a+b);
printf("\nA+10 Value = %d",a+10);
getch();
}
Output :
Enter a Value =
if You Type 20 from Keyboard
Enter a Value = 10
Press Enter...
Enter a Value = 10
Enter b Value = 10
Press Enter...
Finally...You can see Output window..
Enter a Value = 10
Enter b Value = 20
A Value =10
B Value= 20
A+B Value = 30
A+10 Value = 20
Enter b Value = 20
A Value =10
B Value= 20
A+B Value = 30
A+10 Value = 20
Example : 12 Character Variable Declaration
#include<stdio.h>
#include<conio.h>
void main()
{
char ans;
clrscr();
printf("\n ans = %c",ans);
ans=' ';
printf("\n ans= %c",ans);
printf("\n ans= %d",ans); /*Returns ascii value of Space */
ans='Y';
printf("\n ans = %c",ans);
getch();
}
#include<conio.h>
void main()
{
char ans;
clrscr();
printf("\n ans = %c",ans);
ans=' ';
printf("\n ans= %c",ans);
printf("\n ans= %d",ans); /*Returns ascii value of Space */
ans='Y';
printf("\n ans = %c",ans);
getch();
}
Output :
ans = Å
ans=
ans= 32
ans = Y
ans=
ans= 32
ans = Y
Example : 13 String Variable With Scanf Function
#include<stdio.h>
#include<conio.h>
void main()
{
char ans;
char myname[15];
clrscr();
printf("\nEnter Any Character => ");
scanf("%c",&ans);
printf("\nEnter Any Name(String) => ");
scanf("%s",&myname);
printf("\nGiven Character => %c",ans);
printf("\nGiven Name => %s",myname);
getch();
}
#include<conio.h>
void main()
{
char ans;
char myname[15];
clrscr();
printf("\nEnter Any Character => ");
scanf("%c",&ans);
printf("\nEnter Any Name(String) => ");
scanf("%s",&myname);
printf("\nGiven Character => %c",ans);
printf("\nGiven Name => %s",myname);
getch();
}
Output :
Enter Any Character => M
Enter Any Name(String) => Manoj Kumar
Given Character => M
Given Name => Manoj
Enter Any Name(String) => Manoj Kumar
Given Character => M
Given Name => Manoj
Example : 14 String Variable With gets Function
#include<stdio.h>
#include<conio.h>
void main()
{
char ans;
char myname[15];
clrscr();
printf("\nEnter Any Name => ");
scanf("%s",&myname);
printf("\nGiven Name Using Scanf => %s",myname);
/*
Please add fflush(stdin) before gets function
If Omitted this line,You will get wrong output
If any doubt,remove the fflush(stdid); and run again
*/
printf("\nEnter Any Name => ");
fflush(stdin);
gets(myname);
printf("\nGiven Name Using gets => %s",myname);
getch();
}
#include<conio.h>
void main()
{
char ans;
char myname[15];
clrscr();
printf("\nEnter Any Name => ");
scanf("%s",&myname);
printf("\nGiven Name Using Scanf => %s",myname);
/*
Please add fflush(stdin) before gets function
If Omitted this line,You will get wrong output
If any doubt,remove the fflush(stdid); and run again
*/
printf("\nEnter Any Name => ");
fflush(stdin);
gets(myname);
printf("\nGiven Name Using gets => %s",myname);
getch();
}
Output :
Enter Any Name => Vijay Antony
Given Name Using Scanf => Vijay
Enter Any Name => Vijaya Raj
Given Name Using gets => Vijaya Raj
Given Name Using Scanf => Vijay
Enter Any Name => Vijaya Raj
Given Name Using gets => Vijaya Raj
Example : 15 Combine all variables (Emp Info)
#include<stdio.h>
#include<conio.h>
void main()
{
int empno;
char empname[20];
float salary;
clrscr();
printf("\nGive the Employee Details");
printf("\nEmployee ID = ");
scanf("%d",&empno);
printf("\nEmployee Name =");
fflush(stdin);
gets(empname);
printf("\nSalary = ");
scanf("%f",&salary);
printf("\nGiven Emp Details ");
printf("\nEmp ID=%d , Empname= %s,Salary = %.2f",empno,empname,salary);
getch();
}
#include<conio.h>
void main()
{
int empno;
char empname[20];
float salary;
clrscr();
printf("\nGive the Employee Details");
printf("\nEmployee ID = ");
scanf("%d",&empno);
printf("\nEmployee Name =");
fflush(stdin);
gets(empname);
printf("\nSalary = ");
scanf("%f",&salary);
printf("\nGiven Emp Details ");
printf("\nEmp ID=%d , Empname= %s,Salary = %.2f",empno,empname,salary);
getch();
}
Output :
Give the Employee Details
Employee ID = 101
Employee Name =Rakesh Sharma
Salary = 24000
Given Emp Details
Emp ID=101 , Empname= Rakesh Sharma,Salary = 24000.00
Employee ID = 101
Employee Name =Rakesh Sharma
Salary = 24000
Given Emp Details
Emp ID=101 , Empname= Rakesh Sharma,Salary = 24000.00
Example : 16 print employee information with gotoxy function (Read Carefully)
/*Author : C.Saravana Kumar */
#include<stdio.h>
#include<conio.h>
void main()
{
int empno;
char empname[20];
float salary;
clrscr();
/*Syntax of gotoxy function is > gotoxy(Xposition,Yposition) */
/*
-------------------------------------- (x) |
| .(10,1)
|
|
| .(10,40) .(60,40)
(y)
(10,1) Means > gotoxy(10,1);
*/
gotoxy(2,2);printf("Employee ID ");
gotoxy(25,2);scanf("%d",&empno);
gotoxy(2,4);printf("Employee Name ");
gotoxy(25,4);fflush(stdin);gets(empname);
gotoxy(2,6);printf("Salary ");
gotoxy(25,6);scanf("%f",&salary);
gotoxy(2,8);printf("\nGiven Emp Details ");
gotoxy(2,11);printf("Emp No");
gotoxy(9,11);printf("Emp Name");
gotoxy(32,11);printf("Salary");
gotoxy(2,13);printf("%d",empno);
gotoxy(9,13);printf("%s",empname);
gotoxy(32,13);printf("%.2f",salary);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int empno;
char empname[20];
float salary;
clrscr();
/*Syntax of gotoxy function is > gotoxy(Xposition,Yposition) */
/*
-------------------------------------- (x) |
| .(10,1)
|
|
| .(10,40) .(60,40)
(y)
(10,1) Means > gotoxy(10,1);
*/
gotoxy(2,2);printf("Employee ID ");
gotoxy(25,2);scanf("%d",&empno);
gotoxy(2,4);printf("Employee Name ");
gotoxy(25,4);fflush(stdin);gets(empname);
gotoxy(2,6);printf("Salary ");
gotoxy(25,6);scanf("%f",&salary);
gotoxy(2,8);printf("\nGiven Emp Details ");
gotoxy(2,11);printf("Emp No");
gotoxy(9,11);printf("Emp Name");
gotoxy(32,11);printf("Salary");
gotoxy(2,13);printf("%d",empno);
gotoxy(9,13);printf("%s",empname);
gotoxy(32,13);printf("%.2f",salary);
getch();
}
Output :
Employee ID 101
Employee Name Tamil Selvan
Salary 45000
Given Emp Details
Emp No Emp Name Salary
101 Tamil Selvan 45000.00
Employee Name Tamil Selvan
Salary 45000
Given Emp Details
Emp No Emp Name Salary
101 Tamil Selvan 45000.00
No comments:
Post a Comment