标签: printf

Printf和Array

我被问到这个问题是一个课堂练习:

int A[] = {1,3,5,7,9,0,2,4,6};

printf("%d\n", *(A+A[1]-*A));
Run Code Online (Sandbox Code Playgroud)

我无法在纸上弄明白,所以继续编译一个简单的程序并对其进行测试,发现printf("%d",*A)总是给我1输出.

但我仍然不明白为什么会这样,所以如果有人能够解释这一点会很棒.

c arrays printf

3
推荐指数
1
解决办法
221
查看次数

在php printf函数中更改表的颜色时发生错误

我想使用printf函数更改php文件中表格文本的颜色.但是当我使用这两种方法时,我失败了.你能告诉我怎么会发生这种情况?我怎样才能改变颜色?方法A:

 <?php   
      printf('<tr>');  
      printf('<td><font color='red'>abc</font></td>');  
      printf('</tr>');
  ?>
Run Code Online (Sandbox Code Playgroud)

方法B:

 <?php
  printf('<tr>');
  printf('<td style="color:red">abc</td>');
  printf('</tr>');
 ?>
Run Code Online (Sandbox Code Playgroud)

PS:原始问题:(这是代码的一部分)

 $userrow = mysql_fetch_array($userqueryresult);
            $thisuserid = $userrow[0];
            $user_type=$userrow[1];

printf("   <td valign = 'center'  width='40%%'>
<a href='show_user.php?u=%d'>
<font color='red'>%s</font></a></td>\n",$thisuserid, $key);
Run Code Online (Sandbox Code Playgroud)

事实上,当我使用color ="red"时,它会失败而color ='red',它有效."""和""之间有什么区别吗?但正如Julie Pelletier所说,"abc"它起作用了.这也是""".

html php printf html-table

3
推荐指数
1
解决办法
50
查看次数

c ++值和变量之间的不同打印行为

我在mac上尝试了以下代码,输出对我来说有点奇怪.

float a = 2.225;
float b = 123.235;
printf("round: %.2f %.2f \n", a, b);
printf("round: %.2f %.2f \n", 2.225, 123.235);
Run Code Online (Sandbox Code Playgroud)

输出:

round: 2.22 123.24 
round: 2.23 123.23 
Run Code Online (Sandbox Code Playgroud)

g ++ --version

Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.3.0 (clang-703.0.31)
Target: x86_64-apple-darwin15.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
Run Code Online (Sandbox Code Playgroud)

印刷中的舍入规则是什么?如何在这两种情况下获得相同的打印效果?非常感谢.

c c++ printf

3
推荐指数
1
解决办法
107
查看次数

为格式说明符传递的printf参数太少 - 是不确定的行为?

据我所知,参数的格式说明符太少被认为是未定义的行为(格式说明符的参数太少也是未定义的行为).这适用于:

printf("%1$d %1$d", 5);
Run Code Online (Sandbox Code Playgroud)

PS使用"%1$d"as格式说明符是POSIX添加的函数族的扩展printf.

c printf posix

3
推荐指数
1
解决办法
597
查看次数

为什么"printf"不起作用?

我正在学习编程C.你能解释一下这里为什么没有打印出来吗 提前致谢.

#include <stdio.h>

int main (void)
{
    char a[]="abcde";
    printf ("%s", a);
}   
Run Code Online (Sandbox Code Playgroud)

c printf

3
推荐指数
1
解决办法
1万
查看次数

为什么宽度数字在printf格式的"#"之后无法工作?

我想为8号获得"0x08",但是当我使用"%#02x"时,我只得到0x8:

int i = 0x08;
printf("%#02x", i); // only print 0x8, not 0x08
Run Code Online (Sandbox Code Playgroud)

在格式字符串中的"#"后,宽度数似乎无法工作.除了使用"0x%02x"之外,有没有更好的方法来打印我想要的内容?

c printf

3
推荐指数
1
解决办法
155
查看次数

bash printf文字逐字字符串

为了使我的代码可移植,我尝试使用printf而不是echo.但是之后

printf "-dogs-cats" 
Run Code Online (Sandbox Code Playgroud)

返回错误.本案的解决方法是:

printf "-";printf "dogs-cats"
Run Code Online (Sandbox Code Playgroud)

但是有一个通用的,可移植的命令(或一个选项printf),它将打印任意字符串作为文字/逐字,而不是尝试将字符串解释为格式?

我在BSD UNIX(在Mac上)工作,但我的目标是在其他UNIX风格中也可以使用的代码.

unix bash printf bsd verbatim

3
推荐指数
2
解决办法
1434
查看次数

C printf 0标志和width属性与precision标志之间的差异

我目前正在学习printflibc 的功能,但我不了解,两者之间有什么区别:

printf("Test : %010d", 10);
Run Code Online (Sandbox Code Playgroud)

使用0标志和10宽度说明符

和

printf("Test : %.10d", 10);
Run Code Online (Sandbox Code Playgroud)

采用10精密符

产生相同的输出: Test : 0000000010

c printf libc

3
推荐指数
2
解决办法
985
查看次数

Sprintf崩溃我的程序

我只是尝试使用sprintf连接一些字符串,但我有这个问题,我不明白为什么我的程序在C中使用sprintf崩溃.为什么这个代码运行?

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char* dateTime = malloc(16*sizeof(char));

    printf("Date: %s\n", __DATE__);
    printf("Time: %s\n", __TIME__);

    sprintf (dateTime, "%s, %s\0", __DATE__, __TIME__);

    printf("%s", dateTime);
    free(dateTime);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这不是吗?

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char* dateTime = malloc(16*sizeof(char));

    //printf("Date: %s\n", __DATE__);
    //printf("Time: %s\n", __TIME__);

    sprintf (dateTime, "%s, %s\0", __DATE__, __TIME__);

    printf("%s", dateTime);
    free(dateTime);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c printf pointers

3
推荐指数
1
解决办法
342
查看次数

为什么printf只显示第一个字母?

首先,我有一个功能,我有unsigned char*类型存储的标志.例如,Unicode中每个符号Abcdef!?为[65 0] [98 0] [99 0] [100 0] [101 0] [102 0] [33 0] [63 0]2个字节.

当我使用for(unsigned char i=0; i<17; i++) printf("%c", pointer[i]);一切正常时,它显示Abcdef!?.但是当我使用printf("%s" pointer);它时,它只给我,A而不是别的.你能告诉我为什么吗?

c unicode printf

3
推荐指数
1
解决办法
744
查看次数

标签 统计

printf ×10

c ×8

arrays ×1

bash ×1

bsd ×1

c++ ×1

html ×1

html-table ×1

libc ×1

php ×1

pointers ×1

posix ×1

unicode ×1

unix ×1

verbatim ×1