C Examples

Table of Contents

If statement

If statement example to validate voter’s age (one way branching)

# include <stdio.h>
# include <conio.h>
// www.raviroza.com
// 3-Oct-2022, 8.00 pm

void main()
{
	int age=15;
	clrscr();
	printf ("enter your age : ");
	scanf  ("%d",&age);
//	printf ("start\n");
	if (age>=18)
	//if (age<0)
	{
		printf ("you can vote\n");
		printf ("please vote !");
	}
//	printf ("end");

	getch();
}

If else statement to validate voter’s age (two way branching)

# include <stdio.h>
# include <conio.h>
// www.raviroza.com
// 3-Oct-2022, 8.00 pm
# include <stdio.h>
void main()
{
	int age;
	clrscr();
	printf ("enter your age : ");
	scanf  ("%d",&age);
	if (age>=18)
	{
		printf ("you can vote\n");
		printf ("please vote !");
	}
	else
	{
		printf ("\nsorry ! you can not vote\n");
		printf ("\n\nplease do wait for %d year(s)",18-age);
	}

	getch();
}

Calculate bill amount with discount, when bill amount is greater than 2000

# include <stdio.h>
# include <conio.h>
// www.raviroza.com
// 3-Oct-2022, 8.00 pm

# include <stdio.h>
void main()
{
	float bamt,disc=0,fbamt;;
	clrscr();
	printf ("Enter bill amount : ");
	scanf  ("%f",&bamt);
	if (bamt>=2000)
	{
		disc = (bamt * 5)/100;
	}
	fbamt = bamt - disc;
	printf ("\nBill amount  : %f",bamt);
	printf ("\nDiscount amount    : %f",disc);
	printf ("\nFinal bill amount   : %f",fbamt);
	getch();
}

if else if statement to check vowels from given character

# include <stdio.h>
# include <conio.h>
// www.raviroza.com
// 3-Oct-2022, 8.00 pm

# include <stdio.h>
void main()
{
	char c;
	clrscr();
	printf ("enter a character : ");
	scanf  ("%c",&c);

	if (c == 'a')
	{
		printf ("a is vowel");
	}
	else if (c == 'e')
	{
		printf ("e is vowel");
	}
	else if (c == 'i')
	{
		printf ("i is vowel");
	}
	else if (c == 'o')
	{
		printf ("o is vowel");
	}
	else if (c == 'u')
	{
		printf ("u is vowel");
	}
	else
	{
		printf ("char is consonent");
	}
	getch();
}

Switch statement

Program to print day name when user enters a day number from week

# include <stdio.h>
# include <conio.h>
// www.raviroza.com
// 3-Oct-2021, 8.00 pm

# include <stdio.h>
void main()
{
	int day;
	clrscr();
	printf ("enter day number : ");
	scanf  ("%d",&day);

	switch (day)
	{
		case 1:
			printf ("today is monday");
			break;
		case 2:
			printf ("today is tuesday");
			break;
		case 3:
			printf ("today is wednesday");
			break;
		case 4:
			printf ("today is thursday");
			break;
		case 5:
			printf ("today is friday");
			break;
		case 6:
			printf ("today is saturday");
			break;
		case 7:
			printf ("today is sunday");
			break;
		default:
			printf ("invalid day number");
			break;
	}

	getch();
}

Program to check vowel from a given character

# include <stdio.h>
# include <conio.h>
// www.raviroza.com
// 3-Oct-2021, 8.00 pm

void main()
{
	char c;
	clrscr();
	printf ("enter a character : ");
	//scanf  ("%c",&c);
	c = getchar();

	switch (c)
	{
		case 'a':
		case 'e':
		case 'i':
		case 'o':
		case 'u':
		case 'A':
		case 'E':
		case 'I':
		case 'O':
		case 'U':
			printf ("char is vowel");
			break;
		default:
			printf ("char is consonant");
			break;
	}

	getch();
}

