#include<stdio.h>
int func(int x){
printf("Print\n");
return x;
}
void main(){
printf("The value of x is %d",func(50)); /* Print is printed first then the value of x */
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该程序的输出是:
Print
The value of x is 50
Run Code Online (Sandbox Code Playgroud)
所以我的问题是为什么功能打印Print之后它是打印The value of x is 50。为什么The value of x is不在之前打印,因为函数是在语句之后调用的。
Program received signal SIGSEGV, Segmentation fault.
memcpy ()
at ../sysdeps/x86_64/multiarch/../multiarch/memmove-vec-unaligned-erms.S:143
143 ../sysdeps/x86_64/multiarch/../multiarch/memmove-vec-unaligned-erms.S: No such file or directory.
(gdb) bt
#0 memcpy ()
at ../sysdeps/x86_64/multiarch/../multiarch/memmove-vec-unaligned-erms.S:143
#1 0x00007ffff764aa2e in __GI__IO_default_xsputn (f=0x7fffffffcf30,
data=<optimized out>, n=30) at genops.c:438
#2 0x00007ffff761c59c in _IO_vfprintf_internal (
s=s@entry=0x7fffffffcf30, format=<optimized out>,
format@entry=0x55555555773f "%s/%s", ap=ap@entry=0x7fffffffd058)
at vfprintf.c:1637
#3 0x00007ffff763e61b in __IO_vsprintf (
string=0x3930313938393339 <error: Cannot access memory at address 0x3930313938393339>, format=0x55555555773f "%s/%s",
args=args@entry=0x7fffffffd058) at iovsprintf.c:42
#4 0x00007ffff7623717 in __sprintf (s=<optimized out>,
format=<optimized out>) at sprintf.c:32
#5 0x000055555555680e in take_action ( …Run Code Online (Sandbox Code Playgroud) 鉴于以下代码片段:
signed char x = 150;
unsigned char y = 150;
printf("%d %d\n", x, y);
Run Code Online (Sandbox Code Playgroud)
输出是:
-106 150
Run Code Online (Sandbox Code Playgroud)
但是,对于以相同方式在内存中表示的变量,我使用了相同的格式说明符。printf 如何知道它是有符号的还是无符号的。
两种情况下的内存表示为:
10010110
Run Code Online (Sandbox Code Playgroud) 我有一个简单的工作函数,可以创建一个表记录文件信息。
但是,printf以错误的顺序打印我的输出。我尝试使用fflush(stdout)刷新缓冲区,但它没有改变任何东西。
功能:
void printTable(char *libraryName)
{
printf("%-40s%-30s%-25s%-20s\n", "[File Name]", "[Creation Time]", "[Version]", "[Size]");
fflush(stdout);
printf("%-40s%-30d%-25d%-20f\n", libraryName, getCreationTime(libraryName), getVersion(libraryName), getSize(libraryName));
fflush(stdout);
}
Run Code Online (Sandbox Code Playgroud)
get 函数在单独的文件中定义并提供准确的输出。
编译后,输出如下所示:
[File Name] [Creation Time] [Version] [Size]
125 kB
Version: 1.0.0.0 [6-02-2020 03:32:21 PM] Objects.dll
Run Code Online (Sandbox Code Playgroud)
但它需要看起来像这样:
[File Name] [Creation Time] [Version] [Size]
Objects.dll [6-02-2020 03:32:21 PM] 1.0.0.0 125 kB
Run Code Online (Sandbox Code Playgroud)
我需要对我的 printf() 函数做什么才能获得正确的订单输出?我试过刷新缓冲区并重新排列函数的顺序。是否可以在不为每个函数编写单独的 printf() 语句的情况下以正确的顺序获得输出?
每次打印 printf 语句时,行的长度都被添加到末尾。例如:
cout << printf("%s", "foo") << endl;
cout << printf("%s", "foobar") << endl;
Run Code Online (Sandbox Code Playgroud)
在我的控制台中显示:
foo3
foobar6
Run Code Online (Sandbox Code Playgroud)
一个有趣的错误,有人知道它是什么吗?我使用 MinGW g++ 作为我的编译器。
我有现有的脚本,部分脚本将其发布在下面。我想将结果打印到文件中。结果打印在 Linux 屏幕会话上 我的问题是如何将 def 打印到文件而不是显示在屏幕上
my $def=printf '/opt/bin/run server=%s os="%s" version=%s application=%s',
$server, $os, $version, $application;
print $def."\n" ;
Run Code Online (Sandbox Code Playgroud) 我正在为埃隆·马斯克建造一枚火箭,内存使用对我来说非常重要。
我有文本和指向它的指针pText。堆里很冷。
有时我需要分析字符串及其单词。我不在堆中存储子字符串,而是存储两个指针 start/end 来表示文本的子字符串。但有时我需要打印这些子字符串以进行调试。我怎么做?
我知道要打印字符串我需要两件事
有任何想法吗?
// Text
char *pText = "We've sold the Earch!";
// Substring `sold`
char *pStart = &(pText + 6) // s
char *pEnd = &(pStart + 3) // d
// Print that substring
printf("sold: %s", ???);
Run Code Online (Sandbox Code Playgroud) 我需要帮助解决我心中的这个问题,所以如果有人有类似的问题,这会对我有很大帮助。
signed char c=0x10;
printf("%x", c<<0x4|c>>0x4);
Run Code Online (Sandbox Code Playgroud)
为什么输出是101?
对不起,我的英语不好。
我在 M1 Pro MacBook Pro 中使用 macOS 12.3
我想了解C标准库中printf的源代码。
我可以在 usr/include 目录中找到 stdio.h 文件。
但我在同一目录中找不到 printf.c 或 stdio.c 文件。
而且我认为 Apple Open Source 的 printf.c 太复杂,看起来不像 macOS 中的真实代码,对我没有帮助。Apple 开源 printf.c 代码真的在 macOS 中使用吗?
https://opensource.apple.com/source/xnu/xnu-201/osfmk/kern/printf.c.auto.html
这是我的代码,基本上是我测试过的4台计算机,它们都可以完美地处理非常大的数据大小,例如大小达500mb的文本文件,但是当我在服务器上运行它们时,真正的数据甚至小到6mb的文件似乎在某处溢出并将垃圾写入我的文件末尾.
这是整个功能的来源,因此人们可以有更深入的外观
/** Reads values from tagname between start_time and end_time which are strings in the format
of 01/01/1970-12:00, a null string is treated as 01/01/1970, values are stored in
"tagname".csv */
int ReadValues(char * tagname, char * start_time, char * end_time, char * working_directory, char * new_tag_name)
{
long lRet;
int number_of_samples;
int loop;
IHU_DATA_SAMPLE * pSamples=NULL;
IHU_TIMESTAMP StartTime;
IHU_TIMESTAMP EndTime;
FILE *stream;
char outputFileName[200];
char szQuality[100];
char newTempTagName[100];
int Year;
int Month;
int Day;
int Hour;
int Minute; …Run Code Online (Sandbox Code Playgroud)