我尝试使用Formatter.format,但这似乎将尾数留在带有0尾数的数字上,而C版本却没有.在Java中是否有相当于C的%g格式说明符,如果没有,有没有办法伪造它?出于兼容性原因,我的目的是保持尾数与C完全相同.
foo.c的
#include <stdio.h>
int main (int argc, char const *argv[])
{
printf("%g\n", 1.0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Main.java
class Main {
public static void main(String[] args) {
System.out.printf("%g\n", 1.0);
}
}
Run Code Online (Sandbox Code Playgroud)
安慰:
$ javac Main.java && java Main
1.00000
$ gcc foo.c && ./a.out
1
Run Code Online (Sandbox Code Playgroud)
类似地,使用1.2作为输入,Java版本中的尾数较长
$ javac Main.java && java Main
1.20000
$ gcc foo.c && ./a.out
1.2
Run Code Online (Sandbox Code Playgroud) 这是一个奇怪的问题,一直困扰我.
该程序是用C89编写的,它一次将文件读入一个16字节的char*数组(使用fread和sizeof(char)的大小).该文件与"rb"标志一起打开.然后将该数组传递给一个函数,该函数基本上取16个十六进制值并将其粘贴到一个字符串中,每个值由一个空格分隔.
这就是奇怪的地方.该函数产生一个漂亮的十六进制转储,一次16个字节,用于我拥有的文本文件输入.但是如果我在一个小的位图图像上尝试它会搞砸 - 我最终会输出字符串中的输出,如ffffff88而不是88.
使用sprintf("%02x",input [i])将十六进制值放入输出字符串中; 在一个循环中.
为什么这对某些文件而不是其他文件正常工作?
我是C++的完全新手,但不是Java,C#,JavaScript,VB.我正在使用Visual Studio 2010中的默认C++控制台应用程序.
在尝试做printf我得到一些奇怪的字符.每次都不一样,告诉我他每次运行时都会看到不同的内存位置.
码:
#include "stdafx.h"
#include <string>
using namespace std;
class Person
{
public:
string first_name;
};
int _tmain(int argc, _TCHAR* argv[])
{
char somechar;
Person p;
p.first_name = "Bruno";
printf("Hello %s", p.first_name);
scanf("%c",&somechar);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我无法理解C的规则,即在打印双精度或将字符串转换为双精度时要考虑的精度.以下程序应说明我的观点:
#include <errno.h>
#include <float.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv) {
double x, y;
const char *s = "1e-310";
/* Should print zero */
x = DBL_MIN/100.;
printf("DBL_MIN = %e, x = %e\n", DBL_MIN, x);
/* Trying to read in floating point number smaller than DBL_MIN gives an error */
y = strtod(s, NULL);
if(errno != 0)
printf(" Error converting '%s': %s\n", s, strerror(errno));
printf("y = %e\n", y);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我编译并运行这个程序时得到的输出(在带有gcc 4.5.2的Core 2 …
我有一个基于printf样式格式的日志框架:
void Logger::debug(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
this->output(DebugLevel, fmt, args);
va_end(args);
}
Run Code Online (Sandbox Code Playgroud)
如果Logger::output抛出,编译器是否会正确展开堆栈,还是需要va_end(args)在catch子句中添加try/catch块?这可能是RAII',还是太神奇va_end了?如果可能,请包括对标准的参考.
我需要在循环中连接几个字符串并将结果赋值给变量:
格式化字符串示例:
result=$(printf '| %-15s| %-25s| %-15s| %-15s| %-15s\n' $size $name $visits $inbound $outbound);
Run Code Online (Sandbox Code Playgroud)
从我的角度来看,它应该像这样工作:
result=''
while read somevar
do
...
outbound=`cat "$www_path/$name/access.log"|grep \`date +"%d/%b/%Y"\`|awk '{ sum+=$11} END {print sum/1024/1024}'`
result=$(printf '%s| %-15s| %-25s| %-15s| %-15s| %-15s\n' $result $size $name $visits $inbound $outbound);
...
done
echo $result
Run Code Online (Sandbox Code Playgroud)
但它没有:(
UPD:
完整代码列表如下:
www_path='/var/www';
result='';
cd /var/www/; ls -d */ | while read i ; do basename "$i" ; done
while read i;
do du -sh "$i"|
while read size name
do
visits=`cat …Run Code Online (Sandbox Code Playgroud) 我在运行它时在C中重置sprintf的问题时重置计数器变量的值.简而言之,正在发生的事情:
int count = 0;
count++;
printf("%d\n", count); // count will equal 1
char title[7];
sprintf(title, "%.3d.jpg", count);
printf("%d\n", count); // count now equals 0 again
Run Code Online (Sandbox Code Playgroud)
这是sprintf的正常行为吗?
我尝试了下面的c程序,我希望得到编译时错误,但为什么编译器没有给出任何错误?
#include <stdio.h>
int main(void)
{
printf("%d\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么输出依赖于编译器?这是各种编译器的输出
Orwell Dev C++ IDE上的输出(使用gcc 4.8.1):0
Visual Studio 2010提供的Visual C++输出:0
CodeBlocks IDE(使用gcc 4.7.1):垃圾值
在线编译ideone.com:垃圾值
这里出了什么问题?
我刚碰巧看了printf(和其他fprintf类函数)的原型-
int printf(const char * restrict format, ...);
Run Code Online (Sandbox Code Playgroud)
restrict如果我理解正确的关键字,如果标记了其中一个,则禁止通过两个指针访问同一个对象restrict.
这里引用了与C标准相同的例子.
restrict我认为标记格式的一个好处是保存函数,使其不会在执行期间修改格式字符串(比如因为%n格式说明符).
但这是否会产生更大的约束?这会使以下函数调用无效吗?
char format[] = "%s";
printf(format, format);
Run Code Online (Sandbox Code Playgroud)
因为这里有明显的别名.为什么将restrict关键字添加到format参数中printf?
该%g说明符似乎并不在多数来源记录它运行得的方式行事.
根据我发现的大多数来源,在使用printf说明符的多种语言中,说明%g符应该等同于%f或者%e- 对于提供的值产生较短的输出.例如,在撰写此问题时,cplusplus.com表示说明g符意味着:
使用最短的表示:
%e或%f
而PHP手册说这意味着:
g - %e和%f中的较短者.
而且这里有一个堆栈溢出的答案是声称,
%g使用最短的表示.
而Quora的回答声称:
%g以这两个表示中最短的方式打印数字
但这种行为不是我在现实中看到的.如果我编译并运行该程序(作为C或C++ - 它是一个在两者中具有相同行为的有效程序):
#include <stdio.h>
int main(void) {
double x = 123456.0;
printf("%e\n", x);
printf("%f\n", x);
printf("%g\n", x);
printf("\n");
double y = 1234567.0;
printf("%e\n", y);
printf("%f\n", y);
printf("%g\n", y);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
...然后我看到这个输出:
1.234560e+05
123456.000000
123456
1.234567e+06
1234567.000000
1.23457e+06
Run Code Online (Sandbox Code Playgroud)
显然, …