scanf/printf not working when calling generated C code
Show older comments
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!
12 Comments
dpb
on 23 Feb 2026 at 14:59
What is the definition of failure to work here? What are the symptoms, specifically?
If you comment out the call to the function does the loop then run normally with whatever input/output being observed using scanf/printf? If so (and highly likely anyway) there's something wrong with the calling sequence or in the function itself. Without any other detail, it's not possible to say anything specific.
Victor Lam
on 23 Feb 2026 at 15:10
Probably no one has ever done precisely the same thing with the combination of environment and hardware so it's extremely likely the answer to that question is "No".
Knowing nothing of what the function does nor anything about the system you're running, it's unlikely anybody here can help.
If not calling the function removes the symptoms, then it is conclusive that something in either the calling/return linkage is broken or the function itself did something damaging outside returning its expected output(s). One next debugging step would be to create an empty null function in its stead and test calling it. If that is successful, then add the argument list alone, then the output arguments until find what step causes something to break. You could also try replacing the function call with something simple like timestwo.c and make sure the code generator can handle the simple case first. It's always possible there is something unique in the development environment it doesn't know about -- maybe a default convention assumed isn't the same as expected.
"... I use scanf/printf to get user input and to print to the serial monitor"
I would suspect that the serial connection got broken would be a fairly likely possibility...
If that doesn't uncover the problem, you'll probably have to submit the whole enchilada to Mathworks suppor.
Victor Lam
on 25 Feb 2026 at 16:41
dpb
on 25 Feb 2026 at 19:55
Aha! At least do have symptoms to research.
It's certainly possible there's a type mismatch but to say anything definitive would need to see both the snippet of MATLAB code with enough context to be able to verify variables and their types and then the resultant C code. Or, are you saying the printf is MATLAB code, not converted to C and so isn't the C runtime i/o library routine? If the latter, it would look like perhaps your C function isn't returning the same variable type as you are presuming it is. Again, we've got to be able to see what we're talking about...
Victor Lam
about 9 hours 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.
Again, "simplify, simplify"...can you write and successfully call a routine that does nothing but printf() a passed variable of varying type?
ADDENDUM
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
may be an issue -- the integer divide will be integer -- need to promote those to doubles first I think.
Victor Lam
about 3 hours ago
Being able to read the specific code would let folks have at least a chance of spotting an issue -- keeping it hidden does nothing to help advance diagnosing a problem. If I understand correctly, the printf() call is inside the C function. What if you were to return the additional variables to do the timing calculation to the calling routine -- can you get them back correctly and then compute and print the result in the calling MATLAB code instead? That doesn't solve the underlying problem but would perhaps confirm or deny that the internals of the function are doing what is expected.
In the above pseudo code cycles isn't defined -- one presumes it is the difference between the prior two - _after - before which is also going to be uiint64 so there is that issue of mixed mode arithmetic which otomh I don't recall exactly how C handles -- but
c=uint64(pi*1E4)
s=2E8;
t=c/s
t=c/s*1E6
with some just made up dummy numbers, MATLAB coerces to the integer type and the division is going to be integer -- here c<s so --> 0
Victor Lam
about 2 hours ago
Edited: Victor Lam
about 1 hour ago
You can inline any code as text -- the code button won't know syntax formatting rules specific to C but it will leave the linefeeds and so will be as well formatted as the code generator managed to do for perusal.
As I noted in very first response the odds are not high anybody here is going to have any experience with the specific environment and processor so you might have better luck from that standpoint on a C-specirfic forum with somebody spotting bogus code.
I would again bring your attention to the mixed mode operations -- I suspect that's a good chance of being at least part of the issue. My guess from the behavior described is that the io runtime is looking and failing to find a null-terminated string which is the C way of terminating loops on string arrays. That would be consistent with the other described symptoms of a literal string working where the variable doesn't. My guess is either you are referencing a memory location of the variable as a const -- that is the value seen by the printf() argument is probably the value of the variable being treated as the address rather than the address although I don't recall otomh what the C io library function will do if send the uint8 to a '%f' format.although I think all numeric types are ok for it.
What if you just compile and run a main{} program that calls printf -- can you get that to work passing various types and then expand it to mimic other operations?
In short, I don't have a lot of faith that anybody will be able to spot something from the C code, but there's absolutely no chance of anything further without it.
Victor Lam
10 minutes ago
Answers (0)
Categories
Find more on MATLAB Coder in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!