Program to perform arithmetic operation on two number, operation is given as an input, switch case use to identify operation.

# include <stdio.h>
# include <conio.h>
// www.raviroza.com
// 3-Oct-2021, 8.00 pm

void main()
{
	int a,b;
	char c;
	clrscr();
	printf ("enter number 1 : ");
	scanf  ("%d",&a);
	printf ("enter number 2 : ");
	scanf  ("%d",&b);
	printf ("enter a operation (+ - * /) : ");
	flushall();
	c = getchar();

	switch (c)
	{
		case '+':
			printf ("addition = %d",a+b);
			break;
		case '-':
			printf ("subtraction = %d",a-b);
			break;
		case '*':
			printf ("multiplication = %d",a*b);
			break;
		case '/':
			printf ("division = %d",a/b);
			break;
		default:
			printf ("invalid operation");
			break;
	}

	getch();
}

Array

Initialize an Array in C

void main()
{
	
	// Size of the array is taken as the number of elements
	// in the initializer list (5)
	int arr[] = {1, 2, 3, 4, 5};

	// here, size of a is 3
	int a[] = {1, 2, 3};

	// char array
	char n[] = {'r','a','\0'};


	// initialize the two dimensional array.
	int arr[3][3] = {1,2,3,  4,5,6,  7,8,9};
}

One dimensional array example

# include <stdio.h>
# include <conio.h>
// www.raviroza.com
// 25-Nov-2021, 12.00 pm
void main()
{
	int marks[10];
	int i,n;
        clrscr();

	printf ("Enter Size : ");
	scanf  ("%d",&n);

	for (i=0; i<n; i++)
	{
		printf ("enter [%d] element : ",i+1);
		scanf  ("%d",&marks[i]);
	}
	printf ("\nStored Values in Array \n");
	for (i=0; i<n; i++)
	{
		printf ("[%d] ",marks[i]);
	}
	getch();
}
Output :
Enter Size : 4
enter [1] element : 23
enter [2] element : 90
enter [3] element : 87
enter [4] element : 56

Stored Values in Array
[23] [90] [87] [56]

Example to calculate sum and average of array elements. To find minimum and maximum value from array element with index number.

# include <stdio.h>
# include <conio.h>
// www.raviroza.com
// 3-Oct-2021, 8.00 pm

void main()
{
	int mark[5],i,n;
	int s=0, avg=0;
	int min,max;
	int minp,maxp;
	clrscr();

	printf ("\n\nenter array size : : ");
	scanf  ("%d",&n);

	for(i=0; i<n; i++)
	{
		printf ("enter %d element : ",i+1);
		scanf  ("%d",&mark[i]);
	}
	min=mark[0];
	max=mark[0];
	minp=0;
	maxp=0;
	for(i=0; i<n; i++)
	{
		if(mark[i]>max)
		{
			max=mark[i];
			maxp=i;
		}
		if(mark[i]<min)
		{
			min=mark[i];
			minp=i;
		}
		s=s+mark[i];
		printf  ("[%d] -> %d\n",i+1, mark[i]);
	}
	avg=s/n;
	printf ("\n\n Sum = %d",s);
	printf ("\n\n Avg = %d",avg);
	printf ("\n\n [%d] -> Min = %d",minp+1, min);
	printf ("\n\n [%d] -> Max = %d",maxp+1, max);

	getch();
}

Example of two dimensional array

# include <stdio.h>
# include <conio.h>
// www.raviroza.com
// 25-Nov-2021, 12.00 pm
void main()
{
	int marks[10][10];
	int r,c,i,j,n;

	printf ("Enter Row Size : ");
	scanf  ("%d",&r);

	printf ("Enter Column Size : ");
	scanf  ("%d",&c);

	for (i=0; i<r; i++)
	{
	    for (j=0; j<c; j++)
        {
            printf ("enter [%d][%d] element : ",i+1,j+1);
            scanf  ("%d",&marks[i][j]);
        }
	}
	printf ("\nStored Values in Mark Array \n");

	for (i=0; i<r; i++)
	{
	    for (j=0; j<c; j++)
        {
            printf ("[%d]  ",marks[i][j]);
        }
        printf ("\n");
	}
	getch();
}

