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
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.
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?
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.
0 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.
  • 1



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

Bucky Roberts Administrator