C Example Programs: Input and Output Programs
C Example Programs: Input and Output Programs
This document contains numerous example programs to provide a practical aid for learning C programming
language.
#include <stdio.h>
main()
{
int i;
printf("%c\n",scanf("%i",i));
}
This program demonstrates the use of getchar and putchar.
#include <stdio.h>
main()
{
char ch;
do {
ch = getchar();
putchar(ch+1);
} while (ch != '.');
printf("\n\n");
printf("\n\n");
main()
{
char ch;
#include <stdio.h>
main()
{
int lower , upper , step;
float fahr , celsius;
lower = 0 ;
upper = 300;
step = 20 ;
fahr = lower;
#include <stdio.h>
#include <math.h>
main()
{
int i , j , k;
double a=10.0 , b=.0;
do {
printf("%f\n" , pow(a,b)); /* standard function */
b++;
} while (b<100);
i = 2;
main()
{
int a , b;
#include <stdio>
#include <conio>
main()
{
int a,b;
char ch;
printf("Do you want to: \n");
printf"Add, subtract, Multiply, or Divide?\n");
#include <stdio.h>
#include <conio.h>
main()
{
int a,b;
char ch;
switch (ch) {
case 'A' : printf("%d", a+b);
break;
case 'S' : printf("%d", a-b);
break;
case 'M' : printf("%d", a*b);
break;
case 'D' : if (b!=0) printf("%d", a/b);
break;
}
}
The statement sequence associated with a case may be empty allowing two or more cases to share a common
statement sequence.
#include <stdio.h>
#include <conio.h>
main()
{
char ch;
printf("Enter the letter: ");
ch=getchar();
switch(ch) {
case 'a' :
case 'e' :
case 'i' :
case 'o' :
case 'u' :
printf(" is a vowel \n");
break;
default:
printf(" is a consonant");
}
}
A simple program to show the use of the continue statement.
#include <stdio.h>
main()
{
int x ;
#include <stdio.h>
main()
{
int t ;
for ( ; ; ) {
scanf("%d" , &t) ;
if ( t==10 ) break ;
}
printf("End of an infinite loop...\n");
#include <stdio.h>
main()
{
int i , j;
#include <stdio.h>
main()
{
int a , b;
do {
if (a<b)
printf("First number is less than second\n\n");
if (b<a)
printf("Second number is less than first\n\n");
#include <stdio.h>
#include <conio.h>
main()
{
int answer, count, chances, right;
for (count=1; count<11; count++) {
printf("What is %d + %d?", count, count);
scanf("%d", &answer);
if (answer == count+count) printf("Right!\n");
else {
printf("Sorry, you're wrong \n");
printf("Try again\n");
right = 0;
/* nested for *?
for (chances=0; chances<3 && ! right; chances++){
printf("\nWhat is %d + %d?",count, count);
scanf("%d", &answer);
if (answer == count+count){
printf("Right!\n");
right=1;
}
}
/* if answer still wrong, tell user */
if (!right)
printf("the answer is %d.\n", count+count);
} /* end of else part */
} /* end of outter for loop */
}
#include <stdio.h>
main()
{
int number;
prompt() /* function */
{
printf("Input a number... ");
}
factor(int t) /* function */
{
int sum = 1;
int i ;
#include <stdio.h>
void main()
{
double vol;
vol = volume(12.2, 5.67. 9.03);
printf("Volume: %f", vol);
}
#include <stdio.h>
void main(void)
{
recurse(0);
}
void recurse(int i)
{
if (i<10) {
recurse(i+1);
printf("%d ",i);
}
}
This program prints
9876543210
on the screen.
Array programs
This section contains example programs demonstrating the use of arrays.
A simple program to demonstrate the definition and use of an array.
#include <stdio.h>
main()
{
int i[10],j;
#include <stdio.h>
#include <stdlib.h>
main()
{
int i , min_value , max_value ;
int list[10] ;
min_value = 32767 ;
max_value = 0 ;
}
This program uses a bubble sort.
#include <stdio.h>
include <stdlib.h>
main()
{
int item[100];
int a,b,t;
int count;
/* read in numbers */
printf("How many numbers ?");
scanf("%d", &count);
for (a=0;a<count;a++) scanf("%d", &item[a]);
#include <stdio.h>
main()
{
int twod[4][5];
int i,j;
00000
01234
02468
036912
Pointer programs
This section contains example programs demonstrating the use of pointers.
An example program using simple pointers.
#include <stdio.h>
main()
{
int balance;
int *address;
int value;
balance = 5000;
address = &balance;
value = *address;
#include <stdio.h>
main()
{
int *p , num;
p = #
*p = 100;
printf("%d\n" , num);
(*p)++;
printf("%d\n" , num);
(*p)--;
printf("%d\n" , num);
}
An example program demonstrating pointer Arithmetic.
#include <stdio.h>
main()
{
char *c , ch[10];
int *i , j[10];
float *f , g[10];
int x;
c = ch;
i = j;
f = g;
}
An example program using pointers and arrays.
#include <stdio.h>
main()
{
char str[80];
char token[10];
char *p , *q;
printf("Enter a sentence: ");
gets(str);
p = str;
while (*p) {
q = token;
while (*p != ' ' && *p) {
*q = *p;
q++ ; p++;
}
if (*p) p++;
*q = '\0';
printf("%s\n" , token);
}
}
String programs
This section contains example programs to demonstrate the use of strings.
A simple string example.
#include <stdio.h>
main()
{
char str[80] ;
printf("\n");
}
The follow program requests the input of two strings, then demonstrates the four string functions with them.
#include <string.h>
#include <stdio.h>
main()
{
char str1[80], str2[80];
int i;
Structure programs
This section contains example programs to demonstrate the use of structures.
This program stores data in a structure and displays the values.
#include <stdio.h>
void main(void)
{
struct s_type {
int i;
int j;
} s;
int i;
i=10;
s.i=100;
s.j=101;
#include <stdio.h>
struct s_type {
int i;
double d;
} var1;
void main(void)
{
var1=f();
printf("%d %1f", var1.i, var1.d);
}
struct s_type f(void)
{
struct s_type temp;
temp.i=100;
temp.d = 123.23;
return temp;
}
This program passes a structure to a function.
#include <stdio.h>
struct s_type {
int i;
double d;
} var1;
void main(void)
{
var1.i=99;
var1.d = 98.6;
f(var1);
}
#include <stdio.h>
#include <string.h>
strct s_type {
int i;
char str[80];
} s, *p;
void main(void)
{
p= &s;
#include <stdio.h>
#include <stdlib>
#include <ctype.h>
int count[26];
fclose(fp);
}
This program uses command line arguments to read and display the contents of a file supplied as an argument.
#include <stdio.h>
putchar(CLEARS);
#include <stdio.h>
#include <stdlib.h>
if (argc != 3) {
printf("Usage: add number number ...\n");
exit(1);
}
a = atof(argv[1]);
b = atof(argv[2]);
printf("%lf\n" , a + b);
}
/**********************************************************/
/* A simple Data Base Program */
/**********************************************************/
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
main()
{
int choice;
for(;;) {
choice = menu_select(); /* get user's selection*/
switch(choice) {
case 1: enter(); /* enter a new entry */
break;
case 2: del(); /* delete an entry */
break;
case 3: show_list(); /* display the list */
break;
case 4: search(); /* find an entry */
break;
case 5: save(); /* save to disk */
break;
case 6: load(); /* read from disk */
break;
case 7: exit(0);
}
}
}
/*********************************************************/
/* Function del */
/*********************************************************/
del()
{
int i;
char str[255];
/**********************************************************/
/* Function display */
/**********************************************************/
display(int i)
{
printf("%s\n" , list[i].name);
printf("%s\n" , list[i].street);
printf("%s\n" , list[i].town);
printf("%s\n" , list[i].county);
printf("%s\n" , list[i].code);
}
/**********************************************************/
/* Function enter */
/**********************************************************/
enter()
{
int i;
for(;;) {
i = find_free(); /* find a free structure */
if(i<0) {
printf("list full\n");
return;
}
inputs("enter name: ", list[i].name,30); if(!*list[i].name)
break; /* stop entering */
inputs("enter street: ", list[i].street, 40);
inputs("enter town: ", list[i].town, 20);
inputs("enter county: ", list[i].county, 20);
inputs("enter Postal code: ", list[i].code, 10);
}
}
/**********************************************************/
/* Function find */
/**********************************************************/
find(char *name)
{
int i;
for(i=0 ; i<MAX ; i++)
if(!strcmp(name ,list[i].name)) break;
if(i==MAX) return
else return i;
}
/**********************************************************/
/* Function find_free */
/**********************************************************/
find_free()
{
register int i;
for(i=0; i<MAX; i++)
if(!*list[i].name) return i;
return
}
/**********************************************************/
/* Function init_list */
/**********************************************************/
init_list()
{
register int i;
for (i=0 ; i<MAX ; i++)
*list[i].name = '\0'
}
/**********************************************************/
/* Function inputs */
/**********************************************************/
inputs( char *prompt , char *s , int count)
{
char str[255];
do {
printf(prompt);
gets(str);
if(strlen(str) 1; 1; ;>=count) printf("\ntoo long\n");
} while(strlen(str)>=count);
strcpy(s , str);
}
/**********************************************************/
/* Function load */
/**********************************************************/
load()
{
FILE *fp;
/**********************************************************/
/* Function menu_select */
/**********************************************************/
menu_select()
{
char s[80];
int c;
printf("1. Enter a name\n") ;
printf("2. Delete a name\n") ;
printf("3. List the File \n");
printf("4. Search\n") ;
printf("5. Save the file\n") ;
printf("6. Load the file\n") ;
printf("7. Quit\n") ;
do {
printf("\nEnter your choice: ");
gets(s);
c = atoi(s);
} while(c<0 || c>7);
return c;
}
/**********************************************************/
/* Function save */
/**********************************************************/
save()
{
FILE *fp;
/**********************************************************/
/* Function search */
/**********************************************************/
search()
{
int i;
char name[30];
/**********************************************************/
/* Function show_list */
/**********************************************************/
show_list()
{
int i;
printf("\n\n");
}
[program]
The second example program uses functions to play a simple game of tic tac toe.
#include <stdio.h>
#include <stdlib.h>
char matrix[3][3];
main()
{
char done;
init_matrix();
do {
disp_matrix();
get_player_move();
done = check();
if (done != ' ') break;
get_computer_move();
done = check();
} while (done == ' ');
disp_matrix();
}
/**********************************************************/
/* fuction to initialise matrix */
/**********************************************************/
init_matrix()
{
int i , j ;
/**********************************************************/
/* fuction to get players move */
/**********************************************************/
get_player_move()
{
int x , y ;
x--; y--;
/**********************************************************/
/* fuction to get computer move */
/**********************************************************/
get_computer_move()
{
int i , j ;
/**********************************************************/
/* fuction to display matrix */
/**********************************************************/
disp_matrix()
{
int t ;
printf(" 1 2 3\n");
/**********************************************************/
/* fuction to check matrix */
/**********************************************************/
check()
{
int i ;