Output:

Enter Row Size : 2
Enter Column Size : 4
enter [1][1] element : 45
enter [1][2] element : 67
enter [1][3] element : 89
enter [1][4] element : 43
enter [2][1] element : 66
enter [2][2] element : 99
enter [2][3] element : 33
enter [2][4] element : 12

Stored Values in Mark Array
[45]  [67]  [89]  [43]
[66]  [99]  [33]  [12]

Example of multi dimensional array

# include <stdio.h>
# include <conio.h>
// www.raviroza.com
// 29-Nov-2021, 7.00 pm
void main()
{
        // declare 2-array of 2-rows and 2-columns total 2x2x2=8 elements
	int a[2]   [2][2];

	a[0] [0] [0] = 1;
	a[0] [0] [1] = 2;
	a[0] [1] [0] = 3;
	a[0] [1] [1] = 4;

	a[1] [0] [0] = 5;
	a[1] [0] [1] = 6;
	a[1] [1] [0] = 7;
	a[1] [1] [1] = 8;


	printf ("0th array from multi-dimensional array \n");
	printf ("%d ", a[0] [0] [0]);
	printf ("%d ", a[0] [0] [1]);
	printf ("%d ", a[0] [1] [0]);
	printf ("%d ", a[0] [1] [1]);

	printf ("\n\n1st array multi-dimensional array \n");
	printf ("%d ", a[1] [0] [0]);
	printf ("%d ", a[1] [0] [1]);
	printf ("%d ", a[1] [1] [0]);
	printf ("%d ", a[1] [1] [1]);

	getch();
}

Output:

0th array from multi-dimensional array
1 2 3 4

1st array multi-dimensional array
5 6 7 8

Char array example

void main()
{
	int i,n;

	// char array with 20 chars (size)
	char name[20];
	// or
	// char *name;	   // dynamic size, its size will be determine by the number of chars set in array
	clrscr();

	printf ("\n\nenter your name : ");
	scanf  ("%s",name);

	printf ("%s\n\n",name);

	n=strlen(name);
	printf ("\nchar array in ascending order \n");
	for(i=0; i<n; i++)
	{
		printf  ("%c",name[i]);
	}

	printf ("\nchar array in reverse order\n");

	for(i=n-1; i>=0; i--)
	{
		printf  ("%c",name[i]);
	}
	getch();
}

Example of two dimension array of string

# include <stdio.h>
# include <conio.h>
// www.raviroza.com
// 03-Dec-2021, Friday
void main()
{
     char cities[10][20];
     int i,n;

     printf("Enter the no. of cities (max.< 10): ");
     scanf("%d",&n);

     for(i=0; i<n; i++)
     {
         printf("Enter %d city name :",i+1);
         scanf("%s",cities[i]);
     }

     printf("\nEntered city names are:\n");
     for(i=0; i<n; i++)
     {
        //puts(cities[i]);
        printf ("%s (%d)",cities[i],strlen(cities[i]));
     }

     getch();
}

Example to find and replace a character in two dimensional string

# include <stdio.h>
# include <conio.h>
# include <string.h>
// www.raviroza.com
// 03-Dec-2021, Friday

