我需要在Perl中将数字从十进制转换为二进制,其中我的约束是二进制数宽度由变量设置:
for (my $i = 0; $i<32; $i++)
{
sprintf("%b",$i) # This will give me a binary number whose width is not fixed
sprintf("%5b",$i) # This will give me binary number of width 5
# Here is what I need:
sprintf (%b"$MY_GENERIC_WIDTH"b, $i)
}
Run Code Online (Sandbox Code Playgroud)
我可以在我的打印语句中使用解决方法,但如果我可以执行上述操作,代码将更加清晰.
如果我有以下代码,那么句子在哪里sprintf存储?
它是存储在提供的100个字节malloc中还是存储在其他地方,store指针现在指向新位置?
char *store = malloc(100);
int num = 77;
sprintf(store, "the number was: %d", num);
Run Code Online (Sandbox Code Playgroud) 如此问题中所述:LLVM和GCC,不同的输出相同代码,LLVM和GCC导致相同代码的输出不同.
#include <stdio.h>
#define MAX(a,b) ( (a) > (b) ? (a) : (b) )
int increment() {
static int i = 42;
i += 5;
printf("increment returns %d\n",i);
return i;
}
int main( int argc, char ** argv ) {
int x = 50;
printf("max of %d and %d is %d\n", x,increment(),MAX(x, increment()));
printf("max of %d and %d is %d\n", x,increment(),MAX(x, increment()));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
LLVM输出给出:
increment returns 47
increment returns 52
increment returns 57
max of 50 …Run Code Online (Sandbox Code Playgroud) 如果我有一个这样的类型:
type myType ...
func (m myType) String() string { ... }
Run Code Online (Sandbox Code Playgroud)
如何fmt使用默认表示(即,而不是String()调用)打印(使用各种函数)此类型?我想做的是这样的事情:
func (m myType) String() string {
// some arbitrary property
if myType.isValid() {
// format properly
} else {
// will recurse infinitely; would like default
// representation instead
return fmt.Sprintf("invalid myType: %v", m)
}
}
Run Code Online (Sandbox Code Playgroud) 我一直认为多次调用putc比puts或printf更快.要打印"hello",例如,在一个真实的程序中,我总是使用puts或printf,但现在我正在编写一个生成C代码的程序,所以我想知道是否生成代码putchar('h'); putchar('e') ...因为我首先认为它应该更多快点.但我进行了一项测试,结果非常有趣.编译器是GCC.
#include <stdio.h>
#include <time.h>
int main() {
time_t timer;
FILE *f;
int i;
f = fopen("out.txt", "w");
#define START_TIMER(TIMER) TIMER = clock()
#define ELAPSED_TIME(TIMER)\
(double)(clock() - TIMER) / (double)CLOCKS_PER_SEC
enum { NUM_ITERS = 9999999 };
START_TIMER(timer);
for (i = 0; i < NUM_ITERS; i++) {
putc('h', f);
putc('e', f);
putc('l', f);
putc('l', f);
putc('o', f);
putc('\n', f);
}
printf("%.3f\n", ELAPSED_TIME(timer));
START_TIMER(timer);
for (i = 0; i < NUM_ITERS; i++) {
fputs("hello", f);
}
printf("%.3f\n", ELAPSED_TIME(timer)); …Run Code Online (Sandbox Code Playgroud) 我有一个在我的MATLAB脚本中调用的mex文件.mex函数可能需要一段时间才能运行,所以为了防止我的代码"在没有任何输出的情况下停在那里",我printf在mex文件中放了许多语句来输出一些有关正在处理的数据的运行信息.
但是当我调用mex函数时,它没有printf任何东西并且在int运行期间保持不变.最后,在完成工作后,它将printf获得我想要的所有信息 - 不是在运行时,而是在完成之后.这不是我想要的.
所以我想知道如何让它不仅printf是我想要的,而且也是printf我想要的时候.
我有一个void函数,它从全局变量中获取字符串并打印它们.
问题是即使字符串是empy,void函数也会打印一条消息/做东西.因此,如果全局变量为empt,则只打印"message is:"我只想在字符串中包含字符时执行某些操作.
我想要做的是检查字符串是否为空,然后打印一条消息.
// global variable
char msg[30];
scanf("%s", &msg);
void timer(void) {
//i want to only print if there is a message
printf("message: %s", msg);
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助
我是c的初学者.今天我遇到了一个问题.如果我们提供以下输入,按照书:
Enter names, prices and no. of pages of 3 books
A 100.00 354
C 256.50 682
F 233.70 512
Run Code Online (Sandbox Code Playgroud)
输出将如下所示
And this is what you entered
A 100.000000 354
C 256.500000 682
F 233.700000 512
Run Code Online (Sandbox Code Playgroud)
在运行时,它突然终止.
代码如下:
#include<stdio.h>
#include <ctype.h>
main( )
{
struct book
{
char name ;
float price ;
int pages ;
} ;
struct book b1, b2, b3 ;
printf ( "\nEnter names, prices & no. of pages of 3 books\n" ) ;
scanf ( …Run Code Online (Sandbox Code Playgroud) printf("%.2f",..);
Run Code Online (Sandbox Code Playgroud)
我想控制上面例子中的数字2,放一个变量而不是数字2.
因此,如果
int var=5;
Run Code Online (Sandbox Code Playgroud)
printf的参数将是"%.5f".
可能吗?谢谢.
打印十六进制值(%x)和地址(%p)时,格式略有不同.0x在十六进制值的情况下,打印值不会以:
int main()
{
int x = 0x1234;
printf("Value of x: %x\n", x);
printf("Address of x: %p\n", (void*)&x);
}
Run Code Online (Sandbox Code Playgroud)
收益率(gcc):
Value of x: 1234
Address of x: 0xffb0fbfc
Run Code Online (Sandbox Code Playgroud)
为什么0x在地址的情况下强迫你呢?
我想它归结为标准.
0x如果没有我想要的话,打印地址的正确方法是什么?这%p不仅是%x一个附加的0x权利吗?