Why no printf ?

General discussion on mikroC PRO for AVR.
Author
Message
aCkO
Posts: 1119
Joined: 14 Feb 2011 04:07
Location: Bar, Montenegro

Re: Why no printf ?

#31 Post by aCkO » 28 Apr 2015 23:33

jsbhalla88 wrote:I am struggling to print data without printf....!!!
how can i format the data before printing..??
I have to display data corrected to 2 decimal places. I am not able to do that without printf.. may b I don't know how to do it, but it would been very easy to display using printf.....
Display it where? LCD? UART? As Janko already explained, use sprintf with UART_Write_Text.

Regards

seulater
Posts: 74
Joined: 19 Jan 2011 15:14

Re: Why no printf ?

#32 Post by seulater » 29 Apr 2015 01:44

jsbhalla88 wrote:I am struggling to print data without printf....!!!
how can i format the data before printing..??
I have to display data corrected to 2 decimal places. I am not able to do that without printf.. may b I don't know how to do it, but it would been very easy to display using printf.....
I had the same problem. Bottom line is this compiler is just a joke.

jsbhalla88
Posts: 57
Joined: 03 Mar 2015 15:23

Re: Why no printf ?

#33 Post by jsbhalla88 » 29 Apr 2015 14:35

isn't there any of doing it..????
even floatToStr() function is giving incorrect results.
just for testing the this function,
float test = 0.9;
char try[14];
then is used FloatToStr(test, tryy); to convert it to string to display on UART.
it displayed "8.999999e-1"

how to correct this..?????????????????????????????

aCkO
Posts: 1119
Joined: 14 Feb 2011 04:07
Location: Bar, Montenegro

Re: Why no printf ?

#34 Post by aCkO » 29 Apr 2015 17:40

jsbhalla88 wrote:even floatToStr() function is giving incorrect results.
You should learn more about IEEE754 before making such harsh statements. Number 0.9 cannot be represented accurately in binary (actual stored value is 0.899999976158142). If you need to display "0.90" (or something similar) you have to perform rounding, either by using sprintf or your customized formatting function.

For example:

Code: Select all

char txt[20];
float value = 0.9;

sprintf(txt, "%-.2f", value);
UART_Write_Text(txt);   // output: "0.90"
Regards

jsbhalla88
Posts: 57
Joined: 03 Mar 2015 15:23

Re: Why no printf ?

#35 Post by jsbhalla88 » 29 Apr 2015 17:45

aCkO wrote:
jsbhalla88 wrote:even floatToStr() function is giving incorrect results.
You should learn more about IEEE754 before making such harsh statements. Number 0.9 cannot be represented accurately in binary (actual stored value is 0.899999976158142). If you need to display "0.90" (or something similar) you have to perform rounding, either by using sprintf or your customized formatting function.

For example:

Code: Select all

char txt[20];
float value = 0.9;

sprintf(txt, "%-.2f", value);
UART_Write_Text(txt);
Regards
OH really..??
I didn't know that actually.
1.9 is displayed in a similar way, as scientific notation.
THEN why is any number greater than 10 displayed as it is..???
41.9 is displayed as 41.9 only..., and not 41.899999999
can you explain this..???

Also,
i am using PIC16F1709.
THERE IS NO sprintf() IN THIS MICROCONTROLLER...

aCkO
Posts: 1119
Joined: 14 Feb 2011 04:07
Location: Bar, Montenegro

Re: Why no printf ?

#36 Post by aCkO » 29 Apr 2015 18:18

aCkO wrote:1.9 is displayed in a similar way, as scientific notation.
THEN why is any number greater than 10 displayed as it is..???
41.9 is displayed as 41.9 only..., and not 41.899999999
can you explain this..???
Yes, I can. Your inductive reasoning is simply wrong (try with 411.9):

Code: Select all

