1 Data Types

 Basic Data Types:


int: Used to store integer values.
int age = 25;
float: Used to store single-precision floating-point numbers.
float salary = 50000.75;
double: Used to store double-precision floating-point numbers.
double pi = 3.141592653589793;
char: Used to store single characters.
char grade = ’A’;
void: Represents the absence of a type. It is often used as a return type for functions that do not return a value.

void printMessage()
{
printf(“Hello, World!\n”);
}

Derived Data Types:

Arrays: A collection of elements of the same type.
int numbers[5] = {1, 2, 3, 4, 5};
Pointers: Variables that store memory addresses.
int *ptr = &age;
Functions: Blocks of code that perform specific tasks.

int add(int a, int b)
{
return a + b;
}

User-Defined Data Types:

Structures: A collection of variables of different types under a sin- gle name.

struct Person
{
char name[50];
int age;
float salary;
};

Unions: Similar to structures, but all members share the same memory location.

union Data
{
int i;
float f;
char str[20];
};

Enumerations: A user-defined type consisting of a set of named integer constants.
enum Days {Sun, Mon, Tue, Wed, Thu, Fri, Sat};

Data
Type
Size (in
Bytes)
Format
Specifier
Typical Range (32-bit
System)
char 1 %c -128 to 127 (Signed) / 0 to
255 (Unsigned)
int 4 %d -2,147,483,648 to
2,147,483,647
float 4 %f ∼3.4E-38 to ∼3.4E+38 (6
decimal places)
double 8 %lf ∼1.7E-308 to ∼1.7E+308
(15 decimal places)

Note: Sizes may vary depending on system architecture (e.g., 32-bit vs. 64-bit).

Type Casting and Type Conversion

1. Type Casting

Type casting is the process of explicitly converting a variable from one data type to another by the programmer.
Syntax:
(type) expression;
Example:
#include <stdio.h>
int main()
{
int a = 5, b = 2;
float result;
result = (float)a / b;   // type casting
printf(“Result = %f”, result);
return 0;
}
Here, a is converted to float before division.

2. Inbuilt Type Casting Functions in C

Some commonly used library functions for type conversion are:

  • atoi() – converts string to integer
  • atof() – converts string to float
  • atol() – converts string to long integer
  • itoa() – converts integer to string

3. Difference Between Type Casting and Type Conversion

Type Casting Type Conversion
Done explicitly by the programmer Done automatically by the compiler
Also called explicit conversion Also called implicit conversion
The programmer specifies the new data type The compiler decides the data type conversion
Example: (float)a Example: int + float → float

Example of Type Conversion (Implicit):
int a = 5;
float b = 2.5;
float c;