M

@mofe

permutation

C
4 years ago
#include <stdio.h> #include <string.h> #include <stdlib.h> void printArr(char *str, int len){ printf("\n==\n"); for(int i = 0; i < len; i++){ printf("%c %d\n", str[i], str[i]); } printf("==\n\n");

Coding Exercise - Ticket Office

C
4 years ago
#include <stdio.h> #include <stdbool.h> #include <stdlib.h> double ticketPrice(int age, double price, bool welfare){ if(age < 0){ fprintf(stderr, "Age cannot less than 0!"); exit(EXIT_FAILURE); } else if(age < 11){ return 0;

thread challenge 3 mutexes condition variable

C
4 years ago
#include<stdio.h> #include<pthread.h> #include<unistd.h> #define NTHREADS 10 pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t evens_done = PTHREAD_COND_INITIALIZER; int number_evens_finished = 0;

thread challenge 2 mutex_lock

C
4 years ago
#include<stdio.h> #include<pthread.h> #define NTHREADS 10 pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; int counter = 0; void* func1(void* data) {

thread challenge 1

C
4 years ago
#include<stdio.h> #include<pthread.h> #define NTHREADS 10 int counter = 0; void* func1(void* data) { int *x = (int *) data;

signal game

C
4 years ago
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <time.h> #include <string.h> #include <errno.h> #include <signal.h> int score = 0;

divide-by-zero

C
4 years ago
#include <signal.h> #include <stdio.h> #include <stdlib.h> void handler_divideByZero(int signum); int main(void) { int result = 0; int v1 = 0, v2 = 0; void (*sigHandlerReturn)(int);

dynamic loading library https://man7.org/linux/man-pages/man0/math.h.0p.html

C
4 years ago
#include <stdio.h> #include <stdlib.h> #include <dlfcn.h> #include <gnu/lib-names.h> /* Defines LIBM_SO (which will be a string such as "libm.so.6") */ int main(void) { void *handle; double (*cosine)(double);

dynamic loading library

C
4 years ago
#include <stdio.h> #include <stdlib.h> #include <dlfcn.h> #include <gnu/lib-names.h> int main() { void *handle = NULL; double (*cosine)(double) = NULL; char *error = NULL;

function pointer

C
4 years ago
#include <stdio.h> #include <string.h> #include <malloc.h> int array1[] = {10,20,30,40,50,60,70,80,90,100}; int array2[] = { 38, 27, 87, 63, 59, 223, 132, 1, 19, 7 }; int add(int a, int b) { return a + b; }

DEBUG FLAG

C
4 years ago
#include <stdlib.h> #include <stdio.h> int sum(int x, int y, int z) { char c = 2; int *a = NULL; #ifdef DEBUG fprintf(stderr, "x=%d\n", x); fprintf(stderr, "y=%d\n", y);

MACRO SUM

C
4 years ago
#include <stdio.h> #define SUM(x, y) (x+y) int main() { printf("%d\n", SUM(1, 2)); return 0; }

predefined macro

C
4 years ago
#include <stdio.h> int main() { printf("%s %s %s\n", __DATE__, __TIME__, __func__); printf("%s %d\n", __FILE__, __LINE__); printf("%d\n", __STDC__); return 0; }

macros

C
4 years ago
#include <stdio.h> #define PRNT(a, b) \ printf("value 1 = %d\n", a); \ printf("value 2 = %d\n", b); int main() { int x = 2; int y = 3;

union test

C
4 years ago
#include<stdio.h> union p { int x; char y; }; int main() { union p b; b.y = 60; b.x = 12;

string reverse

C
4 years ago
#include<stdio.h> char * reverse(char * str); int main() { char str[100]; char *rev = NULL; printf("Enter the string: ");

reverse char without static variable

C
4 years ago
#include <stdio.h> #include <stdlib.h> #include <string.h> char* reverse(const char* str) { char *_str = strdup(str); // free!! unsigned long len = strlen(_str); if(len == 1){ return _str;

gcd

C
4 years ago
#include <stdio.h> int gcd(int x, int y); int main() { printf("gcd = %d\n", gcd(12, 15)); return 0; }

value copy

C
4 years ago
#include <stdio.h> void func(int x); int main() { int x = 10; printf("value => %d\n", x); printf("pointer address => %p\n", &x); func(x); //printf("Hello world!\n");

getline

C
4 years ago
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { // Read lines using POSIX function getline // This code won't work on Windows char *line = NULL; size_t len = 0;