格式化字符串文字 (:+) 用于显示数字的符号(+ 或 -)。例如:
a = 2
b = -5
print(f"{a+b:+}")
output: -3
Run Code Online (Sandbox Code Playgroud)
或者
a = 2
b = 5
print(f"{a+b:+}")
output: +7
Run Code Online (Sandbox Code Playgroud)
如果输出为 0,则表示为 +0。
有没有一种方法,如果a和b的总和为 0,输出应该不返回任何内容(我不确定这意味着什么),或者可能输出一个空字符串?
示例:Gn = c + (ab)
如果a为3,b也为3,则输出应为Gn = c。不是 Gn=c+0
我尝试应用一些条件语句 wrt 0,以返回空字符串,但格式化字符串文字 (:+) 仅适用于整数。
我有一个旧式的接口函数,经过一些重构,我需要返回其原始const char*指针,该指针不应被删除。
该函数如下所示:
const char* func()
{
...
return "Some string literal that contains some value to be moved out: VALUE";
}
Run Code Online (Sandbox Code Playgroud)
我需要将 VALUE 移到外面并在 return 语句中使用它,如下所示(组合 2 个字符串):
constexpr auto MY_VALUE = "VALUE";
const char* func()
{
...
static const std::string msg = "Some string literal that contains some value to be moved out: " + MY_VALUE;
return msg.c_str();
}
Run Code Online (Sandbox Code Playgroud)
但我不喜欢这个静态变量。
是否有另一种更好的方法来组合两个编译时已知的字符串文字?
我的程序根本不起作用。我收到这个错误。
population/ $ make test
test.c:6:9: error: missing terminating '"' character [-Werror,-Winvalid-pp-token]
printf(R"EOF(
^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
2 errors generated.
Run Code Online (Sandbox Code Playgroud)
#include <stdio.h>
#include <cs50.h>
int main(void)
{
printf(R"EOF(
_____ _ _ _ _ _ _ _____ _
/ ____| | | (_) | | | | | | | / ____| (_)
| (___ _ _| |__ _ __ ___ _| |_| |_ ___ __| | | |__ _ _ | | _ _ …Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
int main(){
char s[] = {"hello"};
printf("%s", s);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这段代码可以编译并成功执行,但是如何将一维数组(char[] s)赋值为二维数组({"hello"})呢?
#include <stdio.h>
int main()
{
printf("%c code\n", 'C');
printf("%d", "");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
#include <stdio.h>
int main()
{
printf("%d", 3); /* result 3 */
printf("%d", '3'); /* result 51, because 3 is char (from ASCII table) */
printf("%d", ""); /
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我是 C 语言的初学者。我阅读了 Greg Pery 和 Dean Miller 所著的《C 编程绝对初学者指南》第四版。有 %c %d 和 %f 的示例。我开始对它们进行实验,并注意到我无法解释它是如何制作的。所以我用 %d 尝试了这些例子并理解了前两个是如何工作的。对于最后一个,我知道计算机如何读取它,但我不确定所有 0 和 1 是如何组合在一起的。如果您对此有解释,我很乐意回复您)))
只是在char *ptr = "Hello World"字符串文字或两个"Hello World"字符串文字?
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char array[] = { "Hello World" };
char *ptr = "Hello World";
printf( "%s\n", array );
printf( "%s\n", ptr );
printf( "%c\n", *array );
printf( "%c\n", *ptr );
printf( "%c\n", array[1] );
printf( "%c\n", ptr[1] );
return EXIT_SUCCESS;
}
# Hello World
# Hello World
# H
# H
# e
# e
Run Code Online (Sandbox Code Playgroud) 在C中,声明像这样的char指针
char* p="Hello";
Run Code Online (Sandbox Code Playgroud)
为字符串文字分配一些内存Hello\0.当我这样做之后
p="FTW";
Run Code Online (Sandbox Code Playgroud)
分配给的内存会发生什么Hello\0?地址p是否指向更改?
我需要在Scala中创建一些包含空格的Windows文件路径到字符串文字中.我已经尝试用双引号包装整个路径并用双引号将整个路径包装起来,每个目录名都有一个带单引号的空格.现在它想在两个地方都想要一个"\ Jun"的转义字符,我不知道为什么.
以下是字符串:
val input = "R:\'Unclaimed Property'\'CT State'\2015\Jun\ct_finderlist_2015.pdf"
val output = "R:\'Unclaimed Property'\'CT State'\2015\Jun"
Run Code Online (Sandbox Code Playgroud)
这是最新的错误:

我用atoi命令编写了一个非常基本的c代码,然后执行它.
void
main
(void)
{
int length;
length = atoi("Content: 111");
printf("atoi(\"Content: 111\") = %d\n",length);
length = atoi("Content: 111" + 10);
printf("atoi(\"Content: 111\" + 10) = %d\n",length);
length = atoi("Content: 1" + 6);
printf("atoi(\"Content: 1\" + 6) = %d\n",length);
length = atoi("Content: 111" + 12);
printf("atoi(\"Content: 111\" + 12) = %d\n",length);
length = atoi("Content-aaaaaa: 111" + 20);
printf("atoi(\"Content-aaaaaa: 111\" + 20) = %d\n",length);
printf("\"aaa\"+7 = %s","aaa"+7);
}
Run Code Online (Sandbox Code Playgroud)
输出如下:
atoi("Content: 111") = 0
atoi("Content: 111" + 10) = 111
atoi("Content: 1" …Run Code Online (Sandbox Code Playgroud) 据我所知,字符串文字存储在只读内存中,并在运行时修改它导致分段错误,但我的下面的代码编译没有分段错误.
#include <string.h>
#include <stdio.h>
int main() {
char* scr = "hello";
strcpy(scr,scr);
printf("%s\n",scr);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:你好
同样的事情,如果我试图将源字符串复制到不同的目标字符串文字,它会抛出一个分段错误
#include <string.h>
#include <stdio.h>
int main() {
char* scr = "hello";
char* dst = "hello";
strcpy(dst,scr);
printf("%s\n",dst);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:分段故障(核心转储)
根据K&R的书,strcpy()的实现类似于下面的内容
void strcpy(char *s, char *t)
{
while ((*s = *t) != '\0') {
s++;
t++;
}
}
Run Code Online (Sandbox Code Playgroud)
如果是这样,我应该在两种情况下都有分段错误.
编译细节:
gcc版本7.3.0(Ubuntu 7.3.0-27ubuntu1~18.04)