我必须编写一个程序,其中main调用其他函数来测试一系列数字(如果有的话)是否小于一个数字,如果所有系列的数字都在两个限制之间,如果有的话是负数.我的代码返回值为1表示true,0表示false表示,但是赋值会将它们打印为"true"或"false".我不知道如何将bool答案从printf打印为字符串.我使用if(atl == false)printf("false"); 在我的at_least.c和main.c中,它只返回一个true或false的长字符串(例如:truetruetrue ....).我不确定这是不是正确的编码,我把它放在错误的位置,或者我需要使用其他一些代码.
这是我的main.c:
#include "my.h"
int main (void)
{
int x;
int count = 0;
int sum = 0;
double average = 0.0;
int largest = INT_MIN;
int smallest = INT_MAX;
bool atlst = false;
bool bet = true;
bool neg = false;
int end;
while ((end = scanf("%d",&x)) != EOF)
{
sumall(x, &sum); //calling function sumall
count++;
larger_smaller(x, &largest, &smallest); //calling function larger_smaller
if (atlst == false)
at_least(x, &atlst); //calling function at_least if x < 50 …Run Code Online (Sandbox Code Playgroud) 我从一个简单的cout没有输出,而printf将始终打印数字:
std::cout << variableuint8; // prints nothing
printf("%u", variableuint8); // prints the number
Run Code Online (Sandbox Code Playgroud)
我以前从未遇到过这种行为,虽然我有一个解决方法,但我想了解它.
耶,指针.所以它可能比所述的更多参与.这是上下文 - 我不认为它应该重要,当cout和printf获取信息时,它被解除引用到一个简单的unsigned char.这可能是解决问题所需的更多信息,但我想完全了解它是否相关.
typedef unsigned char UInt8;
typedef unsigned short UInt16;
typedef struct{
UInt8 len;
// some other definitions. sizeof(DeviceNotification) <= 8
}DeviceNotification;
UInt8 somerandomdata[8];
DeviceNotification * notification;
notification = (DeviceNotification *) somerandomdata;
std::cout << "Len: (" << notification->len << ")\n";
printf("Len: (%u)\n", notification->len);
Run Code Online (Sandbox Code Playgroud)
随机数据在别处初始化.对于此输出,数组中的第一个字节是0x08:
Len: ()
Len: (8)
Run Code Online (Sandbox Code Playgroud)
我使用Cygwin的意大利语本地化,因此我的printf命令使用逗号分隔浮点数,并且不理解以点分隔的浮点数
$ printf "%f" 3.1415
-bash: printf: 3.1415: invalid number
0,000000
$ printf "%f" 3,1415
3,141500
Run Code Online (Sandbox Code Playgroud)
这会产生一些问题,因为基本上其他所有东西都使用点来分隔十进制数字.
如何将小数点分隔符从逗号更改为点?
我正在编写以下代码:
#include<iostream>
#include<stdio.h>
using namespace std;
main() {
unsigned char a;
a=1;
printf("%d", a);
cout<<a;
}
Run Code Online (Sandbox Code Playgroud)
它打印1和一些垃圾.
为什么cout表现如此?
http://play.golang.org/p/joEmjQdMaS
package main
import "fmt"
type SomeStruct struct {
somePointer *somePointer
}
type somePointer struct {
field string
}
func main() {
fmt.Println(SomeStruct{&somePointer{"I want to see what is in here"}})
}
Run Code Online (Sandbox Code Playgroud)
这会打印出这样的内存地址 {0x10500168}
有没有办法让它打印:
{{"I want to see what is in here"}}
这主要是出于调试目的,如果我有一个包含30个指针字段的结构,我不想为30个字段中的每个字段执行println以查看其中的内容.
我正在实现一个使用不同内核的多线程程序,并且同时执行许多线程.每个线程都进行一次printf()调用,结果不可读.
我如何制作printf()原子,以便printf()一个线程中的printf()调用与另一个线程中的调用不冲突?
考虑这个示例程序:
#include <cstdio>
#include <cwchar>
#include <string>
int main()
{
std::string narrowstr = "narrow";
std::wstring widestr = L"wide";
printf("1 %s \n", narrowstr.c_str());
printf("2 %ls \n", widestr.c_str());
wprintf(L"3 %s \n", narrowstr.c_str());
wprintf(L"4 %ls \n", widestr.c_str());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这个输出是:
1 narrow
2 wide
Run Code Online (Sandbox Code Playgroud)
我在想:
printf当我使用ASCII表中的NULL字符时,为什么要打印空格而不是停止?这就是我的意思:
printf("Hello%c, world", 0); //Hello , world
printf("Hello%c, world", '\0'); //Hello , world
Run Code Online (Sandbox Code Playgroud)
只有当我将转义字符放在字符串本身时printf才会停止字符串:
printf("Hello\0, world"); //Hello
Run Code Online (Sandbox Code Playgroud)
我在Windows 8,Windows 10(使用cygwin,MinGW,Netbeans,Code :: Blocks),XUbuntu上试过这个,它都是一样的.
问题出在哪儿?我问过我的一个朋友,但他说他没有这样的问题,所有三个例子都以同样的方式执行.
我在nuwen.net上使用minGW的mingw-w64(x64)分支.这是来自7.1版本的gcc:
gcc --version
gcc (GCC) 7.1.0
Run Code Online (Sandbox Code Playgroud)
我正在编译这个程序:
#include <stdio.h>
int main(void)
{
size_t a = 100;
printf("a=%lu\n",a);
printf("a=%llu\n",a);
printf("a=%zu\n",a);
printf("a=%I64u\n",a);
}
Run Code Online (Sandbox Code Playgroud)
有警告和c11标准:
gcc -Wall -Wextra -Wpedantic -std=c11 test_size_t.c
Run Code Online (Sandbox Code Playgroud)
我收到这些警告:
test_size_t.c: In function 'main':
test_size_t.c:6:14: warning: format '%lu' expects argument of type 'long unsigned int', but argument 2 has type 'size_t {aka long long unsigned int}' [-Wformat=]
printf("a=%lu\n",a);
~~^
%I64u
test_size_t.c:6:14: warning: format '%lu' expects argument of type 'long unsigned int', but argument 2 has type 'size_t {aka long long unsigned …Run Code Online (Sandbox Code Playgroud) 我是 C++17 和std::string_view. 我了解到它们不是空终止的,必须小心处理。
这是 printf() 的正确方法吗?
#include<string_view>
#include<cstdio>
int main()
{
std::string_view sv{"Hallo!"};
printf("=%*s=\n", static_cast<int>(sv.length()), sv.data());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
(或者将它与任何其他 printf 风格的函数一起使用?)