我有一个关于使用printf的问题.
char str[8];
float val = 2.334563;
sprintf(str, format, val);
printf("val = %s.\n", str);
val = -23.34563;
sprintf(str, format, val);
printf("val = %s.\n", str);
val = -0.02334563;
sprintf(str, format, val);
printf("val = %s.\n", str);
val = 233;
sprintf(str, format, val);
printf("val = %s.\n", str);
Run Code Online (Sandbox Code Playgroud)
预期的产出如下:
val = +2.3345
val = -23.345
val = -0.0233
val = +233.00
Run Code Online (Sandbox Code Playgroud)
我需要什么格式字符串?感谢您的关注.
我有一个非常奇怪的问题/ bin/bash和一个使用printf格式化字符串的脚本.
我的脚本看起来像这样
rt=$(printf "%.3f" 13.234324245)
Run Code Online (Sandbox Code Playgroud)
差异,我计算数字13.23 ...以上.当我使用/ usr/bin/zsh时效果很好!偶然/ bin/sh可以做到(但它不能做if if ...)最大的问题是/ bin/bash似乎不理解printf或者当我不使用时确实有另一种格式化方式LANG=C.
我的LANG变量设置为de_AT.UTF-8然后我得到此错误:
/path/to/script: Zeile 12: printf: 13.234324245: Ungültige Zahl.
Run Code Online (Sandbox Code Playgroud)
所以它只是说我给printf的数字无效......
我需要以不同的方式运行printf吗?
编辑:问题似乎是在计算数字:
rt=$(printf "%.3f" $(echo "$res2 - $res1"|bc ))
Run Code Online (Sandbox Code Playgroud)
我怎么能告诉bc使用a ,而不是.?
在C你有"%c"和"%f"的格式标志printf-和scanf式的功能.这两个功能的使用可变长度参数...,它总是转换floats到doubles和chars到ints.
我的问题是,如果这种转换发生,为什么做独立的标志char和float存在吗?为什么不只使用相同的标志int和double?
我正试图制作一张这样的桌子......(只是没有用来分隔每个项目的点)
每周工资表:
名称.....................标题....... ......总税收......网
Ebenezer Scrooge合伙人250.00 ..62.25 .187.75
Bob Cratchit ..........职员...... 15.00 .... 2.00 ..13.00
这是我的代码看起来像这部分....
for (int count=0; count < numberOfEmployees; count++)
{
cout << "Employee: \n";
cout << "\t Name: ";
cin.getline (employees[count].name, EMPLOYEESIZE);
cout << "\t Title: ";
cin.getline (employees[count].title, EMPLOYEESIZE);
cout << "\t SSNum: ";
cin >> employees[count].SSNum;
cout << "\t Salary: ";
cin >> employees[count].Salary;
cout << "\t Withholding Exemptions: ";
cin >> employees[count].Withholding_Exemptions;
cin.ignore();
cout << "\n";
}
double gross;
double tax;
double net; …Run Code Online (Sandbox Code Playgroud) 我想建立一个计数器,告诉我一个长迭代计算(例如in for).
是否可以设置此计数器,当它在屏幕上更新时,它会替换以前的值?
也就是说,打印a的迭代器变量for是不行的,因为Matlab要么将它打印到新行,要么在之前的值之后打印,但是在10000次迭代之后,屏幕将以任一方式填充.另外,我想在每个回合更新计数器.
我对snprintf函数很困惑.首先,我没有在stdio.h下的turbo C版本编译器中找到函数snprintf.其次在GNU编译器中,当缓冲区大小小于格式化字符串时,snprintf返回-1,尽管它应该返回已经存在的字符数如果缓冲区大小足够大则打印.我有以下来源:
#include<stdio.h>
int main()
{
char str[100];
int numchar = snprintf(str,2,"ello jdj");
printf("%d\n",numchar);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
根据我所知的snprintf到目前为止,此代码应该输出8.但它在我的GNU编译器中返回-1.背后有什么事实?
我在Windows Vista上运行带有PyDev和Python 3.2的Eclipse,并且正在学习Python和ctypes的教程.
但是,我发现当我调用msvcrt.printf()来打印字符串时,在显示所有其他打印语句之前,这不会显示在Eclipse的控制台输出中.
这是我使用的确切代码:
from ctypes import *
msvcrt = cdll.msvcrt
message_string = "Hello Worlds!\n"
printf = msvcrt.printf
print(printf("Testing: %s".encode('ascii'),message_string.encode('ascii')))
print("foo")
print("why!?")
Run Code Online (Sandbox Code Playgroud)
这是输出:
23
foo
why!?
Testing: Hello Worlds!
Run Code Online (Sandbox Code Playgroud)
我在别处看到(对一般的)唯一的解释提到的printf是如何缓冲和显示之前需要一个换行符,但在字符串中换行,我还增加了一个直接的printf语句('的printf("测试:%s \n",......")并没有什么区别.
我想在Eclipse中工作,我不想每次想要测试脚本时都要继续打开命令提示符,那么有什么办法可以在控制台输出中修复这个顺序吗?为什么会发生这种情况?
我正在使用gcc编译器和ubuntu 12.04操作系统.我想知道在哪里可以找到目标文件以及在哪个目录下,其中包含printf函数的定义.我不是在寻找包含原型的头文件,而是包含实际定义的目标文件.
我想在c中包含i hex格式的值.
for(i=0;i<10;i++)
sprintf(s1"DTLK\x%x\xFF\xFF\xFF\xFF\xFF\xFF",i);
Run Code Online (Sandbox Code Playgroud)
但上面的代码输出一个错误:\ x使用没有后面的十六进制数字
请任何人建议我一个正确的方式....
在下面的程序中,我希望在输入一个单词并按下回车键之后,我应该立即看到该消息printf.但是,直到我输入其他随机单词才会发生.这是为什么?
#include <cstdio>
#include <cstdlib>
using namespace std;
char tictac[17];
int main()
{
scanf("%s\n", tictac);
printf("%s\n", tictac);
return 0;
}
Run Code Online (Sandbox Code Playgroud)