scanf/printf not working when calling generated C code

61 views (last 30 days)
Victor Lam
Victor Lam on 23 Feb 2026 at 11:36
Commented: dpb 33 minuten ago
In Matlab I have generated C code (.c/.h files) from a Matlab function using codegen from Matlab Coder.
I call this function in a while loop on an ARM quad-core Cortex A53 using AMD Vitis.
Before calling this function I use scanf/printf to get user input and to print to the serial monitor. However, scanf/printf only work once in the 1st iteration (before calling the function) and not in the 2nd iteration or later (after calling the function).
Is this a common problem and has anyone encountered this problem before?
I think, but am not sure, that the problem is in the generated C code and not in Vitis itself.
Thank you in advance!
  7 Comments
Victor Lam
Victor Lam ongeveer 4 uur ago
The printf is the one in C code and I think I found what the cause is of the problem. Here are the most important steps that cause printf to get stuck:
  • obtain the variables cycles_before and cycles_after which are of type uint64_t
  • define the variable clock_speed which is of type const double
  • compute double CPU_time = cycles / clock_speed * 1e6;
  • print the variable with printf("CPU time: %f us\n", CPU_time);
Hopefully, this makes it a bit more clear, let me know if you need more information!
dpb
dpb 33 minuten ago
Without the actual code, it's not possible to say anything definitive. The internal variable definitions and typing and possible conversions and how are retrieved/saved in memory and addressed in the calls are the critical points and cannot be determined explicitly without the code itself.
That a literal string does work and the floating point variable doesn't is pretty definite indication there's a mismatch in there somewhere between what is being passed and what the function prototype is expecting. It's possible there's a mismatch in the default floating point size type in the compiler flags other than inside the code itself, too, you need to confirm what the compiler is being told.

Sign in to comment.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!