char print returns random character

Asked

Viewed 236 times

0

I compile the code below without errors. When I insert the word (or phrase desired) the printf returns a random character like 0 a etc. The program should read a word (or phrase) and the printf show the word (or phrase) that was written by the user.

I use gcc as a compiler. I’m compiling through the terminal at the moment. The distro I’m using is Manjaro 17.0.5 (I don’t know if this interferes with anything).

#include <stdio.h>

int main (void){

  char example[20];

  scanf("%*c", &example);
  printf("example: %c\n", example);

  return 0;
}
  • Why declare a vector and read only one character? Also I’ve never seen this * asterisk in scanf, it seems strange to me

  • According to that reference, the * ignores the reading of scanf. So, even if you put character address in the read, the * would read but would not store anywhere

  • You need to declare as String. A character vector is a string.

  • See more about reading with scanf here: https://answall.com/q/251918/64969

3 answers

3

You are trying to read a word/phrase:

The program should read a word (or phrase) and the printf show the word (or phrase)

But then uses %c indicating the character (only one):

scanf("%*c", &example);
printf("example: %c\n", example);

There are several solutions, and some have already been addressed in other responses such as scanf with %s.

If you want a robust solution use fgets, which allows it to specify the maximum number of characters to read and has the guarantee that it will never read more characters than the specified limit.

Example with fgets:

#include <stdio.h>

int main (void){

  char example[20];

  fgets(example, 20, stdin);
  printf("example: %s\n", example); //%s para string

  return 0;
}

Example in the ideone

The 3 parameters of fgets sane:

  1. Place where the read content is placed
  2. Maximum character size to read
  3. Where the reading is done, stdin indicates input stream that will match the keyboard

2


Try it this way, buddy:

int main(int argc, char** argv) {
    char example[20];

    scanf("%[^\n]", &example);
    printf("example: %s", example);
    return 0;
}
  • That’s just what I’ve been wanting! Thanks for sharing with me

  • You’re welcome, buddy. Good luck on your project !

  • @Raybm , see the message of my edit comment.

  • @Raybm, you don’t need to pass the vector address to read, do scanf("%[^\n]", example); would be enough

  • Here is made explicit why you do not need the & of the vector: https://answall.com/q/251918/64969

1

Your distribution does not interfere, I also use linux and the same problem. Your code didn’t test on a Windows OS, so I don’t know if whatever you developed will work. But about String, your code would look like this:

#include <stdio.h>

int main (void){

  char example[20];

  scanf("%s", &example);
  printf("example: %s\n", example);

  return 0;
}

You just have to be careful because the blank characters are ignored by the compiler. To solve, you would have to use another function to capture the String. But I believe that is not your case now.

  • 1

    The %s would not read the sentence provided by the question. Taking advantage, you do not need to pass the address of the character vector, just use scanf("...", example);, see more here: https://answall.com/q/251918/64969

Browser other questions tagged

You are not signed in. Login or sign up in order to post.