小编N 1*_*1.1的帖子

如何找到机器是32位还是64位

无论如何,从C编程中找出操作系统当前是以32位还是64位模式运行.我正在使用如下的简单程序

int main(void){
     switch(sizeof(void*)){
        case 4: printf("32\n");
        break;
        case 8: printf("64\n");
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是正确的方法吗?这个代码是否适用于所有场景,例如,如果硬件是64位且操作系统是32位,它会返回什么?我没有机器在diff配置中测试它.

感谢您的建议.

c 32bit-64bit

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

gcc:使用-Werror和-pedantic被认为是好习惯吗?

我只是深入研究gcc手册,有些事情我还不清楚:

  1. 指定std时,我是否应该始终使用-pedantic?
  2. 使用-g时,标准级别是否足够,或者我应该指定级别3,即-g3?
  3. 使用-Werror来推广所有错误警告和-pedantic-errors以促进所有迂腐警告错误是一种好习惯吗?

gcc warnings debug-symbols

17
推荐指数
3
解决办法
7659
查看次数

在C中初始化全局结构

在C中完成以下操作的最佳方法是什么?

#include <stdio.h>

struct A
{
    int x;
};

struct A createA(int x)
{
    struct A a;
    a.x = x;
    return a;
}

struct A a = createA(42);

int main(int argc, char** argv)
{
    printf("%d\n", a.x);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我尝试编译上面的代码时,编译器报告以下错误:

"初始化元素不是常数"

糟糕的是这一行:

struct A a = createA(42);
Run Code Online (Sandbox Code Playgroud)

有人可以解释什么是错的吗?我不是很有经验C.谢谢!

c struct initialization constants

16
推荐指数
3
解决办法
5万
查看次数

将位字段转换为int

我有这样声明的位字段:

typedef struct morder {
    unsigned int targetRegister : 3;
    unsigned int targetMethodOfAddressing : 3;
    unsigned int originRegister : 3;
    unsigned int originMethodOfAddressing : 3;
    unsigned int oCode : 4;
} bitset;
Run Code Online (Sandbox Code Playgroud)

我也有int数组,我想从这个数组得到int值,它表示这个位字段的实际值(实际上是某种机器字,我有它的部分,我想要int表示整个字).

非常感谢.

c bit-fields

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

如何在Erlang中读取文件的内容?

我知道你可以这样做:

readlines(FileName) ->
    {ok, Device} = file:open(FileName, [read]),
    get_all_lines(Device, []).

get_all_lines(Device, Accum) ->
    case io:get_line(Device, "") of
        eof  -> file:close(Device), Accum;
        Line -> get_all_lines(Device, Accum ++ [Line])
    end.
Run Code Online (Sandbox Code Playgroud)

:是否有一个单行BIF也可以做到这一点?

erlang

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

防止"hg status"显示未跟踪目录下的所有内容

我发现hg status未跟踪目录的输出太冗长了.假设我有多数民众赞成双方管理的空库githg.所以会有两个目录,.git.hg.

输出git status是:

# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   .hg/
Run Code Online (Sandbox Code Playgroud)

输出hg status是:

? .git/HEAD
? .git/config
? .git/description
? .git/hooks/applypatch-msg.sample
? .git/hooks/commit-msg.sample
? .git/hooks/post-commit.sample
? .git/hooks/post-receive.sample
? .git/hooks/post-update.sample
? .git/hooks/pre-applypatch.sample
? .git/hooks/pre-commit.sample
? .git/hooks/pre-rebase.sample
? .git/hooks/prepare-commit-msg.sample
? .git/hooks/update.sample
? .git/info/exclude
Run Code Online (Sandbox Code Playgroud)

有没有办法将其输出减少到类似下面的行?

? .git/
Run Code Online (Sandbox Code Playgroud)

mercurial

13
推荐指数
2
解决办法
7414
查看次数

动态字符串输入 - 使用scanf("%as")

我正在尝试使用GCC手册中指定的动态读取输入scanf和存储,但它给出了编译时错误.char *

  char *string;
  if (scanf ("%as",&string) != 1){
    //some code
  }
  else{
   printf("%s\n", *string);
   free(string);
   //some code
  }
Run Code Online (Sandbox Code Playgroud)

c scanf

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

使用C中的fread从stdin缓冲读取

我试图stdin通过setvbuf在`_IOFBF~模式下使用来有效地读取.我是新来的缓冲.我正在寻找有用的例子.

输入以两个整数(n,k)开头.下一n行输入包含1个整数.目的是打印可以整除的整数数k.

#define BUFSIZE 32
int main(){
  int n, k, tmp, ans=0, i, j;
  char buf[BUFSIZE+1] = {'0'};
  setvbuf(stdin, (char*)NULL, _IONBF, 0);
  scanf("%d%d\n", &n, &k);
  while(n>0 && fread(buf, (size_t)1, (size_t)BUFSIZE, stdin)){
    i=0; j=0;
    while(n>0 && sscanf(buf+j, "%d%n", &tmp, &i)){
    //printf("tmp %d - scan %d\n",tmp,i); //for debugging
      if(tmp%k==0)  ++ans;
      j += i; //increment the position where sscanf should read from
      --n;
    }
  }
  printf("%d", ans);
  return 0; …
Run Code Online (Sandbox Code Playgroud)

c buffering fread

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

为什么结构名称应该有typedef?

我已经看到源代码总是有一个结构的typedef并使用相同的地方而不是直接使用结构名称作为"struct sname"等?

这背后的原因是什么?这样做有什么好处吗?

c struct typedef

9
推荐指数
1
解决办法
1342
查看次数

如何在python代码中利用locals()?

当我在阅读像Python一样的代码: David Goodger 的惯用Python时,我偶然发现了以下警告.

摘自文章......

print('Hello %(name)s, you have %(messages)i messages' % locals())
Run Code Online (Sandbox Code Playgroud)

这非常强大.这样,您可以执行所需的所有字符串格式设置,而无需担心插值值与模板的匹配.

但是权力可能是危险的."拥有权利的同时也被赋予了重大的责任." 如果将locals()from与外部提供的模板字符串一起使用,则将 整个本地名称空间公开给调用者.这只是要记住的事情.

我试图了解使用可能是危险的具体情况locals().locals()可以理解如何利用代码中存在的任何示例.谢谢!

python namespaces

9
推荐指数
2
解决办法
1453
查看次数