C Data Types: Defining Your Variables

c data typesC is a powerful, flexible, portable and structured high-level programming language. It combines features of a high level language with the elements of an assembler. It suits both the system and application programming. It is the most widely used general purpose language. The C language is designed to process a certain kind of data consisting of characters, numbers and strings. This task of processing data is accomplished by executing a set of instructions called “a program.”  C has its own vocabulary and grammar and it is defined in terms of syntax of various data types and functions.

New to C? Learn how to program with the C language at Udemy.com.

Data Types

The C language is very rich in its data types. The variety of data types available allows the programmer to select an appropriate type according to the application or machine’s requirement. Data types are an extensive system used for declaring variables and functions of different types.

There are three classes of data types:

  • Primary (fundamental) data types
  • Derived Data types
  • User Defined data types

Primary Data Types

Primary data types can be classified into basic and built-in types. These data types are the most basic building blocks of any programming language and numerous composite data types are constructed using them.

Learn how to work with C by taking a class at Udemy.com

Some primary data types are

Integer Types:

This data type as the name suggests, is used to hold only integer values. Generally, an integer stores values in a range limited from -32768 to 32767. A signed integer uses one bit for sign and 15 bits for the magnitude of a number. The integer has three classes of storage both signed and unsigned. These are classified on the basis of range of values held by them:

  • Short integer:  -128 to 127
  • Integer: -32768 to +32767
  • Long Integer:  -2,147,483,648 to 2,147,483,647

Syntax

shorti=value;

int i=value;

long int=value;

int i=3;

long int i =340000

short i=2;

Floating Point Types:

This data type stores real numbers in 32 bits, with 6 digits of precision. The keyword ‘float’ defines the floating-point data type.  The double type is used when more accurate data is required. When more accurate data is required, the double type is used. Double type uses 64 bits giving a precision of 14 digits. Double type precision is more than the float type. Long double further extends the precision it uses by 80 bits.

Void Types:

This data type has no values and usually specifies function type. The void type function does not return any values. It can also play the role of generic type, which means it can represent any standard type.

Syntax:

public void display()

Character Types:

A single character in C is defined as character (char) type data. They are usually stored in 8 bits of storage and the character can be signed and unsigned. Range of characters can be from -128 to 127 signed and 0 to 255 under unsigned.

Syntax

char ch=’a’

User-Defined Data Types:

C supports the features “typedef” that allows users to define the identifier which would represent an existing data type. This defined data type can then be used to declare variables:

Syntax:

typedef int  numbers;

numbers  num1,num2;

In this example, num1 and num2 are declared as int variables. The main advantage of user defined data type is that it increases the program’s readability.

Another type is enumerated type. This is also a user defined data type

Syntax:

enum  identifier {value1,value2, value 3,…}

“Enum” is the keyword and “identifier” is the user defined data type that is used to declare the variables. It can have any value enclosed within the curly braces. For example:

enum day {January, February,March,April,..};

enum day month_st,month_end;

The compiler automatically assigns integer digits beginning from 0 to all the enumeration constants. For example, “January ” will have value 0 assigned, “February”  value 2 assigned and so on. You can also explicitly assign the enumeration constants.

Derived Data Type:

These data types are formed by a combination of two or more primary data types. They have extended the scope of C language. The most common are pointers, arrays, union and structures.

Pointers: Pointers are very powerful features of the C language. The basic concept of a pointer is that it provides the mechanism for determining the address of data. It can reference any data type – basic or derived. However, overuse of pointers affects performance .

Syntax:

//Character pointer

char *char_ptr

//Interger pointer

int * int_ptr

Arrays:

An array is a container used to store several values in one variable and data type. It is defined using square brackets. An integer comes in between the brackets, which defines the array’s size.

Syntax:

constant unsigned int size=49

int array_values[size]

Note that the array can only have unsigned values as size cannot be negative.

Structures:

C allows you to declare variables that can hold various types of data items into one. This is called a structure in C. The “struct” keyword is used to define the structure. It defines new data types with several members.

Syntax

struct [structure tag]

{

member definition1;

member definition2;

...

member definition3;

} [structure variables];

The structure tag is optional and every member definition is a normal variable declaration. Structure variables are defined before the semicolon but this is optional.

struct Books

{

char  title[50];

char  author[50];

char  subject[100];

int book_id;

} book1;

The other way to declare a structure variable is

Syntax:

Books book2

“Books” is the name of the structure defined and “book2” is the variable name.

For example: Accessing the member of a structure.

//Declaring a structure

struct Books

{

char  title[50];

char  author[50];

char  subject[100];

int book_id;

};

int main( )

{

struct Books Book1;        /* Declare Structure variable */

struct Books Book2;

/*Accesing the member of structure*/

 

Book1.title,="Cooking Made Easy";

Book1.author="Chef";

Book1.subject="Cooking";

Book1.id=1234567;

}

Unions

A union stores different data types in the same memory location. It consists of many members but only one member can contain the value at any given time. It provides an efficient way of using the same memory location for various purposes.

Syntax:

//Union declaration

union Data

{

int k;

float f1;

char str[10];

};

int main( )

{

union Data data1;        //Accessing the member of union

printf( "Memory size of data : %d\n", sizeof(data1));

return 0;

}

Sample Program

main()

{

short i=2;

int  num1=45;

long int num2=123456789;

char ch=’a’;

char a[]={‘I’,’A’,’M’,’A’,’R’,’R’,’A’,’Y’};

int *ptr;

ptr=&num1;//Setting ptr to point to num1

System.out.println(“I am Primative data type” +I  +num1 +num3);

System.out.println(“I am Character data type”+ch);

System.out.println(“I am derived data type” ptr);

for (x=0;x<a.length();x++)

{

System.out.println(“I am array elements”a[x]);

}

}

}

Understand how to work with C at Udemy.com.