void main()
{
     char cities[10][20];
     char find,repl;
     int i,n,k, len, cnt=0;

     printf("Enter the no. of cities (max.< 10): ");
     scanf("%d",&n);

     for(i=0; i<n; i++)
     {
         printf("Enter %d city name :",i+1);
         scanf("%s",cities[i]);
     }

     flushall();
     printf ("Enter char to find : ");
     scanf  ("%c",&find);

     flushall();
     printf ("Enter char to replace with : ");
     scanf  ("%c",&repl);

     printf("\nEntered city names are:\n");
     for(i=0; i<n; i++)
     {
        len = strlen(cities[i]);
        for (k=0; k<len; k++)
        {
            if(cities[i][k] == find)
            {
            // replace the found character
            cities[i][k] = repl;
            // to count the no. of replacement
            cnt++;
            }
            printf ("%c",cities[i][k]);
        }
        printf ("\n");
    }

    printf ("'%c' found & replaced %d times ",find,cnt);
    getch();

}

Output:

Enter the no. of cities (max.< 10): 3

Enter 1 city name : Jamnagar
Enter 2 city name : Rajkut
Enter 3 city name : Baroda

Enter char to find : r
Enter char to replace with : @

Entered city names are :
Jamnaga@
Rajkot
Ba@oda

‘r’ found & replaced '2' times

User Defined Functions

FUNCTION WITH NO ARGUMENTS AND NO RETURN VALUE

When a function has no arguments and it does not return a value to the caller, when there is no data transfer between calling function and called function.

// function declaration
void f1();


void f1()
{
    printf ("function f1 is called");
}

void main()
{
   // here main is the calling function

   f1();

   // f1() is the called function
}

FUNCTION WITH ARGUMENTS AND NO RETURN VALUE

When a function has argument(s), but does not return a value to the caller, data is passed to the called function and no data is returned to the calling function

// function declaration
void f1(int);


void f1(int i)
{
    printf ("function f1 is called with data/argument(s) -> %d",i);
}

void main()
{
   // here main is calling function
   // function f1() is called with a data (arguments)

   f1(10);

   // f1() is the called function
}

FUNCTION WITH NO ARGUMENTS AND RETURN VALUE

When a function has no argument, but it does return a value to the caller, no data is passed to the called function but data is returned to the calling function.

// function declaration
int f1();


void f1()
{
    int a = 10;
    int b = 10;

    int c = a + b;

    return c;
}

void main()
{
   // here main is calling function
   // function f1() is called with no data (arguments)
   int t = f1();
   printf ("returned value = %d",t);  
   // or
   printf ("returned value = %d",f1());
}

FUNCTION WITH ARGUMENTS AND RETURN VALUE

When a function has argument(s), and it also does return a value to the caller, data is passed to the called function and data is also returned to the calling function.

// function declaration
int f1(int , int );


void f1(int n1, int n2)
{    
    int c = n1 + n2;

    return c;
}

void main()
{
   // here main is calling function
   // function f1() is called with no data (arguments)
   int t = f1(100,200);
   printf ("returned value = %d",t);  
   // or
   printf ("returned value = %d",f1(100,200));
}

UDF to print Static/Fix line, line with N columns and line with N columns and C character/symbol.

# include <stdio.h>
# include <conio.h>

// www.raviroza.com
// 7-Dec-2021, 11.00 pm

// function prototype
//-------------------
void line1();
void line2(int);
void line3(int,char);
//-------------------


// function definitions

// function to print line with fix columns
void line1()
{
	printf ("\n--------------------\n");
}

// function to print line  with n number of columns
void line2(int n)
{
	int i;
	printf ("\n");
	for(i=1; i<=n; i++)
	{
		printf ("-");
	}
	printf ("\n");
}

// function to print line  with n number of columns & given symbol/char
void line3(int n, char c)
{
	int i;
	printf ("\n");
	for(i=1; i<=n; i++)
	{
		printf ("%c",c);
	}
	printf ("\n");
}

int main()
{
	// to print line with fix columns
	line1();
	// to print line with n columns
	line2(5);
	// to print line with n columns & given symbol/char
	line3 (10,'@');
	getch();
}

CALL BY VALUE

# include <stdio.h>
# include <conio.h>

// www.raviroza.com
// 8-Dec-2021, 10.30 am

