我何时会在PHP中使用printf而不是echo,为什么?我只是不明白为什么理解这一点很重要.谢谢!
我在学习C的过程中一直在疯狂地研究.我一直在调试一个C程序,我认为这里有一些重大问题.现在我有关键问题.我制作了一个虚拟程序,在一个语句中打印两个字符串,如下所示:
#include<stdio.h>
int main(int argc, char* argv[])
{
char *herp = "Derp";
char *derp = "Herp";
printf("Herp %s Derp %s\n", herp, derp);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这按预期打印出来.我明白了
Herp Derp Derp Herp
Run Code Online (Sandbox Code Playgroud)
所以,我想,让我通过做类似的事情调试我自己的程序.我的程序中的以下行
printf("word is: %s and jumbled word is: %s\n", word, jumbleWord);
Run Code Online (Sandbox Code Playgroud)
应打印出类似的东西
Word is: word and jumbled word is: dowr
Run Code Online (Sandbox Code Playgroud)
但它打印出类似的东西
and jumbled word is: dowr
Run Code Online (Sandbox Code Playgroud)
输出的第一部分在哪里?我需要能够在同一行上打印这两个进行调试.此外,这样的陈述不起作用这一事实告诉我,真的很奇怪的事情正在发生,我秃头撕裂了我的头发.正如我的链接帖子所示,我最终想比较这些字符串值,但是如果printf()不能正常工作,我怎么能这样做呢?
我发布了下面的整个程序,这样你就可以看到一切都在发生.我只是在学习如何使用指针.当我想混淆一个单词并且效果不好时,我最初有两个指针指向相同的内存!所以我修复了它,并用我需要的单词获得了两个独立的内存空间.现在,我只是无法打印它们.鉴于以下代码,这一切都有意义:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_WORD_LENGTH 25
//Define global variables
int numWords;
//Preprocessed Functions
void jumblegame();
void readFile(char *[]);
void …Run Code Online (Sandbox Code Playgroud) 我想在C中编写一个简单的main函数,它接收两行字符串输入并在屏幕上打印它们.这是我写的代码:
int main()
{
char a[100];
char b[100];
printf("Enter the first string:\n");
fgets(a,100,stdin);
printf("Enter the second string:\n");
fgets(b,100,stdin);
printf("\n\n THE FIRST STRING IS: %S\n\n THE SECOND STRING IS:%S",a, b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译时,我收到此错误消息:
gcc -g -Wall PN52.c -o myprog
PN52.c: In function ‘main’:
PN52.c:12:2: warning: format ‘%S’ expects argument of type ‘wchar_t *’, but argument 2 has type ‘char *’ [-Wformat]
PN52.c:12:2: warning: format ‘%S’ expects argument of type ‘wchar_t *’, but argument 3 has type ‘char *’ [-Wformat]
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助
所以我有一个长度为10的字符串.我想要打印4-6 [包括两者]的字母,我想跨越特定长度L的输出,数字位于右侧.
例如
如果我有原始字符串123456789,则以下代码应以后续方式显示.
printf(Original);
printf(from 4-6, over 5 spaces)
Run Code Online (Sandbox Code Playgroud)
输出:
123456789
456
Run Code Online (Sandbox Code Playgroud)
那是456分布在5个空格并且向右缩进.
我怎么用C做?
编辑1:如果我的宽度不恒定怎么办,并且我有一个变量,它决定了宽度.类似地,子串的长度是变量.现在有办法吗?
我能做点什么吗
printf("%%d.%ds",width,precision,expression);
Run Code Online (Sandbox Code Playgroud) #include<stdio.h>
#define POOLNAME_FMT "Hello"
void main() {
printf((POOLNAME_FMT "Cannot allocate %d bytes" POOLNAME_FMT "in pool not enough memory",5));
}
Run Code Online (Sandbox Code Playgroud)
当我使用双括号时,为什么会出现分段错误printf.即printf(( ));?
我只是想知道这个表达式是否安全:
int main (void)
{
char my_tab[256];
memset(my_tab,0x61,sizeof(my_tab));
printf("Is it safe ? : %.256s",my_tab); /* is it safe ? */
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试制作一个简单的x86汇编程序(我使用NASM作为我的汇编程序),它使用ANSI代码将终端文本颜色更改为红色,然后打印一些将用红色前景打印的内容.代码如下:
; This macro is equivalent to printf(message)
%macro print 1
lea rdi, [rel %1]
call _printf
%endmacro
; Example call:
; print prompt
; Where prompt is something like:
; prompt:
; db "Hiya dude! What's your name?", 0
; These are the terminal colors, they are ANSI codes that, when printed, will change the color of the text.
section .data
COLOR_FORE_RED:
db "\033[31m",0 ; ANSI Fore Red code
%define SetColor_FRed print COLOR_FORE_RED
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用以下内容时使用这些宏:
SetColorFRed ; Set text color …Run Code Online (Sandbox Code Playgroud) 考虑有一组数字:
my @array = (
1.788139343e-007, 0.0547055073198, -0.703213036125,
-0.583665880391, -1.41198285298, +0.171879081676,
-0.58966025098, -86.0627173425, -0.84449797709,
3.49876623321e-005, 3.02660429162, -0.256948695361);
Run Code Online (Sandbox Code Playgroud)
我想将小数点对齐在总宽度为m的第n列(可能n = 6和m = 25)
如果使用%fi得到很好的对齐数字,但需要科学记数法的数字正在被破坏.usging %g将点后面的precision参数解释为绝对精度,在小数点后产生不同的小数.由于大多数数字在(-10,10)范围内,我不想采用科学记数法%e
是否有我忽略的标志或格式属性(或组合)?
预期的结果将是:
foreach my $f (@array){
printf("[%+25.12g]$/", $f);
}
[ +1.788139343e-007 ]
[ +0.0547055073198 ]
[ -0.703213036125 ]
[ -0.583665880391 ]
[ -1.41198285298 ]
[ +0.0171879081676 ]
[ -0.58966025098 ]
[ -86.0627173425 ]
[ -0.84449797709 ]
[ +3.49876623321e-005 ]
[ +3.02660429162 ]
[ -0.256948695361 ]
Run Code Online (Sandbox Code Playgroud)
甚至更好
[ +1.7881393430000e-007 ]
[ +0.0547055073198 ] …Run Code Online (Sandbox Code Playgroud) 我试图将给定整数中的所有0替换为5。
为此,我正在使用sprintf将整数转换为字符串并对字符串进行操作,最后将字符串转换回整数
下面是代码:
#include<stdio.h>
int main() {
int num=0, i=0;
char str[10];
printf("Enter the number: ");
scanf("%d",&num);
sprintf(str,"%d",num);
printf("string str:%s",str);
while(str[i]!='\0')
{
if(str[i]==0)
str[i]=5;
i++;
}
sscanf(str,"%d",&num);
printf("\nBefore replacement: %s", str);
printf("\nAfter replacement: %d", num);
}
Run Code Online (Sandbox Code Playgroud)
我输出错误
有人可以识别并纠正这里的问题。谢谢 :)