• Nov 18, 2022 •AustinLeath
0 likes • 0 views
#include <stdio.h> #include <stdlib.h> int main(){ int * int_ptr; int_ptr = (int *)malloc(2*sizeof(int)); if(!int_ptr) { printf("Something went wrong while allocating memory. Exiting..."); exit(1); } printf("Enter first integer: "); scanf("%i", &int_ptr[0]); printf("Enter second integer: "); scanf("%i", &int_ptr[1]); printf("Original values: 1st = %i 2nd = %i\n",int_ptr[0], int_ptr[1]); int_ptr[0] = int_ptr[0] ^ int_ptr[1]; //printf("%i\n", int_ptr[0]); int_ptr[1] = int_ptr[0] ^ int_ptr[1]; //printf("%i\n", int_ptr[1]); int_ptr[0] = int_ptr[0] ^ int_ptr[1]; //printf("%i\n", int_ptr[0]); printf("Swapped values: 1st = %i 2nd = %i\n", int_ptr[0], int_ptr[1]); free(int_ptr); exit(0); }
• Nov 19, 2022 •CodeCatch
// The following code is the fast inverse square root implementation from Quake III Arena // this code has been stripped of C preprocessor directives, but includes the exact original comment text float Q_rsqrt( float number ) { long i; float x2, y; const float threehalfs = 1.5F; x2 = number * 0.5F; y = number; i = * ( long * ) &y; // evil floating point bit level hacking i = 0x5f3759df - ( i >> 1 ); // what the fuck? y = * ( float * ) &i; y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration // y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed return y; }
#include <stdio.h> #include <string.h> #include <unistd.h> #define READ 0 /* The index of the read end of the pipe */ #define WRITE 1 /* The index of the write end of the pipe */ char * phrase = "This goes in the pipe"; int main() { int fd[2], bytesRead; char message[100]; /* Parent process' message buffer */ pipe(fd); /* Create unnamed pipe */ if (fork() == 0) /* Child, writer */ { close(fd[READ]); /* Close unused end */ write(fd[WRITE], phrase, strlen(phrase) + 1); /* Include NULL */ close(fd[WRITE]); /* Close used end */ } else /* Parent, reader */ { close(fd[WRITE]); /* Close unused end */ bytesRead = read(fd[READ], message, 100); printf("Parent just read %i bytes: %s\n", bytesRead, message); close(fd[READ]); /* Close used end */ } }
#include <stdio.h> #include <assert.h> #include <signal.h> void myHandler(int iSig) { printf("In myHandler with argument %d\n", iSig); } int main() { void( * pfRet)(int) = signal(SIGINT, myHandler); assert(pfRet != SIG_ERR); printf("Entering an infinite loop\n"); while (1) { printf("."); } return 0; // use CTRL+\ to exit }
• May 13, 2025 •LeifMessinger
0 likes • 2 views
#include <stdio.h> #include <stdlib.h> calculate(a, b) int a; int b; { printf("%d", a+b); } main(argc, argv) int argc; char** argv; { if(argc < 3){ return 0; } calculate(atoi(argv[1]), atoi(argv[2])); }
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> int main() { char * cmd[] = { "who", "ls", "date" }; int i; while (1) { printf("0=who 1=ls 2=date : "); scanf("%d", & i); if (fork() == 0) { /* child */ execlp(cmd[i], cmd[i], (char * ) 0); printf("execlp failed\n"); exit(1); } else { /* parent */ wait((int * ) 0); printf("child finished\n"); } } /* while */ } /* main */