标签: format-specifiers

短整数的格式说明符

我没有正确使用C中的格式说明符.几行代码:

int main()
{
        char dest[]="stack";
        unsigned short val = 500;
        char c = 'a';

        char* final = (char*) malloc(strlen(dest) + 6);

        snprintf(final, strlen(dest)+6, "%c%c%hd%c%c%s", c, c, val, c, c, dest); 

        printf("%s\n", final);
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

我想要的是复制

final [0] =随机char final [1] =随机char final [2]和final [3] =短阵列final [4] =另一个char ....

我的问题是我想将short int的两个字节复制到最终数组的2个字节.

谢谢.

c c++ debugging format-specifiers

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

C printf字符串说明符\ t1?

我正在读一段C堆排序代码并遇到这个:

 do{
    printf("\n\t\t1:INSERT\n");
    printf("\n\t\t2:SEARCH\n");
    printf("\n\t\t3:DELETE\n");
    printf("\n\t\t1:DISPLAY\n");
    printf("Enter your choise\n");
    scanf("%d",&choise);
    switch(choise)
    {
        case 1: printf("Enter value to insert\n");
                scanf("%d",&val);
                last=insert(root,val);
                break;
        case 2:printf("Enter value for search \n");
                scanf("%d",&val);
                search(root,val);
                break;
        case 3:delete(root);
                delete(last);
                break;
        case 4:printf("\n\tHEAP\n");
                display(root);
                break;
        default : printf("INVALID choise ... can't you see properly?\n");

    }
Run Code Online (Sandbox Code Playgroud)

任何人都知道什么\t1\t2printfS和它们是如何工作的?我试过谷歌,但没有得到任何有用的信息.谢谢.

c string format-specifiers

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

将浮点变量显示为十六进制整数会拧紧相邻整数

我有这个简单的程序

#include <stdio.h>
int main(void)
{
 unsigned int a = 0x120;
 float b = 1.2;
 printf("%X %X\n", b, a);
 return 0;
}
Run Code Online (Sandbox Code Playgroud)

我期待输出

some-value 120  (some-value will depend on the bit pattern of `float b` )
Run Code Online (Sandbox Code Playgroud)

但我明白了

40000000 3FF33333
Run Code Online (Sandbox Code Playgroud)

为什么a搞砸的价值?%X把其作为参数signed int,因此它应该检索从堆叠的4个字节并打印的calue b然后获取下一个4个字节打印值的a哪个是0x120

c floating-point printf hex format-specifiers

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

为什么我的%s格式说明符没有在此代码段中捕获?

mconfigQuantity = int(raw_input('Enter the number of (m)achine-configurations that you will need: '))
numberOfmconfig = 1


for mconfigs in range(1,mconfigQuantity+1):
    # %s conditional for mcidentifier
    STRINGnumberOfmconfig = str(numberOfmconfig)
    if STRINGnumberOfmconfig[-1] == '1':
        suffix = 'st'
    elif STRINGnumberOfmconfig[-1] == '2':
        suffix = 'nd'
    elif STRINGnumberOfmconfig[-1] == '3':
        suffix = 'rd'
    else: suffix = 'th'
    finalConfigName = STRINGnumberOfmconfig + suffix
    mcidentifier = raw_input('Enter the letter of your %s (m)achine-configuration: ') % (finalConfigName)
    numberOfmconfig = numberOfmconfig + 1






print mconfig()
Run Code Online (Sandbox Code Playgroud)

我现在已经编写并重写了几次这个片段,对于我为什么打印'%s'而不是用我为它指定的数据替换它是没有意义的.我找不到任何表明它无法正常工作的东西.

python format-specifiers

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

printf("%*.*s",int,int,char*)在c中的含义是什么?

我有一个代码片段,其中有一个声明

printf("%*.*s");
Run Code Online (Sandbox Code Playgroud)

什么%*.*s意思?

代码是

char *c="**********";
int i,n=4;
for(i=1;i<=n;i++)
{
printf("%*.*s\n",i,i,c);
}
Run Code Online (Sandbox Code Playgroud)

输出是:

*
**
***
****
Run Code Online (Sandbox Code Playgroud)

c printf format-specifiers

0
推荐指数
2
解决办法
2146
查看次数

为什么%在C中打印一半?

#include<stdio.h>
main()
{
printf("% % % %");
}
Run Code Online (Sandbox Code Playgroud)

对于上述程序,输出为%%.但为什么?(我用过gcc编译器).

c format-specifiers

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

%zd说明符是C11中的可选功能吗?

我使用了一个非常简单的代码:

int main(void)
{
    size_t variable;
    /*prompt*/
    printf("enter the value of the variable: ");
    scanf("%zd", variable);
    printf("you entered %zd value of the variable", variable);
}  
Run Code Online (Sandbox Code Playgroud)

但是,GCC编译器会产生以下结果:

Enter the vale of the size_t variable: 15
You entered zd value of size_t type
Process returned 35 (0X23)  execution time: 3.094s
Press any key to continue
Run Code Online (Sandbox Code Playgroud)

我的书也直接演示了上面的例子,但没有提到它是某种特殊的格式说明符,如果要包含库文件的话.即使是在线编译器也没有产生正确的结果.为什么代码不起作用?

c scanf format-specifiers c11

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

int x; scanf()包含%d,printf()包含%c

int x; 因此变量将有2个字节的内存.现在,如果我输入66并且因为scanf()带有%d,66将存储在2字节内存中,因为该变量被声明为int.

现在在带有%c的printf()中,应该只从一个字节内存中收集数据来显示.

但是%c通过从内存中获取正确的数据66来正确显示B.

为什么%c不只是从一个字节获取数据?

c format-specifiers conversion-specifier

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

哪个说明符用于打印long double的最大/最小值?

作为一个新的C编程学习者,我试图理解我应该在printf中使用哪个说明符来显示long double类型的最大值和最小值.根据以下StackOverflow答案,我可以使用%Lg:https://stackoverflow.com/a/18797417/1536240

但这似乎没有成功.我已经尝试了%e,%f,%Lf,%g和%Lg,但它不起作用.这是我的代码的相关部分我遇到了问题(我正在做的其他工作正常):

printf("**long double**\nStorage size: %d bytes \t Minimum value: %Lg 
\t Maximum value: %Lg\n", sizeof(long double), LDBL_MIN, LDBL_MAX);
Run Code Online (Sandbox Code Playgroud)

我在顶部包含了这些头文件:

#include <stdio.h>
#include <limits.h>
#include <float.h>
Run Code Online (Sandbox Code Playgroud)

我有点迷失在这里.

编辑:很抱歉没有解释输出是什么.我现在正在使用%Lg,而且我在终端中得到了这个.

我的输出

c floating-point printf types format-specifiers

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

如何在C中格式化指定一个const char *?

我有这个循环:

while (fscanf(file, "%s", word) != EOF)
Run Code Online (Sandbox Code Playgroud)

word这里const char *不是char * 那么我应该如何改变%s

c pointers format-specifiers

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