标签: string-literals

是否可以过滤掉格式化字符串文字中的零值?

格式化字符串文字 (:+) 用于显示数字的符号(+ 或 -)。例如:

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。

有没有一种方法,如果ab的总和为 0,输出应该不返回任何内容(我不确定这意味着什么),或者可能输出一个空字符串?

示例:Gn = c + (ab)

如果a3b也为3,则输出应为Gn = c。不是 Gn=c+0

我尝试应用一些条件语句 wrt 0,以返回空字符串,但格式化字符串文字 (:+) 仅适用于整数。

python string-literals python-3.x

1
推荐指数
1
解决办法
69
查看次数

从组合两个字符串文字的函数返回 const char*

我有一个旧式的接口函数,经过一些重构,我需要返回其原始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)

但我不喜欢这个静态变量。

是否有另一种更好的方法来组合两个编译时已知的字符串文字?

c++ string-literals

1
推荐指数
1
解决办法
119
查看次数

这段代码有什么问题?我总是收到丢失终止 '"' 错误。我是 C 新手,希望使用此 ASCII 来提交 CS50x

我的程序根本不起作用。我收到这个错误。

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)

c string-literals rawstring cs50

1
推荐指数
1
解决办法
151
查看次数

是 char s[] = {"ABC"}; 合法的?

#include <stdio.h>
int main(){
    char s[] = {"hello"};
    printf("%s", s);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这段代码可以编译并成功执行,但是如何将一维数组(char[] s)赋值为二维数组({"hello"})呢?

c arrays declaration string-literals list-initialization

1
推荐指数
1
解决办法
179
查看次数

%d 如何用于字符串中的“”?结果:4210704

#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 是如何组合在一起的。如果您对此有解释,我很乐意回复您)))

c printf integer string-literals conversion-specifier

1
推荐指数
1
解决办法
99
查看次数

C:string-literal - 问题

只是在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 string-literals

0
推荐指数
1
解决办法
623
查看次数

在char*var中分配的内存; 宣言

在C中,声明像这样的char指针

char* p="Hello";
Run Code Online (Sandbox Code Playgroud)

为字符串文字分配一些内存Hello\0.当我这样做之后

p="FTW";
Run Code Online (Sandbox Code Playgroud)

分配给的内存会发生什么Hello\0?地址p是否指向更改?

c string cstring string-literals

0
推荐指数
1
解决办法
240
查看次数

Scala - 将包含空格的Windows文件路径写为字符串文字

我需要在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)

这是最新的错误: 在此输入图像描述

scala escaping path filepath string-literals

0
推荐指数
1
解决办法
2892
查看次数

谁能向我解释为什么这个atoi命令有效以及如何工作

我用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)

c atoi string-literals pointer-arithmetic

0
推荐指数
1
解决办法
489
查看次数

C strcpy()复制字符串文字而没有分段错误

据我所知,字符串文字存储在只读内存中,并在运行时修改它导致分段错误,但我的下面的代码编译没有分段错误.

#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)

c string segmentation-fault string-literals strcpy

0
推荐指数
1
解决办法
93
查看次数