c

Can anyone see what is wrong with this (time related functions in C)

#include <stdio.h> #include <stdlib.h> #include <time.h> static struct tm createDate(unsigned day, unsigned mon, int year) { struct tm b = {0,0,0,day,mon-1,year-1900}; return b; } static int dateExceeded(unsigned day, unsigned mon, int year) { struct tm b = createDate(day,mon,year); time_t y = mktime(&b), now; tim...

How to start debugging?

I have learned C and I would like to start to improve open source software. I would like to hack away one irritating bug in GTK+. To see the bug I need to use Gedit. How can I download the sources of GTK+ and gedit and compile both of them so that I can see where the bug is? And I have never used any debugger in Linux so is there somewh...

Difference between scanf() and fgets().

I want to know what is the difference between fgets() and scanf(). I am using C as my platform. ...

How to read a text file upto certain position in C?

I am passing a string as an argument to my program and extracting its position in a text file. Can we read a text file only upto this certain position in C?? If yes, then please tell me how. ...

Is this an idiomatic C way to convert longs to a binary (char *) representation?

The question is in the title I guess. This is the temporary solution I came up with but I was wondering: If there are disadvantages to representing binary as char*. Is there a better way (considering i would want the ability of bit-shifting etc...) If there is obvious non-idiomatic C (or other errors) in the code below. All suggesti...

About fork system call and global variables

I have this program in C++ that forks two new processes: #include <pthread.h> #include <iostream> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include <cstdlib> using namespace std; int shared; void func(){ extern int shared; for (int i=0; i<10;i++) shared++; cout<<"Process "<<getpid()<<", shared " ...

C's "bad" functions vs. their "good" alternatives

What are the "bad" functions in C, and what are their "good" alternatives? Why are the bad ones bad, and what makes the good ones better? I know, for example, gets() is "bad" because it doesn't have any form of bounds checking. What is its better alternative? fgets()? I've heard scanf() is bad but I can't remember why. Anyone know? Wh...

Creating a cout function in C?

This is a totally hypothetical question, but I have to know the answer. I assume most C++ compilers are written in assembly. Which makes them different languages entirely (I could be wrong). That being said if I were going to create a cout style function for plain old C, how would I do it? cout has some very impressive features take thi...

How does scanf() work inside the OS?

I've been wondering how scanf()/printf() actually works in the hardware and OS levels. Where does the data flow and what exactly is the OS doing around these times? What calls does the OS make? And so on... ...

Why null-terminated strings? Or: null-terminated vs. characters + length storage.

I'm writing a language interpreter in C, and my string type contains a length attribute, like so: struct String { char* characters; size_t length; }; Because of this, I have to spend a lot of time in my interpreter handling this kind of string manually since C doesn't include built-in support for it. I've considered switching...

Why do round() and ceil() not return an integer ?

Once in a while, I find myself rounding some numbers, and I always have to cast the result to an integer : int rounded = (int) floor(value); Why do all rounding functions (ceil(), floor()) return a floating number, and not an integer ? I find this pretty non-intuitive, and would love to have some explanations ! ...

C pre-processor defining for generated function names

I have a situation where I have quite a few generated functions, and would like to point them at some generic functions that I have created (to allow me to reuse the base code when the generated function names change). Essentially, I have a list of function names as follows: void Callback_SignalName1(void); void Callback_SignalName2(vo...

Printing Hex Floating Point Constants from Array of Bytes

How can a hexadecimal floating point constant, as specified in C99, be printed from a array of bytes representing the machine representation of a floating point value? e.g. given union u_double { double dbl; char data[sizeof(double)]; }; An example hexadecimal floating point constant is a string of the form 0x1.FFFFFEp1...

In a WPF tabControl how can I dynamically select a tab page from an object's type?

I've implemented the MVVM pattern and have some viewModels that are bound to tab pages on a tab control. When a specific object type changes (i.e. from Car myVehical, to Bike myVehical), then i want the relevant tab page to become selected. Thanks. ...

What's the proper use of printf to display pointers padded with 0s

In C, I'd like to use printf to display pointers, and so that they line up properly, I'd like to pad them with 0s. My guess was that the proper way to do this was: printf("%016p", ptr); This works, but this gcc complains with the following message: warning: '0' flag used with ‘%p’ gnu_printf format I've googled a bit for it, and the...

What are the important notions in C that you did not learn from your teachers

Hi In September, I will give my first lectures on C to students in engineering school (usually I teach math and signal processing, but I have also done a lot of practical work in C, without giving the lectures). Computer science is not their main topic (they are more studying electronics and signal processing), but they need to have a g...

I cant find any documentation for g_io_channel_win32_make_pollfd

Is there a documentation available for g_io_channel_win32_make_pollfd I want to use this function to create FDs on windows for IPC between main thread and the separate thread. It is only briefly mentioned here and doesn't really explain how to use it. I really need an example. thx ...

Default argument promotions in C function calls

Setup I have a few questions about the default argument promotions when calling a function in C. Here's section 6.5.2.2 "Function calls" Paragraphs 6, 7, and 8 from the C99 standard (pdf) (emphasis added and broken into lists for ease of reading): Paragraph 6 If the expression that denotes the called function has a type that do...

Address of register variable

In C, we cannot use & to find out the address of a register variable but in C++ we can do the same. Why is it legal in C++ but not in C? Can someone please explain this concept in-depth. Thanks, Naveen ...

How to efficiently merge two hashes in Ruby C API?

I am writing a C extension for Ruby that really needs to merge two hashes, however the rb_hash_merge() function is STATIC in Ruby 1.8.6. I have tried instead to use: rb_funcall(hash1, rb_intern("merge"), 1, hash2); but this is much too slow, and performance is very critical in this application. Does anyone know how to go about perf...