我面临一个与sscanf一样奇怪的问题,当我按特定顺序传递参数时,我可以正确读取所有值,如果我改变顺序,它就可以正常工作.有人可以解释为什么这种奇怪的行为?
#include <stdio.h>
#include <stdint.h>
#include <string.h>
int main() {
uint8_t oct1, oct2, oct3, oct4;
char buf[20];
memset(buf, 0, sizeof(buf));
sprintf(buf,"%d.%d.%d.%d", 1, 2, 3, 4);
printf("%s\n", buf);
int f = sscanf(buf,"%d.%d.%d.%d", &oct1, &oct2, &oct3, &oct4);
printf("%d.%d.%d.%d \nSuccessfully read - %d\n", oct1, oct2, oct3, oct4, f);
return 0;
}
Output:
1.2.3.4
0.0.0.4
Successfully read - 4
Run Code Online (Sandbox Code Playgroud)
#include <stdio.h>
#include <stdint.h>
#include <string.h>
int main() {
uint8_t oct1, oct2, oct3, oct4;
char buf[20];
memset(buf, 0, sizeof(buf));
sprintf(buf,"%d.%d.%d.%d", 1, 2, 3, …Run Code Online (Sandbox Code Playgroud) 我正在尝试为以下递归程序编写迭代方法.我尝试了多种方法,但这让我无处可去.
我也试过谷歌搜索,但无法弄明白.有人能给我一些关于如何处理它的想法吗?
请注意我的函数是非尾递归的.在递归结束时我还有其他一些事情要做
def rec(i,j):
print "Inside funciton ", i, j
if i == 3:
return
if j == 3:
return
rec(i+1,j)
# Some code
rec(i,j+1)
# Some code
rec(0,0)
Run Code Online (Sandbox Code Playgroud)
输出:
Inside funciton 0 0
Inside funciton 1 0
Inside funciton 2 0
Inside funciton 3 0
Inside funciton 2 1
Inside funciton 3 1
Inside funciton 2 2
Inside funciton 3 2
Inside funciton 2 3
Inside funciton 1 1
Inside funciton 2 1
Inside funciton 3 1
Inside …Run Code Online (Sandbox Code Playgroud) 我有以下功能,如果我的值是0,它会给出一些奇怪的输出.有人可以解释一下为什么这表现得很奇怪吗?我的意图是仅在非零值时打印该值,否则应为空白
# include<stdio.h>
int main(int argc, char *argv[]) {
int i=0;
printf("Number is %d\n", i ? i : "");
return 0;
}
-> gcc print.c
print.c: In function 'main':
print.c:5: warning: pointer/integer type mismatch in conditional expression
-> ./a.out
Number is 4195848
Run Code Online (Sandbox Code Playgroud)
我知道我可以像下面这样做,但我想用上面的逻辑做同样的事情
if(i != 0) {
printf("%d", i);
} else {
printf("%s", "");
}
Run Code Online (Sandbox Code Playgroud)