void byval(int);

void byval(int i)
{
	i=i+1;
}

void main()
{
	int temp=10;
	byval(temp);        
	printf ("temp = %d",temp);	
	getch();
}

CALL BY REFERENCE

# include <stdio.h>
# include <conio.h>

// www.raviroza.com
// 8-Dec-2021, 10.30 am

void byref(int*);

void byref(int *i)
{
	*i = *i + 1;
}

void main()
{
	int temp=10;
	
	byref(&temp);
	printf ("\n\ntemp = %d",temp);
	getch();
}

ARRAY (int, char) AS PARAMETER TO FUNCTION

# include <stdio.h>
# include <conio.h>

// www.raviroza.com
// 28-Dec-2021, 7.57 am

void fun(int array[], int size)
{
	int i;
	for(i=0; i<size; i++)
		printf ("%d\n",array[i]);
}
void fun1(char array[])
{
	int i;
	for(i=0; i<strlen(array); i++)
		printf ("%c",array[i]);
}
void main()
{
	int arr[]  = {1,2,3,4,5,6,7,8,9,10};
	char *c = "ravi r oza";
	clrscr();

	fun(arr,10);
	fun1(c);
	getch();
}

Structure

STRUCTURE EXAMPLE

# include <stdio.h>
# include <conio.h>

// author : www.raviroza.com
// date : 8-Dec-2021, 8.00 am

struct Student
{
	int no;
	char name[20];
	float perc;
};

void main()
{
	struct Student s1;
	
	fflush(stdin);
	printf ("enter Student number : ");
	scanf  ("%d",&s1.no);
	
	fflush(stdin);
	printf ("enter Student Name : ");
	scanf  ("%s",s1.name);
	
	fflush(stdin);
	printf ("enter Student percentage : ");
	scanf  ("%f",&s1.perc);
	
	printf ("\nStudent Summary\n\n");
	printf ("No.        : %d\n",s1.no);
	printf ("Name       : %s\n",s1.name);
	printf ("Percentage : %f\n",s1.perc);
		
	getch();	

}

ARRAY OF STRUCTURE EXAMPLE

# include <stdio.h>
# include <conio.h>


// author : www.raviroza.com
// date : 8-Dec-2021, 9.45 am

struct Student
{
	int no;
	char name[20];
	float perc;
};

void main()
{
	struct Student stu[5];
	int i, n;
	
	printf ("enter no. of student : ");
	scanf  ("%d",&n);
	
	for (i=0; i<n; i++)
	{
		fflush(stdin);
		printf ("enter Student number : ");
		scanf  ("%d",&stu[i].no);
	
	
		fflush(stdin);
		printf ("enter Student Name : ");
		scanf  ("%s",stu[i].name);
	
		fflush(stdin);
		printf ("enter Student percentage : ");
		scanf  ("%f",&stu[i].perc);
	}
	
	printf ("\nStudent Summary\n");
	printf ("\n----------------------------\n");
	for (i=0; i<n; i++)
	{			
		printf ("No.        : %d\n",stu[i].no);
		printf ("Name       : %s\n",stu[i].name);
		printf ("Percentage : %f\n",stu[i].perc);
		printf ("\n----------------------------\n");
	}	
	getch();	
}

POINTER TO STRUCTURE EXAMPLE


# include <stdio.h>
# include <conio.h>

// author : www.raviroza.com
// date : 20-Dec-2021, 8.00 am

struct Student
{
	int no;
	char name[20];

};

void main()
{
    struct Student *sptr, stu1;
    sptr = &stu1;

    printf("Enter Student No. : ");
    scanf("%d", &sptr->no);

    printf("Enter Student Name : ");
    scanf("%s", &sptr->name);

    printf("Student Details:\n");
    printf("No.    : %d\n", sptr->no);
    printf("Name   : %s",   sptr->name);

    getch();
}

STRUCTURE WITHIN STRUCTURE


