Basic Data Types with Example Programs - int, float, char, etc.
Table of Contents:
- Introduction
- Integer Data Type (int)
- Floating-Point Data Type (float)
- Character Data Type (char)
- Conclusion
Introduction
In C programming, data types play a crucial role in defining the type of data a variable can hold. The basic data types include integers, floating-point numbers, characters, and more. Understanding these data types and their usage is fundamental for writing efficient and error-free C programs. In this post, we will explore the basic data types: int, float, and char, along with example programs to demonstrate their usage.
Integer Data Type (int)
Integers are whole numbers without any fractional parts. The int data type is used to store integer values, and it has a specific range depending on the system architecture.
Definition and Range:
The int data type typically stores signed integers and has a range from -2147483648 to 2147483647 on most systems (32-bit signed integer).
Example Program 1: Addition of Two Integers
#include <stdio.h>
int main() {
int num1, num2, sum;
printf("Enter the first integer: ");
scanf("%d", &num1);
printf("Enter the second integer: ");
scanf("%d", &num2);
sum = num1 + num2;
printf("Sum: %d\n", sum);
return 0;
}
Example Program 2: Finding the Maximum of Two Integers
#include <stdio.h>
int main() {
int num1, num2, max;
printf("Enter the first integer: ");
scanf("%d", &num1);
printf("Enter the second integer: ");
scanf("%d", &num2);
max = (num1 > num2) ? num1 : num2;
printf("Maximum: %d\n", max);
return 0;
}
Floating-Point Data Type (float)
Floating-point numbers are numbers with fractional parts. The float data type is used to store single-precision floating-point values.
Definition and Range:
The float data type typically stores single-precision floating-point numbers with a range of approximately 1.2E-38 to 3.4E38, with 6 decimal places of precision.
Example Program 1: Area of a Circle
#include <stdio.h>
#define PI 3.14159
int main() {
float radius, area;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
area = PI * radius * radius;
printf("Area of the circle: %f\n", area);
return 0;
}
Example Program 2: Conversion from Celsius to Fahrenheit
#include <stdio.h>
int main() {
float celsius, fahrenheit;
printf("Enter the temperature in Celsius: ");
scanf("%f", &celsius);
fahrenheit = (celsius * 9 / 5) + 32;
printf("Temperature in Fahrenheit: %f\n", fahrenheit);
return 0;
}
Character Data Type (char)
Characters represent individual letters, digits, or special symbols. The char data type is used to store a single character.
Definition and Range:
The char data type typically stores ASCII characters and has a range from -128 to 127 on most systems (8-bit signed integer).
Example Program 1: Check for Vowel or Consonant
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf(" %c", &ch);
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
printf("%c is a vowel.\n", ch);
} else {
printf("%c is a consonant.\n", ch);
}
} else {
printf("Invalid input. Please enter an alphabet.\n");
}
return 0;
}
Example Program 2: Convert Uppercase to Lowercase
#include <stdio.h>
int main() {
char ch;
printf("Enter an uppercase letter: ");
scanf(" %c", &ch);
if (ch >= 'A' && ch <= 'Z') {
ch = ch + 32; // ASCII difference between uppercase and lowercase letters
printf("Lowercase equivalent: %c\n", ch);
} else {
printf("Invalid input. Please enter an uppercase letter.\n");
}
return 0;
}
Conclusion
In this post, we explored the basic data types in C programming, namely int, float, and char. We learned about their definitions, ranges, and usage in various example programs. Understanding these fundamental data types is essential for any C programmer, as they form the building blocks for more complex data structures and algorithms.
I hope this post has been helpful in improving your understanding of basic data types in C programming. Happy coding!
0 Comments