我的问题似乎很简单,但我一直对它感到困惑:
bool myBool = TRUE;
if (myBool) printf("1 myBool = true\n");
else printf("1 myBool = false\n");
myBool = !myBool;
if (myBool) printf("2 myBool = true\n");
else printf("2 myBool = false\n");
printf("%d\n", TRUE);
printf("%d\n", FALSE);
Run Code Online (Sandbox Code Playgroud)
所有这些产出:
1 myBool = true;
2 myBool = false;
1
0
Run Code Online (Sandbox Code Playgroud)
我理解为什么这是输出.我的问题是如何才能!1平等false?因为在C和C++中,if检查非零值.我最后一次检查:
TRUE = 0x00000001
FALSE = 0x00000000
Run Code Online (Sandbox Code Playgroud)
因此:
!TRUE = !0x00000001 = 0xfffffffd != 0
Run Code Online (Sandbox Code Playgroud)
编辑:我想这种混乱从我的学习x86汇编,其中一期朵朵not eax, eax将进行逐位不是eax(等效eax = ~eax于C).
今天我正在学习有关C的标准I/O的事情.当我打开stdio.h文件时发现:
typedef struct _iobuf FILE;
Run Code Online (Sandbox Code Playgroud)
当检查struct _iobuf的定义时发现:
struct _iobuf {
char *_ptr;
int _cnt;
char *_base;
int _flag;
int _file;
int _charbuf;
int _bufsiz;
char *_tmpfname;
};
Run Code Online (Sandbox Code Playgroud)
为了更多地了解,我已经给出了关于每一个的描述,不论它是否正确
struct _iobuf {
char *_ptr; /* next character position */
int _cnt; /* characters left */
char *_base; /* location of buffer */
int _flag; /* File status flags */
int _file;
int _charbuf; /*Data transfer buffer */
int _bufsiz; /* Buffer size */
char *_tmpfname; /* Temporary file indicator */
}; …Run Code Online (Sandbox Code Playgroud) 这是我在C中循环重新定义一个数组的后续跟踪.
考虑以下两个例子:
#include <stdio.h>
int main(void)
{
int i = 1;
while (i <= 10) {
int t[10];
if (i == 1)
t[0] = 100;
else {
t[0]++;
printf("i = %d, t[0] = %d\n", i, t[0]);
}
i++;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
结果如预期:
i = 2, t[0] = 101
i = 3, t[0] = 102
i = 4, t[0] = 103
i = 5, t[0] = 104
i = 6, t[0] = 105
i = 7, …Run Code Online (Sandbox Code Playgroud) 首先,是否可以编辑GCC命令行设置,以便我不必每次都使用-std = c99标志进行编译?
其次,为什么c99不是默认行为?使用c99进行编译允许某些现代编程约定,例如在for循环中创建和初始化索引变量.
for(int i = 0; i < 10; i++) {...
Run Code Online (Sandbox Code Playgroud)
没有c99标志,这是不可能的,我将不得不在自己的线上声明,我认为这是愚蠢的.为什么GCC默认不会用C99编译?(正常好奇,不抱怨.请不要抨击)
谢谢.
我正在通过本文学习ALSA编程,并且我尝试编译以下示例:
/*
This example opens the default PCM device, sets
some parameters, and then displays the value
of most of the hardware parameters. It does not
perform any sound playback or recording.
*/
/* Use the newer ALSA API */
#define ALSA_PCM_NEW_HW_PARAMS_API
/* All of the ALSA library API is defined
* in this header */
#include <alsa/asoundlib.h>
int main() {
int rc;
snd_pcm_t *handle;
snd_pcm_hw_params_t *params;
unsigned int val, val2;
int dir;
snd_pcm_uframes_t frames;
/* Open PCM …Run Code Online (Sandbox Code Playgroud) 为什么以下代码会给我一个"丢弃限定符"警告?
double* const a[7];
memcpy(a,b,sizeof(double*)*7);
Run Code Online (Sandbox Code Playgroud)
我使用Apple LLVM版本6.1.0(clang-602.0.53)(基于LLVM 3.6.0svn)获得的错误是
warning: passing 'double *const [7]' to parameter of type 'void *' discards qualifiers
Run Code Online (Sandbox Code Playgroud)
编辑:
奖金问题.为什么restrict关键字也不起作用?
double* restrict a[7];
memcpy(a,b,sizeof(double*)*7);
Run Code Online (Sandbox Code Playgroud)
编辑2:
我问这个问题,因为我想a成为一个const restrict指针.我可以使用以下代码获得此结果:
double* const restrict a[7] = {b[0], b[2], ... b[7]};
Run Code Online (Sandbox Code Playgroud)
这是一件愚蠢的事吗?
我知道我可以使用mbrtowc()在C中迭代一个多字节字符串.但是如果我想倒退呢?或者换句话说,我如何找到以前有效的多字节字符.我尝试了以下方法,它至少部分地使用默认的en_us.UTF-8语言环境在我的Ubuntu系统上运行:
char *str = "\xc2\xa2\xc2\xa1xyzwxfd\xc2\xa9", *tmp = NULL;
wchar_t wc = 0;
size_t ret = 0, width = 1;
mbstate_t state = {0};
//Iterate through 2 characters using mbrtowc()
tmp = str;
tmp += mbrtowc(&wc, tmp, MB_CUR_MAX, &state);
tmp += mbrtowc(&wc, tmp, MB_CUR_MAX, &state);
//This is a simplified version of my code. I didnt test this
//exact code but this general idea did work.
for(tmp--; (ret = mbrtowc(&wc, tmp, width, &state)) == (size_t)(-1) || ret == (size_t)(-2); width++, tmp--)
if(width …Run Code Online (Sandbox Code Playgroud) 在这个简单的C99代码:
int main(void){
int a[3][3] = {1};
int m = 3;
int x;
int b[m][m];
x = sizeof(b);
b[0][0] = -1;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用GDB,我们在返回行设置一个断点并运行.现在让我们看看以下内容:
(gdb) p a
$1 = {{1, 0, 0}, {0, 0, 0}, {0, 0, 0}}
(gdb) p b
$2 = 0x7fffffffe3a0
(gdb) p sizeof(a)
$3 = 36
(gdb) p sizeof(b)
$4 = 0
(gdb) p x
$5 = 36
(gdb) whatis a
type = int [3][3]
(gdb) whatis b
type = int [][]
(gdb)
Run Code Online (Sandbox Code Playgroud)
我想知道这是怎么发生的.C运行时环境假定b的类型是int …
即使编译时,以下程序编译并运行正常-std=c99.
#include <stdio.h>
#include <arpa/inet.h>
int main()
{
printf("%d\n", htons(1));
}
Run Code Online (Sandbox Code Playgroud)
这是输出.
$ gcc -std=c99 foo.c && ./a.out
256
Run Code Online (Sandbox Code Playgroud)
但是以下程序会导致警告和错误.
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
int main()
{
struct addrinfo *res;
getaddrinfo("localhost", NULL, NULL, &res);
printf("%d\n", res->ai_flags);
}
Run Code Online (Sandbox Code Playgroud)
这是警告和错误.
$ gcc -std=c99 bar.c
bar.c: In function ‘main’:
bar.c:9:5: warning: implicit declaration of function ‘getaddrinfo’ [-Wimplicit-function-declaration]
getaddrinfo("localhost", NULL, NULL, &res);
^
bar.c:10:23: error: dereferencing pointer to incomplete type
printf("%d\n", res->ai_flags);
Run Code Online (Sandbox Code Playgroud)
为什么编译器不会抱怨htons()但是getaddrinfo()在编译时会抱怨-std=c99 …
我已经看到了这个解决方案,但我得到了同样的错误.对于一个类,我们不得不切换到c99编译器而不是gnu,但现在timespec似乎被打破了.除了"timespec的存储大小未知"之外,我还得到了一堆其他错误,如"CLOCK_MONOTONIC undeclared","取消引用指向不完整类型'struct timespec'的指针",以及未使用的变量警告,但我认为这些会发生一旦我解决了编译器问题.
我不认为我声明timespec变量的方式有什么问题,
struct timespec startTime;