# include <stdio.h>
# include <conio.h>

// author : www.raviroza.com
// date : 20-Dec-2021, 8.30 am

struct student
{
    int no;
    char *name;
    struct marks
    {
        int m1,m2,m3;
    }mark;
    
};

// typedef is used to create the alias of data types

typedef struct student stu;

void main()
{
    //struct student s;
    stu s;
    s.no=101;
    s.name = "ravi r oza";
    s.mark.m1=75;
    printf ("%d %s %d",s.no,s.name,s.mark.m1);
    getch();
}

Pointers

Pointer for int, float and char.

# include <stdio.h>
# include <conio.h>

// www.raviroza.com
// 11-Dec-2021, 8.00 am

void main()
{
    printf("\n Pointer Demo \n");

    int i = 10;
    int *iptr;
    
    float f = 10.34;
    float *fptr;
    
    char c = 'r';
    char *cptr;
    
    iptr = &i;
    fptr = &f;
    cptr = &c;
    
    printf ("int   : %d is at %p\n",i,iptr);
    printf ("float : %f is at %p\n",f,fptr);
    printf ("char  : %c is at %p\n",c,cptr);
    
    getch();
}

Address of Array elements

# include <stdio.h>
# include <conio.h>

// www.raviroza.com
// 11-Dec-2021, 9.00 am

int main() 
{
   int arr[5];
   int i;

   printf ("\n\n POINTERS/ADDRESS OF ARRAY ELEMENTS \n");

   for(i = 0; i < 5; ++i)
   {
      printf("&arr[%d] = %p\n", i, &arr[i]);
   }

   printf("Base Address of Array is : %p", arr);

   getch();
}

Pointer to an Array

# include <stdio.h>
# include <conio.h>

// www.raviroza.com
// 11-Dec-2021, 9.00 am

int main() 
{
     int arr[5] = {1, 2, 3, 4, 5};
     int* ptr;

     // ptr is assigned the address of the 1st element
     ptr = &arr[0]; 

     printf("*ptr     : %d\n", *ptr);      // 1
     printf("*(ptr+1) : %d\n", *(ptr+1));  // 2
     printf("*(ptr+2) : %d",   *(ptr+2));  // 3
     getch();
}

Pointer to Structure

# include <stdio.h>
# include <conio.h>

// www.raviroza.com
// 7-Oct-2022, 8.25 am

struct student
{
   int age;
   float weight;
};

void main()
{
    struct student *studentPtr, student1;
    studentPtr = &student1;   
    clrscr();

    printf("Enter age : ");
    scanf("%d", &studentPtr->age);

    printf("Enter weight : ");
    scanf("%f", &studentPtr->weight);

    printf("Displaying Student Details:\n");
    printf("Age: %d\n", studentPtr->age);
    printf("weight: %f", studentPtr->weight);

    getch();
}

Pointer to Pointer

# include <stdio.h>
# include <conio.h>

// www.raviroza.com
// 11-Oct-2022, 4.8 pm

void main() 
{
    // integer memory variable
    int x=10;

    // pointer, referencing to integer x 
    int *p;
    
    // *p, pointer to pointer, referencing to pointer p
    int **ptp;

    p = &x;
    ptp = &p;
    
    // changing the value of x using pointer to pointer variable
    **ptp = **ptp + 1;

    printf ("x = %d ",**ptp);

    getch();
}

Pointer as an argument to a function (function call by reference)

# include <stdio.h>
# include <conio.h>

// www.raviroza.com
// 8-Dec-2021, 10.30 am

void byref(int*);

void byref(int *i)
{
	*i = *i + 1;
}

void main()
{
	int temp=10;
	
	byref(&temp);
	printf ("\n\ntemp = %d",temp);
	getch();
}

Storage classes

Automatic example

void main()
{
     int n;
}
// the above code can also be written as
void main()
{
     auto int n;
     // it has the same meaning as above example
}

