小编asw*_*win的帖子

变量顺序对sscanf有影响吗?

我面临一个与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)

c scanf

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

对非尾递归方法的递归函数的迭代

我正在尝试为以下递归程序编写迭代方法.我尝试了多种方法,但这让我无处可去.

我也试过谷歌搜索,但无法弄明白.有人能给我一些关于如何处理它的想法吗?

请注意我的函数是非尾递归的.在递归结束时我还有其他一些事情要做

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)

python iteration recursion loops

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

如果数字非零则如何打印,否则使用printf打印?

我有以下功能,如果我的值是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)

c printf ternary-operator conditional-operator

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