什么是使用之间的区别scanf有以下格式说明,当被扫描的输入是123456与当它是123:
%6d%-6d%0d输出有什么不同?
我正在尝试实现一个返回十六进制值字符串的函数.我使用此函数打印出十六进制值:
void print_hex(unsigned char *hash, const hashid type) {
int i;
for (i = 0; i < mhash_get_block_size(type); i++) {
printf("%.2x", hex[i]);
}
printf("\n");
}
Run Code Online (Sandbox Code Playgroud)
这会输出一些十六进制值,例如 71c092a79cf30c4c7e7baf46a4af3c78cedec9ae3867d1e2600ffc39d58beaf2
如何修改此函数以使其返回字符串?即
unsigned char *get_hash_str(unsigned char *hash, const hashid type) { /* ?? */ }
Run Code Online (Sandbox Code Playgroud)
(目标是稍后比较2个值)
这是我的代码:
int main(){
char *p = "koraytugay";
printf("%s%i byte(s).\n", "Size of variable p:" ,sizeof(p));
printf("%s%i byte(s).\n\n", "Size of what p points to:" ,sizeof(*p));
char t[] = "koraytugay";
printf("%s%i byte(s).\n", "Size of variable t:" ,sizeof(t));
printf("%s%i byte(s).\n\n", "Size of what t points to:" ,sizeof(*t));
printf("%s%c\n", "Printing p[3]: ", p[3]);
printf("%s%c\n", "Printing t[3]: ", t[3]);
printf("%s",*(&p));
}
Run Code Online (Sandbox Code Playgroud)
我得到的输出是:
Size of variable p:8 byte(s).
Size of what p points to:1 byte(s).
Size of variable t:11 byte(s).
Size of what t points to:1 byte(s). …Run Code Online (Sandbox Code Playgroud) int arrSize = 10;
double* arr = new double [arrSize];
for(int i = 0; i < arrSize; i++){
arr[i] = i;
}
for(int i = 0; i < arrSize; i++){
printf("%d", arr[i]);
//std::cout<<arr[i];
}
Run Code Online (Sandbox Code Playgroud)
这里
printf() 打印0000000000.cout 打印0123456789.为什么?
我只是写一个非常小的代码scanf和printf。我正在读取一个双精度值并将其打印出来。转换规范%lf可以正常工作以读取双精度值。但是,它不适用于printf。
当我尝试打印该值时,我得到的输出像 0.000000
double fag;
scanf("%lf", &fag);
printf("%lf", fag);
Run Code Online (Sandbox Code Playgroud)
但是,如果我在printf中使用%f,它可以正常工作。
我做了一个非常简单的程序来打印两个变量的地址.
#include<stdio.h>
int main()
{
int a,b;
printf("%u\n%u",&a,&b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,Clang-3.7编译器发出警告:
warning:format指定类型'unsigned int'但参数的类型为'int*'[-Wformat]`
但是,当我使用GCC-5.x进行编译时,它没有发出任何警告.哪一项是正确的?
我知道的一件事是做错unsigned int num=&a;会因为地址只能存储在指针中.但是,编译器在打印地址时发出警告是否正确?
我从gcc.godbolt.org编译了我的程序
我正在尝试添加c代码,但我的程序没有执行,不幸的是,代码块关闭了.错误是什么?
void main()
{
float a,b;
printf("%30sAddition Of Numbers\n");
printf("\nEnter Number 1: ");
scanf("%f",&a);
printf("\nEnter Number 2: ");
scanf("%f",&b);
printf("\nThe addition of %0.3f and %0.3f is %0.3f",a,b,(a+b));
}
Run Code Online (Sandbox Code Playgroud)
我想直接在printf语句中使用float输入添加结果,但我没有让它工作.
我也尝试将结果放在变量a中,但它也没有用.
void main()
{
float a,b;
printf("%30sAddition Of Numbers\n");
printf("\nEnter Number 1: ");
scanf("%f",&a);
printf("\nEnter Number 2: ");
scanf("%f",&b);
a=a+b;
printf("\nThe addition of %0.3f and %0.3f is %0.3f",a,b,a);
}
Run Code Online (Sandbox Code Playgroud)
我哪里错了?
我正在学习C,由于某种原因,我%s不会打印出类似的东西.它只是随机字符,%d它总是打印一个像4600688的数字,而不是像我设置的那样12:
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("why is %s happening to me"), "this";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这导致:
为什么PE会发生在我身上
(和return文字).
这是编译器错误还是我该如何解决?
printf函数int到%f,浮动到%d试图实验
#include<stdio.h>
int main(){
int i=10;
float x=43.2892f;
printf("i=%f x=%d \n",i,x);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
OUTPUT:
i=43.289200 x=10
Run Code Online (Sandbox Code Playgroud)
需要帮助来理解为什么这些变量是互换的?
当我运行以下代码时:
printf("%d %f %d %d %f\n", 1.2 , 3000, 2.5, 400, 500);
Run Code Online (Sandbox Code Playgroud)
我认为答案可能是一些毫无意义的数字,但结果实际上是:
3000 1.200000 400 500 2.500000
Run Code Online (Sandbox Code Playgroud)
这与我输入的数字和格式相同.
它是如此有意义,以至于我无法说服自己忽视它.
有人能告诉我原因吗?我会很感激.
ps我正在使用Clion作为我的IDE.