0
// merge.cpp
// merges two containers into a third
#include <iostream>
#include <algorithm> //for merge()
using namespace std;
int src1[] = { 2, 3, 4, 6, 8 };
int src2[] = { 1, 3, 5 };
int dest[8];
int main()
{ //merge src1 and src2 into dest
    merge(src1, src1+5, src2, src2+3, dest);
    for(int j=0; j<8; j++) //display dest
    cout << dest[j] << '  ';
    cout << endl;
    return 0;
}

this is the code that I typed and what I expected was a sorted merged array, but the output came out to be :

1210537622105376321053763210537642105376521053766210537682105376

I varied the data, rechecked the syntax but every thing is fine , I think

Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
rishabh jain
  • 108
  • 1
  • 8

3 Answers3

7

In c++ this is a character ' ' (single quotation marks) and this is a string " " (double quotation marks). Avoid to use single quotation marks for more than one character as it is implementation defined.

Fix

cout << dest[j] << '  ';

to

cout << dest[j] << ' '; // remove one whitespace

or

cout << dest[j] << "  "; // change to string

A multi-character character constant is interpreted as integer. In your case ' ' is 2105376. With g++ and clang++ ' ' is translated to 8224 = 256 * ' ' + ' '. In your example either you forgot one whitespace or your compiler translates with a different method: 2105376 = 256 * 256 * ' ' + 256 * ' '+ ' '

Enable and read compiler warnings. This was the first thing the compiler told me.

:13:24: warning: multi-character character constant [-Wmultichar]
Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
  • 1
    @rishabh Note: your compiler seems to be interpreting `' '` as `2105376` which explains your output – Alan Birtles Dec 03 '18 at 16:19
  • 1
    they are allowed but their interpretation is implementation defined – Alan Birtles Dec 03 '18 at 16:21
  • 2
    It **is** allow d to have more than one character between single quotes, but it’s not generally useful. It’s a [multi character literal](https://en.cppreference.com/w/cpp/language/character_literal) (see item 6); it’s type is `int` and its value is implementation-defined. – Pete Becker Dec 03 '18 at 16:25
  • @PeteBecker: Although this is one use: https://stackoverflow.com/questions/45550674/this-source-code-is-switching-on-a-string-in-c-how-does-it-do-that/45550696#45550696 – Bathsheba Dec 03 '18 at 16:26
  • 1
    @Bathsheba — that’s clever, but I’d be more inclined to use named constants. They’re less prone to compiling when there’s a typo. – Pete Becker Dec 03 '18 at 18:00
  • @PeteBecker: Absolutely! – Bathsheba Dec 03 '18 at 18:26
6

' ' is a multicharacter literal, as it encloses more than one character. It's an int type and has an implementation defined value, although 256 * ' ' + ' ' is common. Because it's an int, a different ostream overload to the char one is used, which accounts for your output.

Use " " instead.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
-1

The mistake in this code is very obvious. But yet if someone needs it in future, I can tell you what mistake I had done in this code.

Location of mistake

cout << dest[j] << '  '; //<----------the mistake is here,

Theory

Single quotes are in c/c++ used to enclose single characters. Like 'c', '+' similarly ' '.

Blank space in c/c++ is considered a single character. Double quotes are used to enclose multi-character strings.

Solution

So either replace '' by " ", or replace ' '(two blank spaces) by ' '(single blank space).

rishabh jain
  • 108
  • 1
  • 8