Thread: The problem of the usage of the Printf() in embedded systems

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    9

    The problem of the usage of the Printf() in embedded systems

    Dear Experts,
    I have a semi kind of philosophical question with regards to the usage of Printf() function. My teacher remarked printf() methods could be disturbing when we use that function e.g in the embedded system environment. So removing these printf() calls is preferred.

    Can you show me a illustrative example where it ( removing printf() ) is useful ? Besides of the embedded systems, Is there any other part where these Printf() calls would be disturbing ?

    Thank you very much for your help!

    Code:
    #include <stdio.h>
    int main()
    {
    	int result = sum( 4, 43);
    	printf("result: %d", result);
    	getch();
    }
    int sum ( int a , int b)
    {
    	printf("'a' variable: %d\n",a); // the disturbing printf() -s 
    	printf("'b' variable: %d\n",b);
    	printf("'summary: %d\n",a+b);
    	return a+b;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The printf() function itself isn't a problem.

    The context however is
    - you might have nowhere for the output to be visible.
    - if you do have an output, say a serial line, it might be tediously slow. If your code is time-critical, then that is an issue.
    - the implementation of printf() can be 10's of KB in size, which is a problem for memory constrained systems.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    137
    To some degree, these "issues" can even be recognized in non-embedded systems as well with a slightly exaggerated example.

    For example, if you write some code which counts to 5 million and then when it's finished, it printfs the word "Done," compare that to having it printf every single number as it is counting...

    The second program will be WAY slower and it's not because the computer can't count as fast, it's because it's running printf every single time and the ouput slows it down.

    Also, for the space complexity, if you build a program which calls printf on Windows compiled and linked as a x86-64 binary, open the program up in IDA Support: Freeware Version and find the printf function. As Salem said, you'll be surprised at the jungle of code that gets introduced in the program.
    If I was homeless and jobless, I would take my laptop to a wifi source and write C for fun all day. It's the same thing I enjoy now!

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    "disturbing" - That takes on many meanings... What do they mean by that specifically?

    My comment on that code is why have you got all those printfs? Your code does not need to send comments of everything that it is doing - That takes a lot of time to do, and if you are in an environment that doesn't use threads, everything is waiting for it to be done.

    You could also construct your own string converting the integers and utilise "puts" I suppose, but you will be reinventing the wheel.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ and Embedded Systems
    By Areal Person in forum C++ Programming
    Replies: 0
    Last Post: 03-14-2011, 11:17 AM
  2. Embedded Systems
    By Adamkromm in forum Tech Board
    Replies: 9
    Last Post: 06-27-2010, 04:08 PM
  3. embedded systems
    By chico1st in forum C Programming
    Replies: 9
    Last Post: 05-22-2008, 09:13 AM
  4. C for embedded systems
    By ekarapanos in forum C Programming
    Replies: 8
    Last Post: 04-01-2003, 12:10 PM
  5. What are embedded systems
    By Shadow12345 in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 09-26-2002, 02:18 AM

Tags for this Thread