Programming requires a wide range of data types, and C, with its efficiency and versatility, provides them all. For anyone who wants to become a programmer, understanding these data types is important. Here are eleven crucial C programmes that demonstrate how to use and manipulate various data types:
1.Integer Arithmetic (int):
#include <stdio.h>
int main() {
int a = 5, b = 3;
int sum = a + b;
printf("Sum: %d\n", sum);
return 0;
}
Integer arithmetic is demonstrated in this simple programme by initialising variables as integers and carrying out addition.
2.Floating-Point Arithmetic (float):
#include <stdio.h>
int main() {
float radius = 5.6;
float area = 3.14 * radius * radius;
printf("Area of Circle: %f\n", area);
return 0;
}
This program uses floating-point arithmetic to compute the area of a circle using float data types.
3.Character Handling (char):
#include <stdio.h>
int main() {
char letter = 'A';
printf("Character: %c\n", letter);
return 0;
}
Initialises and prints a single character to show character handling.
4.Array Manipulation (arrays):
#include <stdio.h>
int main() {
int numbers[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
return 0;
}
Initialises and prints the elements of an integer array to demonstrate the fundamentals of arrays.
5.String Handling (strings):
#include <stdio.h>
#include <string.h>
int main() {
char name[] = "John Doe";
printf("Name: %s\n", name);
printf("Length: %d\n", (int)strlen(name));
return 0;
}
Initializes, prints, and computes the length of a string to introduce string handling.
6.Boolean Logic (bool):
#include <stdio.h>
#include <stdbool.h>
int main() {
bool isTrue = true;
if (isTrue) {
printf("It's true!\n");
} else {
printf("It's false!\n");
}
return 0;
}
Shows boolean logic using the C library stdbool.h.
7.Pointer Operations (pointers):
#include <stdio.h>
int main() {
int number = 10;
int *ptr = &number;
printf("Value: %d\n", *ptr);
return 0;
}
By allocating and publishing the value that pointers point to, introduces pointers.
8.Enumeration (enum):
#include <stdio.h>
enum Days { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday };
int main() {
enum Days today = Wednesday;
printf("Today is day number: %d\n", today);
return 0;
}
Assigning numerical values to the days of the week demonstrates enumeration.
9.Size of Data Types (sizeof):
#include <stdio.h>
int main() {
printf("Size of int: %d bytes\n", (int)sizeof(int));
printf("Size of float: %d bytes\n", (int)sizeof(float));
printf("Size of char: %d byte\n", (int)sizeof(char));
return 0;
}
uses the sizeof() operator to display the size in bytes of various data types.
10.Type Casting (type casting):
#include <stdio.h>
int main() {
float result = 15 / 2; // result will be 7.0, not 7.5
float correctedResult = (float)15 / 2; // type casting to get 7.5
printf("Result: %f\n", result);
printf("Corrected Result: %f\n", correctedResult);
return 0;
}
Demonstrates how to transform data types using type casting to guarantee reliable computations.
These eight programs provide an extensive overview of different data types and how they are used in C programming. Gaining experience with and comprehension of these programs will provide a solid basis for learning about data types in C.
More Related Posts:
Informative… Looking forward for more blogs like this