41.9  => 41.9000015258789
411.9 => 411.899993896484
Floats are stored in binary in such a way that provides closest value to its decimal equivalent. "Closest" means that the value can be "higher than", "less then" or "equal to". Use Quick Converter tool in IDE to perform the conversion or some online converter (e.g. http://www.h-schmidt.net/FloatConverter/IEEE754.html) and play around with the last bit.
jsbhalla88 wrote:Also,
i am using PIC16F1709.
THERE IS NO sprintf() IN THIS MICROCONTROLLER...
One more reason not to use heavyweight printf. The solution is to use fixed-point math. See this topic: http://www.mikroe.com/forum/viewtopic.p ... 73#p252984.

Regards

jsbhalla88
Posts: 57
Joined: 03 Mar 2015 15:23

Re: Why no printf ?

#37 Post by jsbhalla88 » 29 Apr 2015 22:23

thanx aCKo for valuable information. I did not know anything about this.
and the link you provided, it worked. I have to implement in my code everywhere now. but, I did not understand the function properly.

thanks for your replay and help. :D

Милош
Posts: 6
Joined: 04 Oct 2015 18:17
Location: Serbia

Re: Why no printf ?

#38 Post by Милош » 04 Oct 2015 19:36

Why didn't all of you people write this yourselves, rich, experienced, employed, as opposed to me?
Why didn't Mikroelektronika's people solve this, if they even have those people who created the compiler now, I doubt they work there any more ?

You can create the function int printf(const char *format, ...)
using the existing signed int sprintf(char *wh, const code char *f,...);, the only thing my solution does not take into account is the help file does not say sprintf returns -1 if there is an error, so if your code expects only -1 and not 0 for an error you need to test if the sprintf returns -1 when there is error in the output... probably does, return value is signed int.

If you looked at the help file for the implemented sprintf function, you would have seen it has the most important features of the printf, the same syntax for parsing the format string, most of the time it would be compatible with the printf format string. You can't implement the printf as a wrapper function yourself, because the mikroC does not allow you to have multiple function definitions with various arguments, and also does not allow for an undefined number of parameters in a user function, also C language can not distinguish between nonstruct blank pointers runtime. But the sprintf function does all this.

Now you define several macros with parameters, for 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, however many of parameters you need, which replace the printf call with a sprintf call, followed by a UART1_Write_Text. I am talking about writing a macro for every number of arguments in a printf call you need. I have defined macros with parameters in mikroC, it works great, but you have to test how it works a little, the help file does not go into detail of exactly how, you need to try. Now you add a \n in between the sprintf command and the UART1_Write_Text command of the macro, which needs to be in a single line. And you have a compile time solution for this compatibility problem you all want solved. I will not write the code for you, I want you real professionals here to write the code, because if I know how to do it, you hard core professionals must know it too. Or am I the best coder here, having almost no experience? :)

This is how you define a macro

Code: Select all

#define macro_identifier(<arg_list>) <token_sequence>
the identifier is

Code: Select all

printf(format, arg1, ... a list of arguments );
the token sequence is the

Code: Select all

sprintf(string, format, arg1, ... a list of arguments );
then you write the line delimiter \n or /n, try it :), then you write the UART1_Write_Text(string); and press enter
and you declare the

Code: Select all

char string[50];
or something in the new printf.c header file before the definition of printf macro.
Don't forget to

Code: Select all

UART1_Init();
in the main() when you use this.
Did you get it, professionals from MikroElektronika, and the other professionals who really earn money for themselves with their coding?
I am kidding, I am sure this will work, try it. I give this info to all the people who have been mistreated by mikroC bugs and the lack of printf, free of charge. :)

Милош
Posts: 6
Joined: 04 Oct 2015 18:17
Location: Serbia

Re: Why no printf ?

#39 Post by Милош » 06 Oct 2015 14:25

I edited post to add code snippets and it got deleted? And I solved this whole thing in it?
I said you can easily build your own printf, using macros with parameters,
because sprintf has the same format string parsing just make one macro for every number of arguments of printf, because variable number of arguments is not allowed in mikroC but it is in sprintf.
This is what you do

Code: Select all

#define macro_identifier(<arg_list>) <token_sequence>
and I will not write the code because if I can do it with almost no experience all of you professionals can.
The identifier is the

Code: Select all

printf(format, arg1, ... arguments);
and the sequence is the sprintf(string, format, arg1, ...); then you type line delimiter \n or /n, try it, then UART1_Write_Text(string); everything in one line, and you declare char string[50]; or something before macros in printf.c header.
You also put UART1_Init(); in the main() when you use this. Did you delete my post because I wrote your professionalism is doubtful if you don't know how to do this for 5 years ?
I guess I will have to come to you personally here in Serbia then to ask you about why you make so many bugs and cost my team so much time, and we use a licenced version of mikroC. Not just another foreigner you can just delete from the forum.

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: Why no printf ?

#40 Post by filip » 07 Oct 2015 09:43

Hi,
I edited post to add code snippets and it got deleted? And I solved this whole thing in it?
Did you delete my post because I wrote your professionalism is doubtful if you don't know how to do this for 5 years ?
It didn't get deleted, but since you are a new forum user, we must approve manually first 5 posts so it can be seen on the forum.
I guess I will have to come to you personally here in Serbia then to ask you about why you make so many bugs and cost my team so much time, and we use a licenced version of mikroC. Not just another foreigner you can just delete from the forum.
We didn't delete anybody from the forum, as I explained just before.

