printf函数中的问号 ? 在C语言中是什么意思?

huangapple go评论55阅读模式
英文:

what does ? mean inside a printf function in c

问题

printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");

In the printf statement you provided, here's what each part means:

  • %s: This is a format specifier used in C's printf function to indicate that a string should be inserted at this position.

  • =: This is a literal character that will be printed as-is.

  • %s: Another format specifier, indicating that another string should be inserted here.

  • \n: This is a newline character, which moves the cursor to the next line after printing the preceding text.

The expression inside the printf statement, argv[i] ? argv[i] : "NULL", is a conditional expression (also known as a ternary operator). It is used to print the value of argv[i] if it is not NULL, and if it is NULL, it prints the string "NULL" instead.

So, the entire printf statement is used to print a line in the format: "ColumnName = ColumnValue" where ColumnName is taken from azColName[i], and ColumnValue is either the value from argv[i] or the string "NULL" if argv[i] is NULL.

英文:

I am learining about how to use sqlite3 on c and was lookinbg at this code:

static int callback(void *data, int argc, char **argv, char **azColName){
   int i;
   fprintf(stderr, "%s: ", (const char*)data);
   
   for(i = 0; i<argc; i++){
      printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
   }
   
   printf("\n");
   return 0;
}

I was wondering what does ?, :, %s = %s mean in printf

printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");

I'm new to c and I'm still getting familiar to the syntax and the quirks of this language. Help would be much appreciated.

答案1

得分: 3

  • "%s = %s\n"
    第一个参数是 printf 的格式字符串,其中包含要打印的文本和转换说明符。转换说明符以 % 开头,%s 表示“用字符串替换此转换说明符”。对于每个转换说明符,printf 期望您还提供与该转换说明符匹配的参数。在您的情况下,有两个 %s,因此在第一个参数之后应该有两个 (const) char* 参数 - 而您也有这两个参数。

  • azColName[i]
    这将 azColName 中的第 ichar* 供应给 printf

  • argv[i] ? argv[i] : "NULL"
    这使用条件运算符 ? : 来提供格式字符串之后 printf 预期的第二个字符串。它构建如下:

    condition ? expression-true : expression-false
    

    在您的情况下,这意味着如果 argv[i] 求值为 true(即它不是 NULL 指针),则表达式的结果将是 argv[i],否则将是 "NULL"expression-trueexpression-false 都是 char*(在 "NULL" 的情况下,它会_衰减_为 char*),因此都会使 printf 满意。

英文:
  • "%s = %s\n"
    The first argument to printf is the format string which contains the text to print mixed with conversion specifiers. The conversion specifiers start with % and %s means "replace this conversion specifier with a string". For every conversion specifier, printf expects that you've also supplied an argument matching that conversion specifier. In your case, you have two %s so there should be two (const) char* arguments after the first argument - which you also have.

  • azColName[i]
    This supplies the i:th char* in azColName to printf

  • argv[i] ? argv[i] : "NULL"
    This uses the conditional operator ? : in order to supply the second string that printf expects (after the format string). It's built like this:

    condition ? expression-true : expression-false
    

    In your case it means, if argv[i] evaluates to true (that is, it's not a NULL pointer), the result of the expression will be argv[i], otherwise it will be "NULL". Both expression-true and expression-false are char* (in the case of "NULL", it decays into a char*) and will therefore make printf happy.

huangapple
  • 本文由 发表于 2023年6月16日 01:22:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76484107.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定