简体   繁体   English

C printf中的奇怪行为

[英]Weird behavior in C printf

I've the following piece of code. 我有以下一段代码。

    char str[MAXS];
    gets(str);
    N = strlen(str);

    for (i = N / 2 - 1; i >= 0; i--) {
        printf("%c", str[i]);
    }

    for (i = N - 1; i > N / 2 - 1; i--) {
        printf("%c", str[i]);
    }
    printf("\n");

For and input string, for example "I ENIL SIHTHSIREBBIG S", it should print simply "THIS LINE IS GIBBERISH". 对于和输入字符串,例如“I ENIL SIHTHSIREBBIG S”,它应该简单地打印“这条线是GIBBERISH”。 But it only prints the content of the second loop, but, if I insert a simple "\\n" in the middle of the two loops, the content, the printf inside the first loop works. 但它只打印第二个循环的内容,但是,如果我在两个循环的中间插入一个简单的“\\ n”,内容,第一个循环内的printf就可以了。 Whats's going on here, here comes the 'working' code (at least, I don't want that '\\n' in the middle') 什么是在这里,这里是'工作'代码(至少,我不希望'\\ n'在中间')

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <memory>
#include <iomanip>
#include <numeric>
#include <functional>
#include <new>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <climits>
#include <cctype>
#include <ctime>

#define REP(i, n) for(int (i) = 0; i < n; i++)
#define FOR(i, a, n) for(int (i) = a; i < n; i++)
#define FORR(i, a, n) for(int (i) = a; i <= n; i++)
#define for_each(q, s) for(typeof(s.begin()) q=s.begin(); q!=s.end(); q++)
#define sz(n) n.size()
#define pb(n) push_back(n)
#define all(n) n.begin(), n.end()

template<typename T> T gcd(T a, T b) {
    if(!b) return a;
    return gcd(b, a % b);
}
template<typename T> T lcm(T a, T b) {
    return a * b / gcd(a, b);
}

template<typename T> void chmin(T& a, T b) { a = (a > b) ? b : a; }
template<typename T> void chmax(T& a, T b) { a = (a < b) ? b : a; }
int in() { int x; scanf("%d", &x); return x; }

using namespace std;

typedef long long Int;
typedef unsigned uint;

const int MAXS = 107;

int N, T;
char str[MAXS];

int main(void) {
    scanf("%d ", &T);

    int i;

    for ( ; T--; ) {
        gets(str);
        N = strlen(str);

        for (i = N / 2 - 1; i >= 0; i--) {
            printf("%c", str[i]);
        }

        for (i = N - 1; i > N / 2 - 1; i--) {
            printf("%c", str[i]);
        }
        printf("\n");
    }
    return 0;
}

There are 73 lines of code in your program in the question, 50 of which are irrelevant to the workings of the program. 程序中有73行代码,其中50行与程序的运行无关。 Please study how to create an SSCCE ( Short, Self-Contained, Correct Example ) so that people don't have to wade through 3 times as much code as necessary. 请研究如何创建一个SSCCE( 简短,自包含,正确的示例 ),以便人们不必趟过所需代码的3倍。

Here's a 22 line program that works: 这是一个有效的22行程序:

#include <cstring>
#include <cstdio>

const int MAXS = 107;

int main(void)
{
    char str[MAXS];

    while (fgets(str, sizeof(str), stdin) != 0)
    {
        int N = strlen(str);
        if (str[N-1] == '\n')
            str[--N] = '\0';
        for (int i = N / 2 - 1; i >= 0; i--)
            printf("%c", str[i]);
        for (int i = N - 1; i > N / 2 - 1; i--)
            printf("%c", str[i]);
        printf("\n");
    }
    return 0;
}

It is C++ in name only; 它只是名义上的C ++; it uses the <cstring> and <cstdio> headers, but if you changed those to <string.h> and <stdio.h> , it would be C code. 它使用<cstring><cstdio>头文件,但如果你将它们更改为<string.h><stdio.h> ,它将是C代码。 Note that it checks for buffer overflow (using fgets() — never, ever use gets() !) and tests that data was read. 请注意,它会检查缓冲区溢出(使用fgets() - 永远不要使用gets() !)并测试数据是否被读取。 It doesn't bother with a count of the lines of input; 它没有考虑输入线的数量; it can detect EOF reliably. 它可以可靠地检测EOF。 It removes the trailing newline that fgets() leaves but gets() does not. 它删除了fgets()离开的尾随换行符,但gets()却没有。 It then prints the first half of the string backwards, and then the second half of the string backwards. 然后它向后打印字符串的前半部分,然后向后打印字符串的后半部分。 I didn't make any substantive changes to the logic of the two printing loops. 我没有对两个打印循环的逻辑做任何实质性的改变。 I did remove the global variables; 我确实删除了全局变量; you should avoid them too, though sometimes global variables are necessary (whereas gets() is just poison and should never be used). 你也应该避免使用它们,尽管有时全局变量是必要的(而gets()只是毒药而且永远不应该被使用)。

Your code seems to work. 你的代码似乎工作。 At least as this : 至少如下:

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

#define MAXS 1024
int main() {
  char str[MAXS];
  int N;
  int i;

  gets(str);
  N = strlen(str);

  for (i = N / 2 - 1; i >= 0; i--) {
    printf("%c", str[i]);
  }

  for (i = N - 1; i > N / 2 - 1; i--) {
    printf("%c", str[i]);
  }
  printf("\n");
}

Check your MAXS define. 检查你的MAXS定义。 No other clue, sorry ! 没有其他线索,对不起!

Edit: Oh! 编辑:哦! you're doing C++, actually... 你正在做C ++,实际上......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM