c

How do I check if an integer is even or odd?

How best can I check if an integer is even or odd in C? I considered how I'd do this in Java, but I couldn't come up with an answer either. Thanks. ...

Error: initializer element is not computable at load time

I have in a function that takes a struct, and I'm trying to store its variables in an array, but I get this when I run gcc -Wall -ansi -pedantic-errors -Werror int detect_prm(Param prm) { int prm_arr[] = {prm.field1, prm.field2, prm.field3}; return 0; } I get Error: initializer element is not computable at load time when I try to...

Is there a nice way of handling multi-line input with GNU readline?

My application has a command line interface, and I'm thinking about using the GNU Readline library to provide history, an editable command line, etc. The hitch is that my commands can be quite long and complex (think SQL) and I'd like to allow users to spread commands over multiple lines to make them more readable in the history. Is it...

Passing structs by value - why not?

Are there any downsides to passing structs by value in C, rather than passing a pointer? If the struct is large, the there is obviously the performancd aspect of copying lots of data, but for a smaller struct, it should basically be the same as passing several values to a function. It is maybe even more interesting when used as return ...

Is one's complement a real-world issue, or just a historical one?

Another question asked about determining odd/evenness in C, and the idiomatic (x & 1) approach was correctly flagged as broken for one's complement-based systems, which the C standard allows for. Do systems really exist in the 'real world' outside of computer museums? I've been coding since the 1970's and I'm pretty sure I've never met ...

K & R Exercise: My Code Works, But Feels Stinky; Advice for Cleanup?

I'm working on the K&R book. I've read farther ahead than I've done exercises, mostly for lack of time. I'm catching up, and have done almost all the exercises from chapter 1, which is the tutorial. My issue was exercise 1-18. The exercise is to: Write a program to remove trailing blanks and tabs from line of input, and to delete...

Parenthesis surrounding return values

Quite often in ANSI C code I can see parenthesis sorrounding a single return value. Like this:- int foo(int x) { if (x) return (-1); else return (0); } Why use () around the return value in those cases? Any ideas? I can see no reason for that. ...

Const in C

Quick question: int testfunc1 (const int a) { return a; } int testfunc2 (int const a) { return a; } Are these two functions the same in every aspect or is there a difference? I'm interested in an answer for the C-language, but if there is something interested in the C++ case I'd like to know as well. ...

How do I detect "Easter Egg" mode in my Palm OS application?

Since the early days, Palm OS has had a special "easter egg" mode that's enabled by making the right gesture in one of the Preference panels. On current Palm Treo and Centro devices, this is turned on by doing a clockwise swirl above the "Tips" button in the Power panel. Some applications, like the Blazer web browser, enable special fe...

Using system's standard Edit menu in a Palm OS app

How can I use the standard Edit menu in my Palm OS application, instead of having to implement my own Cut/Copy/Paste/Keyboard handlers? ...

C, C++, Java, what is next and what will it look like?

What do you think the next evolution of languages will look like? ...

How to retrieve n characters from char array

i have a char array in a C app that i have to split into parts of 250 so i can send it along to another application that doesn't accept more at one time, how would i do that ? Platform: win32. Thanks in advance! ...

Why use pointers?

I know this is a really basic question, but I've just started with some basic C++ programming after coding a few projects with high-level languages. Basically I have three questions: Why use pointers over normal variables? When and where should I use pointers? How do you use pointers with arrays? Also, can any of you recommend a go...

Why is fread reaching the EOF early?

I am writing a C library that reads a file into memory. It skips the first 54 bytes of the file (header) and then reads the remainder as data. I use fseek to determine the length of the file, and then use fread to read in the file. The loop runs once and then ends because the EOF is reached (no errors). At the end, bytesRead = 10624, ft...

On Win32 how do you move a thread to another cpu core?

I'd like to make sure that a thread is moved to a specific cpu core & can never be moved from it by the scheduler. There's a SetThreadAffinityMask() call but there's no GetThreadAffinityMask(). The reason I need this is because high resoultion timers will get messed up if the scheduler moves that thread to another cpu. ...

Serialization of a long array (in C)

Hi, in a C program I have an long* that I want to serialize (thus converting to chars). A long doesn't fit in a single char, and the size varies depending of the processor (can be 4 bytes or 8 bytes). Theres a good way to make the serialization and de-serialization? ...

Why does simple C code receive segmentation fault?

The following code receives seg fault on line 2: char *str = "string"; str[0] = 'z'; printf("%s", str); While this works perfectly well: char str[] = "string"; str[0] = 'z'; printf("%s", str); Tested with MSVC and GCC. ...

printf + uint_64 on Solaris 9?

I have some c(++) code that uses sprintf to convert a uint_64 to a string. This needs to be portable to both linux and Solaris. On linux we use %ju, but there does not appear to be any equivalent on Solaris. The closest I can find is %lu, but this produces incorrect output. Some sample code: #include <stdio.h> #include <sys/types.h>...

How do I provide a suffix for days of the month?

I need a function to return a suffix for days when displaying text like the "th" in "Wednesday June 5th, 2008". It only need work for the numbers 1 through 31 (no error checking required) and English. ...

UTF-8 in Windows

How do I set the code page to UTF-8 in a C Windows program? I have a third party library that has uses fopen to open files. I can use wcstombs to convert my Unicode filenames to the current code page, however if the user has a filename with a character outside the code page then this breaks. Ideally I would just call _setmbcp(65001...