PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, August 18, 2022

[FIXED] What is in reality "endl" (or any output manipulator)? How is it implemented and how does it work with operator<<?

 August 18, 2022     c++, iomanip, output     No comments   

Issue

What is actually endl? Of course, it prints a new line and flushes the ostream buffer. But what is it actually? Can such "entities" like endl be defined by the programer?

There are these "output manipulators" which can be accessed using the library iomanip but what is actually going on when executing a command such as: cout << setprecision(5);

setprecision() looks like a function call, yet nothing is printed when using the cout instance. It changes the precision, but why simply not use the corresponding function member instead of adding more "abstraction" to code writing? By abstraction i mean non-intuitive code.

Thanks!


Solution

What is actually endl? Of course, it prints a new line and flushes the ostream buffer. But what is it actually?

std::endl is a function. It

Inserts a newline character into the output sequence os and flushes it


Can such "entities" like endl be defined by the programer?

Yes.

Here's a demo program.

#include <iostream>

std::ostream& test_manip(std::ostream& out)
{
   return (out << "In test_manip\n");
}

int main()
{
   std::cout << test_manip;
}

and its output.

In test_manip


Answered By - R Sahu
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing