diff options
Diffstat (limited to 'src')
-rwxr-xr-x | src/ctof | bin | 0 -> 15648 bytes | |||
-rw-r--r-- | src/ctof.c | 44 | ||||
-rwxr-xr-x | src/dir | bin | 0 -> 15624 bytes | |||
-rw-r--r-- | src/dir.c | 17 | ||||
-rwxr-xr-x | src/main | bin | 0 -> 15552 bytes | |||
-rw-r--r-- | src/main.c | 25 | ||||
-rwxr-xr-x | src/main2 | bin | 0 -> 15416 bytes | |||
-rw-r--r-- | src/main2.c | 6 | ||||
-rwxr-xr-x | src/shift | bin | 0 -> 15672 bytes | |||
-rw-r--r-- | src/shift.c | 32 |
10 files changed, 124 insertions, 0 deletions
diff --git a/src/ctof b/src/ctof Binary files differnew file mode 100755 index 0000000..7a47ff7 --- /dev/null +++ b/src/ctof diff --git a/src/ctof.c b/src/ctof.c new file mode 100644 index 0000000..e61cfcd --- /dev/null +++ b/src/ctof.c @@ -0,0 +1,44 @@ +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> // for errno +#include <limits.h> // for INT_MAX, INT_MIN + + +/* + F = C x 9/5 + 32 +*/ + +int +main(int argc, char *argv[]) +{ + + if (argc < 2) { + printf("Usage: %s number\n", argv[0]); + return 1; + } + + char *endptr; + + errno = 0; // reset errno + + long value = strtol(argv[1], &endptr, 10); // base 10 + + if (endptr == argv[1]) { + printf("Error: No digits were found\n"); + return 1; + } + if (*endptr != '\0') { + printf("Warning: Trailing characters after number: %s\n", endptr); + return 1; + } + if ((value == LONG_MAX || value == LONG_MIN) && errno == ERANGE) { + printf("Error: Number out of range\n"); + return 1; + } + + float fah = value * 9/5 + 32; + + printf("Fahrenheit: %0.f\n", fah); + + return 0; +} Binary files differdiff --git a/src/dir.c b/src/dir.c new file mode 100644 index 0000000..05803fd --- /dev/null +++ b/src/dir.c @@ -0,0 +1,17 @@ +#include <stdio.h> +#include <dirent.h> + +int main() { + DIR *d; + struct dirent *dir; + d = opendir("."); + if (d) { + while ((dir = readdir(d)) != NULL) { + printf("%s\n", dir->d_name); + } + closedir(d); + } else { + perror("opendir"); + } + return 0; +} diff --git a/src/main b/src/main Binary files differnew file mode 100755 index 0000000..37336e8 --- /dev/null +++ b/src/main diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..30d5c61 --- /dev/null +++ b/src/main.c @@ -0,0 +1,25 @@ +#include <stdio.h> + +// Function that tries to change a number without pointer +void noPointerChange(int num) { + num = 100; // Changes local copy only +} + +// Function that changes a number using pointer +void pointerChange(int *numPtr) { + *numPtr = 100; // Changes original value through pointer +} + +int main() { + int value = 50; + + printf("Before noPointerChange: %d\n", value); + noPointerChange(value); + printf("After noPointerChange: %d\n", value); // value unchanged + + printf("Before pointerChange: %d\n", value); + pointerChange(&value); + printf("After pointerChange: %d\n", value); // value changed + + return 0; +} diff --git a/src/main2 b/src/main2 Binary files differnew file mode 100755 index 0000000..c35260e --- /dev/null +++ b/src/main2 diff --git a/src/main2.c b/src/main2.c new file mode 100644 index 0000000..c63efdb --- /dev/null +++ b/src/main2.c @@ -0,0 +1,6 @@ +#include <stdio.h> + +main() +{ + printf("Hello C\n"); +} diff --git a/src/shift b/src/shift Binary files differnew file mode 100755 index 0000000..6429b03 --- /dev/null +++ b/src/shift diff --git a/src/shift.c b/src/shift.c new file mode 100644 index 0000000..c65b815 --- /dev/null +++ b/src/shift.c @@ -0,0 +1,32 @@ +#include <stdio.h> + +void print_binary(unsigned char value) { + for (int i = 7; i >= 0; i--) { + printf("%d", (value >> i) & 1); + } + printf("\n"); +} + +int main() { + unsigned char value = 0; // start with all bits off + int bit; + + printf("Starting value: "); + print_binary(value); + + while (1) { + printf("Enter bit to toggle (0-7), or -1 to quit: "); + if (scanf("%d", &bit) != 1) break; + if (bit == -1) break; + if (bit < 0 || bit > 7) { + printf("Invalid bit.\n"); + continue; + } + + value ^= (1 << bit); // toggle the chosen bit + printf("Current value: "); + print_binary(value); + } + + return 0; +} |