Compound Types
C has compound types like:
- Array
- Struct
- Union
- Enumeration
- String, basically an array of characters
Array
An array is a collection of items stored at contiguous memory locations. Elements in an array can be accessed randomly using indexes.
int a[5];
int b[] = {0,1,2,3,4};
size_t len = sizeof(a) / sizeof(a[0]);
for(size_t i = 0; i < len; i++) {
a[i] = i;
}
printf("%d\n", a[2]);
Strings
In C, a string is an array of char
data, and terminated with the extra invisible \0
char.
For example:
#include <stdio.h>
#include <string.h>
int main() {
// Declaration and initialization of a string
char greeting[] = "Hello, World!";
char *s = "Hi, Again!";
// Display the string using printf
printf("String: %s\n", greeting);
printf("String: %s\n", s); // Output: Hi, Again!
// Calculate and display the length of the string
int length = strlen(greeting);
printf("Length of the string: %d\n", length);
// Access individual characters in the string
printf("First character: %c\n", greeting[0]); // Output: H
printf("Third character: %c\n", greeting[2]); // Output: l
// Modify a character in the string
greeting[7] = 'Y';
printf("Modified string: %s\n", greeting); // Output: Hello, Yorld!
return 0;
}
Enumerations
Enumerations in C provide a way to create a named list of integer constants. Each constant in the enum has an associated integer value, and these values are assigned automatically by the compiler.
#include <stdio.h>
enum Color {
RED, // 0
GREEN, // 1
BLUE // 2
};
int main() {
enum Color favoriteColor = BLUE;
printf("Favorite color is %d\n", favoriteColor);
// Favorite color is 2
return 0;
}
Structs
In C, structs (structures) allow you to group variables of different types under a single name. They are user defined data storage objects containing public members by default.
#include <stdio.h>
#include <string.h>
struct Person {
char name[50];
int age;
float height;
};
int main() {
struct Person person1;
strcpy(person1.name, "Lite Rank");
person1.age = 30;
person1.height = 175.5;
printf("Name: %s\n", person1.name);
// Name: Lite Rank
printf("Age: %d\n", person1.age);
printf("Height: %.2f\n", person1.height);
return 0;
}
Union
A union is a special data type that allows storing different data types in the same memory location. The size of a union is determined by the largest member's size. Unions provide an efficient way of using the same memory location for multiple purposes.
#include <stdio.h>
union Data {
int intValue;
float floatValue;
char stringValue[20];
};
int main() {
union Data data;
data.intValue = 42;
printf("Integer value: %d\n", data.intValue);
data.floatValue = 3.14;
printf("Float value: %.2f\n", data.floatValue);
strcpy(data.stringValue, "Hello, Union!");
printf("String value: %s\n", data.stringValue);
printf("After reassignment:\n");
printf("Integer value: %d\n", data.intValue);
printf("Float value: %.2f\n", data.floatValue);
printf("String value: %s\n", data.stringValue);
return 0;
}
Code Challenge
Write a C program that uses a
union
to determine the size of various data types (int
,float
,double
,char
) without using thesizeof
operator.