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

#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...
 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.
C++ programming question class Address { public: Address(const std::string& street, const std::string& city, const std::string& state,...
C++ programming question class Address { public: Address(const std::string& street, const std::string& city, const std::string& state, const std::string& zip) : StreetNumber(street), CityName(city), StateName(state), ZipCode(zip) {} std::string GetStreetNumber() const { return StreetNumber; } void SetStreetNumber(const std::string& street) { StreetNumber = street; } std::string GetCity() const { return CityName; } void SetCity(const std::string& city) { CityName = city; } std::string GetState() const { return StateName; } void SetState(const std::string& state) { StateName = state; } std::string GetZipCode() const { return ZipCode; }...
Translate c++ code into mips assembly: int main() {                 cout << "Numbers:" << endl;            &nbs
Translate c++ code into mips assembly: int main() {                 cout << "Numbers:" << endl;                                 int x[] = {18, 12, 6, 500, 54, 3, 2, 122};                 int i;                                 for (i=0; i<8; i++)                 {                                                 cout << x[i] << endl;                 }                 return 0; } below is my code: .data        str1: .ascii "Numbers:"     str2: .ascii "\n"    x: .word 18,12,6,500,54,3,2,122       .text                      ...
Hello, I'm confused on part 'C' on how to graph the answer I received in part...
Hello, I'm confused on part 'C' on how to graph the answer I received in part 'B' B. A steam boiler is required as part of the design of a new plant. The types of fuel that can be used to ignite the boiler are natural gas, fuel oil, and coal. The cost of installation including all required controls is $40,000 for natural gas, $50,000 for fuel oil, and $120,000 for coal. In addition, the annual cost of fuel oil...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT