Tag: programming

C/C++ Tokens – GeeksforGeeks

In C/C++ tokens are the smallest element of a program. Tokens can be split into the following

1. Keywords
Predefined tokens in a programming language having fixed meaning.
e.g:- switch, case, int,return etc.

C++ has 31 additional keywords
few of the common ones used are
bool, class,static_cast,try, catch etc

2. Identifiers
Identifiers are used to name variables, labels etc. There are certain rules for the identifiers
a. They can start only with a letter or underscore(_).
b. They must contain only digits, letters and underscore.
c. They cannot contain white space.
d. maximum up to 31 characters long.
e. white space not allowed.

3. Constants
Constants refer to fixed values. e.g:- integer constants, floating point constants, character constants, octal or hexadecimal constants and string constants.
char *p=”Yogi”;
int age=43;

In the above examples “Yogi” and 43 are string and integer constants.

4. Strings
They are array of characters ended with null character(\0)

eg:- char test[20]=”Yogi”;

5. Symbols
Symbols have special meaning.
a. Brackets[]- used for array subscripts.
b. Paranthesis() – used for function calls and parameter passing.
c. Braces{} – Used for separating blocks of code. A variable defined inside the brace will have the scope within the brace.
d. comma, – separates statements and parameters.
e. asterisk * – Use to dereference a pointer.

There are other set of symbols also used and each has its own meaning.

6. Operators
Operators are symbols that are used to perform with operands to create a statement.
Based on the number of operands operator can act upon, operators can be classified as

a. Unary operator – i++, i–;
b. Binary operators – Arithmetic, relational, logical, assignment, conditional, bitwise
c. Ternary operator – ?:

Source: C/C++ Tokens – GeeksforGeeks

Line Splicing 

In C and C++ you can join the next line to the current line using Line spicing. Requires ‘\’ to be added at the end of the line.


Example code to demostrate line splicing.

[yogi@192 devel]$ cat linesplice.c
#include <stdio.h>

int main(int argc, char *argv[])
// test line splice

return 0;
[yogi@192 devel]$ gcc -o linesplice linesplice.c
[yogi@192 devel]$ ./linesplice
[yogi@192 devel]$ vi linesplice.c
[yogi@192 devel]$ cat linesplice.c
#include <stdio.h>

int main(int argc, char *argv[])
// test line splice \

return 0;
[yogi@192 devel]$ gcc -o linesplice linesplice.c
[yogi@192 devel]$ ./linesplice
[yogi@192 devel]$ vi linesplice.c
[yogi@192 devel]$ gcc -o linesplice linesplice.c
[yogi@192 devel]$ cat linesplice.c
#include <stdio.h>

int main(int argc, char *argv[])
// test line splice \
printf(“Hello\n”); \

return 0;
[yogi@192 devel]$ ./linesplice
[yogi@192 devel]$


Source: Line Splicing in C/C++ – GeeksforGeeks

Signals in C

Signals are one of the IPC mechanism used to communicate between 2 processes or between OS and a process. If a program hits a serious error, the OS may raise the respective signal for the process to be terminated after a core dump file is generated.


The common error signals are

SIGFPE:- some arithhmetic errors or floating point errors like divide by zero.

SIGILL:- Illegal instruction. Usually happens when an unknown instruction or an elevated privilege instructions is run. Common scenario is object file is corrupted or a stack overflow happens.

SIGSEGV:- Happens when a process access the physical memory which it doesn’t have access to. Common scenario is accessing a NULL pointer, stack or heap corruption.

SIGBUS:- Bus error. Invalid memory is accessed. In case of bus error the memory accessed itself is invalid. One scenario is issues with HW where the memory mapped address access fails to read the data from the hardware.

SIGABRT:- The signal is raised when the program calls abort() api. assert() in C++ uses internally abort().

SIGSYS:- The signal is raised when a process passes invalid arguments to a system call.

SIGTRAP:- This signal is raised in conjunction when a debugger is attached to the process. When the code hits a debug point the SIGTRAP can be raised.

Source: Program error signals – GeeksforGeeks

To be continued…

Compiling a C program. Various stages involved.

C is a high level programming language which converts a C code to executable which can be run in the respective target OS environment.

