Reading:  

Getting started with C Language


Datatypes in C

Data types in C

Data types are used to define the type of data used in the program. The amount of storage allocated to variables define the data types. There are a rich set of data types in C through which we can accept the values and can perform the operations on those values stored in variables. There are many fundamental data types in c for the fundamental  data storage.  The ANSCII C support three types of data types, they are

  • Primary or fundamental data types
  • User defined data types
  • Derived data types

Primary or fundamental data types

    Primary or fundamental data types are the basic data types where further division is impossible. The data types can be declared as  ASCII C support five fundamental data types

  • character
  • Integer
  • Float
  • Double
  • Void

Character

A single character can be defined as a character data type .   It is stored in 1 byte(8 bit). A character can be signed or unsigned.

Declaration of a character:

char a;

a=’b’;

where ‘a’ is a variable of data type character.

 

Integer 

With integer data type we can store whole numbers within a range of values. The size of an integer is 2 bytes(16 bits). C support 3 types of integers  int , short int , long int. all these three types have signed and unsigned classification.

Declaration of an integer:

int a;

a=100;

Using the variable ‘a’ we can store the whole numbers within the range.

 

Float

The floating point numbers are stored using the data type float, which uses 4 bytes to store it in the memory.

Declaration of a float:

float a;

a=3.14

 

Double

Double is the same as a float data type with much more accuracy because of giving a precision of 14 digits. The size is 8 bytes. Double have another division long double with size 10 bytes.

double a;

a=12.123456

 

Void 

As the name indicates it does not return any value. 

The table illustrates the size , keyword equivalent and range  of each primary data types

 

         Data type

Keyword

Size-Byte

         Range

         Character

Char

     1

-128 to 127

Integer

Int

     2

-32768 to 32767

         Float

folat

     4

3.4E - 38 to 3.4E + 38

         Double

double

     8

1.7E - 308 to 1.7E + 308



User defined data types 

User defined data types are the data types which is defined by the programmer with the help of features of C. the user defined data types in C are

  • Type definition
  • Enumeration
  • Storage classes

 

Type definition 

It is a feature of C which allows the programmer to define an identifier which represents an existing data type. It cannot create a new data type.

Syntax:

typedef type new-name;

Here type denotes the existing data type.

Example:

typedef int rolno;

tpedef float marks;

here rolno symbolizes integer and marks are float values thus we can later use these rolno and marks as

rolno r1,r2;

marks m1,m2,m3;



Enumeration 

This can be used to declare variables which may be any one among the expected values called enumerated constants. To define it we use the keyword enum

Syntax :

enum identifier {value1, value2, …….valuen};

example :

enum day{Monday=1,Tuesday…….Sunday}

here Monday got the value 1 and thus the remaining constants gets the values that increase successively by 1, the compiler automatically assigns integer values which begins with 0 when declare this as

enum day{Monday,Tuesday ,…. Sunday}



Derived data types 

The data types which are derived from one or more data types are called derived data types. Thus further subdivisions are possible in derived data types. They are

  • Arrays
  • Structures
  • Pointers
  • Union
  • Functions

 

Arrays

Arrays are the collection of similar data types referenced under one name. Using arrays we can store and manipulate similar type data with ease. Consider an example that if we want to store six marks of a student then in normal case we want six integers m1,m2,m3,m4,m5,m6 to manipulate these marks. If the number of data stored increase then it will be pretty difficult to manage the variables. An array can be used for replacing these variables because the marks are of similar types.

 

Syntax :

Data type array-name[size];

Example :

int  marks[10];

     

Integer is the data type and mark is the array name and 10 is the size of the array. When declaring  an array the consecutive locations will be allocated to store the values .

Dimensions of an array

Array can be of many dimensional  from one to n dimensional.

Syntax  of one dimensional array:

datatype  arrayname [size]; 

 
 
 
 
 

 

Matrix

An array with rows and columns are called the matrix (or two dimensional array).

Syntax  of two dimensional array- matrix:

datatype arrayname [rows][columns];

Example:

int a[4][2];

 

   
   
   
   

 

Multi dimensional arrays

Syntax  of  multi dimensional array:

datatype  arrayname [size1][size2]…..[sizen];

 

Structure 

Structure is the collection of different type of data referenced under one name.  The keyword  ‘struct’ is used to define  a structure.  For instance if we are saving the details of a student with out structures it will be scattered information gathering  and cannot save collectively. We can use a structure as follows

Syntax :

    struct  structure name
      {
  Data type 1;
         Data type 1;
         Data type 1;
      }structure variable;

Example :

struct  student      
{        
int rolno;  
char name[20];  
float avg;    
}s;

Using the structure variable ‘ s’ we can manipulate the data types in the structure. There is another method to define a structure variable .

Syntax:

struct structure name structure variables ;

struct student s;

Pointers

A pointer is a variable that contains the address of another variable in memory. The program execution speed can be increased by the use of pointers. It also reduces the length and complexity of a program.

 

Accessing address of a variable

The location of a variable is system dependent. To get the address of a variable we use the  operator ‘&’ called address operator. The address operator & preceding a variable will returns the address of that variable.

 

Representation of a variable 

A variable have 3 parts. They are

  • Name-name given to the variable
  • Value -the value which the variable holds
  • Address-where the variable stores in the memory

Example :

{
int a;
a=10;
}

 

we can use the pointer as follows

p=&a;

then the address of  ‘a’ which is 1008 is stored as the value of p. The variable p also have these three parts and the value stored in p will be 1008, the address of a.

 

 

Union

Union is almost same as structure concept. As in structures it consist of two or more data types. The only difference is in the storage, that is when declare any data type as structure members it will have its own storage space there in memory but all the members in union share the same memory location

Syntax:

union union- name
{
Data type1;
Data type2;
………………
Data type n;
}union variable;

Example :

union
{
int a;
float b;
}u;

 

Functions

Functions are the named part of a program which can be invoked from any part of the program.  There are two types of functions

  • Library functions
  • User defined functions

Library functions

The functions which already exist in C compilers are called library functions, it is defined by compiler.

Example:

scanf(),printf()

User defined functions

The sub-programs created for any special purpose is called as functions. These are used for easy to understand,debug and test. And the more important thing is it is reusable.

Example :

main()



 

 



Description

This tutorial introduces you to C language. Many tutorials has been written. We are writing this tutorial to be brief and to the point, so that you concentrate on what matter, rather than finding yourself in a mess. We want you to get a start and build form knowledge you will gain from this tutorial



Environment

We will cover environment needs as we progress through this tutorial. At the very least you will need a PC with windows or Linus operating system.

Audience

Programmers new to C Language

Learning Objectives

Learn basics of C language and build from that knowledge.

Author: Subject Coach
Added on: 3rd Jan 2015

You must be logged in as Student to ask a Question.

None just yet!