A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

Jump to

Quick reference

These warnings:

warning: initialization makes pointer from integer without a cast
warning: assignment makes pointer from integer without a cast

...for a line containing malloc could mean that you forgot to #include "stdlib.h".
Functions are assumed to return an integer type by default if not declared.

C printf and scanf format specifiers

Format Specifier

Type

%d (or %i)

int

%c

char

%f

float

%lf

double

%s

string

%xhexadecimal

Comparing strings in C

In c it is not possible to directly compare two strings so a statement like if (string1==string2) is not valid. Most c libraries contain a function called the strcmp().This is used to compare two strings in the following manner.

if(strcmp(name1,name2)==0)
 puts("The names are the same");
else
 puts("The names are not the same.");

Thanks johnt

debugging technique

When programming C, it can be difficult to track down a segmentation fault.
It is often caused by attempting to read uninitialised memory.

Try skipping parts of the program until you locate the statement causing it.

Then replace each part of the data by hard-coded values, one by one, until you find the uninitialised variable/pointer.

c logging

if your fprintf statements don't reach the apache error log, you may have to flush the output buffer:

fprintf(stderr, "error statement\n");
fflush(stderr);