我有一个UINT16无符号整数
4455, 312, 560 or 70.
Run Code Online (Sandbox Code Playgroud)
如何使用printf在最后两位数之前插入一个小数点,以便示例数字显示为
44.55, 3.12, 5.60 or 0.70
Run Code Online (Sandbox Code Playgroud)
如果没有printf解决方案,还有其他解决方案吗?
我不想使用浮点数.
我正在编写一个名为nauty的程序.该程序使用规范函数名称getline,它也是标准GNU C库的一部分.
是否有可能在编译时告诉GCC使用这个程序定义的函数?
我从很多人那里听说你不能保证类型转换会无损.只有在您不了解处理器时才会这样,也就是说,您还没有验证用于数据类型的字节数?让我举个例子:
如果执行以下操作:
typedef struct
{
int i;
char c;
float f;
double d;
} structure;
size_t voidPtrSz = sizeof(void *);
size_t charPtrSz = sizeof(char *);
size_t intPtrSz = sizeof(char *);
size_t floatPtrSz = sizeof(float *);
size_t doublePtrSz = sizeof(double *);
size_t structPtrSz = sizeof(structure *);
size_t funcPtrSz = sizeof(int (*)(float, char));
printf("%lu\n", voidPtrSz);
printf("%lu\n", charPtrSz);
printf("%lu\n", intPtrSz);
printf("%lu\n", floatPtrSz);
printf("%lu\n", doublePtrSz);
printf("%lu\n", structPtrSz);
printf("%lu\n", funcPtrSz);
Run Code Online (Sandbox Code Playgroud)
......输出如下......
4
4
4
4
4
4
4
Run Code Online (Sandbox Code Playgroud)
您能否假设在所有情况下都可以安全地将特定数据类型指针类型转换为另一个数据类型指针?例如,如果您执行此操作:
int foo(float, char)
{
}
void …Run Code Online (Sandbox Code Playgroud) 我正在将一个较旧的项目从C++ Builder 2009移植到XE5.在旧项目中,Unicode字符串的编译器选项设置为" _TCHAR映射到:char".这在旧项目中运行良好.
移植时,我在XE5中设置了相同的编译器选项.但我仍然得到这样的代码编译器错误:
std::string str = String(some_component.Text).t_str();
Run Code Online (Sandbox Code Playgroud)
这会出现以下错误:
[bcc32 Warning] file.cpp(89):W8111访问不推荐使用的实体'UnicodeString :: t_str()const'
[bcc32错误] file.cpp(89):E2285无法找到'operator string :: =(wchar_t*)'的匹配项
显然XE5已经决定String::t_str()应该给我一个wchar_t*而不是一个char*,即使我已经设置了如上所述的编译器选项.
我该如何解决这个问题?
我很清楚C++ Builder已经采取了内部使用Unicode的步骤(即使在2009版本中),但这是一个200k LOC的旧项目.将其更新为Unicode将是一项非常低优先级的陡峭任务.
编辑
我可以通过更改代码来使其工作
std::string str = AnsiString(some_component.Text).c_str();
Run Code Online (Sandbox Code Playgroud)
但这意味着我必须在很多地方更改代码.有没有更好的方法不涉及重写代码?
void main() {
if(-1 > 0U)
printf("True\n");
else
printf("False\n");
}
Run Code Online (Sandbox Code Playgroud)
它是依赖于处理器的(大端/小端)吗?
就像标题所说,我需要code::blocks与之合作C11,我无法弄清楚如何去做.
我去了settings=> compiler settings=> Other options我添加-std=c11并尝试了-std=gnu11,两者似乎都没有用.
我编译gcc-5.2然后我更改了默认编译器(gcc-4.9)仍然没有结果.
当我尝试编译以下程序时:
#include<stdio.h>
int main(void){
int arr[] = {0,1,2,3,4};
for(int i=0;i<5;i++){
printf("%d ",arr[i]);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到以下内容:
|6|error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode|
Run Code Online (Sandbox Code Playgroud)
但如果我在终端(ubuntu 15.04,64BIT,gcc-5.2)中这样做:
./install/gcc-5.2.0/bin/gcc5.2 program.c -o program
Run Code Online (Sandbox Code Playgroud)
似乎工作正常.
我的问题是,如何code::blocks与之合作c11?
我试图做变量位算术a和b.当我反转时a,其值为0xAF,结果显示为8位数.与其他显示为2位数的人不同.
我不知道为什么会发生这种情况,但是猜测它与%x显示方式和小端有关?
这是我的代码:
#include <stdio.h>
int main()
{
int a = 0xAF; // 10101111
int b = 0xB5; // 10110101
printf("%x \n", a & b); // a & b = 10100101
printf("%x \n", a | b); // a | b = 10111111
printf("%x \n", a ^ b); // a ^ b = 00011010
printf("%x \n", ~a); // ~a = 1....1 01010000
printf("%x \n", a << 2);// a << 2 = 1010111100
printf("%x …Run Code Online (Sandbox Code Playgroud) 码:
static volatile unsigned char TMR0 @ 0x01;
static volatile unsigned char PORTA @ 0x05;
static volatile unsigned char PORTB @ 0x06;
static volatile unsigned char PORTC @ 0x07;
Run Code Online (Sandbox Code Playgroud)
此代码来自PIC16F877A的HT-PICC编译器pic.h库文件
我理解静态volatile和其他关键字的含义.这里Timer0的寄存器地址是0x01,但为什么它们@在它前面使用呢?它与指针有关吗?
我在codeacademy上学习c,并且从不兼容的类型“ void *”错误中分配了“ int *”。我的文件是一个.c文件,其中包括stdlib.h。我不理解该错误,似乎更正使用了相同的代码行。
我正在尝试使用malloc创建一个数组。
我试图找到其他主题的答案。看来malloc并不是做到这一点的最佳方法,但我想找到一种使它起作用的方法。
我在Mac上,我使用emacs进行编码和gcc进行编译。
这是我的代码的一部分:
int main(int argc, char *argv[])
{
char mot_secret[] = "PISCINE";
int nombre_lettres = strlen(mot_secret);
int *pnombre_lettres = &nombre_lettres;
int *decouverte = NULL;
int compteur = 10;
decouverte = malloc(nombre_lettres * sizeof(int));
if (decouverte == NULL)
{
exit(0);
}
Run Code Online (Sandbox Code Playgroud)
这是解决方案:(我尝试翻译一些变量)
int main(int argc, char* argv[])
{
char lettre = 0;
char secretword[100] = {0};
int *lettreTrouvee = NULL;
long coupsRestants = 10; // Compteur de coups restants (0 …Run Code Online (Sandbox Code Playgroud) 我对sizeof()C语言的输出感到困惑。说我有:
struct foo {
char a;
char b;
char c;
char d[0];
};
Run Code Online (Sandbox Code Playgroud)
我希望sizeof(struct foo)是4。但是,用gcc编译后返回3。另外,在使用严格的设置编译代码时-pedantic-errors,会出现编译器错误。
有人可以帮助我了解这种行为吗?