Let’s look at some of the queries received from readers relating to programming in C.
Q. Where are enum members stored in memory? How does the compiler generate code for enumerations? – Ashwin Karanth
Enumerations are just named constants. For this reason, the initialisation value of enumerations should be “constant expressions”. A C/C++ compiler replaces them with their equivalent values during compilation. So enumerations don’t have any role to play when the program executes.
Q. Types int and long are of the same size. So, since data type of int is enough, why do we need the long data type? – Naveen P N
In your machine, maybe the sizes of int and long are the same. That need not be the case for all machines. Having two types, int and long, becomes very useful when these sizes are different.
Typically, in 32-bit machines/compilation mode, UNIX flavours have 32 bit for int, long and pointer; so it is known as ILP32. However, in 64-bit machines/compilation mode, int is typically of size 32 bits, but long and pointer are of 64 bits; so it is known as LP64. While programming (usually, the default mode is 32-bit mode), we often assume the sizes of int, long and pointer are the same and treat them interchangeably (for example, in casts). This causes major problems when porting from 32-bit mode to 64-bit mode; so do not treat them interchangeably.
Q. Can I pass float data-type as an argument using the
%X specifier in C language? For example,
printf("%X", fVal); – Mac
%X is for printing an unsigned integer value in hexadecimal format. So, it should not be used for floating point values. We cannot predict the behaviour of the program if we do so. When I tried
printf("%X", 1.1f);, the program received signal 8; the signal name is SIGFPE, which stands for
signal — floating point exception”.
Q. Can you provide me interrupt numbers and services in terms of using the
int86() function? Can I develop a client-server chat program using TurboC++ 2? – Shoaib Konnur
Q. Where can I get a GCC compiler for Windows XP? – Santhanu Kumar Patel
I am often surprised to know that most students still use DOS and Turbo C/C++ compilers! The
int86 function in DOS is for BIOS interrupts and we can access hardware directly — like processing keystrokes or displaying characters on screen (by writing to “video memory”). Read the book on DOS called C Odyssey, which covers these details. I would suggest you to use Win32 APIs, or better still, move to Linux and use system calls for systems programming.
You can write networking code using any compiler, provided you have the API support. Use better and modern compilers like GCC on Linux. If you still want to use Windows, you can use GCC with Cygwin or MinGW. If you are just learning programming and have Internet access, you can use online compilers like ideone.com: You can type, store, compile and run the programs (for around 50 languages) on Linux using this website!
Q. When would the memory allocation be contiguous while using malloc? – Giridhar Kannan
Memory allocation routines are usually implemented in a shared library, say
libc, and are known as “heap implementation” routines. While using malloc for allocating small memory blocks, it might happen that the memory allocated is contiguous (i.e., adjacent memory blocks would get allocated). This usually happens for two reasons: Small subsequent malloc calls are made, and all the allocation is within the same large memory block.
Most heap implementations get a large chunk of memory from the underlying OS; for example, when using
mmap in Linux. Heap implementation uses this large block to allocate small blocks of memory as requested by malloc. When the large block is exhausted, the implementation again gets more memory (as a different block) from the OS. So, subsequent calls will not be contiguous. When malloc is called.
Feature image courtesy: Robert Nunnally. Reused under the terms of CC-BY 2.0 License.