c

Difference between scanf() and strtol() / strtod() in parsing numbers

Note: I completely reworked the question to more properly reflect what I am setting the bounty for. Please excuse any inconsistencies with already-given answers this might have created. I did not want to create a new question, as previous answers to this one might be helpful. I am working on implementing a C standard library, and am c...

Pre-preprocessor

Hi, I want to have a C pre-preprocessor which is filtering some #define statements from the sourcecode without changing anything else. Why? This should be used to remove some client specific code from the sources if the source is handed out to another client. Does anyone know of an existing solution? Thanks! Simon ...

What to read after Kernighan-Ritchie?

I have finished K&R book but I want to learn some more advanced things about C. Which book do you suggest me to read next? Something with data structures? Let me say that I have already read Sedgewick's "Data Structures in Java", if that helps. ...

Calling Py_Finalize() from C

This is a follow up to http://stackoverflow.com/questions/1417473/call-python-from-c At the startup of the programm I call the following function to initialize the interpreter: void initPython(){ PyEval_InitThreads(); Py_Initialize(); PyEval_ReleaseLock(); } Every thread creates it's own data structure and acquires the lo...

C for R programmers - recommended resources/approaches once past the basics

I would like to improve my C skills in order to be more competent at converting R code to C where this would be useful. What hints do people have that will help me on my way? Background: I followed an online Intro to C course a few years ago and that plus Writing R Extensions and S Programming (Venables & Ripley) enabled me to convert b...

assignment of two values in parentheses in C

What does this piece of code in C do: p = (1, 2.1); What do we know about p? ...

Any function to query the size of an allocated block?

I realize that any such function is likely to be non standard, but that's ok for my use case. Basically, I need a method (even if it's only exposed through glibc's syscall() interface) that I can pass a pointer to (a pointer that was returned by a previous call to malloc()) that returns the size of the block the pointer points at. Does s...

Detecting EOF in C

Hi, I am using the following code in C to take input from user until EOF occur, but problem is this code is not working, it terminate after taking first input. Anyone can tell me whats wrong with this code. Thanks in Advance. float input; printf("Input No: "); scanf("%f", &input); while(!EOF) { printf("Output: %f"...

Getting CPU time in OS X

I have an objective-c application for OS X that compares two sqlite DB's and produces a diff in json format. The db are quite large (10,000 items with many fields). Sometimes this applications runs in about 55 sec(using 95% of the cpu). Sometimes it takes around 8 min (using 12% of the cpu). This is with the same DB's. When it is only us...

File Pointer Position

Here is the snippet of code typedef struct { double testA; double testB[500]; bool isProcessed; } MYSTURCT; I have a binary file which is written with multiple structs of type "myStruct". Now, in another function, I m trying to read the file and update in the middle. void test() { FILE* fp = fopen (testFile, "r+") ...

A C preprocessor macro to pack bitfields into a byte?

I'm getting into microcontroller hacking and while I'm very comfortable with bitwise operators and talking right to the hardware, I'm finding the resulting code very verbose and boilerplate. The higher level programmer in me wants to find an effective but efficient way to clean it up. For instance, there's a lot of setting flags in reg...

How to cast sockaddr_storage and avoid breaking strict-aliasing rules

I'm using Beej's Guide to Networking and came across an aliasing issue. He proposes a function to return either the IPv4 or IPv6 address of a particular struct: 1 void *get_in_addr( struct sockaddr *sa ) 2 { 3 if (sa->sa_family == AF_INET) 4 return &(((struct sockaddr_in*)sa)->sin_addr); 5 else 6 return &(((st...

What is the function of the ~ operator?

Unfortunately, search engines have failed me using this query. For instance: int foo = ~bar; ...

why doesn't gcc like this

Greetings I am trying to learn C and finding myself getting stuck a lot, no complains : ) Anyway, I wrote a program and gcc does not like it, the following code is NOT the program but demonstrate the problem: #define MAXLINE = 1000 int main() { int tmp = MAXLINE; char line[MAXLINE]; return 0; } when compile, I get the fol...

How to build an Array with Bison/Yacc and a Recursive Rule

With Bison, I figured out how to get everything into one long string as follows: arg_list: WORD arg_list { strcat( $1, "IFS" ); $$ = strcat($1, $2); } | WORD ; and: WORD arg_list { printf("%s, %s\n", $1, $2); } But the problem is that I will then have to split up $2 in the second rule again to parse it. Is there a way...

Bringing libcurl into a C++ program

I'm trying to pull libcurl into a large C++ project. However I am having trouble getting it to compile. I see errors coming from ws2def.h, winsock2.h, and ws2tcpip.h Some of the errors look like this: error C2061: syntax error : identifier 'iSockaddrLength' ws2def.h 225 error C3646: 'LPSOCKADDR' : unknown override specifier ws2...

More efficient: large array or many scalars

Working in an embedded (PIC) environment, programming in c. I have to keep track of 1000 values for a variable (history) and return the moving average of those values. I'm just wondering if it will be more efficient in terms of speed, ROM and RAM usage if I use an array or 1000 16bit variables. Is there a difinitive answer to that? O...

Given a start, end, and increment value, I want an algorithm that counts up and down.

Ok, I have a control that want to fade in and out (continuously). In order to fade it I adjust the transparency value of the control in the drawing routine. So I set up a timer that runs and I adjust the m_transparency member. The timer runs and it should sweeps back and forth between the two defined values m_start, m_end. These can be 0...

c read a file's permissions

I have a relatively trivial question. How can I check if a file has read permissions in C? thanks ...

Include struct in the %union def with Bison/Yacc

I am trying to include a struct as part of the union with Bison, but I get an error on the 'struct node args' in %union: parser.y:17: error: field ‘args’ has incomplete type The Code: struct node { char * val; struct node * next; }; %} %union { char * string; struct node args; } %token <string> CD WORD PWD EXIT ...