c++ - 代码块问题

标签 c++ printf codeblocks

您好,我正在做一项类(class)作业,我遇到了收到的错误消息,它们是:

error 'strtoul' was not declared in this scope
error 'print' was not declared in this scope
error 'printf' was not declared in this scope

我输入的代码是:

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");
}

但我不断收到错误信息:

error 'strtoul' was not declared in this scope
error 'print' was not declared in this scope
error 'printf' was not declared in this scope

无论我尝试什么,当我尝试声明它们时,我总是收到相同的错误消息,有什么帮助吗??

非常感谢,

最佳答案

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

#include <stdlib.h>
#include <stdio.h>

stdio库允许您进行输入/输出操作和 stdlib库定义了几个通用函数,包括将字符串转换为无符号长整数。

您需要将 print 方法移到 main 之前,您还应该将 ULL 更改为 NULL你调用 strtoul 因为我认为那是一个错字。您可以在我提供的链接中查看文档。

关于c++ - 代码块问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20253847/

相关文章:

C++ 从 Linux 上的剪贴板获取字符串

c++ - 调试断言失败:下标超出 std::vector 的范围

c - %a 转换如何在 printf 语句中工作?

c - 读取字符数组

c - 为什么在用户输入第一个命令后我会打印两次此消息?

c++ - "Process terminated with status -1073741819"带 vector 的简单程序

ubuntu - 如何在 Ubuntu 14.04 的代码块上启用 "Abbreviations"?

c++ - 当外部类被模板化时嵌套类的外联构造函数

c++ - 指针 vector 的 Push_back 方法导致 C++ 崩溃

c++ - 错误C2420:第一个操作数中的符号无效