我想使用printf格式化一些输出,但由于某种原因,它会在每个项目之后输出一个数字.有关为什么会这样以及如何修复它的任何想法?
$array = array("Mo" => "09:30-19:00",
"Di" => "09:30-19:00",
"So" => "geschlossen");
foreach( $array as $key => $value ){
echo printf("%3s:%15s", $key, $value);
}
Run Code Online (Sandbox Code Playgroud)
产量
Mo: 09:30-19:0019 Di: 09:30-19:0019 So: geschlossen19
Run Code Online (Sandbox Code Playgroud)
谢谢
可能重复:
对C宏扩展和整数运算感到困惑
一个谜语(在C中)
以下C程序的输出是打印数组中的元素.但是当实际运行时,它不会这样做.
#include<stdio.h>
#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {23,34,12,17,204,99,16};
int main()
{
int d;
for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
printf("%d\n",array[d+1]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是为什么?
我正在研究printf,sprintf,我不明白几点,如果有人可以帮我理解这些点,
在PHP链接的这个链接:
有解释编号从一到六:
我不明白的是:第一和第二(1(符号说明符),2(填充说明符)),如果能有一个请帮助我的例子,我会非常感激.
void say(char msg[])
{ // using pointer to print out the first char of string
printf("%c\n", *msg);
}
void say(char msg[])
{ // using pointer to print out the memory address of the first char of string
printf("%p\n", msg);
}
void say(char msg[])
{ // using pointer to print out the whole string
printf("%s\n", msg);
}
Run Code Online (Sandbox Code Playgroud)
前两个有意义,但我不太明白第三个函数是如何工作的.我所知道的是,msg指向字符串第一个字符的内存地址.提前致谢.
在Perl中,如何将文件行中的多个十进制数转换为十六进制等值?
我有代码将转换找到的最后一个数字,但保留先前的数字未转换,我想转换所有数字,而不仅仅是找到的最后一个数字:
if ($line =~ /[0-9]+/) {
$loc = index($line,/\s+[0-9]+\s*/);
$mybyte = substr($line,$loc);
$newbyte = sprintf("%x\n", $mybyte);
$newline = substr($line,0,$loc).$newbyte;
print my_report $newline;
}
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
int i=1;
printf((i==1)?" ":" " "hello");
printf(" " "hello");
Run Code Online (Sandbox Code Playgroud)
我惊讶地发现第一个printf只提供一个空格作为输出,第二个printf输出一个空格后跟字符串hello.在第一个的情况下,我期待输出像第二个.但这里有什么我想念的东西.请在这件事上给予我帮助 ...
对于一个学校项目,我正在写一个河豚加密(只是加密,而不是解密).我已经完成了加密,我决定将解密用于娱乐(在Blowfish中很容易).
我使用无符号字符来表示字节(尽管我认为uint8_t会更便携).当我试图打印出解密的字节时,我遇到了这个问题.我正在加密纯文本消息.我已经能够打印出我加密的实际文本消息,但只能在非常具体的位置打印出来.相同的确切代码似乎无法在其他任何地方工作.这里是:
int n;
for(n = 0; n < numBlocks; n++) // Blocks are 32-bit unsigned ints (unsigned long)
{
// uchar is just a typedef for unsigned char
// message is an array of uchars
message[n] = (uchar) ((blocks[n]>>24));
message[n+1] = (uchar) ((blocks[n]>>16));
message[n+2] = (uchar) ((blocks[n]>>8));
message[n+3] = (uchar) ((blocks[n]));
// Printing works here; exact message comes back
printf("%c%c%c%c", message[n], message[n+1], message[n+2], message[n+3]);
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用两行完全相同的代码时,它不起作用.
for(n = 0; n < numBlocks; n++)
{
// Printing doesn't …Run Code Online (Sandbox Code Playgroud) 我有这个代码:
#include <stdio.h>
#include <conio.h>
void main()
{
int n = 5;
clrscr();
printf("n=%*d", n);
getch();
}
Run Code Online (Sandbox Code Playgroud)
我得到的输出是:n= 5.为什么有空间?它是如何产生的?有什么用的*代码?
我有这样的代码:
printf("Starting nets allocation...");
while(...)
{
...some operations...
}
puts("DONE");
Run Code Online (Sandbox Code Playgroud)
代码应该立即打印字符串"Starting nets allocation ..."然后,在循环之后,应该打印"DONE".
相反,程序首先执行循环,然后打印字符串"Starting nets allocation ... DONE"为什么会发生?我该如何解决这个问题?
下面的代码很好地显示在matlab命令面板上
fprintf('%f\n%f\n%f\n',MDR,TER,FAR);
Run Code Online (Sandbox Code Playgroud)
每个变量都显示为新行
但是当我尝试这种方式并通过记事本检查新制作的文本文件时,我看不到任何新行
fileID=fopen('my_file.txt','w');
fprintf(fileID,'%f\n%f\n%f\n',MDR,TER,FAR);
fclose(fileID);
Run Code Online (Sandbox Code Playgroud)
可能是什么问题?在文本文件中,所有变量都写入时没有任何新的行或空格
谢谢