Printing to the console code snippet
Summary
Source code
C++: print_to_console.cpp
#include <iostream>
using namespace std;
int main() {
// cout is an output stream, that represents the standart output stream
// "<<" operator is known as "insertion operator"
// is outputs data to the stream
cout << "A string";
// to insert a newline, output manipulator endl
cout << endl;
// or one can combine those two outputs in one line of code
cout << "Another string" << endl;
// one can output a number in a similar way
cout << "Number: " << 7 << endl;
// a trick you will learn in "if statement" snippet
cout << "Is 2 + 2 equal to 4? " << boolalpha << (2 + 2 == 4) << endl;
return 0;
}
C: print_to_console.c
#include "stdio.h"
int main() {
// prints "A string to console"
printf("A string");
// inserts a newline
printf("\n");
// can be done together
printf("Another string\n");
// prints a number
// %d shows, that one want to print integer number
printf("Number: %d\n", 5);
// boolean trick from C++ code snippets
// will output only "1" (true in a "machine language")
printf("Is 2 + 2 equal to 4? %d\n", (2 + 2 == 4));
return 0;
}
Sample run
C++
A string
Another string
Number: 5
Is 2 + 2 equal to 4? true
C
A string
Another string
Number: 5
Is 2 + 2 equal to 4? 1