Category Archives: C Tutorials

Here you will learn basics of C programming.

C struct

In C programming struct is a structured (record type) that creates a collection of different type of variable under a single name. If we want to store record of student. We can store name , rollno, phone no , address etc. using one variable, then struct makes it possible.

Syntax of struct in c :

struct  tag_name {

type attribute;

type attribute;

/ * ……. */


For example to store info of student :

Declaration of stu_info :

Accessing attributes:

Accessing attribute rollno;

rollno can be accessed by s1.rollno;

similiary other varibles also can be accessed.

Pointer to the struct:



Array of structure :

We have seen array of int , float , char .Similarly array of struct can also be defined. This is used to store students info , customer info etc..




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 Chars & ascii

Characters are used to store literal values, like names of person , names of items etc..  Array of character is known as strings. Strings are terminated by null (‘’) character.

Single char variable takes 1 byte. so if we declare char array of 10 size it will take 10 bytes.

Declaration :

Above declaration will consume 1 byte of memory.

above declaration will consume 10 byte of memory

Initialization at the time of declaration:

Character arithmetic :

In c every characters has its ascii values.The below chart shows ascii value and character. There are 128 ascii values in C.

Functions supported by chars and strings :

Some function are listed below used to manipulate string and chars.

Function Name Description
getc() Used to input char from stdin
putc() Used to print char on  stdout (screen)
Gets() Used to input string from stdin
Puts() Used to print string on  stdout (screen)
Strcpy() Used to copy one string into other string variable
Strcat() Used to join two strings
Strchr() To find position of char in string
Strlen() To find length of string

C Pointers

C supports pointers , a type of reference that stores memory address of any identifier or function. Pointers can be dereferenced to access data at which pointer points to. size of pointer depends on architecture of machine. 32 bit machine has pointer size as 4 byte , similiary 64 bit machine has pointer size as 8 byte. Pointers are used for many purpose in c. To allocate Dynamic memory pointers are used. To implement tree data structures pointers are used. There are many applications of pointers in C.

A null pointer value does not refers to any address location. Dereferencing a null pointer is not defined, It will harm your program and often results to segmentation fault.

A void pointer points to a identifier whose type is not specified. Thus the size and type of the void type variable is not known so void pointers cannot be dereferenced. Simply they can be converted to other type of identifier or object.

Declaration of pointer :

data_type  *pointer_var_name ;

e.g. :

To define integer pointer :

Initialization of pointer :

Pointer to array :


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

C Variable Scope

Scope of variable means the existence of variable within block or region in which variable can be accessed.  Variable accessing scopes are local and global scopes.

Local Variable: A variable declared between a block or function is known as local variable.Local variables are accessed through local scope. They can be accessed through the function and block in which they are declared,other functions cannot access these variables.

Global Variable: A variable declared outside of all function is known as global variable.Global variables are accessed through all functions.


Examples :

Local Variable :

Here a,b,c are local variables to the main function

Global Variable:

a is local to foo.

b is local to main.

g is a global variable.

C functions

Functions are easy to use. They allow complicated programs to be break  into small blocks, each of which is easier to write, read, and maintain. We have already used the function main and scanf, printf . where main is used-defined function and scanf and printf are library functions. main() is a function from there program execution starts.

The functions defined by user itself is known as user defined function.Function used from external files or library known as library functions. printf , scanf , getchar(), gets(), puts() are library functions.

 Function has following layout.

1. Function returning value

return-type   function-name( argument-list-if-you-want){



return return-value;


2. Function no return-value

void  function-name( arguments-list-if-you-want){




Return type :

Return type refers to the type of value which is returned by function.

eq. if function return integer value then return type should be int.

Example :

A function to add 2 integer values.

above code will print 8 as output.



C loops

If you have some situation where you have to executed some set of statements multiple time. There you need loop. In looping set of statements are executed untill the loop termination condition is satisfied.

A loop consist of two segments. One for body and other for condition.

if condition is true then body part of loop will be executed otherwise loop will be terminated.

C programming provides following loop statements:

1. for

2. while

3. do while

Loop Name




For( exp1;exp2 or condition;exp3){ 


……;  }

exp1-> Variable initialization

exp2-> Conditon checking

exp3-> increment or decrement



Statements; }

relational operators are used to check conditions

do .. while

do { 



do while loop executed atleast one time befor condition check.

Examples :

For loop : Printing 1 to 10 numbers

While loop: Adding 1 to 10 numbers

Do while loop: printing even nos between 1 to 10


C input output

When we say for input in programming language it is about getting data from user (command line ) or from file. C programming provides various function for inputting the data. We will talk about mostly used library <stdio.h> and the functions scanf printf.

Before we go for input and output we will learn about format specifier.

The below table shows the format specifier used by scanf and printf. The “Format specifier” is the sequence passed as the formatting string argument.

Format specifier Characters matched Argument type
%c any single character char
%d, %i integer integer type
%u integer unsigned
%o octal integer unsigned
%x, %X hex integer unsigned
%e, %E, %f, %g, %G floating point number floating type
%p address format void *
%s any sequence of non-whitespace characters,strings char

scanf function:

The int scanf(const char * format, …. ) function reads input from standard input stream stdin (keyboard) and stores the input according to format provided.

Example :

Q. Write a format specifier in scanf function to read two inputs from keyboard.

Ans :

&a , &b refers to the respective addresses.


printf Function:

The int scanf(const char * format, …. ) function writes output to the standard output stream stdout (screen) and prints output to the screen according to format provided.


Q. Write a printf format specifier to print two variables int and double values respectively.


a=10 b=11.10