小编ami*_*n__的帖子

在引用的字符串中扩展宏变量

我在c中的两本着名书籍中发现了两个不同的东西,第一个是"宏观扩展中的引用字符串中没有替换形式参数" - 由K&R c语言第76页

第二个是代码,

#define PRINT(var,format) printf("variable is %format\n",var)
PRINT(x_var,f);
Run Code Online (Sandbox Code Playgroud)

稍后的宏调用将扩展为

printf("x_var is %f\n",x_var);
Run Code Online (Sandbox Code Playgroud)
  • 这是通过在第448页的ansi c - E. balagurusamy编程.

当然有两个引用是相互矛盾的.到目前为止,我知道第一个是真的,我的编译器给我的结果如此.但第二本书也是众所周知和受欢迎的.我想知道在先前版本的c中是否有这样的东西,或者第二次引用是假的.

c string macros c-preprocessor

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

使用带有联合的位字段是否有效?

我使用了具有这样结构的位字段,

struct
{
       unsigned int is_static: 1;
       unsigned int is_extern: 1;
       unsigned int is_auto: 1;
} flags;
Run Code Online (Sandbox Code Playgroud)

现在我想知道是否可以通过联合完成这样我修改代码,如,

union
{
       unsigned int is_static: 1;
       unsigned int is_extern: 1;
       unsigned int is_auto: 1;
} flags;
Run Code Online (Sandbox Code Playgroud)

我找到了带有union的bit字段,但是从输出中可以理解,union中的所有字段都被赋予了一个位.现在我看到使用带有union的位字段并不是错误的,但在我看来,像这样使用它在操作上是不正确的.那么答案是什么 - 将bit字段与union结合使用是否有效?

c structure unions bit-fields

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

Tensorflow 停止训练并随机挂在 GPU 上

当我在 GPU 上运行以下代码时,它可以很好地训练一些时期,然后就挂了。虽然挂起的进程仍然存在,但 GPU 使用率变为 0%。在下面的代码中,我使用来自 tf.contrib.data.Dataset 的数据集 API。但我也尝试使用占位符和提要字典方法,该方法在训练期间也挂在随机时期。过去 2-3 周我一直在努力解决这个问题,但找不到出路。我在远程 GPU 集群上运行代码。这里有一些关于集群节点的信息,使用 tensorflow gpu version 1.4

NodeName=node050 Arch=x86_64 CoresPerSocket=1 CPUAlloc=0 CPUErr=0 CPUTot=24 CPULoad=12.03 Features=Proc24,GPU4 Gres=gpu:4
NodeAddr=node050 NodeHostName=node050 Version=15.08 OS=Linux RealMemory=12.03 =125664 Sockets=24 Boards=1
State=IDLE ThreadsPerCore=1 TmpDisk=0 Weight=1 Owner=N/A
BootTime=2017-11-07T08:20:00 SlurmdStartTime=2017-11-07T08:24:06
CapWatts=n /a CurrentWatts=0 LowestJoules=0 ConsumedJoules=0
ExtSensorsJoules=n/s ExtSensorsWatts=0 ExtSensorsTemp=n/s

代码

dat_split = np.load('data/dat_split2.npy')
X_train = dat_split[0].astype(np.float32)
X_test = dat_split[1].astype(np.float32)
y_train = dat_split[2].astype(np.int32)
y_test = dat_split[3].astype(np.int32)

num_epochs = 100


train_data_len = X_train.shape[0]
test_data_len = X_test.shape[0]
num_joints = len(considered_joints)
num_classes = len(classes) …
Run Code Online (Sandbox Code Playgroud)

slurm tensorflow

7
推荐指数
1
解决办法
2719
查看次数

snprintf混乱

我对snprintf函数很困惑.首先,我没有在stdio.h下的turbo C版本编译器中找到函数snprintf.其次在GNU编译器中,当缓冲区大小小于格式化字符串时,snprintf返回-1,尽管它应该返回已经存在的字符数如果缓冲区大小足够大则打印.我有以下来源:

#include<stdio.h>
int main()
{
  char str[100];
  int numchar = snprintf(str,2,"ello jdj");
  printf("%d\n",numchar);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

根据我所知的snprintf到目前为止,此代码应该输出8.但它在我的GNU编译器中返回-1.背后有什么事实?

c string printf

5
推荐指数
1
解决办法
678
查看次数

与sizeof一起使用的表达式的评估

是否有任何表达式将被评估为sizeof的操作数.我已经知道在具有sizeof的可变长度操作数的情况下,将评估表达式.但我不能举个例子,我写了下面的代码,

int a[]={1,2,3};
printf("%d",sizeof(a[1]++));
printf("%d\n",a[1]);
Run Code Online (Sandbox Code Playgroud)

但是在这里我从输出表达式观察到a[1]++没有评估.如何做一个例子?

c expression sizeof

5
推荐指数
1
解决办法
322
查看次数

为什么这不适用于c中的printf()

我有以下代码:

    int i=1;
    printf((i==1)?" ":"   " "hello");
    printf(" " "hello");
Run Code Online (Sandbox Code Playgroud)

我惊讶地发现第一个printf只提供一个空格作为输出,第二个printf输出一个空格后跟字符串hello.在第一个的情况下,我期待输出像第二个.但这里有什么我想念的东西.请在这件事上给予我帮助 ...

c string printf

3
推荐指数
1
解决办法
181
查看次数

在声明结构变量时需要`struct`关键字吗?

我从书中了解到,为了声明一个结构变量,它必须是一个前面的struct关键字,但是struct在我的Bloodshed\DevC++编译变量中没有前面的那个,可以声明没有任何错误如下,

struct stype
{
       int ival;
       float fval;
       double dval;
};
Run Code Online (Sandbox Code Playgroud)

在主要的,

stype s;
s.ival=10;s.dval=23.23;s.fval=233.23;
printf("%d %f %lf\n",s.ival,s.fval,s.dval);
Run Code Online (Sandbox Code Playgroud)

这样可以正确打印应该打印的内容.struct在变量声明中使用此关键字后是否有任何修改或什么?为什么这段代码工作?

c structure declaration

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

C/C++中int的字符串化

下面的代码应输出100我的字符串化知识.vstr(s)应该扩展为100,然后str(s)得到100,它应该返回字符串"100".但是,它输出"a"代替.是什么原因?但是,如果我用宏定义的常量调用,foo那么它输出"100".为什么?

#include<stdio.h>
#define vstr(s) str(s)
#define str(s) #s
#define foo 100
int main()
{
    int a = 100;
    puts(vstr(a));
    puts(vstr(foo));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c c++ macros stringify

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

为什么流式传输cout的指针不能打印地址?

当我打印一个char指针时printf(),它使用转换说明符决定是应该打印地址还是根据%u或%s打开整个字符串.

但是当我想做同样的事情时cout,如何cout决定在地址和整个字符串中应该打印什么?这是一个示例来源:

int main()
{
  char ch='a';
  char *cptr=&ch;
  cout<<cptr<<endl;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

在我的GNU编译器中,cout尝试将ch输出为字符串.

我怎样才能得到的地址ch通过cptr使用cout

c++ string pointers cout char

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

使用system()函数时混淆控制台和文件输出

我有以下代码,

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

int main()
{
    //freopen("file1","w",stdout);
    char str[]="command line with file";
    char s[]="extra string";
    puts(str);
    puts(s);
    system("PAUSE");    
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我在控制台上看到输出时它显示了我,

command line with file
extra string
Press any key to continue . . . 
Run Code Online (Sandbox Code Playgroud)

我通过删除代码中的注释行将输出写入文件时期望相同​​的输出.但它输出像,

Press any key to continue . . . 
command line with file
extra string
Run Code Online (Sandbox Code Playgroud)

为什么文件和控制台之间的区别输出??? 这里system("PAUSE")函数负责字符串输出Press any key to continue . . .

c file-io system

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

通过宏观中的参数评估方向

正如我在C中所知,函数评估的实际参数的传递从最右边开始并从定向到左边开始.带参数的宏定义是什么情况?我制作了一个代码,使感觉清晰,但输出混淆了我......这是代码.,

#define parsing(a,b) a*b

int parsefun(int a, int b)
{
    return a*b;    
}

int main()
{
    int i=10;
    printf("%d\n",parsing((i++),i));
    i=10;
    printf("%d\n",parsing(i,(i++)));
    i=10;

    printf("%d\n",parsefun((i++),i));
    i=10;
    printf("%d\n",parsefun(i,(i++)));
    system("PAUSE");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

此代码输出,100 100 100 110

我希望宏的相同输出作为函数.但这里的关键点在哪里?

c macros operator-precedence c-preprocessor

0
推荐指数
1
解决办法
299
查看次数

goto不能使用?:运算符

出于学习目的,我编写了以下代码片段:

for(int i=0;i<10;i++)
{
  for(int j = 0;j<5;j++)        
  {
    //(i==j && i==3)? (goto found) : printf("stya here\n");        
    if(i==j && i==3){goto found;} else {printf("stay here\n");}
  }
}

found:
  printf("yes I am here");
Run Code Online (Sandbox Code Playgroud)

但我想知道我什么时候发现内部循环中的省略语句没有给出错误,现在我很困惑if-else并不总是可以用?:运算符替换.这是什么事实?为什么评论语句会出错?

c goto conditional-operator

0
推荐指数
1
解决办法
1448
查看次数

有人能简单地解释一下这个功能吗?

我有以下代码,

float a = 0.7;
if(0.7 > a)
    printf("Hi\n");
else
    printf("Hello\n");   //Line1
Run Code Online (Sandbox Code Playgroud)

float a = 0.98;
if(0.98 > a)
    printf("Hi\n");
else
    printf("Hello\n");   //Line2
Run Code Online (Sandbox Code Playgroud)

这里line1输出Hi但Line2输出Hello.我假设有一个关于双常数和浮点数的标准,即它们中的任何一个在评估时会变大.但是这两个代码澄清了我,当双常数变大并且其他时候浮点变大时,情况可能会出现.这背后有没有四舍五入的问题?如果是,请解释我.我非常需要这个明确的...感谢提前

c floating-point double rounding

0
推荐指数
1
解决办法
213
查看次数