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?

4

2 回答 2

4

隐式声明错误仅意味着您的编译器本身没有printf. 除非您也遇到链接器错误,否则链接器(链接通常在编译之后,除非您通过-c禁用它)可能会直接在标准库上打耳光,在这种情况下,您可以简单地通过包含stdio.h或更少通过声明来解决您的警告int printf(char const*, ...);.

如果您真的没有标准库,则需要手动将整数转换为字符串,例如:

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++;
}

然后将其传递给系统的原始 IO 例程,您需要为此设置系统输入程序集。

如果您没有系统,它会更具技术性,您可能不会问这个问题。

于 2018-05-05T15:04:33.380 回答
1

printf()在标准库头文件中声明<stdio.h>

你必须#include <stdio.h>使用printf(). 它是一个库调用,就像 C 中的所有其他库调用一样。

于 2018-05-05T16:05:18.053 回答