标签: c

跟踪#include依赖项的工具

有什么好建议吗?输入将是头文件的名称,输出应该是所有文件的列表(最好是树),包括它直接或间接.

c c++ header

170
推荐指数
8
解决办法
7万
查看次数

如何在C或C++中反转字符串?

如何在C或C++中反转字符串而不需要单独的缓冲区来保存反向字符串?

c c++ string reverse

170
推荐指数
13
解决办法
31万
查看次数

四舍五入到下一个2的幂

我想写一个函数,返回最近的2个数的下一个幂.例如,如果我的输入是789,输出应该是1024.有没有任何方法可以实现这一点而不使用任何循环但只使用一些按位运算符?

c optimization bit-manipulation

170
推荐指数
14
解决办法
16万
查看次数

静态变量存储在C和C++中的哪个位置?

在可执行文件的哪个段(.BSS,.DATA,其他)中存储了静态变量,以便它们没有名称冲突?例如:


foo.c:                         bar.c:
static int foo = 1;            static int foo = 10;
void fooTest() {               void barTest() {
  static int bar = 2;            static int bar = 20;
  foo++;                         foo++;
  bar++;                         bar++;
  printf("%d,%d", foo, bar);     printf("%d, %d", foo, bar);
}                              }
Run Code Online (Sandbox Code Playgroud)

如果我编译两个文件并将其链接到重复调用fooTest()和barTest的main,则printf语句将独立增加.有意义,因为foo和bar变量是翻译单元的本地变量.

但是存储分配在哪里?

需要明确的是,假设您有一个工具链可以输出ELF格式的文件.因此,我相信,有将一些空间,对于那些静态变量的可执行文件保留.
出于讨论目的,我们假设我们使用GCC工具链.

c c++ compiler-construction

169
推荐指数
10
解决办法
18万
查看次数

这段代码如何生成印度地图?

此代码打印印度地图.它是如何工作的?

#include <stdio.h>
main()
{
    int a,b,c;
    int count = 1;
    for (b=c=10;a="- FIGURE?, UMKC,XYZHello Folks,\
    TFy!QJu ROo TNn(ROo)SLq SLq ULo+\
    UHs UJq TNn*RPn/QPbEWS_JSWQAIJO^\
    NBELPeHBFHT}TnALVlBLOFAkHFOuFETp\
    HCStHAUFAgcEAelclcn^r^r\\tZvYxXy\
    T|S~Pn SPm SOn TNn ULo0ULo#ULo-W\
    Hq!WFs XDt!" [b+++21]; )
    for(; a-- > 64 ; )
    putchar ( ++c=='Z' ? c = c/ 9:33^b&1);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c obfuscation

169
推荐指数
2
解决办法
5万
查看次数

解释了将double转换为32位int的快速方法

在阅读Lua的源代码时,我注意到Lua使用a将a macro舍入double到32位int.我解压缩了macro,它看起来像这样:

union i_cast {double d; int i[2]};
#define double2int(i, d, t)  \
    {volatile union i_cast u; u.d = (d) + 6755399441055744.0; \
    (i) = (t)u.i[ENDIANLOC];}
Run Code Online (Sandbox Code Playgroud)

这里ENDIANLOC定义为endianness,0对于little endian,1对于big endian.Lua小心翼翼地处理字节序.t代表整数类型,如intunsigned int.

我做了一些研究,并且有一个更简单的格式macro使用相同的想法:

#define double2int(i, d) \
    {double t = ((d) + 6755399441055744.0); i = *((int *)(&t));}
Run Code Online (Sandbox Code Playgroud)

或者以C++风格:

inline int double2int(double d)
{
    d += 6755399441055744.0;
    return …
Run Code Online (Sandbox Code Playgroud)

c c++ floating-point performance

169
推荐指数
2
解决办法
1万
查看次数

为什么在使用rand()时会得到这种特殊的颜色模式?

我试图创建一个图像文件,如下所示:

uint8_t raw_r[pixel_width][pixel_height];
uint8_t raw_g[pixel_width][pixel_height];
uint8_t raw_b[pixel_width][pixel_height];
uint8_t blue(uint32_t x, uint32_t y)
{
    return (rand()%2)? (x+y)%rand() : ((x*y%1024)%rand())%2 ? (x-y)%rand() : rand();
}
uint8_t green(uint32_t x, uint32_t y)
{
    return (rand()%2)? (x-y)%rand() : ((x*y%1024)%rand())%2 ? (x+y)%rand() : rand();
}
uint8_t red(uint32_t x, uint32_t y)
{
    return (rand()%2)? (y-x)%rand() : ((x*y%1024)%rand())%2 ? (x+y)%rand() : rand();
}

for (y=0; y<pixel_height; ++y)
{
    for (x=0; x<pixel_width; ++x)
    {
        raw_b[x][y]=blue(x, y);
        raw_g[x][y]=green(x, y);
        raw_r[x][y]=red(x, y);
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望随机得到一些东西(白噪声).但是,输出很有意思:

你知道原因吗?


编辑

现在,很明显它与之无关rand().

也试试这段代码: …

c random image

169
推荐指数
2
解决办法
7457
查看次数

如何以标准方式修剪前导/尾随空格?

是否有一个干净的,最好是标准的方法来修剪C中字符串的前导和尾随空格?我会自己动手,但我认为这是一个同样常见的解决方案的常见问题.

c string whitespace trim

168
推荐指数
6
解决办法
31万
查看次数

168
推荐指数
7
解决办法
7万
查看次数

如何正确比较字符串?

我试图让一个程序让用户输入一个单词或字符,存储它,然后打印它,直到用户再次键入它,退出程序.我的代码看起来像这样:

#include <stdio.h>

int main()
{
    char input[40];
    char check[40];
    int i=0;
    printf("Hello!\nPlease enter a word or character:\n");
    gets(input);
    printf("I will now repeat this until you type it back to me.\n");

    while (check != input)
    {
        printf("%s\n", input);
        gets(check); 
    }

    printf("Good bye!");


    return 0;
}
Run Code Online (Sandbox Code Playgroud)

问题是我不断打印输入字符串,即使用户输入(检查)与原始(输入)匹配.我比较错误吗?

c string strcmp

168
推荐指数
6
解决办法
41万
查看次数