Skip to content

Commit

Permalink
6-data-types/
Browse files Browse the repository at this point in the history
  • Loading branch information
hengxin committed Oct 31, 2024
1 parent 32dda6a commit 4dfe8d3
Show file tree
Hide file tree
Showing 45 changed files with 1,323 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions 6-data-types/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Objects, size, precision, width, limits
add_executable(size size.c)
add_executable(precision precision.c)
add_executable(int-limits int-limits.c)
add_executable(exact-width exact-width.c)

add_executable(unsigned unsigned.c)
add_executable(for-unsigned for-unsigned.c)
add_executable(sizet sizet.c)
add_executable(timing-primes timing-primes.c)

add_executable(char char.c)

add_executable(unsinged-wrap unsigned-wrap.c)
add_executable(unsigned-wrap-fix unsigned-wrap-fix.c)

add_executable(implicit-conversion implicit-conversion.c)
add_executable(integer-promotion integer-promotion.c)
add_executable(explict-conversion explict-conversion.c)

add_executable(float-limits float-limits.c)

add_executable(sum-product sum-product.c)
add_executable(loop loop.c)

add_executable(compare compare.c)
target_link_libraries(compare m)
23 changes: 23 additions & 0 deletions 6-data-types/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 7-data-types

## `int-limits.c`

## `unsigned.c`

## `timing.c`

## `char.c`

## `int-overflow.c`

## `implicit-inversion.c`

## `explicit-inversion.c`

## `float-limits.c`

## `sums.c`

## `loop.c`

## `compare.c`
16 changes: 16 additions & 0 deletions 6-data-types/char.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Created by hfwei on 2024/10/30.

#include <limits.h>
#include <stdio.h>

int main() {
printf("CHAR_MIN = %d\n", CHAR_MIN);
printf("CHAR_MAX = %d\n", CHAR_MAX);

char c = 150;
int i = 900;

printf("i / c = %d\n", i / c);

return 0;
}
28 changes: 28 additions & 0 deletions 6-data-types/compare.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* file: compare.c
*
* See https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
*
* Created by hfwei on 2022/11/10.
*/

#include <float.h>
#include <math.h>
#include <stdio.h>
#include <stdbool.h>

#define EPSILON 1e-5

bool IsEqual(double x, double y);

int main() {
printf("%d\n", IsEqual(DBL_MAX, DBL_MAX - 100));

printf("%.50f\n", DBL_MAX - (DBL_MAX - 100));

return 0;
}

bool IsEqual(double x, double y) {
return fabs(x - y) <= EPSILON;
}
10 changes: 10 additions & 0 deletions 6-data-types/exact-width.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Created by hfwei on 2024/10/31.

#include <stdint.h>

int main(void) {
int8_t small = -100;
int32_t large = 100000;

return 0;
}
18 changes: 18 additions & 0 deletions 6-data-types/explict-conversion.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Created by hfwei on 2024/10/30.

#include <limits.h>
#include <stdio.h>

int main() {
double pi = 3.14159;

// below: obtain its fractional part
double fraction = pi - (int)pi;

int num = 100000000; // (9 digits)
printf("LLONG_MAX = %lld\n", LLONG_MAX);
long long llint = (long long)num * num;
printf("i = %lld\n", llint);

return 0;
}
29 changes: 29 additions & 0 deletions 6-data-types/float-limits.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Created by hfwei on 2024/10/30.

#include <float.h>
#include <stdio.h>

int main() {
// float pi = 3.14F;

// 3.402823e+38
printf("FLT_MAX = %e\n", FLT_MAX);
// 1.175494e-38
printf("FLT_MIN = %e\n", FLT_MIN);
// 1.401298e-45
printf("FLT_TRUE_MIN = %e\n", FLT_TRUE_MIN);
// 1.192093e-07
printf("FLT_EPSILON = %e\n\n", FLT_EPSILON);

// %lf for scanf
// 1.797693e+308
printf("DBL_MAX = %e\n", DBL_MAX);
// 2.225074e-308
printf("DBL_MIN = %e\n", DBL_MIN);
// 4.940656e-324
printf("DBL_TRUE_MIN = %e\n", DBL_TRUE_MIN);
// 2.220446e-16
printf("DBL_EPSILON = %e\n\n", DBL_EPSILON);

return 0;
}
14 changes: 14 additions & 0 deletions 6-data-types/for-unsigned.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Created by hfwei on 2024/10/31.

