Reading multiple strings with scanf

+1 Arend Peter Castelein · July 20, 2014
Hello. I'm new to c++ and I'm trying to do a programming challenge where I need to process 2 strings in one line but I'm getting an issue.
I've created a new file isolating the error. Here's what I have:
#include <stdio.h>

int main(){
char a[4],c[4];
int b,d;
scanf("%4s %d %4s %d",a,&b,c,&d);
printf("%s %d %s %d",a,b,c,d);
return 0;

Here's what I get when I input it to the GNU compiler in cmd
move 4 onto 5 // my input
 4 onto 5 //the output

It completed skips the reading of the first string. 

Thank you in advance for your help

Post a Reply


Oldest  Newest  Rating
+1 Mathias Frits Rørvik · July 20, 2014
First of all, you posted your code in the wrong section. This is C, not C++

Your 4 char long arrays are too small to hold a 4 character long string. You set aside space for 4 characters, but what is missing? 

What indicates the end of a string?
0 Arend Peter Castelein · July 20, 2014
My bad, I thought I put it in c++.

Thank you for the help. I looked up the size of chars in c++. I didn't realize it was only 1 byte. I have a Java background so I'm used to them being 2 bytes. I assume string characters in C++ are 2 bytes, is that correct? I tried looking up string character size but I couldn't really find anything. So if I were to use char[8] datatypes instead then would that fix the problem?

I looked into scanf. It returns the number of elements it was able to scan in. I was actually using my return type in my original code. Since the problem involved a variable amount inputs I was able to check for the end of the input this way. It looked something like this.
int main(){
char a[4],c[4];
int b,d;
while(scanf("%4s %d %4s %d",a,&b,c,&d)==4){
//do stuff with a,b,c & d
return 0;

I'm not too worried about input verification in this particular case since there's standardized input which doesn't come from users but I want keep good habits all the same so thanks for the advice.
+2 Mathias Frits Rørvik · July 20, 2014
Wait.. you're saying that you are learning C++? This looks like C code to me. If you were actually using C++, you should be using std::string, std::cin and std::cout. Header file should also be <cstdio> not <stdio.h>. Also you wouldn't need it, since you would be using <iostream>. Now are you learning C, or C++?

In either of the cases, which resources are you using to learn either on of them (book, videos etc.)? 


You don't need to look at what size the chars are, you need to figure out which CHARACTER indicates the end of the string. You'll get a more correct result if you increase the size of your char arrays by one. Then it'll fit the 4 characters and the character that indicates the end of the string.
0 Arend Peter Castelein · July 20, 2014
I'm not formally learning C++. I'm reading competitive programming by Steven Halim to learn problem solving strategies. The book recommends using C++ as opposed to Java since it's faster. Additionally all the examples are written in C++. 

I'm familiar with cin/cout however the book recommends using the c variation since it's a little faster.

So it sounds like char[5] should work then. I thought that c determined a string ended when there's a space but that applies when it's scanning the input not when it's being read from memory. In memory it needs an extra character at the end of each char[] to show that it's finished reading the string. Is that a correct summary?
+1 Mathias Frits Rørvik · July 20, 2014
Yes, basically. It's called the null terminator, it can be represented as \0
0 Arend Peter Castelein · July 20, 2014
Syntactic Fructose 
Thanks I'll keep that in mind. The book is specified towards competitive programming so I guess efficiency takes precedence over type safety. String formatting isn't an issue since input is standardized in competitions. I'll try not mix C and C++ concepts. I didn't realize cin/cout had those benefits, I'll stick to cin/cout for non-competitive purposes.

Mathias Frits Rørvik 
I tested it and the code works now. Thanks for the help.

Linguist Llama 
Here's the quote I'm referring to from the book. 

Tip 6: Optimizing Your Source Code
There are many tricks that you can use to optimize your code. Understanding computer hardware and how it is organized, especially in the I/O, memory, and cache behavior, can help you design better code. Some examples (not exhaustive) are shown below:

1. A biased opinion : Use C++ instead of Java. An algorithm implemented using C++ usually runs faster than the one implemented in Java in many online judges, including UVa [online judge]. Some programming contests give Java users extra time to account for the difference in performance.

2. For C/C++ users, use the faster C-style scanf/printf rather than cin/cout. For Java users, use the faster BufferedReader/BufferedWriter classes as follows:

BufferedReader br=new BufferedReader(
  new InputStreamReader(;
// Note: String splitting and/or input parsing is needed afterwards

PrintWriter pr=new PrintWriter(new BufferedWriter(
  new OutputStreamWriter(System.out)));
// PrintWriter allows us to use the pr.printf() function
// do not forget to call pr.close() before exiting your Java program

[continues to end up with a list of 12 items]
Competitive Programming 3 ~ Steven Halim & Felix Halim

[] are additions by me

The author has competed in and coached multiple teams up to the international level of ACM ICPC competitive programming, so I think it's fair to assume he knows what he's talking about. He may be referring to compilers specifically used on competition computers but as he says in this quote he does have a bias towards C++ since it's what he's always used so that could be it too.
+1 Arend Peter Castelein · July 23, 2014
I thought you guys might be interested to know that I did a experiment where I submitted a problem twice once with cin/cout and once with scanf/printf. scanf/printf ran in 0.019 seconds and cin/cout ran in 0.012 seconds. The difference is indeed minimal but I didn't expect it to favor cin/cout. Thanks again for clarifying this. I guess I'll email the author now.
0 Mathias Frits Rørvik · July 24, 2014
When writing C++, write C++ not C. =)
  • 1



Used in many types of software including music players, video games, and many large scale applications.

Bucky Roberts Administrator