C - IntroductionC - Hello World ProgramC - Exercise 1C - Basic structure of a C programC - TokensC - Data TypesC - Type ConversionC - Exercise 2C - Character Input Output OperationsC - Input Output operation using scanf and printf functions

Operators

C - Arithmetic OperatorsC - Relational OperatorsC - Logical OperatorsC - Assignment OperatorsC - Increment Decrement OperatorsC - Bitwise Operators

Precedence and Associativity

C - Precedence and AssociativityC - Exercise 3

Conditions

C - If Else decision making statementsC - Switch Case decision making statements

Loop

C - While LoopC - Do While LoopC - For LoopC - Exercise 4

Array

C - ArraysC - Two Dimensional ArraysC - Multi Dimensional ArraysC - Exercise 5

String

C - StringC - Exercise 6C - String Manipulation

Functions

C - FunctionsC - Functions CategoryC - Function Call - Flow of ControlC - RecursionC - Functions and ArraysC - Functions and Strings

Structures

C - StructuresC - Structures and ArraysC - Passing structure to functionC - Function returning structureC - Structure in Structure

Pointers

C - PointersC - Pointers and VariablesC - Pointers and Variables Memory RepresentationC - Pointers Chaining

Pointers and Arrays

C - Pointers and One Dimensional ArrayC - Pointers and Two Dimensional ArrayC - Array of Pointers

Pointers and Strings

C - Pointers and Strings

Pointers and Functions

C - Pointers and Functions - Call by Value and Call by ReferenceC - Function returning pointer

Pointers and Structures

C - Pointers and StructuresC - Pointers and Array of StructuresC - Passing structure pointer to function

Handling Files

C - File Handling - Getting StartedC - File Handling - Read and Write CharactersC - File Handling - Read and Write IntegersC - File Handling - Read and Write multiple dataC - File Handling - Randomly Access Files

Command Line Arguments

C - Command Line Arguments

Dynamic Memory Allocation

C - Dynamic Memory Allocation - Getting StartedC - Dynamic Memory Allocation - malloc functionC - Dynamic Memory Allocation - calloc functionC - Dynamic Memory Allocation - realloc function

C - Data Types

C Programming

In this tutorial we will learn about data types in C programming language.

Variables

A variable is a named location to hold a data value.

The value stored in a variable can change and is opposite to a constant whose value never changes.

Example of variable: name, isGameOver, next_page, _nextgame, stage123 etc.

Rules for variable names

Following are the rules for naming a variable.

  • First character must be a letter or underscore
  • Can use letters (A-Z and a-z), digits (0-9) and underscore _
  • Must not be a keyword
  • Must not contain white space

Variable names are case-sensitive meaning hello and Hello are treated as two separate variables.

Data Types in C

Data Types are storage representation and tells the compiler what type of data is stored in the variable.

C divides its data types into 3 categories.

  • Primary
  • Derived
  • User defined

Primary Data Types

C provides 5 primary or fundamental data types namely character char, integer int, floating point float, double precision floating point double and void.

So, if we want to create a variable that will hold a character we use the char data type.

char ch = 'a';

Similarly, if we want to store integer value we use the int data type.

int gameScore = 10;

Range of primary data types

In the following table we have the range of the values of the primary or fundamental data type.

DATA TYPEKEYWORDEQUIVALENT SIZE (bits)RANGE
characterchar8-128 to 127
integerint16-32768 to 32767
floating point (6 decimal place)float323.4e-38 to 3.4e+38
double-precision floating point (12 decimal place)double641.7e-308 to 1.7e+308

Extended data type

We can also use the short, long, signed and unsigned keywords to extend the primary data types.

A short data type has a smaller range compared to the long data type.

A signed data type can hold both positive and negative values. But an unsigned data type can only hold positive values.

The following table list different combination of the primary data type along with their value range.

DATA TYPEKEYWORDEQUIVALENT SIZE (bits)RANGE
characterchar8-128 to 127
signed charactersigned char8-128 to 127
unsigned characterunsigned char80 to 255
short integershort int or short8-128 to 127
signed short integersigned short int8-128 to 127
unsigned short integerunsigned short int80 to 255
integerint16-32768 to 32767
signed integersigned int16-32768 to 32767
unsigned integerunsigned int160 to 65535
long integerlong int or long32-2147483648 to 2147483647
signed long integersigned long int32-2147483648 to 2147483647
unsigned long integerunsigned long int320 to 4294967295
floating point (6 decimal place)float323.4e-38 to 3.4e+38
double-precision floating point (12 decimal place)double641.7e-308 to 1.7e+308
extended double-precision floating pointlong double803.4e-4932 to 1.1e+4932

Use the data type that correctly matches the data range you are trying to store.

Void

The void data type takes no value and is generally used with functions to denote that the function is not going to return any value.

Declaring variables

When we declare a variable the compiler gets to know two things.

  • Name of the variable
  • Type of data that variable will hold

Example: Create a variable that will hold level of a game.

int level;

Example: Declaring multiple variables

int level;
int score;
int life;

We can merge the three variables having the same data type int in one single line by separating them with comma.

int level, score, life;

Assign value to a variable

We use the = assignment operator to assign value to a variable.

Example: Create a variable to store score of a match and assign 0 to it.

//declaring variable
int level;

//assigning value
level = 0;

We can merge the declaration and assignment step into one.

//declare and assign
int level = 0;

Derived Data Types

These are the data types derived from the primary data type.

Example: Array, Function, Pointer

We will learn about the derived data types in their respective tutorial.

User Defined Data Type

C allows programmers to create user defined types using the typedef and enum.

typedef

We use the typedef keyword to give new identifier name to a exisiting data type.

The general format to use typedef is given below.

typedef type identifier

Where type is any existing data type and identifier is the new name given to it.

Example:

typedef int num;

In the above line we are giving integer int data type a new name num.

In the following example we have used typedef to give int a new name num. And then created a new variable and assigned integer value 10 to it.

#include <stdio.h>
int main(void)
{
  typedef int num;
  num score = 10;
  printf("Score: %d", score);
  return 0;
}

The above code will print the following output.

Score: 10

enum

enum is enumerated data type that we can use to create user defined data type.

Using enum we can create a data type and also define the value that can be assigned to the variable of that type.

In the following example we are creating an enumerated data type color and it can take only 4 values RED, GREEN, BLUE and WHITE. Next we are creating a variable carColor of type color and assigning the value GREEN to it.

enum color {
  RED,
  GREEN,
  BLUE,
  WHITE
};

color carColor = GREEN;

We can combine the enum definition and variable creation as follows

enum color {
  RED,
  GREEN,
  BLUE,
  WHITE
} carColor = GREEN;

By default, the compiler assign integer value to the enum constants starting from 0.

So, in the above color enum the RED constant gets the integer value 0, GREEN is assigned 1 and so on.

The following code will print "Car Color: 1" as output since the enum constant GREEN gets the integer value 1.

#include <stdio.h>
int main(void)
{
  enum color {
    RED,
    GREEN,
    BLUE,
    WHITE
  } carColor = GREEN;
  printf("Car Color: %d\n", carColor);
  return 0;
}

We can assign our own integer value to the enum constants.

When we set value to a enum constant then the successive constants get value that increases by 1.

For example, if we want to set GREEN to 2 then we can write the following.

enum color {
  RED,
  GREEN = 2,
  BLUE,
  WHITE
} carColor = GREEN;

So, RED will be 0 (default as enum constant value starts from 0).

GREEN will be 2 (we have set that value).

BLUE will be 3 and WHITE will be 4.

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT