Category ‘C/C++’
- TECHPULP |
What is the difference between a macro and an inline function in C
The fundamental difference between a macro and an inline function is that they are handled in different phases of binary creation. A macro is expanded during pre-processing stage by the “cpp” command. The “cpp” command is the C preprocessor. On... - TECHPULP |
How to embed binary data in an ELF file
An object file can be created based on a pure binary data file and linked to an application. This application can get the start address, end address and size of binary data dynamically using three symbols that are placed by... - TECHPULP |
Why my application can’t open more than 1024 or 4096 sockets in Linux
Typically socket “open” call doesn’t fail unless you have missed out closing the previous connections using “close” system call. Typically Linux/Unix sets a maximum limit for the number of open FDs. That means you can’t keep FDs in open state... - TECHPULP |
How to redirect standard output and standard error to same file
Sometimes while compiling C source code, the screen scrolls down if lot of warnings and errors are generated. This is uncomfortable as you can’t see all errors and warnings and you can’t attend to all warnings and warning at once... - TECHPULP |
Why macro can’t be used instead of typedef in C
The C preprocessor blindly expands the macro with whatever they are defined with. Let us examine the following example. #define CHARPTR char* CHARPTR p1,p2; The C preprocessor expands the above code to the following. char *p1,p2; But we would like... - TECHPULP |
How do I resolve compiler error with my multi-lined macro in C
Let us look at the following plain multi-lined and multi-statement macro. #define SAY_HELLO() \ printf("Hi, There!\n"); \ printf("Nice to meet you\n"); If you use the above macro as shown below, you will encounter compilation errors. if(manager) SAY_HELLO(); else printf("You are... - TECHPULP |
How to swap two integers without using third variable
The safest way to swap two variables without using third variable is to use XOR operation as shown below. This example C program swaps integer values of two variables i and j. [neo@techpulp ~]# cat iswap.c #include <stdio.h> int main(int... - TECHPULP |
How to get the names of all files in a directory in C
The opendir(), readir() and closedir() library functions supported by C library can be used to get the names of files presented in a directory. The readdir() function should be invoked repeatedly to get all the files until it returns NULL.... - TECHPULP |
Print IP address in dotted-decimal format without converting to string in C
Few binary operations can be used to print a binary IP address in human-readable dotted-decimal format without using inet_ntoa() function that requires a string buffer. The following example shows how to do it. [neo@techpulp ~]# cat pip.c #include <stdio.h> #define... - TECHPULP |
How to write a function that takes variable number of arguments in C
The C language supports function that takes variable number of arguments using standard argument interface (stdarg.h). The following are the functions used to handle variable number of arguments in a function. #include <stdarg.h> void va_start(va_list ap, last); type va_arg(va_list ap,...