1

I am programming a Teensy micro-controller as a part of a C course and am trying to work out the value of one of my integer variables. I have an integer variable called Contrast, which is initialised to the value of a constant defined as a hexadecimal number at the beginning of the .c file:

#define LCD_DEFAULT_CONTRAST    0x3F 
int Contrast = LCD_DEFAULT_CONTRAST;

I am trying to investigate how this Contrast value is stored and displayed, if it shows up as 63 or 0x3F, and if they are interchangeable. I tried to use:

printf("%d", Contrast);

to print out the Contrast value to the terminal and I got the error implicit declaration of function 'printf'. I thought printf() was part of the built-in C library, so I am confused why this is not working.

Can anyone please tell me how I print the value of this variable to the screen?

david_10001
  • 492
  • 1
  • 6
  • 22
  • 8
    so you **did** include `stdio.h`? –  May 05 '18 at 14:36
  • 1
    btw, this value is stored as a number, in **binary** ... for printing, you convert it to a string, and `%d` will result in a **decimal** number in that string, while `%x`, which is for **unsigned** numbers, results in a **hexadecimal** number This has nothing to do with how it is stored. –  May 05 '18 at 14:38
  • 4
    What do you mean by "without any libraries"? Why don't you want them? How does your micro-controller display anything? Monitor? Segment display? Pixel display? Morse-beeper? – Yunnosch May 05 '18 at 14:38
  • 3
    A cross-compiler for a microcontroller might need to have `printf` specifically enabled, because the function has a lot of code which may be dead weight on the memory available. – Weather Vane May 05 '18 at 14:48
  • "*how this Contrast value is stored and displayed, if it shows up as 63 or 0x3F*" - It's stored as a number. It's displayed however your code decides to display it. – melpomene May 05 '18 at 15:02

2 Answers2

4

The implicit declaration error just means your compiler proper doesn't have a declaration for printf. Unless you're also getting a linker error, the linker (linking usually follows compilation, unless you pass -c to disable it) is probably slapping the standard lib right on, in which case you can simply solve your warning by including stdio.h or less preferably by declaring int printf(char const*, ...);.

If you trully don't have the standard lib, you'll need to convert the integer to a string manually with something like:

int n = 42;
char buf[20];
char *end = buf+(sizeof(buf)-1), *p = end;
*p--=0;
if(n==0) *p=='0';
else{
    while(n){
        printf("%d\n", n%10);
        *p--=n%10+'0'; 
        n/=10;
    }
    p++;
}

and then pass it to your system's raw IO routine for which you'll need to have set up the system-entering assembly.

If you don't have a system, it'd be even more technical, and you probably wouldn't be asking this question.

Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
1

printf() is declared in standard library header <stdio.h>.

You have to #include <stdio.h> to use printf(). It is a library call, much like all other library calls in C..

  • 1
    `#include` in C. You are thinking of another language. (Technically, `printf` is **declared** in stdio.h.) – Pascal Cuoq May 05 '18 at 16:12
  • Edited. How can you even print to stdout without using any libraries. I checked souirce of stdio.h and it is full of #define and functions beginning with two underscores. – Ṃųỻịgǻňạcểơửṩ May 05 '18 at 16:14
  • 2
    "How can you even print to stdout without using any libraries" --> `` and the _standard library_ are different things. It is possible to use one and not the other - and cause this type of trouble. – chux - Reinstate Monica May 05 '18 at 17:41