标签: printf

Printf在printf中插入字符串值的附加换行符

printf("It is currently %s's turn.\n", current->name);
Run Code Online (Sandbox Code Playgroud)

我想知道为什么这会在%s之后打印出额外的换行符.我知道C中的字符串总是以\ 0结尾.如何在没有它的情况下打印出来?

c printf

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

c - printf("%s")打印额外的@

我正确地知道cs50编程教程.在这里,我应该破解DES加密的字符串.

首先,我专注于创建一个64位大阵列,其中包含盐中使用的所有可能的字符.

在下一步中,我将其抛入两个for循环中,以打印出这两个for循环的所有可能组合.这就是我现在所处的位置.

出于调试原因,我只是打印出来printf("%s",salt).盐被定义为char salt[2].但由于某种原因,它总是打印出来xx@(xx每次按预期更改,我不知道@它来自哪里).

首先,我认为它可能会出于一些奇怪的原因而超出阵列并@从随机存储器中获取.这就是我将它从我的在线IDE复制到我的本地XCode的原因.仍然是相同的@符号.而现在我对它的来源感到困惑@.

链接到我的代码.

#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define _XOPEN_SOURCE       // DES - implementation
#include <unistd.h>

// shorthands
#define cypher argv[1]

#define ascii_dot 46
#define ascii_slash 47
#define ascii_zero 48
#define ascii_nine 57
#define salt_size 64

#define majA 65
#define majZ 90

#define minA 97
#define minZ 122

int main(int argc, string argv[]) {
    // Checking input
    if …
Run Code Online (Sandbox Code Playgroud)

c printf cs50

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

C编程printf("%d",50.2);

#include <stdio.h>

int main(void) {
    printf("%d\n", 50.2);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

〜当我执行它时.

在此输入图像描述

50.2的二进制是(0100 0010 0100 1000 1100 1100 1100 1101).

所以,我预计1,112,067,277,但是,值不是,也不是固定的.

每次都会改变这些值,为什么?

c floating-point int printf

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

在c中打印出字符

我希望能够在c中打印出charcter的值,如下所示:

fprintf("%c", alphabet[val]);
Run Code Online (Sandbox Code Playgroud)

,其中字母表被初始化为

for(i = 0; i < 26; i++){
    alphabet[i] = i + 65;
}
Run Code Online (Sandbox Code Playgroud)

但是,此行会出现以下错误:

encode.c: In function ‘main’:
encode.c:76:13: warning: passing argument 1 of ‘fprintf’ from incompatible pointer type [-Wincompatible-pointer-types]
     fprintf("%c", alphabet[val]);
             ^
In file included from encode.c:1:0:
/usr/include/stdio.h:356:12: note: expected ‘FILE * restrict {aka struct _IO_FILE * restrict}’ but argument is of type ‘char *’
 extern int fprintf (FILE *__restrict __stream,
            ^
encode.c:76:19: warning: passing argument 2 of ‘fprintf’ makes pointer from integer without a …
Run Code Online (Sandbox Code Playgroud)

c arrays printf char

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

为什么scanf需要两个数字?

我希望程序获取用户的身高和体重,然后将其输入到bmi公式中,然后输出结果.

高度问题工作正常,但是当您在重量问题中输入一个数字并按下回车键时,它只会换行.输入另一个数字后,它会将其设置为bmi并打印出来.

#include<stdio.h>

int main()
{
int h, w, bmi;

printf("What is your height (in inches)? ");
scanf("%d", &h);

printf("What is your weight? ");
scanf("%d", &w);

bmi = (w/(h*h))*703;
scanf("%d", &bmi);

printf("\t\tBMI\n");
printf("US: %d\n", bmi);
}
Run Code Online (Sandbox Code Playgroud)

c printf scanf stdio

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

C - printf显示意外输出

我对编程很陌生,想编写一个程序来读取5个数字并添加它们.这就是它的样子.

#include <stdio.h>

int main(int argc, char *argv[])
{
    int a,b,c,d,e,sum;

    printf("Enter the 5 numbers\n");
    scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);

    sum=a+b+c+d+e;
    printf("Sum of entered integers is %d\n,sum");

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

到目前为止我还没到达过循环.那么,为什么我的程序没有给出正确的结果呢?它没有向我显示编译错误.但是当我输入数字1,2,3,4,5时,它给出了一些乱码的结果,如2752264!

c printf scanf

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

如何在C中的main函数中使用包含scanf()的函数

标题描述了我正在尝试做的事情,但我收到的错误消息是我从未声明过base1.我实际上知道这一点,但我不确定如何真正解决问题.

int getBase1(void);
int setBase1(double);

int main(void){
    getBase1();
    setBase1(base1);
}

int getBase1(void){
    printf("Please enter the length of a base: ");
    return;
}

int setBase1(double base1){
    scanf("%lf", &base1);
}
Run Code Online (Sandbox Code Playgroud)

c printf scanf getter-setter

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

我的C程序在新行打印-39,我无法弄清楚原因

我一直在玩,并试图为我的大学课程试验C,我找到了我的程序所做的事情,即使我没有告诉它!

我的完整代码:

#include <stdio.h>

int main(void) {
  int c;
  while ((c = getchar()) != EOF) {
    printf("%d\n", (c - '1'));
  }
 return 0;
}
Run Code Online (Sandbox Code Playgroud)

它的输出看起来像这样:

7
6
-39
Run Code Online (Sandbox Code Playgroud)

现在,谁能告诉我为什么-39正在打印?

c printf program-entry-point stdio

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

printf C出错

所以我在C中有这个代码,应该打印下面显示的地图.

printf ("(1)--------------------------------------------4T------5I        6P------7T-------------------------------------------(2)\n");
printf ("[%i]                                           [%i]     [%i]       [%i]     [%i]                                           [%i]\n", bs[1].leader, bs[4].leader, bs[5].leader, bs[6].leader, bs[7].leader, bs[2].leader);
printf (" \                                             |        \       /        |                                             / \n");
printf ("  \                                            |         \     /         |                                            /  \n");
printf ("   \                                           8T         \ 9T/         10T                                          /   \n");
printf ("    \                                         [ ]----------[ ]----------[ ]                                         /    \n");
printf ("     \                                         |            |            |                                         /     \n");
printf ("      \                                        |            |            |                                        /      \n");
printf ("      11P                                     12I          13I          14I                                     15P      \n");
printf ("      [ …
Run Code Online (Sandbox Code Playgroud)

c printf

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

使用sprintf和%g将double转换为string的意外结果

当我使用MBCS和msvcr120.dll(12.0.40660.0)时,使用%g和sprintf将double转换为字符串时会出现意外结果.%g的文档说默认精度为6.为什么我看到下面的结果?

{
    double d = 1234567.00;
    char buf[100];

    sprintf_s(buf, sizeof(buf), "%g", d);

    //result is 1.23457e+006
}
Run Code Online (Sandbox Code Playgroud)

为什么结果1.23457e+006而不是1.23456e+006?截断是否发生在6位数之后?

c++ printf

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

标签 统计

printf ×10

c ×9

scanf ×3

stdio ×2

arrays ×1

c++ ×1

char ×1

cs50 ×1

floating-point ×1

getter-setter ×1

int ×1

program-entry-point ×1