我没有正确使用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堆排序代码并遇到这个:
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和\t2在printfS和它们是如何工作的?我试过谷歌,但没有得到任何有用的信息.谢谢.
我有这个简单的程序
#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
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'而不是用我为它指定的数据替换它是没有意义的.我找不到任何表明它无法正常工作的东西.
我有一个代码片段,其中有一个声明
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) #include<stdio.h>
main()
{
printf("% % % %");
}
Run Code Online (Sandbox Code Playgroud)
对于上述程序,输出为%%.但为什么?(我用过gcc编译器).
我使用了一个非常简单的代码:
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)
我的书也直接演示了上面的例子,但没有提到它是某种特殊的格式说明符,如果要包含库文件的话.即使是在线编译器也没有产生正确的结果.为什么代码不起作用?
int x; 因此变量将有2个字节的内存.现在,如果我输入66并且因为scanf()带有%d,66将存储在2字节内存中,因为该变量被声明为int.
现在在带有%c的printf()中,应该只从一个字节内存中收集数据来显示.
但是%c通过从内存中获取正确的数据66来正确显示B.
为什么%c不只是从一个字节获取数据?
作为一个新的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,而且我在终端中得到了这个.
我有这个循环:
while (fscanf(file, "%s", word) != EOF)
Run Code Online (Sandbox Code Playgroud)
但word这里const char *不是char *
那么我应该如何改变%s?