代码阻止问题

Ben*_*min 2 c++ printf codeblocks

嗨,我正在做一个课程作业,我很难收到我得到的错误信息,他们是:

error 'strtoul' was not declared in this scope
error 'print' was not declared in this scope
error 'printf' was not declared in this scope
Run Code Online (Sandbox Code Playgroud)

我输入的代码是:

using namespace std;

int main (int argc, const char * argv[]) {

unsigned long int a, tmp;

a = strtoul("01011111000110001001001011010011",ULL,2);
print(a);

//We always work on "a" pattern
print(tmp = a >> 4);
print(tmp = a << 6);
print(tmp = a & (long int) 0x3);
print(tmp = a & (char) 0x3);
print(tmp = a | (unsigned short) 0xf00f);
print(tmp = a ^ (long int) 0xf0f0f0f0);

return 0;
}


//Function prints unsigned long integer in hexadecimal and binary notation
void print(unsigned long b)


{

    int i, no_bits = 8 * sizeof(unsigned long);
    char binary[no_bits];

    //Print hexadecimal notation
    printf("Hex: %X\n", b);

    //Set up all 32 bits with 0
    for (i = 0; i < no_bits; i++) binary[i] = 0;

    //Count and save binary value
    for (i = 0; b != 0; i++) {
        binary[i] = b % 2;
        b = b/2;
    }

    //Print binary notation
    printf("Bin: ");
    for (i = 0 ; i < no_bits; i++) {
        if ((i % 4 == 0) && (i > 0)) printf(" ");
        printf("%d", binary[(no_bits - 1) - i]);
    }
    printf("\n\n");
}
Run Code Online (Sandbox Code Playgroud)

但我一直得到错误消息:

error 'strtoul' was not declared in this scope
error 'print' was not declared in this scope
error 'printf' was not declared in this scope
Run Code Online (Sandbox Code Playgroud)

无论我尝试什么,当我尝试并声明它们时,我一直得到相同的错误消息,那里有任何帮助?

非常感激,

Dan*_*Dan 6

您需要在程序顶部包含这些头文件:

#include <stdlib.h>
#include <stdio.h>
Run Code Online (Sandbox Code Playgroud)

标准输入输出库让你做输入/输出操作和STDLIB库定义了几个通用的功能,包括将字符串转换为无符号长整型.

您将要移动你的print方法之前main,你也应该改变ULL,以NULL当你打电话strtoul,因为我相信这是一个错字.您可以在我提供的链接中查看文档.

  • 他还需要转发声明打印或将其放在main之前. (2认同)