Loading...
More C Posts
#include <stdio.h>#include <stdlib.h>// give an array of identical coins (ints), represented by 1's and one 2// we can find the heavy coin (2) by summing the contents of the thirdsint sum_subarr(int arr[], int l, int r){int sum = 0;for(int i = l; i <= r; i++){sum += arr[i];}return sum;}int heavy_coin(int arr[], int l, int r){if(r == l){return r;}int midl = l + (r - l)/3;int midr = r - (r - l)/3;int lw = sum_subarr(arr, l, midl);int mw = sum_subarr(arr, midl + 1, midr);if ( lw > mw ){return heavy_coin(arr, l, midl);}else if ( lw < mw ){return heavy_coin(arr, midl + 1, midr);}else{return heavy_coin(arr, midr + 1, r);}}int main(int argc, char *argv[]){int arr[] = {1,1,1,1,2,1,1,1,1};int l = 0;int r = 8;int res = -1;res = heavy_coin(arr, l, r);printf("Hevy coin is at index: %d\n", res);return EXIT_SUCCESS;}
// compile: gcc socketTCPClient.c -o socketTCPClient.out// usage : ./socketTCPClient.out port// Doesn't send anything, just connects and prints out the response from the server#include <sys/socket.h>#include <sys/types.h>#include <netinet/in.h>#include <netdb.h>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <unistd.h>#include <errno.h>#include <arpa/inet.h>int main(int argc, char *argv[]){int sockfd = 0, n = 0, portno;char recvBuff[1025];struct sockaddr_in serv_addr;memset(recvBuff, '0', sizeof(recvBuff));if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){printf("socket error\n");exit(EXIT_FAILURE);}serv_addr.sin_family = AF_INET;portno = atoi(argv[1]);serv_addr.sin_port = htons(portno);serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");if(connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0){printf("connect error\n");exit(EXIT_FAILURE);}while((n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0){recvBuff[n] = 0;if (fputs(recvBuff, stdout) == EOF){printf("fputs error\n");}}if(n < 0){printf("read error\n");}return 0;}
#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 */
#include <stdio.h>#include <sys/types.h>#include <unistd.h>int main() {pid_t pid; /* could be int */int i;pid = fork();printf("PID=%d\n", pid);if (pid > 0) {/* parent */for (i = 0; i < 10; i++)printf("\t\t\tPARENT %d\n", i);} else {/* child */for (i = 0; i < 10; i++)printf("CHILD %d\n", i);}return 0;}
#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}
// 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 textfloat 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 hackingi = 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 removedreturn y;}