Home > OS >  Something weird happens when I try to get user input and print out strings
Something weird happens when I try to get user input and print out strings

Time:06-09

I'm playing around with user input and printing strings. Whenever I run the code, the last string that I am trying to print is being glitched and doesn't print out correctly.

my code is:

#include <stdio.h>

int main() {
  char inputRoses[] = "";
  char inputViolets[] = "";
  char inputAnd[] = "";
  char roses[] = "Roses are: ";
  char violets[] = "Violets are: ";
  char and[] = "and: ";
  
  printf("\n%s", roses);
  scanf("%s", inputRoses);
  
  printf("\n%s", violets);
  scanf("%s", inputViolets);
  
  printf("\n%s", and);
  scanf("%s", inputAnd);
  
  return 0;
}

Got this in the console:

Roses are: red

Violets are: blue

ue

I also tried this:

#include <stdio.h>

int main() {
  char inputRoses[] = "";
  char inputViolets[] = "";
  char roses[] = "Roses are: ";
  char violets[] = "Violets are: ";
  
  printf("\n%s", roses);
  scanf("%s", inputRoses);
  
  printf("\n%s", violets);
  scanf("%s", inputViolets);

  return 0;
}

But got this in the console:

Roses are: red

ed

CodePudding user response:

You haven't allocated any memory for the scanned input. char inputRoses[] = "" is an array of one char that contains a null-termination character (value is 0)

You need to allocate some memory and then also limit scanf via a formatter to not overflow this memory space when writing the user input into the buffer.

#include <stdio.h>

int main() {
  char inputRoses[50] = "";
  char inputViolets[50] = "";
  char inputAnd[50] = "";
  char roses[] = "Roses are: ";
  char violets[] = "Violets are: ";
  char and[] = "and: ";
  
  printf("\n%s", roses);
  scanf("Is", inputRoses);
  
  printf("\n%s", violets);
  scanf("Is", inputViolets);
  
  printf("\n%s", and);
  scanf("Is", inputAnd);
  
  return 0;
}

Scanf will now limit the the size of the user's input to fit in the memory available for the input buffers. We use the size of the buffer minus one in order to allow for a null terminator at the end of the string.

Results are like:

$ ./a.out 

Roses are: red

Violets are: blue

and: stackoverflow has a big cazoo!

CodePudding user response:

You are trying to put many characters in one slot. You must create enough slots in memory for the maximum characters entered. It is also enough to use one input array as buffer unless you want to use them again in the code. Even if it is so, you better use a single input buffer and copy the entered characters to their final destination using strncpy library function.

#include <stdio.h>

int main() {
  char input[20]; // Must define the size (of character slots in memory) depending on the max possible input
  char roses[] = "Roses are: ";
  char violets[] = "Violets are: ";

  printf("\n%s", roses);
  scanf("%s", input);

  printf("\n%s", violets);
  scanf("%s", input);

  return 0;
}

CodePudding user response:

You should initialize your empty arrays like This:

char inputRoses[60] = "";
char inputViolets[50] = "";
char inputAnd[40] = "";

and your problem solved

this happens because you are declaring an empty character array rather you have to specify the size of your character array and if you want to get dynamic size area then use character pointers.

  •  Tags:  
  • c
  • Related