我长期以来一直认为,goto如果可能的话,永远不应该使用它.在前几天阅读libavcodec(用C语言编写)时,我注意到它的多种用途.goto在支持循环和函数的语言中使用是否有利?如果是这样,为什么?
int main()
{
i/*nt*/a = 10;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我有上面的代码并且我想计算令牌,它是14个令牌还是13个令牌?
在变量名中写注释是否有效?您可以假设int i, int a,int ia是全局定义的。
开始研究复杂性,我正在努力解决这个问题:
void what(int n) {
int i;
for (i = 1; i <= n; i++) {
int x = n;
while (x > 0)
x -= i;
}
}
Run Code Online (Sandbox Code Playgroud)
好吧,第一个循环显然是O(n).第一次迭代是O(n),第二次是O(n/2)..而且就像log(n)我猜的那样?这意味着O(n) * O(log(n)) = O(n * log(n)) complexity.我做对了吗?
编辑:(不是重复)我知道Big O是什么.我已经在特定情况下询问了正确的评估.
为什么这样:
#include <stdio.h>
#include <limits.h>
#include <inttypes.h>
int main() {
enum en_e {
en_e_foo,
en_e_bar = UINT64_MAX,
};
enum en_e e = en_e_foo;
printf("%zu\n", sizeof en_e_foo);
printf("%zu\n", sizeof en_e_bar);
printf("%zu\n", sizeof e);
}
Run Code Online (Sandbox Code Playgroud)
4 8 8用C和8 8 8C++ 打印(在4字节整数的平台上)?
我的印象是,UINT64_MAX赋值将强制所有枚举常量至少为64位,但en_e_foo在纯C中保持为32.
这种差异的理由是什么?
在阅读德州仪器为其SensorTag提供的示例代码时,我遇到了以下片段.
void SensorTagIO_processCharChangeEvt(uint8_t paramID) {
...
if (!!(ioValue & IO_DATA_LED1)) {
PIN_setOutputValue(hGpioPin, Board_LED1, Board_LED_ON);
} else {
PIN_setOutputValue(hGpioPin, Board_LED1, Board_LED_OFF);
}
if (!!(ioValue & IO_DATA_LED2)) {
PIN_setOutputValue(hGpioPin, Board_LED2, Board_LED_ON);
} else {
PIN_setOutputValue(hGpioPin, Board_LED2, Board_LED_OFF);
}
if (!!((ioValue & IO_DATA_BUZZER))) {
Clock_start(buzzClockHandle);
}
...
}
Run Code Online (Sandbox Code Playgroud)
声明就像这样(在同一个文件中).
#define IO_DATA_LED1 0x01
static uint8_t ioValue;
Run Code Online (Sandbox Code Playgroud)
是否if (!!(ioValue & IO_DATA_LED1))提供任何优势if (ioValue & IO_DATA_LED1)?
我想基准glibc的strlen功能,出于某种原因,发现它显然执行多慢与GCC启用优化,我不知道为什么。
这是我的代码:
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
int main() {
char *s = calloc(1 << 20, 1);
memset(s, 65, 1000000);
clock_t start = clock();
for (int i = 0; i < 128; ++i) {
s[strlen(s)] = 'A';
}
clock_t end = clock();
printf("%lld\n", (long long)(end - start));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在我的机器上,它输出:
$ gcc test.c && ./a.out
13336
$ gcc -O1 test.c && ./a.out
199004
$ gcc -O2 test.c && ./a.out
83415 …Run Code Online (Sandbox Code Playgroud) 为什么这个.c文件#include本身?
vsimple.c
#define USIZE 8
#include "vsimple.c"
#undef USIZE
#define USIZE 16
#include "vsimple.c"
#undef USIZE
#define USIZE 32
#include "vsimple.c"
#undef USIZE
#define USIZE 64
#include "vsimple.c"
#undef USIZE
Run Code Online (Sandbox Code Playgroud) 我是C编程的初学者,我知道struct type declaration和typedef struct declaration之间的区别.我遇到了一个答案,说如果我们定义一个结构,如:
typedef struct {
some members;
} struct_name;
Run Code Online (Sandbox Code Playgroud)
然后它就像为匿名结构提供别名(因为它没有标记名称).所以它不能用于前瞻性声明.我不知道"前瞻性宣言"是什么意思.
另外,我想知道以下代码:
typedef struct NAME {
some members;
} struct_alias;
Run Code Online (Sandbox Code Playgroud)
有什么区别struct和typedef?或者两者都相等,因为struct_alias是struct NAME的别名?
此外,我们可以声明类似的变量struct:
struct_alias variable1;
Run Code Online (Sandbox Code Playgroud)
和/或喜欢:
struct NAME variable2;
Run Code Online (Sandbox Code Playgroud)
或者喜欢:
NAME variable3;
Run Code Online (Sandbox Code Playgroud) 当sscanf()该scanf族或该族的另一个函数被赋予一个数字序列时,其转换值超过目标整数类型的最大值,
我想用C程序读取用户的输入.我不想使用数组,
char names[50];
Run Code Online (Sandbox Code Playgroud)
因为如果用户给出长度为10的字符串,那么剩余的空间就会被浪费掉.
如果我使用字符指针,
char *names;
Run Code Online (Sandbox Code Playgroud)
那么我需要以这样的方式为它分配内存,
names = (char *)malloc(20 * sizeof(char));
Run Code Online (Sandbox Code Playgroud)
在这种情况下,也存在内存浪费的可能性.
所以,我需要的是为字符串动态分配内存,该字符串与字符串的长度完全相同.
让我们假设,
如果用户输入是"stackoverflow",那么分配的内存应该是14(即字符串的长度= 13和'\ 0'的1个额外空间).
我怎么能实现这个目标?