c

C interview question---run-length coding of strings

Hello, A friend of mine was asked the following question a Yahoo interview: Given a string of the form "abbccc" print "a1b2c3". Write a function that takes a string and return a string. Take care of all special cases. How would you experts code it? Thanks a lot ...

Debugging a big double array.

Hello, I am using a C++ library that provides an object that, for the sake of simplicity, is more or less like this: class ExampleSO { public double* narray; }; I have an instance of ExampleSO whose narray is about 200. Some other method ExampleSO::method() does a lot of arithmetic functions with this array and assigns it to diff...

Do math functions of constant expressions get pre-calculated at compile time?

I tend to use math functions of constant expressions for convinience and coherence (i.e log(x)/log(2) instead of log(x)/0.3...). Since these functions aren't actually a part of the language itself, neither are they defined in math.h (only declared), will the constant ones get precalculated at compile time, or will they be wastefully calc...

how many times will strlen() be called in this for loop?

Will the strlen() function below get called just once (with the value stored for further comparisons); or is it going to be called every time the comparison is performed? for (i = 0; i < strlen(word); i++) { /* do stuff */ } ...

How to run something every t seconds in C?

In C, I want a block of statements to be executed repeatedly every t seconds. How do I do this? ...

Is there a limit for the total variables size on the stack?

While coding should we consider some limit on the total size of variables created on the stack? If yes, on what basis should we decide it? Is it dependent on OS, Memory Availability etc? Are there any compiler options which can check this? Any pointers in the direction will also be helpful. ...

Possibility of strcat failing?

Is there a possibility that strcat can ever fail? If we pass some incorrect buffer or string, then it might lead to memory corruption. But, apart from that is it possible that this function can return failure like strcat returning NULL even if destination string passed is Non-NULL? If no, why strcat has a return type specified at all? ...

Some really ugly C macro

I need some really ugly C macro (as an example, not to any real project), that does some simple thing (like adding one, for example) really hard and unreadable way. Anyone has some idea? ...

Extract the fields of a C struct

I often have to write code in other languages that interact with C structs. Most typically this involves writing Python code with the struct or ctypes modules. So I'll have a .h file full of struct definitions, and I have to manually read through them and duplicate those definitions in my Python code. This is time consuming and error-...

Is "argv[0] = name-of-executable" an accepted standard or just a common convention?

When passing argument to main() in a C or C++ application, will argv[0] always be the name of the executable? Or is this just a common convention and not guaranteed to be true 100% of the time? ...

Can I set a breakpoint in included code?

I have something analogous to the following code snippets: // file alpha.c void function_A() { int i; #include "code.h" } and //file beta.c void function_B() { int i; #include "code.h" } and // file code.h for(i = 1;i < 10;i++) { // do some stuff if (very_rare_event) { // do something else }...

c function pointers

This program sorts lines alphabetically/numerically depending on the arguments passed to main. And im working on this exercise from k&R now: Add the option -f to fold upper and lower case together, so that case distinctions are not made during sorting; for example, a and A compare equal. Is what i wrote in my_strcmp good? And will it w...

How do I free memory obtained by sbrk() ?

I have a custom allocator function which uses sbrk() to obtain memory. How do I release this memory when it's no longer needed? Is there a function equivalent to free() for malloc() ? or do I have to use brk() to set the end of the data segment ? ...

How exactly does __attribute__((constructor)) work?

It seems pretty clear that it is supposed to set things up. When exactly does it run? Why are there two brackets? Is __attribute__ a function? A macro? Syntax? Does this work in C? C++? Does the function it works with need to be static? When does __attribute__((destructor)) run? Example in Objective C: __attribute__((constructor)) s...

What is the best way of implementing a dynamically resizing stack in C?

What is the best correct way of implementing a dynamically resizing stack in C? For example, i want to allocate an amount of memory to a stack but when that stack gets full, the memory allocated is doubled to accomodate new data, etc. I have a stack implemented at the minute using a simple array of void pointers, this way i can store p...

What tools are there for suggesting potential refactorings.

I don't want tools for actually performing refactorings, but tools for finding and suggesting potential refactorings. Particularly tools for identifying blocks of code that are similar and could be merged into a utility functions. The background here is that I've been asked to investigate reducing the code size of an embedded C system. ...

min and max value of data type in C

hi, what is the function to determine the min and max possible of value of datatypes (i.e, int, char.etc) in C? Thanks, ...

how to check if at least three of last four bits (LSB) are on

I tried doing this in C: int val = 0xCAFE; int uc = val & 14; if (val & 15 == 15 || val & 7 == 7 || val & 11 == 11|| val & 13 == 13 || val & 14 == 14){ printf("asdjfkadscjas \n"); } However this is not printing the random string as it should. It worked for 15,7,11,13 tho. If anyone knows of a better way that would be helpfu...

which language is better for packet capturing and processing

I want to write a program that has 2 threads , one thread continuously captures packets from the network and dumps them in a buffer every t seconds and the other thread continuously reads this buffer and processes it every t seconds.. Can this be done in C ? or will Java be a better option ? ...

bit operations in c

how do you reverse and rotate hex numbers and return the number in C with bitwise operators? for example: 0xabcd -> 0xdcba 0xabcd -> 0xdabc ...