-1

I was learning some string handling in C++ and was doing hit and trail on a code and surprisingly got output for the given code.

#include<bits/stdc++.h>
using namespace std;
int main(){
    char str[12]={'\67','a','v','i'};
    cout<<str;
    return 0;
}

Surprisingly I get 7avi printed .

But if I replace '\67' with '\68'. The following error is shown on Repl.it (https://repl.it/languages/cpp)

#include<bits/stdc++.h>

using namespace std;

int main(){
    char str[12]={'\68','a','v','i'};
    cout<<str;
    return 0;
}



main.cpp:6:19: warning: multi-character character constant [-Wmultichar]
    char str[12]={'\68','a','v','i'};
                  ^
main.cpp:6:19: error: constant expression evaluates to 1592 which cannot
      be narrowed to type 'char' [-Wc++11-narrowing]
    char str[12]={'\68','a','v','i'};
                  ^~~~~
main.cpp:6:19: note: insert an explicit cast to silence this issue
    char str[12]={'\68','a','v','i'};
                  ^~~~~
                  static_cast<char>( )
main.cpp:6:19: warning: implicit conversion from 'int' to 'char' changes
      value from 1592 to 56 [-Wconstant-conversion]
    char str[12]={'\68','a','v','i'};
                 ~^~~~~
2 warnings and 1 error generated.
compiler exit status 1

Please someone explain this behavior.

  • 3
    [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – R Sahu May 28 '19 at 04:01

2 Answers2

5

The \nnn notation, where nnn are digits between 0 and 7, is for octal (base 8) notation. So in \68, 68 is not a valid octal number. The number one more than 67 is 70. The way it's interpreting the code is that you have '\6' as character 6 in octal, but then an additional '8' ASCII character inside your character literal - hence a multi-character constant, which can not be stored in a char variable. You could store it in a "wide character":

wchar_t str[12]={'\68','a','v','i'};

But, there is no operator<< overload to display an array of wchar_t, so your cout << str line will match the void* overload and just display the memory address of the first element in the array, rather than any of the characters themselves.

You can fix that using:

wcout << str;

Separately, I recommend putting a newline after your output too. Without it, your output may be overwritten by the console prompt before you can see it, though that doesn't happen in the online REPL you're using. It should look like:

wcout << str << '\n';
Tony Delroy
  • 94,554
  • 11
  • 158
  • 229
  • I doubt the OP actually wants wide characters. They just wanted ASCII #67 to appear instead. That's obviously me reading into it, though. –  May 28 '19 at 05:53
  • @Chipster: yeah - OP said he was just learning by trial and error, so while I'm also sure he had no intention of using wide characters, serendipity has brought them to his attention.... – Tony Delroy May 28 '19 at 08:24
  • 1
    @TonyDelroy Thank you so much Sir. Now I get an insight of what was happening actually. – Gemar Launda May 28 '19 at 09:56
0

I think you're trying to type in an ASCII character using either octal or hex.(octal usually begins with a 0, but hex with a 0x). Just don't put the the ASCII code in quotes, instead put the code straight into the array, like so:

char str[12] = {68, 'a', 'v', 'i'}; //decimal 
char str[12] = {0x44, 'a', 'v', 'i'}; //hex  
char str[12] = {0104, 'a', 'v', 'i'}; //octal

Side Note

Please don't use <bits/stdc++.h>. It's not standardized(see here, for a more detailed explanation.) Instead include <iostream> for cout and the other requisite libraries for your other needs.

Dylan Gentile
  • 106
  • 1
  • 5