c

Fast Euclidean division in C

I am interested in getting the remainder of the Euclidean division, that is, for a pair of integers (i, n), find r such as: i = k * n + r, 0 <= r < |k| the simple solution is: int euc(int i, int n) { int r; r = i % n; if ( r < 0) { r += n; } return r; } But since I need to execute this tens of million o...

What happens when float pointer is typecasted to char pointer?

int main() { float f = 12.2; char *p1; p1 = (char *)&f; printf ("%d", *p1); } This outputs 51. ...

Please tell me why this condition is always true

enum bool{true,false}; void main() { if(true==(2==3)) { printf("true\n"); } else { printf("false\n"); } return 0; } ...

Escaping a # symbol in a #define macro?

OK, without going into the gory details I want to use a #define macro that will expand to a #include but the '#' sign is confusing the preprocessor (as it thinks I want to quote an argument.) For example, I want to do something like this: #define MACRO(name) #include "name##foo" And use it thus: MACRO(Test) Which will expand to: ...

MySQL server has gone away Error while Selecting rows from a table

Using a C program with Mysql C API connected to a database and the initiation was successful but tried to do SELECT ,INSERT operations it throws an Error " MySQL server has gone away" on my VPS Server but the same works fine locally. The VPS Server Mysql Server version is : 5.0.81-community MySQL Community Edition (GPL) . wait...

How have your ideas about C programming practices changed in the last ten years?

Object-Oriented programmers seem to have all the fun. Not only are they treated to major framework revisions every two years, and new and Improved languages every five, they also get to deal with design practices tailor-made to their programming style. From test-driven development to design patterns, Object-Oriented programmers have a ...

Assigning value in while loop condition

I found this piece of code on Wikipedia. #include <stdio.h> int main(void) { int c; while (c = getchar(), c != EOF && c != 'x') { switch (c) { case '\n': case '\r': printf ("Newline\n"); break; default: printf ("%c",c); } } return 0; } I'm curious about expression ...

A portable way of getting the nr of processing units (# cpu,cores) in c?

Is there a way to get the nr of processing units like cpus or cores in a system? I am going to write an app with pthreads so if there is a posix call that would be great. I know about reading from /proc/cpuinfo but that is not so portable. Is there another more portable way? ...

How would you declare a two dimensional array of pointers in C?

...without using typedef. My boss claims he was once asked this in an interview, and when he gave his answer the interviewers told him he couldn't use typedefs because it was poor style. Regardless, he likes to throw the question out at people just to see if they can get it right , usually at new programmer lunches. No one ever gets ...

How do i get started with CUDA development on UBUNTU 9.04?

How do i get started with CUDA development on Ubuntu 9.04? Are there any prebuilt binaries? Are the default accelerated drivers sufficient? My thought is to actually work with OpenCL but that seems to be hard to do right now so i thought that i would start with CUDA and then port my application to OpenCL when that is more readily avail...

Open-source pdf library in c

Hi folks, I am currently looking for open-source pdf generation (and manipulation) library written in C. I found libHaru which seems to be quite nice. I actually do not need all the features of pdf and libHaru seems to be very low-level. Are there any good alternatives you could recommend and that are maybe a little bit more abstract? ...

Pthread and wait conditions

I'm learning pthread and wait conditions. As far as I can tell a typical waiting thread is like this: pthread_mutex_lock(&m); while(!condition) pthread_cond_wait(&cond, &m); // Thread stuff here pthread_mutex_unlock(&m); What I can't understand is why the line while(!condition) is necessary even if I use pthread_cond_signal() to ...

How does the Levenberg–Marquardt algorithm work in detail but in an understandable way?

Im a programmer that wants to learn how the Levenberg–Marquardt curvefitting algorithm works so that i can implement it myself. Is there a good tutorial anywhere that can explain how it works in detail with the reader beeing a programmer and not a mathemagician. My goal is to implement this algorithm in opencl so that i can have it run ...

Log file rotation name

I have written this simple C program that changes the rotation file name. Eg: For A_B_C_1 Need to rotate it to A_B_C_2 and if A_B_C need to rotate it to A_B_C_1 Problem is the strncat is not working as expectedly. It is giving me output like: A_B_C_1 (Works fine for single instance) A_B_C_2 A_B_C_23 A_B_C_2324 Logic used is simple...

why does this code give a access violation when run on msdev ?

while(*a++ = *b++){} where a and b are valid char pointers. ...

What is the best library to manage a wiimote ?

There are many libraries that manage the wiimote but I am looking for the "best" one, or at least that has the following features: open-source portable (at least Win32 and Linux) written and usable in c or c++ good coverage of wiimote devices I rely on people that already used such library. Google is good source of information but it...

Displaying the #include hierarchy for a C++ file in Visual Studio

Problem: I have a large Visual C++ project that I'm trying to migrate to Visual Studio 2010. It's a huge mix of stuff from various sources and of various ages. I'm getting problems because something is including both winsock.h and winsock2.h. Question: What tools and techniques are there for displaying the #include hierarchy for a Vis...

Problem in fread and an EoF mark

For some reason, fread stoppes reading at a place in the file that contains 1A1A (raw bytes, ofcourse, not text). The file is open in "rb" mode, and it does skip 1A's, but not the double 1A. Any solution? ...

Understanding double pointer in doubly linked list in C

Hi, I have an exam tomorrow and I was trying to understand this doubly linked list example that the instructor placed on the class website but I'm having a hard time understanding a bit of it... Here's the code: #include <stdio.h> #include <stdlib.h> typedef struct dl { int key; float value; struct dl *next; struct dl...

In C on Unix, how can a process tell what permissions it has to a file without opening it?

I can use stat() to figure out what permissions the owner, group, or others have and I can use geteuid() and getpwuid() to get the user name of the process. I'm not quite sure how to get the groups a user belongs to without a system call though. Even knowing how to get the groups, it seems like a lot of work to integrate all of this ...