diff options
Diffstat (limited to 'src/c/sizeof')
-rw-r--r-- | src/c/sizeof/Makefile | 30 | ||||
-rwxr-xr-x | src/c/sizeof/main | bin | 0 -> 15488 bytes | |||
-rw-r--r-- | src/c/sizeof/main.c | 50 |
3 files changed, 80 insertions, 0 deletions
diff --git a/src/c/sizeof/Makefile b/src/c/sizeof/Makefile new file mode 100644 index 0000000..d6e2bf1 --- /dev/null +++ b/src/c/sizeof/Makefile @@ -0,0 +1,30 @@ +# Minimal Makefile to compile main.c into an executable named main + +# Compiler +CC = gcc + + +# Flags +CFLAGS = -Wall -Wextra -O2 + + +# Default target +# Tells make what dependencies to compile +all: main + + +# Dependency 'main' +# main.c is the source file +main: main.c + $(CC) $(CFLAGS) -o main main.c + + +# Test target +test: main + @echo -n "main.c test............................" + @./main >/dev/null 2>&1 && echo "OK" || echo "FAILED" + + +# Clean target +clean: + rm -f main diff --git a/src/c/sizeof/main b/src/c/sizeof/main Binary files differnew file mode 100755 index 0000000..d902a63 --- /dev/null +++ b/src/c/sizeof/main diff --git a/src/c/sizeof/main.c b/src/c/sizeof/main.c new file mode 100644 index 0000000..0d16080 --- /dev/null +++ b/src/c/sizeof/main.c @@ -0,0 +1,50 @@ +#include <stdio.h> +#include <stdint.h> // for fixed-width integer types +#include <stddef.h> // for size_t, ptrdiff_t +#include <limits.h> // for limits of fundamental types +#include <inttypes.h> // for PRId64 macros etc. + +int main(void) { + printf("=== Fundamental integer types ===\n"); + printf("char : %zu byte(s) (%zu bit) range: %d to %d\n", + sizeof(char), sizeof(char) * 8, CHAR_MIN, CHAR_MAX); + printf("short : %zu byte(s) (%zu bit) range: %d to %d\n", + sizeof(short), sizeof(short) * 8, SHRT_MIN, SHRT_MAX); + printf("int : %zu byte(s) (%zu bit) range: %d to %d\n", + sizeof(int), sizeof(int) * 8, INT_MIN, INT_MAX); + printf("long : %zu byte(s) (%zu bit) range: %ld to %ld\n", + sizeof(long), sizeof(long) * 8, LONG_MIN, LONG_MAX); + printf("long long : %zu byte(s) (%zu bit) range: %lld to %lld\n", + sizeof(long long), sizeof(long long) * 8, LLONG_MIN, LLONG_MAX); + + printf("\n=== Fixed-width integer types (from <stdint.h>) ===\n"); + printf("int8_t : %zu byte(s) (%zu bit) range: %" PRId8 " to %" PRId8 "\n", + sizeof(int8_t), sizeof(int8_t) * 8, INT8_MIN, INT8_MAX); + printf("int16_t : %zu byte(s) (%zu bit) range: %" PRId16 " to %" PRId16 "\n", + sizeof(int16_t), sizeof(int16_t) * 8, INT16_MIN, INT16_MAX); + printf("int32_t : %zu byte(s) (%zu bit) range: %" PRId32 " to %" PRId32 "\n", + sizeof(int32_t), sizeof(int32_t) * 8, INT32_MIN, INT32_MAX); + printf("int64_t : %zu byte(s) (%zu bit) range: %" PRId64 " to %" PRId64 "\n", + sizeof(int64_t), sizeof(int64_t) * 8, INT64_MIN, INT64_MAX); + + printf("\n=== Unsigned fixed-width types ===\n"); + printf("uint8_t : %zu byte(s) (%zu bit) range: 0 to %" PRIu8 "\n", + sizeof(uint8_t), sizeof(uint8_t) * 8, UINT8_MAX); + printf("uint16_t : %zu byte(s) (%zu bit) range: 0 to %" PRIu16 "\n", + sizeof(uint16_t), sizeof(uint16_t) * 8, UINT16_MAX); + printf("uint32_t : %zu byte(s) (%zu bit) range: 0 to %" PRIu32 "\n", + sizeof(uint32_t), sizeof(uint32_t) * 8, UINT32_MAX); + printf("uint64_t : %zu byte(s) (%zu bit) range: 0 to %" PRIu64 "\n", + sizeof(uint64_t), sizeof(uint64_t) * 8, UINT64_MAX); + + printf("\n=== Pointer and memory-related types ===\n"); + printf("void* : %zu byte(s) (%zu bit)\n", + sizeof(void*), sizeof(void*) * 8); + printf("size_t : %zu byte(s) (%zu bit) range: 0 to %zu\n", + sizeof(size_t), sizeof(size_t) * 8, (size_t)-1); + printf("ptrdiff_t : %zu byte(s) (%zu bit)\n", + sizeof(ptrdiff_t), sizeof(ptrdiff_t) * 8); + + return 0; +} + |