Question

In: Computer Science

C++: How are std::cout << “hello world” and std::cout << “hello” << ‘ ‘ << “world”...

C++: How are std::cout << “hello world” and std::cout << “hello” << ‘ ‘ << “world” different?

Solutions

Expert Solution

code:

#include <iostream>

int main()
{
std::cout << "hello world" ;
return 0;
}

Explain:

On compiling the above code, Hello World will be printed on the screen.

Let's start with std::cout << "Hello World"; - std is a namespace and cout is defined in this std namespace.

Here , in this code the space between two words hello and world is already give by user hence space will print automatically.

Basically, a namespace is a special area inside which something is defined. So, in this case, cout is defined in std namespace.

Thus, std::cout states that cout is defined in the std namespace or to use the definition of cout which is defined in std namespace.

So, std::cout is used to use the definition of cout from std namespace.

<< is the output operator.
here << is used to pass "Hello World" to cout. Thus, std::cout << "Hello World"; passes "Hello World" to cout and cout will print this Hello World on the screen.

code:

#include <iostream>

int main()
{
std::cout << "hello" << ' ' << "world" ;
return 0;
}

Explain:

In the above example, std::cout << "hello" << ' ' << "world" ; printed "Hello world" .

Thus, the space between two words hello and world given by user by using <<' ' that's why they seperated from each otherwise user not give space it will print like Helloworld i, e no space will be there .

std::cout << “hello world” The space is already given in program itself hence the output is Hello world the space is give already between two words hello and world
std::cout << “hello” << ‘ ‘ << “world”

The space is given using << ' ' if the space will not provide using using <<' ' the words will not seperate and print like Helloword

But because space is given it print output like Hello world


Related Solutions

***Convert the C++ to Python*** #include <iostream> using std::cout; using std::cin; using std::endl; int charClass; char...
***Convert the C++ to Python*** #include <iostream> using std::cout; using std::cin; using std::endl; int charClass; char lexeme[100]; char str[200]; char nextChar; const int LETTER = 0; const int DIGIT   = 1; const int UNKNOWN = -1; const int OPAREN = 2; const int CPAREN = 3; const int PLUS = 4; const int MINUS = 5; const int MUL = 6; const int DIV = 7; const int ID_CODE = 100; const int PLUS_CODE = 101; const int MINUS_CODE =...
#include "IntVariableTable.h" #include "Tokens.h" #include <assert.h> #include <iostream> #include <iomanip> using std::cout; using std::endl; using std::left;...
#include "IntVariableTable.h" #include "Tokens.h" #include <assert.h> #include <iostream> #include <iomanip> using std::cout; using std::endl; using std::left; using std::right; using std::setw; using std::string; // The IntVariableTable constructor dynamically allocates the fixed size array of integer variables. IntVariableTable::IntVariableTable() { int_variable_table = new IntVariableEntry[MAX_INT_VARIABLES]; } // The IntVariableTable destructor deallocates the integer variable array. IntVariableTable::~IntVariableTable() { delete[] int_variable_table; } // Returns the number of variables added to the integer variable table. int IntVariableTable::numVariables() const { return num_int_variables; } // Returns the index of...
#include using namespace std; int menu(){    int x;    cout<<"1.Add Entry\n";    cout<<"2.Edit Entry\n";   ...
#include using namespace std; int menu(){    int x;    cout<<"1.Add Entry\n";    cout<<"2.Edit Entry\n";    cout<<"3.remove Entry\n";    cout<<"4.print Entry\n";    cout<<"5.Close Console\n";    cout<<"Enter Your Choice:-";    cin>>x;              return x; } int main() {    int x;    int entry[1000];    int entnum = 0;    int num = 0;    int nom =0;     while (1)     {         int choice = menu();         if (choice == 1){            cout<<"A entry " <<...
#include <iostream> using namespace std; double print_instructions() { cout << "WELCOME TO BandN book stores" <<...
#include <iostream> using namespace std; double print_instructions() { cout << "WELCOME TO BandN book stores" << endl; cout << "Today we have a deal on e-books. Customers will receive a discount off their entire order.." << endl; cout << "The discount is 15% off your total order and cost per book is 8.99." << endl; cout << "Customers who buy 20 or more e-books will receive 20% off instead." << endl; cout << endl; return 0; } int no_of_books() {...
Given: #include <iostream> using std::cout; template <typename T> struct Node { T data; Node *link;   ...
Given: #include <iostream> using std::cout; template <typename T> struct Node { T data; Node *link;       Node(T data=0, Node *p = nullptr) { //Note, this constructor combines both default and parameterized constructors. You may modify the contructor to your needs this->data = data;        link = p; } }; template <typename T> class linked_list { Node<T> *head,*current; public: //default constructor linked_list() { head = nullptr;//the head pointer current = nullptr;//acts as the tail of the list } //destructor...
1) As mentioned in this chapter, C++ predefined identifiers such as cout and cin can be...
1) As mentioned in this chapter, C++ predefined identifiers such as cout and cin can be redefined by the programmer. However, why is it not wise to do so? 2) The text mentioned that the char data type can be cast into an int. What are some possible uses of this functionality? 3) Introduce the C++ data type string, which is a programmer-defined data type available in the C++ library. Define a string as a sequence of zero or more...
Print "Hello World" in the most complicated way (java)
Print "Hello World" in the most complicated way (java)
 VISUAL STUDIO (File Creation and Submissions)  FLOWCHART  Writing program in C++ ( cout,...
 VISUAL STUDIO (File Creation and Submissions)  FLOWCHART  Writing program in C++ ( cout, \n, \t, solving expressions )  Correcting errors Q1: Draw flow Chart of the following problems: a) Display “Hello World” on screen. b) Display Your Name, date of birth and mobile number on screen. c) Compute and display the perimeter and area of a rectangle with a height of 7 inches and width of 5 inches. d) Compute and display the perimeter and area...
Write a function in C++ that reads the line separates it with comma. Input: hello how...
Write a function in C++ that reads the line separates it with comma. Input: hello how are you. hello world hello_world I am there for you! Output: hello, how, are and you. hello and world hello_world I, am, there, for and you! just add a comma and (and) before the last word. CODE IN C++ ONLY.
Hello, In C++ how would I get it to ignore a percent sign in an input....
Hello, In C++ how would I get it to ignore a percent sign in an input. cout << "Enter the annual interest rate in %: "; cin >> interestRate; I want the user to input "3%" for example but having the % sign makes the program terminate. How can I get it to just read 3% (or any percent number) as just "3"
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT