问题是静态分配一个足够大的缓冲区以适合打印的double,格式化为%g,最大精度.这似乎是一个足够简单的任务,我遇到了麻烦.我想出的最好的(假设要打印的数字是x)是
char buf[1 + DBL_DIG + DBL_DIG + 1 + 1 + 1 + DBL_DIG + 1];
int len = sprintf(buf, "%.*g", DBL_DIG, x);
Run Code Online (Sandbox Code Playgroud)
DBL_DIG宏来自float.h,显然它应该表示double类型的最大精度.我们需要:
我使用有效位数作为指数中位数的上限.我有任何错误吗?有更好的解决方案吗?我应该只分配64,128或256字节并希望最好吗?
我想使用printf在ARM微控制器的串行端口上显示文本.我无法这样做.任何帮助表示赞赏.
我的init_serial看起来像这样
void init_serial (void)
{
PINSEL0 = 0x00050000; /* Enable RXD1 TxD1 */
U1LCR = 0x00000083; /*8 bits, 1 Stop bit */
U1DLL = 0x000000C2; /*9600 Baud Rate @12MHz VPB Clock */
U1LCR = 0x00000003; /* DLAB=0*/
}
Run Code Online (Sandbox Code Playgroud)
这显然是错的.
所以,我的类型为__be16(2个字节).在十六进制中,该值表示为十进制的0x0800或2048.(16 ^ 2*8)
所以,当我打印这个; 我这样做:
printf("%04X", value); //__be16 value;
//Print a hex value of at least 4 characters, no padding.
Run Code Online (Sandbox Code Playgroud)
输出:0008
printf("%i", value); //Print an integer.
Run Code Online (Sandbox Code Playgroud)
输出:8
我应该分别得到0800和2048,我做错了什么?
我有这个printf声明:
printf("name: %s\t"
"args: %s\t"
"value %d\t"
"arraysize %d\t"
"scope %d\n",
sp->name,
sp->args,
sp->value,
sp->arraysize,
sp->scope);
Run Code Online (Sandbox Code Playgroud)
它位于for循环中,因此它为指针列表打印多行.
问题是,如果打印的某些东西更长或更短,则会导致事物不对齐.如何让它始终排队?
在过去的48小时左右我一直在削减我的牙齿试图在C中实现这个哈希表功能.我的代码相当长(我意识到它不是最有效的,有些更多是我玩C来获取感觉它是如何工作的等等.
我遇到的问题是我的主程序的最后一行在底部(打印MyEntry-> Name).我收到一个总线错误,我不确定为什么.我不相信我应该在主指针中为这个指针分配内存,但我可能是错的.
抱歉,此代码的长度.BTW SymEntry是'struct SymEntry {char*Name,void*Attributes,struct SymEntry*Next}
#include <strings.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
#include "SymTab.h"
struct SymTab * CreateSymTab(int Size)
{
struct SymTab *symtable;
if(!(symtable=malloc(sizeof(struct SymTab)))) return NULL;
if(!(symtable->Contents=calloc(Size, sizeof(struct SymEntry*)))) {
free(symtable);
return NULL;
}
symtable->Size=Size;
return symtable;
}
/* hash form hash value for string s, taken from 'The C Programming Language'*/
unsigned hash(struct SymTab *ATable, const char *s)
{
unsigned hashval, size;
size = ATable->Size;;
for (hashval = 0; *s != '\0'; …Run Code Online (Sandbox Code Playgroud) 我有一个处理一些数据的函数,并找到用最低错误对数据进行分类的阈值.它看起来像这样:
void find_threshold(FeatureVal* fvals, sampledata* data, unsigned int num_samples, double* thresh, double* err, int* pol) {
//code to calculate minThresh, minErr, minPol omitted
printf("minThresh: %f, minErr: %f, minPol: %d\n", minThresh, minErr, minPol);
*thresh = minThresh;
*err = minErr;
*pol = minPol;
}
Run Code Online (Sandbox Code Playgroud)
然后在我的测试文件中我有这个:
void test_find_threshold() {
//code to set up test data omitted
find_threshold(fvals, sdata, 6, &thresh, &err, &pol);
printf("Expected 5 got %f\n", thresh);
assert(eq(thresh, 5.0));
printf("Expected 1 got %d\n", pol);
assert(pol == 1);
printf("Expected 0 got %f\n", err);
assert(eq(err, …Run Code Online (Sandbox Code Playgroud) 打印格式字符串如何作为参数传递?
example.cpp:
#include <iostream>
int main(int ac, char* av[])
{
printf(av[1],"anything");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
尝试:
example.exe "print this\non newline"
Run Code Online (Sandbox Code Playgroud)
输出是:
print this\non newline
Run Code Online (Sandbox Code Playgroud)
相反,我想:
print this
on newline
Run Code Online (Sandbox Code Playgroud) 这是我的编码,它给我错误'警告:未知的转换类型字符0x20格式'
int subtotal;
long long a,b,c,d,e,f,g,h,i,j,k,l,m;
subtotal = (1*(a+c+e+g+i+k))+(3*(b+d+f+h+j+l));
printf(" = %d % 10 = %d; (10 - %d) % 10 = %lld\n", subtotal,subtotal%10,subtotal%10,m);
Run Code Online (Sandbox Code Playgroud)
知道为什么这是错的吗?
我正在写一个任务,涉及在Solaris盒子上向PostgreSQL添加一些功能.作为任务的一部分,我们需要在客户端打印一些信息(即:使用elog.)
PostgreSQL已经有很多帮助方法可以打印出所需的信息,但是,帮助方法包含了数百个printf调用,并且该elog方法仅适用于c风格的字符串.
我可以暂时将printf调用重定向到缓冲区,以便我可以轻松地将其发送elog到客户端吗?
如果那是不可能的,那么修改辅助方法以最终将缓冲区作为输出的最简单方法是什么?
编辑: 主要的完整代码在这里http://codepad.org/79aLzj2H
再一次,这就是奇怪的行为正在发生
for (i = 0; i<tab_size; i++)
{
//CORRECT OUTPUT
printf("%s\n", tableau[i].capitale);
printf("%s\n", tableau[i].pays);
printf("%s\n", tableau[i].commentaire);
//WRONG OUTPUT
//printf("%s --- %s --- %s |\n", tableau[i].capitale, tableau[i].pays, tableau[i].commentaire);
}
Run Code Online (Sandbox Code Playgroud)
我有一系列以下strcuture
struct T_info
{
char capitale[255];
char pays[255];
char commentaire[255];
};
struct T_info *tableau;
Run Code Online (Sandbox Code Playgroud)
这是阵列的填充方式
int advance(FILE *f)
{
char c;
c = getc(f);
if(c == '\n')
return 0;
while(c != EOF && (c == ' ' || c == '\t'))
{
c = getc(f);
}
return fseek(f, -1, …Run Code Online (Sandbox Code Playgroud) printf ×10
c ×8
formatting ×3
c++ ×2
string ×2
bus ×1
double ×1
embedded ×1
newline ×1
pointers ×1
postgresql ×1
redirect ×1
serial-port ×1
solaris ×1