While programming in a compiled language, Many a time, there comes a situation where we need to check a small piece of code or a portion of the program quickly without waiting for the build system to compile the code, especially in a large build system.

This is why programming languages like Python, Ruby, Lisp etc. that can be used in a REPL, are very easy to learn, as we can explore/test an idea /check codes on the fly without creating any project file, then waiting for compilation, linking and then running to see the result/output of the program.

Same way Cling lets you test a C/C++ piece of code interactively, thus avoiding the overhead of creating a new project, file, compiling and linking.

The C++ interactive interpreter Cling is based on LLVM and its C++ frontend clang. For more information on cling, you can also check the links below :

Why to use Cling

  • one can use Cling to aid their C/C++ learning Process. users can check the behavior of C/C++ concepts on the fly as they are learning.

  • Rapid Application Development

    Cling can be used for Rapid Application Development allowing for prototyping and proofs of concepts taking advantage of dynamicity of feedback during the implementation process, as users don't need to go through the build and linking process.


Before we proceed to know the usage of the cling interpreter, Let's understand some meta-commands clings provides to perform some administrative tasks like displaying the list of commands, loading a file or library etc. These meta-commands can help us in using Cling interpreter. Meta-commands are always preceded by a ".". some important meta-commands are listed below:

Metaprocessor Commands

Command Usage
.help display brief descriptions of all meta-commands.
.L libname Load the given file or library
.x filename.cpp|filename.cxx loads filename and calls void filename() if defined.
.I path adds an include path
.rawInput toggle on/off raw input.
.q exit Cling Interpreter.

As we understand some meta-commands, Now its time to get our hands dirty with some C/C++ examples.

Let's type something in Cling prompt and see how it behaves

                [cling]$ xyz
input_line_3:2:2: error: use of undeclared identifier 'xyz'

yayy... we got error message. Now let's try help meta-command:

                [cling]$ .help

 Cling (C/C++ interpreter) meta commands usage
 All commands must be preceded by a '.', except
 for the evaluation statement { }
 Syntax: .Command [arg0 arg1 ... argN]

   .L <filename>                - Load the given file or library

   .(x|X) <filename>[args]      - Same as .L and runs a function with
                                  signature: ret_type filename(args)

we can use cling prompt as calculator also:

                [cling]$ 1+2
(int) 3
[cling]$ 2*3
(int) 6
[cling]$ 3-2
(int) 1

Note: no ";" is required if you want to print the output of C/C++ statements right away or else expressions will be evaluated but it will not be shown in the terminal. like it is shown below:

                [cling]$ int i=10
(int) 10
[cling]$ int j=20;
[cling]$ i
(int) 10
[cling]$ j
(int) 20
[cling]$ i;
                [cling]$ 1+2
(int) 3
[cling]$ 2*3
(int) 6
[cling]$ 3-2
(int) 1

Evaluating C codes in Cling

Now let's try some C codes in Cling. For example checkout helloworld statements below:

                [cling]$ #include<stdio.h>
[cling]$ printf("Hello World\n");
Hello World
[cling]$ printf("Hello World\n")
Hello World
(int) 12

Cling also lets you do some complicated maths stuffs in c. Let's try including math.h :

                [cling]$float r1 = 4/5;
[cling]$ float r2 = (float)4/5;
[cling]$ r1
(float) 0.00000f
[cling]$ r2
(float) 0.800000f
[cling]$ #include <math.h>
[cling]$ cos(7)
(double) 0.75390225
[cling]$ pow(2,3)
(double) 8.0000000

Can you use looping statements also ? Let's try:

                [cling]$ #include <stdio.h>
[cling]$ int a[]={1,2,3,4,5,6,7,8,9,10};
[cling]$ for(int i=0; i<10; i++) {
[cling]$ ?   printf("%d\n",a[i]);
[cling]$ ?   }

Wow.. that looks great. Now, Let's try loading library for using pthread.h :

                [cling]$ .L libpthread
[cling]$ #include <pthread.h>
[cling]$ pthread_self()
(pthread_t _Nonnull) 0x7fffd68cd3c0

looks very impressive!! In next section we will try to evaluate some C++11 codes.

Evaluating C++ codes in Cling

In this section, we will evaluate cling to test various C++ codes regarding lambda functions, functions, and classes.

  • C++ Hello World:
                    [cling]$ #include <iostream>
    [cling]$ using namespace std;
    [cling]$ cout<<"Hello World\n";
    Hello World
  • Lambda Expressions/functions:
                    [cling]$ auto func = [](int a, int b) -> int { return a+b; };
    [cling]$ func(1,2)
    (int) 3
    [cling]$ func(10,2)
    (int) 12
  • Functions:
  • We will use .rawInput meta-command:

                    [cling]$ .rawInput
    Using raw input
    [cling]! int add(int i, int j) {
    [cling]! ?   return i+j;
    [cling]! ?   }
    [cling]! .rawInput
    Not using raw input
    [cling]$ add(2,3)
    (int) 5
    [cling]$ add(5,3)
    (int) 8
    [cling]$ cout<[cling]$
  • Classes:
                    [cling]$ #include <iostream>
    [cling]$ using namespace std;
    [cling]$ #include <cstring>
    [cling]$ class Students {
    [cling]$ ?     char name[20];
    [cling]$ ?      public:
    [cling]$ ?     Students() {
    [cling]$ ?      strcpy(name,"no name");
    [cling]$ ?            }
    [cling]$ ?     Students(char n[]) {
    [cling]$ ?           strcpy(name, n);
    [cling]$ ?      }
    [cling]$ ?      char* get_name() {
    [cling]$ ?            return name;
    [cling]$ ?      }
    [cling]$ ?   };
    [cling]$ Students s;
    [cling]$ s
    (Students &) @0x1169210d0
    [cling]$ s.get_name()
    (char *) "no name"
    [cling]$ Students ram("Ram");
    input_line_14:2:15: warning: ISO C++11 does not allow conversion from string literal to 'char *'
     Students ram("Ram");
    [cling]$ ram
    (Students &) @0x11692148c
    [cling]$ ram.get_name()
    (char *) "Ram"
    [cling]$ char sam[] = "sam"
    (char [4]) "sam"
    [cling]$ Students samobj(sam);
    [cling]$ samobj
    (Students &) @0x116921928
    [cling]$ samobj.get_name()
    (char *) "sam"

    Here is a video explaining the usage of Cling as a C/C++ Interpreter:


    We just saw that cling is a very powerful tool, capable of evaluating C as well as C++ statements and can be very useful to all class of programmers.


    Further information on Cling can be found in the links below:

    Read our documentation for more information on other REPLs