c

Trying qsort on K&R "The C Programming Language"

I'm pretty new at this, so I tried compiling the main on page 119 (§5.11) along w/ its dependencies. I managed to get a clean build with this: #include <stdio.h> #include <string.h> #define ALLOCSIZE 10000 #define MAXLINES 5000 #define MAXLEN 1000 int getline(char *, int); char *alloc(int); char *lineptr[MAXLINES]; int readlines(cha...

Is assert evil?

The Go language creators write: Go doesn't provide assertions. (...) Programmers use them as a crutch to avoid thinking about proper error handling and reporting. What is your opinion about this? ...

writing formatted data of unknown length to a string (C programming)

The following C function: int sprintf ( char * str, const char * format, ... ); writes formatted data to a string. The size of the array passed as str should be enough to contain the entire formatted string. However, what if the length of the formatted string is not known ahead of time? How can this function (or another function like ...

writing a replacement for a function which takes a variable number of parameters (c programming)

I'm looking to write a function to replace fprintf int fprintf ( FILE * stream, const char * format, ... ); I'm not sure how to define a function such as this, because, after the format parameter, this function takes a variable number of parameters. Specifically, it takes at least as many additional arguments as were specified in the ...

C macro/#define indentation?

I'm curious as to why I see nearly all C macros formatted like this: #ifndef FOO # define FOO #endif Or this: #ifndef FOO #define FOO #endif But never this: #ifndef FOO #define FOO #endif (moreover, vim's = operator only seems to count the first two as correct.) Is this due to portability issues among compilers, or is it ...

Printing sub strings from command line arguments, in C.

Why doesn't this work. printf("%s\n", argv[1][3]); When this works? printf("%c\n", argv[1][3]); ...

c runtime problem

this error appeared while creating file using fopen in c programming language the NTVDM cpu has encountered an illegal instruction CS:0000 IP0075 OP:f0 00 f0 37 05 choos 'close to terminate the operation ...

Loading data from file in C

i am really struggling to load some numeric floating point data from a file into a C program...the file has floating point numbers with precision of 3 decimal points, each of which is in a single line...i wanted to load these values into an float array and then perform some calculations in it...however i tried loading it into an array of...

Pros/cons to using char for small integers in C

Is there any disadvantage to using char for small integers in C? Are there any advantages other than the occupancy/memory benefit? In particular, is the processor likely to cope with integer arithmetic on a char better or worse than it would on a (long/short) int? I know this will be processor/system/compiler specific, but I'm hoping f...

Problem with building project in C

Hi Everyone, I am doing a project in C and I have a problem with building it. It is: I have two separate sub systems[A and B] in my project that use the functionality of yet another sub system[C]. To do this, they #include the header files necessary. The obligation is that both the sub systems[A and B] have to be built separately, by w...

How to write a GUI wrapper around a command line program in *C*?

System(); is able to call a program in PATH. How is it possible to send stdin read from e.g. a text field on a GUI to a command line program such as ftp, sftp ... with their own prompts? System() waits for a program to quit, but ftp does not without user interaction. It's also not possible to create a batch file since it's read only one ...

maximum value of int

is there any code to find the maximum value of integer (which is acccording to the compiler) in c/c++ like Integer.MaxValue function in java ...

Printf seems to mess the output of a simple C program

Hi there. I have some code to add fractions. #include <stdio.h> #include <stdlib.h> struct frac { int enumerator; int denominator; }; typedef struct frac frac_t; frac_t *Add(frac_t *b1, frac_t *b2) { frac_t rfrac; frac_t *p; p = &rfrac; (*p).enumerator= ((*b1).enumerator* (*b2).denominator) + ((*b2).enumerator* (*b1).d...

Memory alignment on modern processors?

I often see code such as the following when, e.g., representing a large bitmap in memory: size_t width = 1280; size_t height = 800; size_t bytesPerPixel = 3; size_t bytewidth = ((width * bytesPerPixel) + 3) & ~3; /* Aligned to 4 bytes */ uint8_t *pixelData = malloc(bytewidth * height); (that is, a bitmap allocated as a contiguous bloc...

Multiplying two long long ints C

I am working on a program in C as a part of Homework in which I have to get the product of two long numbers which are taken as character string. eg: 123456789021 and 132456789098. Since it is taken as a string, I converted them to long long int for the multiplication. But the resulting product will be very large(larger than long long int...

Writing files in bit form to a file in C

i am implementing the huffman algorithm in C. i have got the basic functionality down upto the point where the binary codewords are obtained. so for example, abcd will be 100011000 or something similar. now the question is how do you write this code in binary form in the compressed file. i mean if i write it normally each 1 and 0 will be...

When to use static keyword before global variables?

Can someone explain when you're supposed to use the static keyword before global variables or constants defined in header files? For example, lets say I have a header file with the line: const float kGameSpriteWidth = 12.0f; Should this have static in front of const or not? What are some best practices for using static? Thanks! ...

Having trouble with pipes and select()

Hello, I seem to be having trouble with pipe and select. Context: Have to program something which will be shell executed as such: logn [--tick n] cmd [args] [, cmd [args]]... Basically, it's a program that multiple programs simultanously. Constraints: Each output line has to start with it's command number in front in format printf ...

I need some weak entropy. I know I can use an unitialized buffer, but if I just need an integer's worth, would its address suffice instead?

I'm on Linux, which has address space layout randomization. Is it necessary to to declare a buffer on the stack, leave it uninitialized, and use it for entropy, or can I just take the address of something already on the stack, cast it to an integer and (knowing that it is somewhat random due to address space layout randomization) use tha...

Eigenvector computation using OpenCV

Hi I have this matrix A, representing similarities of pixel intensities of an image. For example: Consider a 10 x 10 image. Matrix A in this case would be of dimension 100 x 100, and element A(i,j) would have a value in the range 0 to 1, representing the similarity of pixel i to j in terms of intensity. I am using OpenCV for image proc...