Which bugs are you referring to exactly ? We appreciate your feedback so we can improve the compiler.
Why didn't all of you people write this yourselves, rich, experienced, employed, as opposed to me?
Why didn't Mikroelektronika's people solve this, if they even have those people who created the compiler now, I doubt they work there any more ?
We appreciate your effort, surely our users will benefit from this.

For every compiler there is a timetable/priority list, so basically everything our users suggest or encounter, we report to our developers
and they put thins into this list.

Thank you for the contribution. :)

Regards,
Filip.

aCkO
Posts: 1119
Joined: 14 Feb 2011 04:07
Location: Bar, Montenegro

Re: Why no printf ?

#41 Post by aCkO » 09 Oct 2015 01:50

Милош wrote:You can't implement the printf as a wrapper function yourself, because the mikroC does not allow you to have multiple function definitions with various arguments
The term you were looking for is "function overloading", but that's not the reason why you can't implement the wrapper function. Function overloading is a feature of C++ and no ANSI C compiler supports it.
Милош wrote: and also does not allow for an undefined number of parameters in a user function
How did you come to that conclusion? Ever heard of variadic functions and ellipsis operator in C?
Милош wrote: also C language can not distinguish between nonstruct blank pointers runtime.
Not sure what you meant by that.
Милош wrote:But the sprintf function does all this.
Милош wrote:because variable number of arguments is not allowed in mikroC but it is in sprintf.
And how do you think sprintf function was written? Using magic?
Милош wrote:Now you define several macros with parameters, for 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, however many of parameters you need, which replace the printf call with a sprintf call, followed by a UART1_Write_Text. I am talking about writing a macro for every number of arguments in a printf call you need.
A valid, but very primitive solution for someone having aspirations to become a good programmer.
Милош wrote: And you have a compile time solution for this compatibility problem you all want solved. I will not write the code for you, I want you real professionals here to write the code, because if I know how to do it, you hard core professionals must know it too.
Seriously?! So why did you make those comments? To brag about solving a "problem" we were all trying, but were unable to solve? If that's the case, here's a little homework for you:

Code: Select all

#define printf(f, ...)   do {sprintf(buffer, f, __VA_ARGS__); UART1_Write_Text(buffer);} while (0)
How does that compare to your solution?
Милош wrote:Or am I the best coder here, having almost no experience?
Experience alone does not make a good programmer. It is necessary, but not sufficient.
Милош wrote:Did you get it, professionals from MikroElektronika, and the other professionals who really earn money for themselves with their coding?
Yes, we got it.
Милош wrote:I am kidding, I am sure this will work, try it. I give this info to all the people who have been mistreated by mikroC bugs and the lack of printf, free of charge.
Well, thank you, in case you expect some gratitude.
Милош wrote:and I will not write the code because if I can do it with almost no experience all of you professionals can.
We sure can, but we can live without it quite well.
Милош wrote:Did you delete my post because I wrote your professionalism is doubtful if you don't know how to do this for 5 years ?
It's not like someone has been trying to "solve" this "issue" for 5 years. Also, it's not like most of us missed printf that much (beginners not included). If someone is capable of writing compilers for 7 architectures, do you really think this presents a challenge for them? Your priorities as a beginner are not obliged to coincide with that of compiler developers.

Regards

Милош
Posts: 6
Joined: 04 Oct 2015 18:17
Location: Serbia

Re: Why no printf ?

#42 Post by Милош » 09 Oct 2015 14:38

You sure told me off, just like you did to the man needing printf here
Display it where? LCD? UART? As Janko already explained, use sprintf with UART_Write_Text.
Helpful indeed.
I suppose you are one of the people writing the compilers?
Then the state of them is much clearer to me, reflecting your attitude towards work and people. You know, being from Montenegro and all. :) Don't hate me just for being from Serbia like some people from Montenegro do. And if you do,take all your compatriots to your country to not hate them too. Of course I wouldn't expect gratitude from you, I was trying to help someone asking for help.

But it's nice I made you actually try to help here.
Nice to see you can.
Just don't want to before someone calls you out.
Or is it?

If you are helping the compilers, how about you go about implementing a working ADC function for AVR and PIC which have adc on PORTC, which is causing more and more people all over the internet to want their money back and persuade other people not to buy mikroC.

About your solution, it is 1. second to be available, 2. the same as mine is compile time, maybe mine is smaller compiled.

aCkO
Posts: 1119
Joined: 14 Feb 2011 04:07
Location: Bar, Montenegro

Re: Why no printf ?

#43 Post by aCkO » 10 Oct 2015 01:27

