2

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
1
  • 1
    to add to the previous comment and to clear any confusion inputRoses can store only an empty string as strings in C require a null terminating character. Commented Jun 8, 2022 at 11:26

2 Answers 2

2

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("%49s", inputRoses);
  
  printf("\n%s", violets);
  scanf("%49s", inputViolets);
  
  printf("\n%s", and);
  scanf("%49s", 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!
Sign up to request clarification or add additional context in comments.

Comments

0

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;
}

1 Comment

scanf("%s", input); without a width limit like scanf("%19s", input); is poor programming.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.