Category Archives: C Code

C codes.

String input in C using gets

The problem with scanf function is it doesnot recognises space in Name. Exmple if you entered string as “Coding street” it will store only coding in string variable so it is recommended by us to use gets over string input. Because it recognizes space and takes space also as input.

Example :


Input basic Data types (int, float, char)

Below program takes input of integer, float, string value from keyboard and prints it on screen.


C File Handling

File handling in c means we can create,open , delete files and modify contents in file.For this operations we will use some file handling functions. fopen, fclose, fscanf, fprintf ,fgets, fputs, feof etc.

File operations follow this procedure :

name of file

open or create file

read or write on file.

close file

File Handling Modes :

File Mode



Opens a file in read only mode.


Opens a file in write mode , it creates a new file with provided file name if it doesnot exist.If file exists then file will be overwritten.


Opens a file in append mode ( new info will be added at end of file), If file doesnot exist then it will create a new file.


Opens a file for read and write mode.


Opens a file for read and write mode.It creates a new file if it doesnot exist.


Opens a file for read and write mode. It create a new file if it doesnot exist.


Opens a binary file for reading.


Opens a binary file for writing.If file exist it will be over written.


Opens a binary file in append mode.Info is added at end of file.

r+b or rb+

Opens binary file for reading and writing

w+b or wb+

Opens a binary file for read and write mode.It creates a new file if it doesnot exist.

a+b or ab+

Opens a binary file for read and write mode. It create a new file if it doesnot exist.

File handling functions :

fopen : To open or create file

fclose : To close a open file, we need file pointer of opened file.

Where fp is  file pointer.

fscanf : To read from file according to format provided.

fprintf : To write into file according to format provided.

feof : To check end of file.

 fgets : Reads a line from file and stores in string.

fputs: Writes a string into file.

 fgetc: Reads a single character from file into char variable.

 fputc: Writes a single character to file.

 Example :

Program to copy content of one file into other file.


Source filename :src.txt

Destination filename: dst.txt

Explanation :

Above code copies content of one txt file into other txt file.

sfp  & dfp are file pointers.

src file is opened in read mode and dst file is is opened in write mode.

We will stop copying content when source file(src.txt) file pointer reaches to EOF (EOF is end of file which indicates that we have reached at end of file ).

we are using fgetc function to get char from src file. We are storing fetched content from source file into character variable c, then we are storing back the value of character variable c into the destination file using fputc function.

After copying the content we are closing the opened files.

C 1D,2D Array

Array is data structure in C . They are defined at compile time. Used to store same kind of information in sequential manner. Contiguous memory is allocated for array.

If we want to store marks of 100 students. Instead of declaring 100 float variables like (float marks1,marks2, …. ,marks100) we can declare one variable marks[100];

Caution: array starts from 0;

so information can be stored in marks[0]. marks[1], .. . . . , marks[99].

How to declare an array ?

data_type   identifier[ size_of_array];


int a[100];

float b[30];

char c[50];


How to initialize an array at the time of declaration ?

int a[5]={1,2,3,4,5};

value of a[0] is 1;

value of a[1] is 2;

similarly value of a[4] is 5;

a[5] is not accessible because we have declare array as int a[5] which consist of 5 variables from a[0] to a[4].


Accessing element in array :

float arr[6];

we can access 5 th element in array by arr[4] .


Types of Array :

  • 1D Array
  • 2D Array
  • Multidimensional array.

1-D Array :

Declaration :

int arr[10];

Element access :

eq. 2nd element


2-D Array :

Declaration :

int arr[5][5];

the above declaration will create 5 X 5 array with name arr.

Element access:

To access 2nd element in row and 3rd element in column we can access by a[1][2];


=>similarly we can apply rule in multidimensional array.


1-D Array

To declare an array of size 5 ,input the value from user printing the same.

2-D Array 

Declare a 2-D array of size 3X3 and store below matrix and print.

1 2 3

4 5 6

7 8 9


Click here to read more