Милош wrote:I suppose you are one of the people writing the compilers?
No, I am not, nor am I in any way affiliated with MikroElektronika.
Милош wrote:Don't hate me just for being from Serbia like some people from Montenegro do. And if you do,take all your compatriots to your country to not hate them too.
You're a little off topic, but have it your way. May I remind you that Serbs make one third of population in Montenegro (myself included)? And what does nationality have to do with anything discussed here?
Милош wrote:Of course I wouldn't expect gratitude from you, I was trying to help someone asking for help.
How exactly did you help anyone? You didn't provide even one valid line of code.
Милош wrote:But it's nice I made you actually try to help here.
Nice to see you can.
Just don't want to before someone calls you out.
Or is it?
You didn't make me do anything that I haven't already be doing for the last 4 years here in the forums.
Милош wrote:If you are helping the compilers, how about you go about implementing a working ADC function for AVR and PIC which have adc on PORTC, which is causing more and more people all over the internet to want their money back and persuade other people not to buy mikroC.
You do realize that this is library you are talking about, not the compiler? If you tried yourself to write the library in question, it wouldn't have taken you more than 10 minutes to do so. Let me help you with that:

Code: Select all

void ADC_Init() {
  ADMUX |= (1 << REFS0);
  ADCSRA = (1 << ADEN) | (7 << ADPS0);
}

unsigned int ADC_Read(unsigned char ch) {
  ADMUX &= 0xF0;
  ADMUX |= ch;
  ADCSRA |= (1 << ADSC);
  while (!(ADCSRA & (1 << ADIF)));
  ADCSRA |= (1 << ADIF);

  return (ADCH << 8) | ADCL;
}
Feel free to use the datasheet to tweak the code according to your needs.
Милош wrote:About your solution, it is 1. second to be available, 2. the same as mine is compile time, maybe mine is smaller compiled.
1. What was the first?
2. It is carried out by the preprocessor, not the compiler. Mine is generic, yours is format specific. Also, the code size is the same because this is how the block of code is normally included in the preprocessor macro. The false condition in the while loop makes the compiler ignore the loop so it doesn't affect code size. If you are interested why the preprocessor is used this way and avoid macro expansion "gotchas", you can read it here.

Regards

Милош
Posts: 6
Joined: 04 Oct 2015 18:17
Location: Serbia

Re: Why no printf ?

#44 Post by Милош » 12 Oct 2015 07:11

Preprocessor directives create "compile time" solutions, "run time" is code execution time (like the ellipsis ennumeration).

Then Mikroelektronika should put you on contract for a part time job, repairing 15 of these "little problems for begginers" weekly, also explaining the fixes to all the "begginers" across the internet some of who are retired professional coders who started coding using STANDARD COMMANDS in the sixties or seventies, example http://www.rcgroups.com/forums/showthread.php?t=2473035
This would result in big added revenue weekly for Mikroelektronika, from people who decided they don't like it having a non working standard library. :)
(also when some people with arduino AVR boards test mikroC and see ADC not working, they will not buy it.)


Your allegiance shoud be to God and people using your software, not the language you write "perfect in all aspects" code in. It is more important to deliver what you promise and sell to people, then exactly how you do it, if you look at implementations of functions in any C compiler and also mikroC, you will see you can probably do it better, but the implementation works great(except the one I mentioned here).

Much more important aspects of software are, collecting all software demands, answering them, having tested code in all stages, more testing... when there is a library which gets used as an include noone ever sees the perfect minimalistic code, only how it looks in an assembler listing.

PPK
Posts: 18
Joined: 08 Jan 2018 06:27

Re: Why no printf ?

#45 Post by PPK » 21 Mar 2018 22:56

Hi,

@aCkO I do not understand how the below code works.

Code: Select all

#define printf(f, ...)   do {sprintf(buffer, f, __VA_ARGS__); UART1_Write_Text(buffer);} while (0)
How do you use this within the source code?

Richard Lowe wrote a "print" and placed it in the LibStock but this code does not work. At least with the latest version of MikroC and on the XMega chips.
https://libstock.mikroe.com/projects/vi ... rt-avr-arm

Code: Select all

void prtChar(char c){
     UARTF0_Write(c);
}
char *happyTxt = "I like it alot";
void main(){
    printf_init( prtChar );
    printf( "My Text: %s\r\n", happyTxt );

This just produces a stream alphabet output ie aaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbccccccccccccccccccdddddddddddddddddddd etc.
Not sure if it is possibly related to the Optimization Level (Four) and Enable SSA Optimization? I have tried various version of these.


And then there is the MikroC "PrintOut" function
I tried this before I read the fine print.
Image

Post Reply

Return to “mikroC PRO for AVR General”