#include <stdio.h>
#define LEN 100

int main(void) {
int numbers[LEN] = {0};

for (unsigned int i = LEN; i >= 0; i--) {
printf("%u : %d\n", i, numbers[i]);
}

return 0;
}
35 changes: 35 additions & 0 deletions 6-data-types/implicit-conversion.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Created by hfwei on 2024/10/30.

#include <limits.h>
#include <stdio.h>

int SquareInt(int num);
double SquareDouble(double num);

int main() {
// narrowing conversion (still in the range)
int i = 3.14159;

// out of the range: undefined behavior!!!
int j = UINT_MAX;

// arguments; narrowing conversion
double pi = 3.14;
SquareInt(pi);

// return value; narrowing conversion
int val = SquareDouble(pi);

// from int to float; narrowing conversion
int big = 1234567890;
float approx = big;

printf("big = %d\t approx = %f\t diff = %d\n", big, approx,
big - (int)approx);

return 0;
}

int SquareInt(int num) { return num * num; }

double SquareDouble(double num) { return num * num; }
Binary file added 6-data-types/int-limits
Binary file not shown.
35 changes: 35 additions & 0 deletions 6-data-types/int-limits.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Created by hfwei on 2024/10/30.
// Run on Windows and Linux

#include <limits.h>
#include <stdio.h>

int main() {
// INT_MIN = -2147483648
// INT_MAX = 2147483647 (10 digits)
printf("INT_MIN = %d\n", INT_MIN);
printf("INT_MAX = %d\n\n", INT_MAX);

// printf("UINT_MIN = %u\n", 0U);
// printf("UINT_MAX = %u\n\n", UINT_MAX);

printf("LONG_MIN = %ld\n", LONG_MIN);
printf("LONG_MAX = %ld\n\n", LONG_MAX);

// printf("ULONG_MIN = %lu\n", 0UL);
// printf("ULONG_MAX = %lu\n\n", ULONG_MAX);

// long long int: >= 64 bits

// LLONG_MIN = -9223372036854775808
// LLONG_MAX = 9223372036854775807 (19 digits)
printf("LLONG_MIN = %lld\n", LLONG_MIN);
printf("LLONG_MAX = %lld\n\n", LLONG_MAX);

// printf("ULONG_LONG_MIN = %llu\n", 0ULL);
// printf("ULONG_LONG_MAX = %llu\n\n", ULONG_LONG_MAX);
//
// printf("ULLONG_MAX = %llu\n\n", ULLONG_MAX);

return 0;
}
13 changes: 13 additions & 0 deletions 6-data-types/integer-promotion.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Created by hfwei on 2024/10/31.

#include <stdio.h>

int main(void) {
signed char left = 100;
signed char mid = 3;
signed char right = 4;

signed char result = left * mid / right;

printf("result = %d\n", result);
}
17 changes: 17 additions & 0 deletions 6-data-types/loop.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Created by hengxin on 2024/10/30.

#include <stdio.h>

int main() {
/**
* Do not use a counter of type float/double,
* although it works on some platforms.
*
* 0.1 cannot be exactly represented in machines.
*/
for (double x = 0.1; x <= 1.0; x += 0.1) {
printf("%.20f\n", x);
}

return 0;
}
23 changes: 23 additions & 0 deletions 6-data-types/precision.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Created by hfwei on 2024/10/31.

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

unsigned int pow2(unsigned int exp);

int main(void) {
unsigned int exp = 30;

unsigned int pow = pow2(exp);
printf("2^%d = %d\n", exp, pow);
}

unsigned int pow2(unsigned int exp) {
if (exp >= sizeof(unsigned int) * CHAR_BIT) {
printf("Exp is too large!\n");
exit(1);
}

return 1 << exp;
}
Loading

0 comments on commit 4dfe8d3

Please sign in to comment.