简体   繁体   中英

To print something without using cout, printf or puts()

I had learned that

inline ostream & _Cdecl ostream::operator<< (const signed char * _s) {
    outstr(_s, (const signed char *)0);
    return *this;
}

is how the insertion operator (<<) is declared(overloaded) in the iostream.h header file. Can I possibly use the same function to print a string value on screen?

I tried

#include<iostream.h>
int main() {
    outstr("Hello world!", (const signed char *)0);
    return 0;
}

it ended up in error. I would like to use something like this in order to see if there is some possible way to answer this query of printing something on screen without using printf, cout or puts().

Update: I would welcome if you have any suggestions other than

#include<stdlib.h>
void main() {
    system("echo /"Hello world!/"");
}

NB: I have no restrictions if you can provide the C equivalent code that can print without a printf(), cout or puts()

Yes you could call the function directly, however your reasoning to do so is flawed. The time you save by eliminating the subroutine call to the operator is negligible when compared to the time taken to perform the actual function; this would be like closing the windows of your car while the convertible roof is down in order to reduce the rain.

The time required to make a function call is much , much smaller than the amount of time it takes to print your string. The amount of time you might save with your approach can (and usually should) be ignored.

If you want portability across all standards compliant C++ implementations, you can print a string to standard output in the following ways

const char * str = "Hello World\n";
fprintf(stdout, str);
fputs(str, stdout);
for (int i=0; str[i]!=0; ++i)
    putchar(str[i]);
for (int i=0; str[i]!=0; ++i)
    putc(str[i], stdout);
for (int i=0; str[i]!=0; ++i)
    fputc(str[i], stdout);
fwrite(str, sizeof(*str), strlen(str), stdout);

Additionally, you can use std::cerr and std::clog . They write to stderr instead of stdout , but from the user's perspective, that's often the same place:

std::cerr << str;
std::clog << str;

From an efficiency perspective, I doubt any of these are going to help you. For that purpose, you might want to look at something a bit more platform specific. For POSIX systems, see the answer given by Dave S . For Windows, see this link .

What you shouldn't do, is open up your header files and imitate what they use. At least, not at the middle levels, where they are using different various obscure functions within their own implementation. Those functions might not exist upon the next release. However, if you go to the deepest levels, you will find OS specific calls like the ones in the link I provided above. Those should be safe to use as long as you stay on the same OS, or even between OS versions.

On a UNIX type system, you can do the following.

#include <unistd.h>
#include <stdio.h>

int main()
  {
  const char x[] = "Hello World!";
  write(STDOUT_FILENO, x, strlen(x)); // Feel free to check the return value.
  return 0;
  }

I'm curious what your motivation for doing this would be. Outside of signal handlers, I'm reluctant to go to the lower level calls like this. The performance of the I/O will be the primary driver of time, not the intermediate function calls which are usually fairly heavily optimized / inlined.

You can directly use system calls.

http://docs.cs.up.ac.za/programming/asm/derick_tut/syscalls.html

This page, for example, explains linux system calls. You can start from the link I copied, and use many methods using assembly, or to say it in the other way, do something without calling the function of it.

But I'm guessing that was a trick question and if I had a company, I would never hire a person that uses system calls instead of functions.

This is an example of using sys_write(4) with standart output(1). You can inline assembly codes into your C/C++ code. http://docs.cs.up.ac.za/programming/asm/derick_tut/#helloworld

The extraction operator is overloaded in the ostream class. So you cannot actually use it without having an object of that class with it.

It is implemented in the following manner:

cout<<"Hii"; 

is equivalent to:

cout.operator<<("Hii")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM