-5

Console in CLion won't print line after scaning input, but in iTerm after compiling it all works perfectly. Code:

#include <stdio.h>
#include <stdlib.h>
int main() {
    char personName;
    printf("Hello, what is your name?\n");
    scanf("%s", personName);
    printf("Hello, %s\n", personName);
    return 0;
}

I just got this in CLion console:

Hello, what is your name?
Mike

Process finished with exit code 11
Paul
  • 13,042
  • 3
  • 41
  • 59
jovicbg
  • 1,523
  • 8
  • 34
  • 74
  • 1
    Undefined behaviour `personName` is not a string. –  Nov 17 '17 at 23:55
  • You need to allocate space for the string. `personName` is a single char, but you are trying to read a multi-character string into it. That leads to undefined behavior. – pstrjds Nov 17 '17 at 23:55
  • How it works in iTerm? @NeilButterworth – jovicbg Nov 17 '17 at 23:57
  • 1
    Half of the tags were completely irrelevant. Pick one C or C++, but know that if this is intended to be C++, you're being educated from sources about 25 years out of date. – user4581301 Nov 17 '17 at 23:57
  • Undefined behaviour has undefined results. I doesn't work in iTerm. It merely LOOKS like it works. – user4581301 Nov 17 '17 at 23:58
  • 1
    Compilers [warn about this](https://godbolt.org/g/AcPtSa). – chris Nov 17 '17 at 23:58
  • 1
    Worthwhile reading so that next time you see Process finished with exit code 11 you know what it means: https://stackoverflow.com/questions/31103254/meaning-of-exit-code-11-in-c – user4581301 Nov 18 '17 at 00:01
  • I just add [50] at the end of char personName and that's a solution. Thank you everyone. :) – jovicbg Nov 18 '17 at 00:01
  • 1
    That works until someone puts in a 51 character long name. – user4581301 Nov 18 '17 at 00:02
  • It's ok for now, If you know how to help, please, not just looking for mistakes in question. @user4581301 – jovicbg Nov 18 '17 at 00:05
  • No offence intended. It is OK for now. This is a common problem in C and not trivial to solve because you cannot easily change the size of an array based on the input. In C++, you use `std::string` and iostreams. `std::string name; std::cin >> name;` will work so long as you don't have a compound name like "Billy Bob". – user4581301 Nov 18 '17 at 00:12
  • 1
    @user4581301 Off by 1. With `add [50]`, that works until someone puts in a 50 character long name. – chux - Reinstate Monica Nov 18 '17 at 03:06
  • Yep. Forgot the terminator. My bad. – user4581301 Nov 18 '17 at 03:09

1 Answers1

1

You need to use an array for personName. The code will be,

#include <stdio.h>
#include <stdlib.h>
int main() {
    char personName[32];
    printf("Hello, what is your name?\n");
    if(scanf("%s", personName))
        printf("Hello, %s\n", personName);
    return 0;
}
Rahat Zaman
  • 1,322
  • 14
  • 32
  • 2
    Using a length as short as 10, while an order of magnitude better than the question, is not 'safe'; it is far too easy to overflow. It would be best to use `char personName[32];` and `if (scanf("%31s", personName) == 1) printf("Hello, %s\n", personName);`. The different-by-one on the lengths is unfortunate but hallowed by history and enshrined in the standards. – Jonathan Leffler Nov 18 '17 at 00:21