c

Bit shifts in C

If the bit pattern corresponding to a signed integer is shifted to the right then 1 vacant bit will be filled by the sign bit 2 vacant bit will be filled by 0 3 The outcome is implementation dependent 4 none of the above The answer to this question is 3rd option.. Can anybody explain this,, Also give some basic idea, abou...

Should a buffer of bytes be signed or unsigned char buffer?

Should a buffer of bytes be signed char or unsigned char or simply a char buffer? Any differences between C and C++? Thanks. ...

Reading Zend Engine API code: What does ## (double hash) means?

Out of curiousity, I'm reading the Zend Engine API code and encountered quite a number of ## in their #define's. For example, at /usr/lib/php5/Zend/zend_API.h: #define ZEND_FN(name) zif_##name #define ZEND_MN(name) zim_##name What does the ## (double hash) symbols mean in these two lines? ...

An example project for network-programming written in C/C++

Are there good open-source projects on P2P file-sharing systems or distributed file systems written in C/C++ ? I need a project to start with network-programming. Can anyone give me any suggestions? ...

Determining C executable name

When we are compiling a C program the output is stored in a.out. How can we redirect the compiled output to another file? ...

What are C macros useful for?

I have written a little bit of C, and I can read it well enough to get a general idea of what it is doing, but every time I have encountered a macro it has thrown me completely. I end up having to remember what the macro is a and substitute it in my head as I read. The ones that I have encountered that were intuitive and easy to understa...

Factorial in C without conditionals, loops and arithmetic operators

How can I find the factorial of a number (from 1 to 10) in C, without using: loop statements like for, while, and do while; conditional operators like if and case; and arithmetic operators like + , − , * , % , /, ++, −−? FYI: I found this question in C aptitude. ...

C++ Optimization Techniques

Is there a site that provides a great list of common C++ optimization practices? What I mean by optimization is that you have to modify the source code to be able to run a program faster, not changing the compiler settings. ...

Profiling programs written in C or C++

What would you suggest the best tool to profile C/C++ code and determine which parts are taking the most time. Currently, I'm just relying on logs but ofcourse the information is not accurate since unnecessary delays are introduced. Preferrably, the tool would also be able to detect/suggest areas which could be optimized, if such tool e...

NCurses initialization without clearing the screen.

I am writing a program that's similar to a shell. Once started up, there's a prompt and you enter in some app-specific commands. So far this works just fine. However, I want to add support for command history like in Bash, so the user could hit the up or down arrow and see previously entered commands. I have included the ncurses libr...

.o files vs .a files

What is the difference between these two file types. I see that my C++ app links against both types during the construction of the executable. How to build .a files? links, references, and especially examples, are highly appreciated. ...

What REALLY happens when you don't free after malloc?

This has been something that has bothered me for ages now. We are all taught in school (at least, I was) that you MUST free every pointer that is allocated. I'm a bit curious, though, about the real cost of not freeing memory. In some obvious cases, like when malloc is called inside a loop or part of a thread execution, it's very impo...

Convert a Static Library to a Shared Library

I have a third-party library which consists mainly of a large number of static (.a) library files. I can compile this into a single .a library file, but I really need it to be a single .so shared library file. Is there any way to convert a static .a file into a shared .so file? Or more generally is there a good way to combine a huge n...

Two's complement binary form

In a TC++ compiler, the binary representation of 5 is (00000000000000101). I know that negative numbers are stored as 2's complement, thus -5 in binary is (111111111111011). The most significant bit (sign bit) is 1 which tells that it is a negative number. So how does the compiler know that it is -5? If we interpret the binary value gi...

Meaning of gcc -O2

I see this flag a lot in the makefiles. What does it mean? and when should it be used? ...

How can I monitor traffic going through a pipe?

The situation I have is this: I'm redirecting input from one spot in my program to another through a pipe. However, it does not appear that this is working correctly, so I'd like to monitor what's going through the pipe. Currently, I'm using dup2() to simply overwrite the stdin and stdout from the pipe. How can I monitor what's going b...

How can I define an array in my Objective-C header file?

I have this code in my main file: int grid[] = { 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 2 , 3 , 2 , 3 , 2 , 3 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 0 , 1 , 0 , 1 , 0 , 1 , 1 , 1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 1 ...

Is there any way to get readable gcc error and warning output at the command line?

For some long errors, the gcc output is dense and has lots of line-wrapping etc. Especially when errors are subtle, it can take me 10-30 seconds of squinting to parse it with my eyes. I've taken to pasting this in an open code-editor window to get some basic syntax highlighting and enable reformatting with regex's. Has anyone invented...

How to overwrite stdout in C

Not sure if this is phrased well or not... In most modern shells, you can hit the up and down arrows and it will put, at the prompt, previous commands that you have executed. My question is, how does this work?! It seems to me that the shell is somehow manipulating stdout to overwrite what it has already written? I notice that progra...

Trim a string in C

Briefly: I'm after the equivalent of .NET's String.Trim in C using the win32 and standard C api (compiling with MSVC2008 so I have access to all the C++ stuff if needed, but I am just trying to trim a char*). Given that there is strchr, strtok, and all manner of other string functions, surely there should be a trim function, or one t...