# How to "print" a variable (aka the newbiest of newbie questions)

**URL:** <https://root-forum.cern.ch/t/how-to-print-a-variable-aka-the-newbiest-of-newbie-questions/49489>\
**Category:** Newbie\
**Created:** [April 6, 2022, 6:46am UTC](https://root-forum.cern.ch/t/how-to-print-a-variable-aka-the-newbiest-of-newbie-questions/49489 "2022-04-06T06:46:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jaysonl](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jaysonl/32/29368_2.png) [@jaysonl](https://root-forum.cern.ch/u/jaysonl)\
**Post date:** [April 6, 2022, 6:46am UTC](https://root-forum.cern.ch/t/how-to-print-a-variable-aka-the-newbiest-of-newbie-questions/49489/1 "2022-04-06T06:46:16Z")

</div>

This may be the most newbie question of all.

Is there a way to get programmatic access to whatever magic ROOT does when you just enter an expression and hit ENTER?

Example: I’m messing around with a YAML parser library. This is what it looks like:

```auto
#include <yaml.h>
yaml_parser_t parser;
yaml_event_t event;
FILE *file = fopen("/home/jayson/.config/joystick_to_teensy.yaml", "rb");
yaml_parser_initialize(&parser);
yaml_parser_set_input_file(&parser, file);
yaml_parser_parse(&parser, &event);
while (event.type != YAML_NO_EVENT) {
	printf("%d\n", event.type);
	yaml_parser_parse(&parser, &event);
}
fclose(file);

```

Note the `printf` in the middle of the `for` loop. It just prints out boring old numbers:

> **(boring old numbers)**
>
> ```auto
> 1
> 3
> 9
> 6
> 9
> 6
> 9
> 6
> 9
> 6
> 9
> 6
> 6
> 6
> 6
> 6
> 6
> 6
> 6
> 10
> 6
> 9
> 6
> 7
> 6
> 6
> 6
> 6
> 8
> 6
> 7
> 6
> 6
> 6
> 6
> 8
> 10
> 10
> 10
> 10
> 10
> 4
> 2
> 
> ```

However, if I step through this loop by hand and inspect the value of `event.type` by evaluating it on the REPL, I get all this lovely extra data:

```auto
root [0] #include <yaml.h>
root [1] yaml_parser_t parser;
root [2] yaml_event_t event;
root [3] FILE *file = fopen("/home/jayson/.config/joystick_to_teensy.yaml", "rb");
root [4] yaml_parser_initialize(&parser);
root [5] yaml_parser_set_input_file(&parser, file);
root [6] yaml_parser_parse(&parser, &event);
root [7] event.type
(yaml_event_type_t) (YAML_STREAM_START_EVENT) : (unsigned int) 1
root [8] yaml_parser_parse(&parser, &event)
(int) 1
root [9] event.type
(yaml_event_type_t) (YAML_DOCUMENT_START_EVENT) : (unsigned int) 3
root [10] yaml_parser_parse(&parser, &event)
(int) 1
root [11] event.type
(yaml_event_type_t) (YAML_MAPPING_START_EVENT) : (unsigned int) 9
root [12] yaml_parser_parse(&parser, &event)
(int) 1
root [13] event.type
(yaml_event_type_t) (YAML_SCALAR_EVENT) : (unsigned int) 6
root [14] yaml_parser_parse(&parser, &event)
(int) 1
root [15] event.type
(yaml_event_type_t) (YAML_MAPPING_START_EVENT) : (unsigned int) 9
root [16]

```

Is there a command that would allow me to print out that super cool introspection-aware information from inside my `for` loop?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [April 6, 2022, 7:03am UTC](https://root-forum.cern.ch/t/how-to-print-a-variable-aka-the-newbiest-of-newbie-questions/49489/2 "2022-04-06T07:03:59Z")

</div>

I do not have the yaml.h file nor your data file. So I cannot try your example. But I guess what you are looking for is what is printed when you do something like:

```auto
root [1] int i=1
(int) 1
root [2] auto h = new TH1F()
(TH1F *) 0x7fe133762110
root [3] 

```

ie the two lines:

```auto
(int) 1
(TH1F *) 0x7fe133762110

```

I am not sure what the cling interpreter is doing at that point and if there is a _generic_ _way_ to get the same in a compiled program. May be @vvassilev can help you.

---

<div class="post-metadata">

**Author:** ![vvassilev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/f0a364/32.png) [@vvassilev](https://root-forum.cern.ch/u/vvassilev)\
**Post date:** [April 6, 2022, 7:22am UTC](https://root-forum.cern.ch/t/how-to-print-a-variable-aka-the-newbiest-of-newbie-questions/49489/3 "2022-04-06T07:22:26Z")

</div>

Are you using cling or ROOT?

In cling you might be able to do something like:

```auto
[cling] #include <cling/Interpreter/Interpreter.h>
[cling] ...
[cling] for(...) { gCling->echo("event.type"); }

```

---

<div class="post-metadata">

**Author:** ![jaysonl](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jaysonl/32/29368_2.png) [@jaysonl](https://root-forum.cern.ch/u/jaysonl)\
**Post date:** [April 6, 2022, 7:52am UTC](https://root-forum.cern.ch/t/how-to-print-a-variable-aka-the-newbiest-of-newbie-questions/49489/4 "2022-04-06T07:52:18Z")

</div>

I’m using ROOT.

I was under the impression that ROOT is the new CLING, sort of like CLING was the new CINT.

I see a couple of different Interpreter.h files kicking around in the ROOT source code, quite a few references to gCling, and even a few places where gCling-\>echo gets called, but nothing that I can seem to use from the ROOT prompt.

---

<div class="post-metadata">

**Author:** ![jaysonl](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jaysonl/32/29368_2.png) [@jaysonl](https://root-forum.cern.ch/u/jaysonl)\
**Post date:** [April 6, 2022, 8:01am UTC](https://root-forum.cern.ch/t/how-to-print-a-variable-aka-the-newbiest-of-newbie-questions/49489/5 "2022-04-06T08:01:28Z")

</div>

Thanks for responding.

yaml.h is coming from the “libyaml” library, which comes from package `libyaml-dev` on Debian Linux (+ derivatives).

The YAML file itself is something of little to no consequence, just something to feed to the parser. It looks like this:

```auto
joysticks:
  8Bitdo NES30 Pro:
    NES:
      EV_KEY:
        BTN_A: 0x01
        BTN_B: 0x02
        BTN_SELECT: 0x04
        BTN_START: 0x08
      EV_ABS:
        ABS_HAT0X: [0, 0x40, 0, 0x80]
        ABS_HAT0Y: [0, 0x10, 0, 0x20]

```

The same thing can be demonstrated with no external libraries like this:

```auto
typedef enum sammich_type_e {
	CHICKEN_SAMMICH,
	HAM_SAMMICH,
	GRILLED_CHEESE_SAMMICH,
	KNUCKLE_SAMMICH
} sammich_type_t;

sammich_type_t sammy = HAM_SAMMICH;

```

If you then enter `sammy` on the prompt, you get:

```auto
root [16] sammy
(sammich_type_t) (HAM_SAMMICH) : (unsigned int) 1

```

As opposed to what you get if you use `printf`:

```auto
root [17] printf("%d\n", sammy);
1

```

So what I’m ultimately looking for is something that will convince ROOT to print out the more informative `(sammich_type_t) (HAM_SAMMICH) : (unsigned int) 1`.

---

<div class="post-metadata">

**Author:** ![vvassilev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/f0a364/32.png) [@vvassilev](https://root-forum.cern.ch/u/vvassilev)\
**Post date:** [April 6, 2022, 8:06am UTC](https://root-forum.cern.ch/t/how-to-print-a-variable-aka-the-newbiest-of-newbie-questions/49489/6 "2022-04-06T08:06:25Z")

</div>

if you use root that might be able to help:

```auto
std::unique_ptr<TInterpreterValue> v = gInterpreter->MakeInterpreterValue();
gInterpreter->Evaluate("event.type", *v);
v->ToString();

```

---

<div class="post-metadata">

**Author:** ![jaysonl](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jaysonl/32/29368_2.png) [@jaysonl](https://root-forum.cern.ch/u/jaysonl)\
**Post date:** [April 6, 2022, 9:38am UTC](https://root-forum.cern.ch/t/how-to-print-a-variable-aka-the-newbiest-of-newbie-questions/49489/7 "2022-04-06T09:38:39Z")

</div>

C purists, C++ purists, look away…

```auto
#include <yaml.h>
std::unique_ptr<TInterpreterValue> v = gInterpreter->MakeInterpreterValue();
yaml_parser_t parser;
yaml_event_t event;
FILE *file = fopen("/home/jayson/.config/joystick_to_teensy.yaml", "rb");
yaml_parser_initialize(&parser);
yaml_parser_set_input_file(&parser, file);
yaml_parser_parse(&parser, &event);
while (event.type != YAML_NO_EVENT) {
	gInterpreter->Evaluate("event.type", *v);
	cout << v->ToString() << endl;
	yaml_parser_parse(&parser, &event);
}
fclose(file);

```

Output:

```auto
(yaml_event_type_t) (YAML_STREAM_START_EVENT) : (unsigned int) 1

(yaml_event_type_t) (YAML_DOCUMENT_START_EVENT) : (unsigned int) 3

(yaml_event_type_t) (YAML_MAPPING_START_EVENT) : (unsigned int) 9

(yaml_event_type_t) (YAML_SCALAR_EVENT) : (unsigned int) 6

(yaml_event_type_t) (YAML_MAPPING_START_EVENT) : (unsigned int) 9

(yaml_event_type_t) (YAML_SCALAR_EVENT) : (unsigned int) 6

(yaml_event_type_t) (YAML_MAPPING_START_EVENT) : (unsigned int) 9

(yaml_event_type_t) (YAML_SCALAR_EVENT) : (unsigned int) 6

(yaml_event_type_t) (YAML_MAPPING_START_EVENT) : (unsigned int) 9

(yaml_event_type_t) (YAML_SCALAR_EVENT) : (unsigned int) 6

(yaml_event_type_t) (YAML_MAPPING_START_EVENT) : (unsigned int) 9

(yaml_event_type_t) (YAML_SCALAR_EVENT) : (unsigned int) 6

(yaml_event_type_t) (YAML_SCALAR_EVENT) : (unsigned int) 6

(yaml_event_type_t) (YAML_SCALAR_EVENT) : (unsigned int) 6

(yaml_event_type_t) (YAML_SCALAR_EVENT) : (unsigned int) 6

(yaml_event_type_t) (YAML_SCALAR_EVENT) : (unsigned int) 6

(yaml_event_type_t) (YAML_SCALAR_EVENT) : (unsigned int) 6

(yaml_event_type_t) (YAML_SCALAR_EVENT) : (unsigned int) 6

(yaml_event_type_t) (YAML_SCALAR_EVENT) : (unsigned int) 6

(yaml_event_type_t) (YAML_MAPPING_END_EVENT) : (unsigned int) 10

(yaml_event_type_t) (YAML_SCALAR_EVENT) : (unsigned int) 6

(yaml_event_type_t) (YAML_MAPPING_START_EVENT) : (unsigned int) 9

(yaml_event_type_t) (YAML_SCALAR_EVENT) : (unsigned int) 6

(yaml_event_type_t) (YAML_SEQUENCE_START_EVENT) : (unsigned int) 7

(yaml_event_type_t) (YAML_SCALAR_EVENT) : (unsigned int) 6

(yaml_event_type_t) (YAML_SCALAR_EVENT) : (unsigned int) 6

(yaml_event_type_t) (YAML_SCALAR_EVENT) : (unsigned int) 6

(yaml_event_type_t) (YAML_SCALAR_EVENT) : (unsigned int) 6

(yaml_event_type_t) (YAML_SEQUENCE_END_EVENT) : (unsigned int) 8

(yaml_event_type_t) (YAML_SCALAR_EVENT) : (unsigned int) 6

(yaml_event_type_t) (YAML_SEQUENCE_START_EVENT) : (unsigned int) 7

(yaml_event_type_t) (YAML_SCALAR_EVENT) : (unsigned int) 6

(yaml_event_type_t) (YAML_SCALAR_EVENT) : (unsigned int) 6

(yaml_event_type_t) (YAML_SCALAR_EVENT) : (unsigned int) 6

(yaml_event_type_t) (YAML_SCALAR_EVENT) : (unsigned int) 6

(yaml_event_type_t) (YAML_SEQUENCE_END_EVENT) : (unsigned int) 8

(yaml_event_type_t) (YAML_MAPPING_END_EVENT) : (unsigned int) 10

(yaml_event_type_t) (YAML_MAPPING_END_EVENT) : (unsigned int) 10

(yaml_event_type_t) (YAML_MAPPING_END_EVENT) : (unsigned int) 10

(yaml_event_type_t) (YAML_MAPPING_END_EVENT) : (unsigned int) 10

(yaml_event_type_t) (YAML_MAPPING_END_EVENT) : (unsigned int) 10

(yaml_event_type_t) (YAML_DOCUMENT_END_EVENT) : (unsigned int) 4

(yaml_event_type_t) (YAML_STREAM_END_EVENT) : (unsigned int) 2

```

That works beautifully! Thank you!
