标签: printf

打印多个输出输出0

我正在练习c编程,但我遇到了一个我似乎无法弄清楚的问题.我有一个printf声明,其中包含两个不同int值的标记.无论第一个int是什么,它都会打印0,但第二个int打印正常.这是代码:

#include <stdio.h>
#include <stdlib.h>

int a, temp;
int toBinary();
int toDecimal();

int main()
{
    char c;
    for(;;)
    {
        scanf("%d",&a);
        scanf(" %c",&c);
        switch(c)
        {
            case 'a' :
                printf("%d converted to binary: %d\n",a,toBinary());
                break;
            case 'b' :
                printf("%d converted to decimal: %d\n",a,toDecimal());
                break;
            case 'c' :
                printf("EXIT\n");
                return 0;
                break;
            default :
                printf("ERROR c value: %c\n",c);
                return 0;
        } 
    }
}
int toBinary()
{
    if (a == 0)
        return 0;
    else
    {
        temp …
Run Code Online (Sandbox Code Playgroud)

c printf

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

在关闭的文件上fprintf不应该失败吗?

请注意以下终端输出:

$ cat fprintf-closed-file.c
#include <stdio.h>
int main()
{
    FILE * ofile = fopen("/tmp/goo", "w");
    int success = fprintf(ofile, "Hello %s\n", "World!");
    printf("Success: %d\n", success);
    fclose(ofile);
    success = fprintf(ofile, "Trying again...");
    printf("Success: %d\n", success);
}
$ clang -o fprintf-closed-file fprintf-closed-file.c
$ ./fprintf-closed-file
Success: 13
Success: 15
$ cat /tmp/goo
Hello World!
Run Code Online (Sandbox Code Playgroud)

C11标准的第7.21.6.1节说:

fprintf函数将输出写入stream指向的流,在指向格式的字符串的控制下,该格式指定后续参数如何转换为输出.

接下来是转换说明符的描述,但结尾为:

返回

fprintf函数返回传输的字符数,如果发生输出或编码错误,则返回负值.

看来假定文件在第二次fprintf调用时关闭,它应该失败,但事实并非如此.我在Kubuntu Trusty 64位上使用Clang 4.0.1和GCC 4.8.4对此进行了测试.

我对标准的理解是否有缺陷,还是应该提交错误?

c printf

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

printf():%n写什么时候?

请考虑以下代码:

#include <stdio.h>

int main() {
  int i = 0;
  printf("hello%n%d\n", &i, i);
}
Run Code Online (Sandbox Code Playgroud)

为什么打印hello0而不打印hello5?

c printf string-formatting

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

如何用awk在最后添加新行

我正在寻找并尝试了很多不同的方法,但是他们并没有真正做到我需要的方法.希望之前没有被问到过百万次.

我在我的bashrc中有这个别名:

alias temp='awk '\''{ printf ("%0.1f",$1/1000); }'\'' < /sys/devices/platform/sunxi-i2c.0/i2c-0/0-0034/temp1_input'
Run Code Online (Sandbox Code Playgroud)

输出是:

measure@remote ~ $ temp
10.6measure@remote ~ $
Run Code Online (Sandbox Code Playgroud)

我想要实现的是这样的输出:

measure@remote ~ $ temp
10.6
measure@remote ~ $
Run Code Online (Sandbox Code Playgroud)

bash shell awk printf newline

3
推荐指数
2
解决办法
4213
查看次数

如何在printf语句中创建条件?

我有功课,我必须创建一个C程序,从最小到最大排序5个数字.我知道如何使用函数和if语句(使用>=和<=)轻松编程.

然而,美中不足的是,我只允许使用printf和scanf,所以我必须要计算所有的>=和<=内部的printf.我不允许使用三元运算符.

我一直在努力.所以我试着只排序3个数字,我仍然无法通过排序最小的数字.它只是保持打印1.

// This printf is just trying to figure out the smallest number from 3 numbers provided but it keeps printing 1.
printf("The ascending order of the numbers are: %d ",
    (
     (num1 <= num2 && num1 <= num3) || // Checking if num1 is the smallest
     (num2 <= num1 && num2 <= num3) || // Checking if num2 is …
Run Code Online (Sandbox Code Playgroud)

c printf conditional if-statement

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

打印出浮子的内六角

所以我认为我可以打印出这样的内部十六进制float:

const auto foo = 13.0F;
const auto bar = reinterpret_cast<const unsigned char*>(&foo);

printf("0x%02hhX%02hhX%02hhX%02hhX\n", bar[0], bar[1], bar[2], bar[3]);
Run Code Online (Sandbox Code Playgroud)

这输出:

0x00005041

但是当我查看调试器时,它报告的十六进制foo是:

0x003EF830

有人可以帮助我理解为什么这不起作用以及我需要做些什么来使它工作?

c++ floating-point printf hex ieee-754

3
推荐指数
2
解决办法
81
查看次数

printf()在错误的位置打印换行符

我有点像C的初学者,所以请原谅任何粗鲁的错误!

我试图用printf()打印一些格式化的值,但它在输出的中间开始换行,即使没有'\n'!问题出在442行的list_topic函数中.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

typedef struct
{
    int t_id;
    char t_name[35];
    char date_creation[30];
    char last_post_date[30];
    int sub_users;
    int num_posts;
    char t_desc[500];
} topic;

typedef struct
{
    int u_type;
    int t_maxreach;
    int u_sub_topic[15];
    char u_name[16];
    char u_pass[16];
} utilizador;

void instalar();
void menu_inicial();
void menu_admin();
void menu_topic();
void menu_user();
void login();
void options();
void topic_gest();
void user_gest();
void stats();
void new_topic();
void alt_topic();
void rmv_topic();
void list_topic();
void new_user(); …
Run Code Online (Sandbox Code Playgroud)

c printf

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

虽然禁用了缓冲,但Printf不会立即打印

我在C中的printf函数有问题.虽然缓冲被禁用,但它不打印输出:

setbuf(stdout, NULL);  
Run Code Online (Sandbox Code Playgroud)

和

setvbuf(stdout, NULL, _IONBF, 0);
Run Code Online (Sandbox Code Playgroud)

我也在使用fflush(stdout);,但它仍然无效.
这是确切的代码:

int setup(){
    //...
    printf("Setup successful\n");
    fflush(stdout);
    return 0;
}
int main(int argc, char *argv[]){
    setbuf(stdout, NULL);
    setvbuf(stdout, NULL, _IONBF, 0);
    setup();
    //...
)
Run Code Online (Sandbox Code Playgroud)

如果信息有帮助; 我在Linux(raspberry Pi)上.
提前致谢!

c linux printf buffer raspberry-pi3

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

R字符串操作:我怎么能优化这个?

TL; DR:我想通过左边的给定字符将列表的每个字符串填充到给定的大小.我想快点.请参阅下面的代码并举例说明

我有很大的字符串向量,包含......很好的东西,但最大(已知)字符数.我希望通过将Zero's留给给定大小来完成thoose字符串(优于char的最大数量)

假设:

c("yop",NA,"1234567","19","12AN","PLOP","5689777")
Run Code Online (Sandbox Code Playgroud)

假设客观大小为10,我希望:

[1] "0000000yop" NA "0001234567" "0000000019" "00000012AN" "000000PLOP" "0005689777"
Run Code Online (Sandbox Code Playgroud)

结果,尽可能快.

我试着写自己的,但它不是很快......你能帮助我加快速度吗?我有数十亿的待办事项.

这是我的实际代码:

library(purrr)
zero_left <- function(field,nb){
  map2_chr(
    map(abs(nb-nchar(field)),~ rep("0",.x)),
    field,
    ~ paste0(c(.x,.y),collapse=""))
}

trial <- c("yop","1234567","19","12AN","PLOP","5689777")
zero_left(trial,10)
Run Code Online (Sandbox Code Playgroud)

这段代码甚至不处理NA案例...但没有它它可行,但太慢了.

printf r

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

数组在末尾显示随机字符

我想用C上的数组测试一下,因为我刚开始学习这门语言.这是我的代码:

#include <stdio.h>
main(){
  int i,t;
  char orig[5];
  for(i=0;i<=4;i++){
    orig[i] = '.';
  }

  printf("%s\n", orig);
}
Run Code Online (Sandbox Code Playgroud)

这是我的输出:

.....?
Run Code Online (Sandbox Code Playgroud)

就是这样.那些神秘人物是什么?我做错了什么?

c arrays printf

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