Difference between "endl" and "\n" [duplicate] - c++

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
C++: “std::endl” vs “\n”
I'm wondering if there is any significant difference between these two ways to print newline :
cout << endl; //approach1
cout << "\n"; //approach2
Is there any practical difference?

Yes, they're different.
"\n" is just a string of length 1 that gets appended to stdout.
std::endl, instead, is an object that will cause to append the newline character ("\n") AND to flush stdout buffer. For this reason it will take more processing.

Related

Is std::cout thread safe for single output [duplicate]

This question already has answers here:
Is cout synchronized/thread-safe?
(4 answers)
How to easily make std::cout thread-safe?
(11 answers)
Closed last month.
Suppose there are several threads, each thread invokes
std::cout << 123;
for each statement, it should output 3 characters, '1', '2', and '3'. So does the C++ standard guarantees that the '1' '2' and '3' will not interleave? i.e., the system will not output the following result:
112233
Thanks

Strange behaviour while adding string to another string in C++ [duplicate]

This question already has answers here:
How can I repeat a string a variable number of times in C++?
(10 answers)
Closed 2 years ago.
one is 2, and ans is "000000".
string ans = "000000";
ans += string("1", one);
cout<<ans<<endl;
The output is:
0000001�
But I want the output:
00000011
What am I doing wrong?
string("1", one) does not do what you think it does. It does not duplicate the "1" string one number of times. It instead copies the 1st one number of chars from "1", which in this case is the '1' character and the '\0' null-terminator that follows it, which is where the � is coming from in the output. That is not what you want.
Use string(one, '1') instead. That will duplicate the '1' character one number of times, like you want, eg:
ans = "000000";
ans += string(one, '1');
cout << ans << endl;
Just use c++ strings and use + operator to catenate strings.

Different answers with single and double quotes while displaying in C++ [duplicate]

This question already has answers here:
What do single quotes do in C++ when used on multiple characters?
(5 answers)
Closed 6 years ago.
I have displayed a statement in C++ program with single quote and answer which i got was the random numbers where as when i used the double quotes in C++ it displayed me the statement.
cout << 'Hello world'; //gave me the random numbers
cout << "Hello world"; //displayed me the statement i.e Hello world
Why This happened pls do let me know and what those random numbers were at the time of execution ?
Single quotes are used for characters:
char c = 'A';
In case you want to print a string which is more than one character, you use double quotes:
cout << "Hello World";
You are trying to print an array of characters, which require double quotes.
char str[3] = "abc"; //an array of characters, use double quotes
str[0] = 'x'; //set a character in this array, use single quote

c++ program tries to read from stream after eof [duplicate]

This question already has answers here:
Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?
(5 answers)
Why is “while( !feof(file) )” always wrong?
(5 answers)
Closed 7 years ago.
I have a file containing only one character 1 (there is no newline symbol after it)
Reading the contents with this method:
int value;
ifstream in("somefile");
for (!in.eof())
{
in >> value;
cout << "input: " << value << " , eof" << in.eof() << "'\n";
}
gives me the following output:
input: 1 , eof: 0
input: 1 , eof: 1
The questions are:
1) Why EOF flag is not set after first reading try? I mean, if program successfully reads the number on the first try, it somehow knows that the string representatiion of it is over. To find it out it has to try read at least one byte after 1 character and there it should have hitted EOF. Why that doesn' happen?
2) That said, if I have a file with one value per line, I always do have a duplicate of last input. Does it mean that I always have to discard it in any way and that would be correct? Or for example add extra check for EOF after in >> value; and only if it succeeds, do any logic I want?
I know that I can work around with readline() or while(in >> value) methods but it's more a question of understanding what really happens there.
Thank you.

Strlen returns undefined behaviour with C++ [duplicate]

This question already has answers here:
Strlen returns unreasonable number
(4 answers)
Closed 8 years ago.
int main ()
{
char* tab=new char[14] ;
cout << " lenght with sizeof: "<<sizeof(tab)<<endl;
cout << " length with strlen: "<<strlen(tab)<<endl;
system(" pause");
return 0;
}
I got the output:
length with sizeof: 4
length with strlen: 30
I expect the result of sizeof but not what return strlen!
For those who will hurry to publish that it's a duplicate question.
I want to say that it's not the opportunity at all. Because I know about compile time and run-time and many other things concerning strlen and sizeof however I cannot find explanation to this result.
Thank you for help in advance.
Since you're allocating a char array but do not initialize it, strlen() will count from the beginning of the tab pointer to the first NUL character. So the result depends on the contents of your program's heap.