c

Mixing OCaml and C: is it worth the pain?

Hi all, I am faced with the task of building a new component to be integrated into a large existing C codebase. The component is essentially a kind of compiler, and will be complicated enough that I would like to write it in OCaml (for reasons along the lines of those given here). I know that OCaml-C interaction is possible (as per th...

objective-c string, string-formatter

I have a program with which dreamlax worked a lot with me on, which uses Objective-C to convert temperatures between the Fahrenheit, Celsius, Kelvin and Rankine temperature scales, but converting console-input into Kelvin, and then from Kelvin to the end-user's desired temperature scale. Now, I have an idea I would like to implement for...

Can __attribute__((packed)) affect the performance of a program?

I have a structure called log that has 13 chars in it. after doing a sizeof(log) I see that the size is not 13 but 16. I can use the __attribute__((packed)) to get it to the actual size of 13 but I wonder if this will affect the performance of the program. It is a structure that is used quite frequently. I would like to be able to rea...

Creating a program loop for the first time in objective-c

I am trying to add some "replay-value" if you will to my temperature scale conversion console program in Objective-C by adding a simple loop. Now, here is the code for my current main.m file: #import <Cocoa/Cocoa.h> #import "class.h" int main(int argc, char *argv[]) { int result; int prompt, prompt2, sourceTempText; double sourceTe...

Question regarding dequeues and testing

Hello guys, I am preparing for an interview and I came across these questions. Can some one please help how to solve these questions. Imagine you've 2D system which is just testing whether 2 rectangles are in collision state or not, and you supposed to make a program which takes the code of this system from its developers and test it ...

How Do Vector drawing applications do this?

In Illustrator, you can drag a rectangle and it will select all objects in it. It does beyond a bounding box test since it ensures its touching an actual part of the polygon. How does it efficiently do this then? (A C or C++ implementation would be preferable) Thanks ...

whats the difference between C strings and C++ strings?

whats the difference between C Strings and C++ strings. Specially while doing dynamic memory allocation ...

iphone: How to mix enum like NSCalendar

I just looked at the code in NSCalendar.h like this: enum { NSEraCalendarUnit = kCFCalendarUnitEra, NSYearCalendarUnit = kCFCalendarUnitYear, NSMonthCalendarUnit = kCFCalendarUnitMonth, NSDayCalendarUnit = kCFCalendarUnitDay, NSHourCalendarUnit = kCFCalendarUnitHour, NSMinuteCalendarUnit = kCFCalendarUnitMinute, ...

Example code to trigger Clang's static analyser

I would like to see a small but complete snippet of code that will cause Clang's static analyser to complain. My motivation is mostly that I'm trying to get it to work on my PIC32 code, and I need a way to distinguish between "all the code is fine" and "it's not actually doing anything". It's also partly curiosity, since I can't seem to ...

Validate Input to a C Program

i have a c program in which i am accepting 2 numbers as input. How do i validate if input entered is numbers only and not characters. void main() { int a,b; printf("Enter two numbers :"); scanf("%d%d",&a,&b); printf("Number 1 is : %d \n Number 2 is : %d",a,b); } [Edit] Added Sample Code ...

Generic method to display enum value names

Is there a way to display the name of an enum's value? say we have: enum fuits{ APPLE, MANGO, ORANGE, }; main(){ enum fruits xFruit = MANGO; ... printf("%s",_PRINT_ENUM_STRING(xFruit)); ... } using the preprocessor #define _PRINT_ENUM_STRING(x) #x won't work as we need to get the value of the variable 'x' and then c...

Kiling processes in C with Signals

The main process in my program forks 3 more process with say process ids as pid1, pid2, pid3. Pid1 and pid2 processes are in infinite loop. What I want is when pid3 process is over all the process including the main are terminated. As of now, I am using : wait(pid3); kill(0, SIGKILL); which do all above as i said, but it prints Killed...

UVa 3n+1 Case Recursive Stack Overflow

hi, im trying to solve this very first challange but i get stuck, i like fast program, so i decided to use recursive method not iteration unfortunately, when the input is a big integer (100000 > input > 1000000), its often crash so i debug it, and it shows stack overflow error please help me, i dont know what to do, ive tried to chang...

Problem using Scanf?

Why does scanf give a max value in case of "int" but crash the program in case of "char" when the limit is exceeded? #include<stdio.h> main(){ int a; char ch[10]; scanf("%d",&a); printf("%d",a); scanf("%s",ch); printf("%s",ch); } ...

Wrong value generated from math equation using a preprocessor directive

I have this preprocessor directive: #define INDEXES_PER_SECTOR BYTES_PER_SECTOR / 4 where BYTES_PER_SECTOR is declared in another header file as: #define BYTES_PER_SECTOR 64 I have this simple math equation that I wrote where after executing I get an assertion error as the value assigned to iTotalSingleIndexes is incorrect. int...

This program sounds the bell!!!

Hello there, I'm a brand-new student in programming arena so I can't grasp this program written in my book that I have been following for a few days. The program is like this: #include "stdio.h" main() { printf("\a"); } What does this program mean? Does this program mean that we could hear a ringing bell? I can't hear any ringing b...

A good C equivalent of STL vector?

I've noticed that at several places in our code base we use dynamically expanding arrays, i.e. a base array coupled with an element counter and a "max elements" value. What I want to do is replace these with a common data structure and utility functions, for the usual object-oriented reasons. The array elements can be either basic dat...

Preprocessor based exclusion of namespace qualified function calls.

I’m currently working on a reporting library as part of a large project. It contains a collection of logging and system message functions. I’m trying to utilize preprocessor macros to strip out a subset of the functions calls that are intended strictly for debugging, and the function definitions and implementations themselves, using cond...

Writing 'packed' structs to file using C

How can I "pack" and "write" a struct to a file using C so that: struct a { uint64_t a; char* b; uint16_t c; } a; a b; b.a = 3; b.b = "Hello"; b.c = 4; gets written to the file as 00 00 00 00 00 00 00 03 48 65 6c 6c 6f 00 00 04 ...

Why expose event of my GTK+ widget will be freezed after a long time? Is it a GTK+ bug?

Hello... Here is my code: #include <gtk/gtk.h> static int counter = 0; static PangoLayout* layout; static GdkGC* gc1; static GdkGC* gc2; //**/static GMutex* mu; static gboolean on_expose_event(GtkWidget* widget, GdkEventExpose* event) { gchar the_string[20]; //**/g_mutex_lock(mu); gdk_draw_rectangle(GDK_DRAWABLE(widget->...