C Data Types Examples

1. Integer Data Type (int)

#include <stdio.h>

int main() {
int age = 25;
int year = 2025;

printf(“Age: %d\n”, age);
printf(“Year: %d\n”, year);

return 0;
}

Output:

Age: 25
Year: 2025

2. Floating-Point Data Types (float and double)

#include <stdio.h>

int main() {
float pi = 3.14;
double e = 2.718281828;

printf(“Pi: %.2f\n”, pi); // float
printf(“Euler’s number: %.6lf\n”, e); // double

return 0;
}

Output:

Pi: 3.14
Euler's number: 2.718282

Note: %.nf and %.nlf control the number of decimals.


3. Character Data Type (char)

#include <stdio.h>

int main() {
char grade = ‘A’;

printf(“Grade: %c\n”, grade);

return 0;
}

Output:

Grade: A

4. Unsigned Data Type

  • Can only store positive values, doubling the upper limit.

#include <stdio.h>

int main() {
unsigned int positiveNumber = 100;

printf(“Positive Number: %u\n”, positiveNumber);

return 0;
}

Output:

Positive Number: 100

5. Short and Long Integers

#include <stdio.h>

int main() {
short int small = 32000; // 2 bytes
long int large = 1000000; // 4–8 bytes

printf(“Short: %d\n”, small);
printf(“Long: %ld\n”, large);

return 0;
}

Output:

Short: 32000
Long: 1000000

6. Example Combining Multiple Data Types

#include <stdio.h>

int main() {
int age = 25;
float height = 5.9;
double pi = 3.1415926535;
char grade = ‘A’;
unsigned int positiveNum = 100;

printf(“Age: %d\n”, age);
printf(“Height: %.1f\n”, height);
printf(“Pi: %.5lf\n”, pi);
printf(“Grade: %c\n”, grade);
printf(“Positive Number: %u\n”, positiveNum);

return 0;
}

Output:

Age: 25
Height: 5.9
Pi: 3.14159
Grade: A
Positive Number: 100

Key Points from Examples:

  1. int → Whole numbers

  2. float → Single-precision decimals

  3. double → Double-precision decimals

  4. char → Single characters

  5. unsigned → Positive numbers only

  6. short/long → Adjust memory size and range

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *