C language content

In C programming, basic data types are fundamental building blocks used to define the type and size of data that can be stored in variables. Here’s a detailed look at the basic data types int, float, char, and double:

1. int (Integer)

  • Description: Used to store integer (whole) numbers without a fractional component.
  • Size: Typically 4 bytes (32 bits) on most modern systems, but it can vary depending on the platform.
  • Range: Generally from -2,147,483,648 to 2,147,483,647 for a 32-bit int.

Example:

				
					int age = 25;
int year = 2024;

				
			

2. float (Floating-Point)

  • Description: Used to store single-precision floating-point numbers (numbers with a fractional part).
  • Size: Typically 4 bytes (32 bits).
  • Range: Approximately from 1.2E-38 to 3.4E+38, with 6-7 decimal digits of precision.

Example:

				
					float temperature = 36.6f;
float pi = 3.14159f;

				
			

3. char (Character)

  • Description: Used to store single characters. Can also be used to store small integers.
  • Size: Typically 1 byte (8 bits).
  • Range: Generally from -128 to 127 or 0 to 255, depending on whether it is signed or unsigned.

Example:

				
					char initial = 'A';
char newline = '\n';  // Special character for a new line

				
			

4. double (Double Precision Floating-Point)

  • Description: Used to store double-precision floating-point numbers. Provides more precision and a wider range compared to float.
  • Size: Typically 8 bytes (64 bits).
  • Range: Approximately from 2.3E-308 to 1.7E+308, with 15-16 decimal digits of precision.

Example:

				
					double gravity = 9.80665;
double largeNumber = 1.23456789012345;

				
			

Example Program Using Basic Data Types

Here’s a simple C program that demonstrates the usage of these basic data types:

				
					#include <stdio.h>

int main() {
    // Declare and initialize variables
    int age = 25;
    float temperature = 36.6f;
    char initial = 'A';
    double pi = 3.141592653589793;

    // Print the values
    printf("Age: %d\n", age);
    printf("Temperature: %.2f\n", temperature);
    printf("Initial: %c\n", initial);
    printf("Pi: %.15f\n", pi);

    return 0;
}


				
			

Explanation:

  1. int age: Declares an integer variable age and initializes it to 25.
  2. float temperature: Declares a float variable temperature and initializes it to 36.6.
  3. char initial: Declares a char variable initial and initializes it to ‘A’.
  4. double pi: Declares a double variable pi and initializes it to 3.141592653589793.
  5. printf: Used to print the values of the variables to the console. The format specifiers %d, %.2f, %c, and %.15f are used for int, float, char, and double respectively.

This program demonstrates how to declare variables of different basic data types, initialize them, and print their values. The format specifiers in the printf function ensure that the values are correctly formatted when displayed.

Scroll to Top