Usually a C program code is written with an extension .c. An IDE or editor is used to add code to the programs. Once done a compiler is used to compile the program code to executable. Some IDE provide built-in compiler while others provide option to select the compiler and debugger.

Steps involved in creating and executable.

  1. Add code using an editor.
  2. Compile using a compiler to create the executable binary. Linux uses gcc and windows uses Microsoft visual studio integrated compiler cl. There are other compilers available in multiple OS environments like borland C compiler.
  3. Fix any coding issues and move to step 1. Otherwise go to step 4.
  4. Run the final executable.
  5. Fix and run time found issues and move to step 1. Otherwise go to step 6.
  6. Final program ready.

The compilation phase can be split into 4.

  1. Pre-processing phase.
  2. Compilation phase.
  3. Assembly phase.
  4. Linking phase.

Pre-processing phase includes the following sub phases. This will generate filename.s file(in linux) which does have the below steps completed.

  • Removal of Comments.
  • Expansion of Macros.
  • Expansion of the included files.
  • Conditional compilation.

Compilation phase takes the pre-processed file and convert to assembly instructions. In case of linux it will generate the GNU assembly code.

Assembly phase include converting the assembly code into object code. filename.s will be converted to filename.o in Linux. This file will contain machine level instruction.

Linking phase is the final phase where the static libraries are linked as well as symbol lookup done for the dynamic libraries which are going to be loaded as part of run time load. The linker also add the extra code to call the main function as well as handling the exit from the program.

Note:- gcc uses dynamic linking for the standard C library functions like printf.


Source: Compiling a C program:- Behind the Scenes – GeeksforGeeks

Use -save-temps gcc option to generate the intermediate files.
gcc -Wall -save-temps -o sample -L .  -I . application.c -lcall_lib

yogi@192 0830]$ ls application.*
application.c application.i application.o application.s

application.c – program code.

application.i – preprocessed file.

application.s – assembly file

application.o – object file with machine instructions.

sample – Final program.

Quick summary on shared libraries

Shared libraries are reusable libraries which can be used by multiple applications at the same time. The advantages of shared libraries are

  1. Can be loaded at application start or on demand at run time.
  2. Can be upgraded separately and reloaded.

Only disadvantage compared to statically linked library is, there is an overhead for loading the library and resolving the symbols at run time.

In case of loading during application start, the loader will load the library into process virtual address space and resolve before the application is launched.

In case of loading dynamically the same can be achieved with the respective system library apis when in need..

Windows it will be a dynamically linked library(*.dll) and uses the same PE format as windows executable.

Linux it will be a shared object(*.so) and uses the ELF format as a normal linux executable.

In linux use the -shared gcc option to create a shared object. if your library name provided is abc.so, the linux linker will create it as libabc.so.

While statically linking, the option -labc should be provided to link with the same.

If you are using dynamic linking, you need to use dlopen(3) ,dlsym(3) and dlclose(3) apis to load, access the symbol and unload the library.

Sample codes


#include <stdio.h>
#include “call_lib.h”

int main(int argc, char * argv[])
printf(“In application\n”);

return 0;


int call_lib(int num);


#include <stdio.h>
#include “call_lib.h”

int call_lib(int num)
printf(“passed value is %d\n”, num);

return 0;

To compile the library code
gcc -shared -fPIC -o library.so library.c

To compile the application code
gcc -o sample -L /home/yogi/libs -lrary application.c

Running the executable

set first the library path

$export LD_LIBRARY_PATH=/home/yogi/libs:$LD_LIBRARY_PATH
$ ./sample
In application
passed value is 3

Source: Working with Shared Libraries | Set 1 – GeeksforGeeks

Assertions in C/C++

Assertions help programmers to test conditions and abort the programs. Mainly used when the code cannot execute further if the condition is not met.

The syntax of assert is

void assert(scalar expression);

Need to include the following header which defined the
assert macro.

#include <assert.h>

Assert prints the error message to standard error
and terminates the program by calling abort(3).


assert( val == 0)


prog: some_file.c:16: some_func: Assertion `val == 0' failed.

Assertions can be turned off by defining NDEBUG macro. Adding NDEBUG will cause assert not to generate any code to print error and abort.

Recommendation is to not use the NDEBUG macro if using assert to detect error conditions as software may behave non-deterministically.

Source: Assertions in C/C++ – GeeksforGeeks

Page 1 of 2
1 2