c

C printf using %d and %f

I was working on this program and I noticed that using %f for a double and %d for a float gives me something completely different. Anybody knows why this happens? int main () { float a = 1F; double b = 1; printf("float =%d\ndouble= %f", a, b); } This is the output float = -1610612736 double = 1903598371927661359216126713647498937...

How to determine how many bytes an integer needs?

I'm looking for the most efficient way to calculate the minimum number of bytes needed to store an integer without losing precision. e.g. int: 10 = 1 byte int: 257 = 2 bytes; int: 18446744073709551615 (UINT64_MAX) = 8 bytes; Thanks P.S. This is for a hash functions which will be called many millions of times Also the byte sizes do...

C++ for Wireless Sensor Networks

Similar to: Why are RTOS only coded in C, but: Besides the numerous myths about C++, why is it not used as much as C/nesC (TinyOS) for WSN? Knowing C++ can be used for Simulating Wireless Sensor Networks with OMNeT++ it is hard not to think that it can also be used in real-time embedded systems as C is to accomplish event handling. I ...

What are the possible reasons the system() function can not find the executable?

if( system("tail -500 log.txt") == -1) { //Error calling tail.exe on log //errno is a system macro that expands int returning //the last error. strerror() converts the error to it's //corresponding error message. printf("Error calling tail.exe with system(): %s",strerror( errno )); } System() is c...

Win32 API timers....

I was using the system timer (clock() function, see time.h) to time some serial and USB comms. All I needed was approx 1ms accurace. The first thing I noticed is that individual times can be out (plus or minus) 10ms. Timing a number of smaller events led to less accurate timing as events went by. Aggregate timing was slightly better. Aft...

Code readability vs conciseness

What are the merits and demerits of the following two code snippets: return n==0 ? 0 : n==1 ? 1 : fib(n-1) + fib(n-2); and if(n==0) return 0; if(n==1) return 1; return fib(n-1) + fib(n-2); for calculating the nth letter in the Fibonacci sequence? Which one would you favour and why? ...

Trimming a string in C (without using library functions)

Hi, i was hoping for some help, opposed to full solutions and i have an idea that i need to know what i am doing wrong when trying to implement basically i am trying to remove spaces from the end of an array of characters in c this is what i am doing i have a method to work out the length of the string and store it in to an int i se...

how to use setuid() from root to become user, with the possiblity of becoming root again later ?

Hello all, I'm trying to do the safe thing, and have a program that needs to runs as root to drop its privileges when it doesn't need them. This works well if I chmod my binary with the SUID bit, and make it belong to root, as now I have UID = some user, and EUID = root, so I can use seteuid(0) and seteuid(getuid()) to respectively rais...

Stuck with C syntax

I am trying to remove spaces from the end of a char array (string). This is the pseudo code of what I am doing, but it keeps deleting the whole string: if(string length - 1 != a space) return Otherwise, it must equal a space, so while *mypointer-- != a space //This should loop back to where there is a character. Outside of the...

passing statically allocated 2D arrays as function arguments in C

Hi All, Consider this code: #include <stdio.h> #define N 5 void printMatrix(int (*matrix)[N],int n) { int i,j; for(i=0;i<n;i++){ for(j=0;j<n;j++) printf("%d",matrix[i][j]); printf("\n"); } } int main() { int R[N][N]={{1,2,3},{4,5,6},{7,8,9}}; printMatrix(R,3); } This works fine as expected. Now, I...

What should COMSPEC and PATH environmental variables be to locate the command-interpreter, using system()?

Are the OS (XP) environmental variables the same used in a process running from visual studio .NET C++? It seems the command interpreter is not found: When using NULL as the command, system() returns 0 and with command - ENOENT Command interpreter cannot be found. In windows (System->Environmental Variables), COMSPEC contains the pa...

Parsing JSON using C?

There are several C libraries available for parsing JSON, that will compile on Linux. Which library would you recommend? ...

How to use structure in a structure?

Hi! This coulde be a dumm question, but i can't figure it out what i am doing wrong( i haven't used two structures in each other ). I have two structures: struct test { struct ddata* difference; int diff; }; struct test *MSG; struct ddata { char *filename; char *size; }; struct ddata *difference And i want to giv...

After casting pParam, why do I get random characters back?

Hi! This is the first time that i want to use threads, so i don't understand them fully for now. I have two structures: struct ddata //difference content { char *filename; char *size; }; struct ddata *difference = (struct ddata *) malloc( dif * sizeof *difference ); struct test { struct ddata* difference; int diff; ...

Unix makefile errors " 'ake: Fatal error: Don't know how to make (c file here)"

I've written the below makefile: hw2p1: hw2p1_main.o hw2p1_getit.o hw2p1_parseit.o hw2p1_moveit.o hw2p1_showit.o gcc hw2p1_main.o hw2p1_getit.o hw2p1_parseit.o hw2p1_moveit.o hw2p1_showit.o hw2p1_main.o: hw2p1_main.c gcc -c hw2p1_main.c hw2p1_getit.o: hw2p1_getit.c gcc -c hw2p1_getit.c hw2p1_parseit.o: hw2p1_parseit.c gc...

Are there any low-level languages that can be used in place of scripts?

I am a "high-level" scripting guy. All my code is Class-based PHP or JavaScript. However, I want to know if there is any form of useful interpreter projects for "low-level" compiled languages like C or C++ (strange sounding huh?). This all came about when I stumbled upon http://g-wan.com/ and was fascinated by the fact that you could se...

OpenCV Image Processing -- C++ vs C vs Python

I was thinking of trying OpenCV for a project and noticed that it had C, C++ and Python. I am trying to figure out whether I should use C++, C or Python -- and would like to use whatever has the best OpenCV support. Just from looking at the index page for the various documentation it looks like the C++ bindings might have more featur...

getting rid of sscanf() warnings

Hi! I'm reading MAC addresses (in standard hex notation, e.g. 00:11:22:33:44:55) from stdin and converting them into a 6 byte variable hw_addr as decimals: u8 hw_addr[6]; scanf("%2x:%2x:%2x:%2x:%2x:%2x", &hw_addr[0], &hw_addr[1], &hw_addr[2], &hw_addr[3], &hw_addr[4], &hw_addr[5]); The only problem is that I'm getting 6 scanf warning...

Rotating an image in C/C++

I need code for rotating an image in C++ which functions like imrotate function of matlab. Please suggest a good link. Or if someone can provide the code for imrotate. Or at least please explain the algorithm. Its not a homework. I need this code for my project. And we can use any external library or code. ...

Update file using c

I am using fprintf command for writing on the text file, but every time when the function called its remove the previous data, I actually want to update the file so that the previous data remain. Kindly guide me how I can make it possible? ...