c

char *a, *b; what type is (b-a) and how do I printf it?

{ char *a, *b; printf("%lx\n",(b-a)); } Usually works, in fact, I can't imagine it giving a warning or failing on a 32-bit or 64-bit machine. But is that the proper thing to do for ANSI C and size awareness? I'd like this code to work on every platform possible, including non-Unixes and embedded systems. ...

Does the declaration or definition determine the scope of a variable in C/C++?

Which determines the scope of a variable, the declaration or definition? ...

c define 64bit on 32bit

If I do: #define TIMEFIXCONST 11644473600 on a 32bit machine, will it overflow or will it be stored as a long long and still work properly? Should I just define a global unsigned long long and use that instead? ...

C/C++ function/method decoration

DISCLAIMER: I haven't done C++ for some time... Is it common nowadays to decorate C/C++ function/method declarations in order to improve readability? Crude Example: void some_function(IN int param1, OUT char **param2); with the macros IN and OUT defined with an empty body (i.e. lightweight documentation if you will in this example). ...

strtoull and long long arithmetic

Can anyone explain the output of this program and how I can fix it? unsigned long long ns = strtoull("123110724001300", (char **)NULL, 10); fprintf(stderr, "%llu\n", ns); // 18446744073490980372 ...

pthreads, setjmp, longjmp. How can you tell when a function is finished running?

I am writing a user space thread library. I have a struct that manages each thread. My threads are very simple, they take a function ptr and its arguments, and just run that function one time. Each thread has a jmp_buf and I use setjmp and longjmp to switch between threads. One thing I cant figure out is how to tell when this function i...

Save reference to Lua's userdata

Not really know how to ask so bare with me please :) #1 Lua: local test = Test(); #2 C: //creating "lua's test" luaL_newmetatable(L, "someTable"); lua_userdata *userData = (lua_userdata *)lua_newuserdata(L, sizeof(lua_userdata)); luaL_getmetatable(L, "someTable"); lua_setmetatable(L, -2); #3 Lua: function test.newMethod() end ...

K&R 1st Edition vs 2nd Edition

I doubt I'll ever become an expert C programmer, but I decided to check out the K&R book to sharpen my skills and see what supposedly one of the best programming language books is like. I requested a copy of the second edition through my library, but the first edition was loaned to me. Is the first edition good to learn from? Are there...

Need help defining functions in my C programming

I am learning C functions and trying to write a simple script that outputs a lettergrade depending on the number the user inputs. Yet I am getting the following error and I can't figure out where I'm lacking... 102809.c: In function ‘function_points’: 102809.c:44: error: ‘lettergrade’ redeclared as different kind of symbol 102809.c:41: ...

How do I write to a different thread's stack in C/C++?

I know this is a bad idea! Certainly for safe programming the stack for a given thread should be considered private to that thread. But POSIX at least guarantees that all of a thread's memory is shared and writeable by other threads, which means the stack of one thread can (in theory) be written to by another thread. So I'm curious how o...

winapi CreateWindowEx -> create button with user system styles?

I want to make a button that looks just like the OK button when you go My Computer -> Help -> About Windows. I am creating a button like this: hButton = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("BUTTON"), TEXT("Text"), WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON, 10, 10, 30, 30, hwnd, (HMENU)IDC_MAIN_BUTTON, GetModuleHandle(NULL), NULL); ...

Different string initialization yields different behavior?

How come when I use the following method, to be used to convert all the characters in a string to uppercase, while (*postcode) { *postcode = toupper(*postcode); postcode++; } Using the following argument works, char wrong[20]; strcpy(wrong, "la1 4yt"); But the following, doesn't, despite them being the same? char* wrong = "la1 ...

Do any POSIX functions or glibc extensions implement a breadth-first file tree walk?

I am writing a daemon that utilizes inotify to monitor file access and it is critical that I don't miss anything on a recursive search. I found this interesting idea and have begun to implement it. ftw() and ftw64() do not use a breadth-first algorithm, its more "pre-order". nftw() gives me the option of depth-first, but I'm worried abo...

C state-machine design

I am crafting a small project in mixed C and C++. I am building one small-ish state-machine at the heart of one of my worker thread. I was wondering if you gurus on SO would share your state-machine design techniques. NOTE: I am primarily after tried & tested implementation techniques. UPDATED: Based on all the great input gathered o...

What does this define statement mean?

I have this code to draw an ellipse in the screen but i dont understand what does it means the long define statement, and i only want to know how to write the same code without all that confuse define statement. #define incx() x++, dxt += d2xt, t += dxt #define incy() y--, dyt += d2yt, t += dyt void ellipse(int xc, int yc, int rx, int...

Unbuffered I/O in ANSI C

For the sake of education, and programming practice, I'd like to write a simple library that can handle raw keyboard input, and output to the terminal in 'real time'. I'd like to stick with ansi C as much as possible, I just have no idea where to start something like this. I've done several google searches, and 99% of the results use l...

Is it possible to check whether you are building for 64-bit with Microsoft C Compiler?

Is there a simple preprocessor macro that is defined for a 64-bit build? I thought _WIN64 might have been it, but even when I build a 32-bit target, the parts enclosed in a #ifdef _WIN64 ... #endif are compiled in, and this is causing problems. It's Friday and I can't think straight, but I'm sure I'm overlooking something very simple her...

How to get more vfat attributes of files in Linux using C?

This is a follow-up to my other question here: http://stackoverflow.com/questions/1644416/how-to-read-vfat-attributes-of-files-in-linux-using-c -- I saw this struct in linux/msdos_fs.h: struct msdos_dir_entry { __u8 name[8],ext[3]; /* name and extension */ __u8 attr; /* attribute bits */ __u8 lcase; /* Case for ...

Running a simple TCP server with poll(), how do I trigger events "artificially"?

I have a fairly basic TCP server keeping track of a couple connections and recv'ing data when it's available. However, I'd like to artificially trigger an event from within the program itself, so I can send my TCP server data as if it came from sock1 or sock2, but in reality came from somewhere else. Is this possible, or at all clear? s...

looping through a structure array

Hello, I have this structure and I thought I could set the condition if the structure is pointing to a NULL value. Here is my simple structure for this example: typedef struct { char *name; char *data; } details_t; details_t emp_details [] = { { "peter", "lawyer" }, { "john", NULL }, /* No data for john */ { NU...