标签: format-specifiers

float variables in C

May be this is a simple question but I am not sure about how float variables are stored in memory and why it is behaving in this way, can someone please explain about the following behavior.

#include<stdio.h>

int main ()
{
    int a = 9/5;
    printf("%f\n", a);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Output:

0.000000
Run Code Online (Sandbox Code Playgroud)

I have looked at some information on how float variables are stored in memory, it has stuff about mantissa, exponent and sign. But I am not getting how …

c format-specifiers

1
推荐指数
1
解决办法
522
查看次数

模板化类型的格式说明符

我有一个在整数类型上模板化的C++类,例如,

template<typename int_type>
Run Code Online (Sandbox Code Playgroud)

比如那个类中的某个地方,我想用来sscanf从文件中读取一些值,例如,

int_type num_rows;
fgets( buffer, BUFSIZE, in_file );
sscanf( buffer, "%d", &num_rows);
Run Code Online (Sandbox Code Playgroud)

格式说明符仅int_type在内在函数时才能正常工作int.

是否有更好的方法来处理格式说明符int_type

c++ templates format-specifiers

1
推荐指数
1
解决办法
1320
查看次数

Java和C之间关系不一致

如果我的问题看起来很不自然,请原谅我.这是我在这里的第一个问题.

我脑子里有一个简单的查询.Java语言已经使用CC++实现.

我的问题是

在C语言中,使用与给定数据类型不同的修饰符时,未定义C的格式化行为.

int c=10;
printf("%f",c);    // unspecified behaviour
Run Code Online (Sandbox Code Playgroud)

而在Java中,如果我们使用类似的概念来使用不同的格式说明符打印数字,我们得到一个IllegalFormatConversionExceptoion.

例外细节: -

线程"main"中的异常java.util.IllegalFormatConversionException:f!= java.lang.Integer

我的问题是: -

为什么Java和C之间存在差异,尽管Java是从C派生的!

请清除我的怀疑.我会感谢天才......

c java format exception format-specifiers

1
推荐指数
1
解决办法
67
查看次数

为什么C和Objective-C都没有布尔值的格式说明符?

我正在处理的应用程序有一个信用响应对象,其中包含布尔"已批准"字段.我试图在Objective C中注销这个值,但由于没有布尔值的格式说明符,我不得不求助于以下内容:

NSLog("%s", [response approved] ? @"TRUE" : @"FALSE");
Run Code Online (Sandbox Code Playgroud)

虽然不可能,但我更愿意做以下事情:

NSLog("%b", [response approved]);
Run Code Online (Sandbox Code Playgroud)

...其中"%b"是布尔值的格式说明符.

在做了一些研究之后,似乎一致的共识是C和Objective-C都没有相当于"%b"的说明符,并且大多数开发者最终都会自己滚动(类似上面的选项#1).

显然Dennis Ritchie&Co.知道他们写C时他们在做什么,我怀疑这个丢失的格式说明符是一个意外.我很想知道这个决定背后的理由,所以我可以向我的团队解释(他们也很好奇).

编辑: 下面的一些答案表明它可能是一个本地化问题,即"TRUE"和"FALSE"太具有英语特色.但这不是所有语言都面临的两难选择吗?即不只是C和Objective-C?除其他外,Java和Ruby能够实现"True"和"False"布尔值.不知道为什么这些langs的作者没有同样的选择.

此外,如果本地化是问题,我希望它也会影响语言的其他方面.例如,使用受保护的关键字.C使用英语关键词如"include","define","return","void"等,而非英语使用者解析这些关键词比"true"或"false"这样的关键词更难.

c objective-c format-specifiers

1
推荐指数
1
解决办法
152
查看次数

为什么以下代码提供此输出?

我正在调整printf()函数.因为printf()函数采用格式说明符来识别要打印的数据类型(纠正我,如果我错了)作为参数传递给它的数据类型的值,所以我尝试做这样的事情---

#include<stdio.h>
int main()
{
char *f="%d";
char *g="asd";

printf(f,g);
return 0;
}
Run Code Online (Sandbox Code Playgroud)

o/p是 -

4210739

我无法理解为什么o/p是一个数字?

c printf format-specifiers

1
推荐指数
2
解决办法
102
查看次数

为什么这个程序不能打印所需的输出?

我只知道这个链接的%i格式说明符

printf中格式说明符%i和%d之间的差异

我尝试用这个程序实现它.

#include <stdio.h>

int main(){

    long long a,b;

    printf("Input: ");

    scanf("%i %lld",&b,&a);

    printf("Output: %i %lld",b,a);
}
Run Code Online (Sandbox Code Playgroud)

%我工作正常,但%lld在变量a中存储垃圾值.

这是该程序的输出.

输入:033 033

产量:27 141733920846

进程返回0(0x0)执行时间:4.443 s按任意键继续.

谁能解释一下,为什么我在变量a中得到垃圾值?

c format-specifiers

1
推荐指数
1
解决办法
70
查看次数

关于C中的转换说明符

我对C中的转换说明符有疑问.

在第5句中,如果我使用%lf%Lf代替%f,则不会发生错误.但是,如果我使用,为什么会发生错误%f

#include <stdio.h>

int main(void)
{
    long double num;
    printf("value: ");
    scanf("%f",&num);  // If I use %lf or %Lf instead of %f, no error occurs. 
    printf("value: %f \n",num);
}
Run Code Online (Sandbox Code Playgroud)

c format-specifiers

1
推荐指数
1
解决办法
577
查看次数

在整数指针前面写(void*)有什么意义?

我读了以下程序代码:

#include <stdio.h> 
#include <stdlib.h>


int main () {
    int *p;
    p= malloc(sizeof(int)); 
    *p=42; 

    printf("Pointer p has address %p and points to %p\n",
    (void*)&p, (void*)p);

   free(p);    

}
Run Code Online (Sandbox Code Playgroud)

我的问题涉及以下部分:

printf("Pointer p has address %p and points to %p\n", (void*)&p, (void*)p);
Run Code Online (Sandbox Code Playgroud)

我不明白(void*)它在做什么.这是演员吗?这样做有什么意义?

简单地写下面的内容有什么区别?

 printf("Pointer p has address %p and points to %p\n", &p, p);
Run Code Online (Sandbox Code Playgroud)

c printf pointers void-pointers format-specifiers

1
推荐指数
1
解决办法
390
查看次数

是"printf("%d \n",(int)a);" unsigned int的未定义行为?

如果我使用错误的格式说明符,如下所示:

unsigned int i = -1;
printf("%d\n", i);
Run Code Online (Sandbox Code Playgroud)

它被调用未定义的行为,因为%u格式说明符unsigned.

C11标准§7.21.6.1(P9):

如果转换规范无效,则行为未定义.282)如果任何参数不是相应转换规范的正确类型,则行为未定义.

但是,如果我这样写:

unsigned int i = -1;
printf("%d\n", (int)i); // unsigned to signed 
Run Code Online (Sandbox Code Playgroud)

它也是未定义的行为吗?

c format-specifiers language-lawyer

1
推荐指数
1
解决办法
115
查看次数

打印新值时得到以前的浮点值

我正在0.23从第二个输出printf。但是类型转换可以提供所需的输出。如果我不使用类型转换,则会打印以前的值。编译器版本为GCC 6.3

#include <stdio.h>

int main() {
    printf("%f ", 0.23);
    printf("%f", 0);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

链接IDE

c format-specifiers

1
推荐指数
1
解决办法
72
查看次数