c

References for implementing calendar functionality in an embedded system?

I have an embedded system that currently keeps track of seconds until an event is supposed to occur using a real-time clock driven by a watch crystal. Now it needs to keep track of the actual date and time. So, I need to be able to calculate the day, month, year, hour, minute and second from a start date/time and offset in seconds. C...

c function merge help

I have two functions: void free_this(THIS *this) { THIS *this_tmp; while (this_tmp = this) { if (this->str) free(this->str); this = this_tmp->next; free(this_tmp); } } void free_that(THAT *that) { THAT *that_tmp; while (that_tmp = that) { if (that->id) ...

How much does the order of case labels affect the efficiency of switch statements?

Consider: if (condition1) { // Code block 1 } else { // Code block 2 } If I know that condition1 will be true the majority of the time, then I should code the logic as written, instead of: if (!condition1) { // Code block 2 } else { // Code block 1 } since I will avoid the penalty of the jump to the second code block ...

Quickest way to insert into SQLite using C API?

I want to do a lot of inserts (let's say a couple of million) into a database as quick as possible. Since I'm calling from C I thought that there might be a shortcut for doing this in the API. I can't help but feel that creating strings from data and having SQLite parse the strings back, all in one call, is less efficient than it could b...

Portable equivalent to gcc's __attribute__(cleanup)

Recently I came across a gcc extension that I have found rather useful: __attribute__(cleanup) Basically, this allows you to assign a cleanup call to a local variable at the time it exits scope. For instance, given the following section of code, all memory must be maintained and handled explicitly in any and all cases within the call to...

Force Whitespace in C Macro?

I've got the following Macro to define a new function following a special naming scheme: #define CREATE_HOOK_STUB( func ) void ##func_STUB() { /* some code*/ } However, the preprocessor always concatenates void and ##func_STUB but I obviously want it to preserve the whitespace at that position. I know that I could just prepend some s...

How can I understand that the user pressed which keyword?

I am coding a terminal like Linux terminal with C under Linux OS and I need to quit program when the user presses ctrl+D keywords. But I don't know how to understand that the user pressed these keywords. Thanks for your helping. I'm getting inputs with fgets() ...

UDP Sockets in C

I'm working on a homework problem for class. I want to start a UDP Server that listens for a file request. It opens the file and sends it back to the requesting client with UDP. Heres the server code. // Create UDP Socket if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("Can't create socket"); exit(-1); ...

How Does PHP's main.c Start Execution

I was poking around the PHP 5.3.1 source tree, and decided to take a look at main.c. I was curious what was happening behind the scenes whenever PHP runs. I was under the impression that any C or C++ program starts execution in a function named main, but I don't see a function with that name in main.c. Where does PHP code actually sta...

Whats wrong with my UDP Client Server?

I have a UDP Server that should sit and wait for a client to connect to it, and send it a string of a filename. Right now I just want it to echo back the filename to the client. Here is my code Server #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/s...

file lock leases via NFS v4 in C

does anybody know how to use the fancy file locking features of NFS v4? (described in e.g. About the NFS protocol (scroll down)). supposedly NFS v4 supports file lock leasing with a 45 second lifetime. I would like to believe that the linux kernel (I'm using gentoo 2.6.30) happily takes care of these details, and I can use fcntl() and it...

Which most common extensions are there (or used) to ANSI C?

You can put a link to comparison matrix or lists of extensions available to main compilers. If none of this is available, you could write a list of extension you use or like in your favorite compiler. ...

traverse a Binary search tree

I am trying to do a preorder traverse ...

Pointers in c (how to point to the first char in a string with a pointer pointing somewhere else in the same string)

If I have a pointer that is pointing somewhere in a string, let's say it is pointing at the third letter (we do not know the letter position, basically we don't know it is the third letter), and we want it to point back to the first letter so we can make the string to be NULL how do we do that? For example: if we have ascii as a pointe...

Concatenating Variable Names in C?

Is it possible to concatenate variable names in C? Specifically, I have a struct that contains 6 similar variables in it called class1, class2, class3, etc. I want to run through a for loop to assign each variable a value, but I can't see how to do it without somehow concatenating the variable name with the value of the for loop counter...

Using GTK/GDK to get Pango-rendered pixels

I'm trying to get a bitmap rendered by Pango with a given character, in this case the letter "T". I thought this should work, but it just prints out garbage.. In this code, gtk_window is already initialized as a GTK window. int width, height; PangoLayout *layout = gtk_widget_create_pango_layout(gtk_window, "T"); PangoFontDescription *...

how do I test the format of a string?

Hi, Im having trouble matching the format of a string, the format is that of the .srt timing which is "00:00:01,000 --> 00:00:04,000", hour,minutes,seconds,mili seconds. how do I match this in a string? Im trying to add and subtract the time here. ...

How to draw the graph in framebuffer using c language..?

Hi friends, i am new to this linux framebuffer so do anybody guide me to draw the line-graph in framebuffer. And i have the code to draw graph in turbo c but now in linux. So please help me out. Thank You, Rahul ...

Is this undefined?

Well, I'm not really in serious need of this answer, I am just inquisitive. Expressions like *ptr++ = a are perfectly valid since we are operating on two objects ptr and *ptr but if i write *ptr++ = *ptr + a is it still valid ? For example consider the following snippet: int main(void){ int a[] = {5,7,8,9,2}; int* p =a; *p+...

Logical value of an assignment in C

while (curr_data[1] != (unsigned int)NULL && ((curr_ptr = (void*)curr_data[1]) || 1)) Two part question. What will (curr_ptr = (void*)curr_data[1]) evaluate to, logically. TRUE? Also, I know its rather hack-ish, but is the while statement legal C? I would have to go through great contortions to put the assignment elsewhere in the...