External example

extern int i = 99;
//above is accessed from multiple files.
or
int i = 99;
// above is restricted to current program.
void fun1()
{
      i = 1;
}
void fun2()
{
     i = i + 1;
}
void main()
{
    printf ("%d",i);
    f1();
    printf ("%d",i);
    f2();
    printf ("%d",i);
}

Static example

void fun1()
{ 
   static int i=1;
}
void main()
{
   fun1();
   fun1();
}

Register example

void main()
{    
    register int i;
    for (i=1; i<=5; i++)
    {
         printf ("it is printed faster using register variables\n");
    } 
}

File Handling

Write data to file (Single Character )

# include <stdio.h>
# include <conio.h>

// www.raviroza.com
// 07-Jan-2022, 6.07 pm

void main()
{    
    char c;
    FILE *fp;
    
    printf ("Enter char to write in file : ");
    scanf  ("%c",&c);

    fp = fopen("f1.txt","w");    
    putc(c,fp);
    fclose(fp);

    printf ("a character is written in file");
    getch();    
}

Read data from File (single character)

# include <stdio.h>
# include <conio.h>

// www.raviroza.com
// 09-Jan-2022, 4.05 pm

// read single (probably first) character from file
void main()
{    
    char c;
    FILE *fp;
    
    fp = fopen("f1.txt","r");    
    c = getc(fp);
    printf ("%c",c);
    getch();
    fclose(fp);
}

Read complete data using getc() from File

# include <stdio.h>
# include <conio.h>

// www.raviroza.com
// 09-Jan-2022, 4.15 pm

// read full file, using while loop and EOF (end of file)
void main()
{    
    char c;
    FILE *fp;
    
    fp = fopen("f1.txt","r");    
    while((c = getc(fp)) != EOF)
    {
        printf ("%c",c);
    }
    fclose(fp);
    getch();
}

Read and Write int data

# include <stdio.h>
# include <conio.h>

// www.raviroza.com
// 09-Jan-2022, 4.45 pm

void main()
{    
    int age;
    FILE *fp;
    
    fp = fopen("fint.txt","w");    

    printf ("WRITE integer data from file\n");
    
    printf ("Enter age : ");
    scanf  ("%d",&age);
    putw(age,fp);
    
    printf ("Enter age1 : ");
    scanf  ("%d",&age);    
    putw(age,fp);
    
    fclose(fp);

    printf ("READ integer data from file\n");
    fp = fopen("fint.txt","r");    
    
    while((age = getw(fp)) != EOF)
    {
        printf ("%d",age);
    }
    fclose(fp);
    
   getch();    
}

fprintf() and fscanf() example

# include <stdio.h>
# include <conio.h>

// www.raviroza.com
// 10-Jan-2022, 4.54 pm

void main()
{    
    int age,c;
    char name[10];
    FILE *fp;
    
    fp = fopen("fmixdata.txt","w");    

    printf ("WRITE Mix-Data to File\n");
    
    printf ("Enter Name : ");     scanf  ("%s",name);
    printf ("Enter age  : ");     scanf  ("%d",&age);
    
    fprintf(fp,"%10s%3d",name,age);        
    
    fclose(fp);

    printf ("READ Mix-Data from file\n");    
    fp = fopen("fmixdata.txt","r");    
        
    while((fscanf(fp,"%10s%3d",name,&age)) != EOF)    
    {
        printf ("Name -> %s\n",name);
        printf ("Age  -> %d\n",age);
    }
    fclose(fp); 
    getch();      
}

Misc.

Program to find ASCII value of given character

# include <stdio.h>
# include <conio.h>

// www.raviroza.com
// 3-Oct-2022, 8.00 pm

void main()
{
	char a;
	//int b=98;
	clrscr();
	printf ("enter a character : ");
	scanf  ("%c",&a);
	
	printf ("ASCII value of %c is -> %d ",a);

	getch();
}