简体   繁体   English

printf() 函数不起作用

[英]printf() function not working

i have created ac program to print all possible strings of 8 characters.我创建了 ac 程序来打印所有可能的 8 个字符的字符串。 But the printf() function inside the program does not work at all.但是程序内部的 printf() 函数根本不起作用。 Here's the program这是程序

#include <stdio.h>
#include <string.h>
#include <conio.h>
void ini(void);
void check(void);
void add(void);
static char str[9]; //permuting string
static char test[9]; //test string
static short int c = 1;
void print(char*);
void print(char a[]) {
    short int i,ln = (int)(strlen(str)-1);
    for(i = 0; i <= ln; i++)
    printf("%c",a[i]);
    printf("n");
}
void ini() {
    //initialzing the strings.
    short int i = 0;
    for(i = 0; i < 9; i++){
        str[i] = 'A';
        test[i]= 'z';
    }
    puts("ini.....");
    //initializing done
}
void check() {
    //check if the strings 'str' and 'test' are equal
    c = strcmp(str, test);
    puts("checking..........");
}
void add() {
    //this is the heart of the program
    short int i = 0;
    for( i = 7; i > 0; i--)
    {
        if((int)str[i] >= (int)'z') {
            str[i] = 'A';
            str[i-1]++;
        }
        else if(str[i] < 'z'){
            str[i]++;
            break;
        }
    }
    puts("adding.......");
}
int main() {
    //now we execute the functions above
    puts("in main.....");
    int i = 0;
    while( c != 0 ) //execute the loop while the permuting string 'str' is not equal to the final string 'test'
    {
        puts("inside while.......");
        for(i = 65; i <= 122; i++) { //loop to make the last character of 'str' permute from 'a' to'Z'
            str[7] = (char)i;
            puts("inside for");
            print(str); //print the whole string to the screen
        }
        add(); //change the next char
        check(); //check to see if 'str' has reached it's final point.
    }
    return 1;
    getch();
}

and here's the result.............这是结果…………

在此处输入图片说明

The program enters the for loop in main() but it does not execute the print() function.程序在 main() 中进入 for 循环,但不执行 print() 函数。 I have tried printf() but that shows the same result.我试过 printf() 但结果是一样的。 What am I doing wrong?我究竟做错了什么?

You decleared,你声明,

static char str[9]; 

So, all elements in str is NULL ( \\0 ) now.所以, str所有元素现在都是NULL ( \\0 )。 Then,然后,

ln = (int)(strlen(str)-1);

Here, strlen counts characters until a \\0 found.在这里, strlen对字符进行计数,直到找到\\0为止。 So, as the first character in str is \\0 , ln will be -1 .因此,由于str的第一个字符是\\0 ,因此ln将为-1 In the for loop ,for loop

for(i = 0; i <= ln; i++)

i <= ln condition will fail. i <= ln条件将失败。

You may try, in the main function,你可以试试,在main函数中,

str[0] = (char) i; 

or, call ini() before calling print(str);或者,在调用print(str);之前调用ini() print(str);

But careful, your while loop will never break!但是小心,你的while loop永远不会中断! As c 's never gonna be 0 .因为c永远不会是0
Because your code does not changing the 9th character from A to z so strcmp(str, test) is not going to return 0 .因为您的代码不会将第 9 个字符从A更改为z所以 strcmp(str, test) 不会return 0

In your case, str is a global static array, all elements are initialized to 0.在您的情况下, str是一个全局静态数组,所有元素都初始化为 0。

Next, in your code, you're only setting the index 7 and trying to pass the array to print() function.接下来,在您的代码中,您只设置索引 7 并尝试将数组传递给print()函数。 But the fact is, the very first element is a null (NOte: not a NULL , it's null which is 0 or '\\0' ).但事实是,第一个元素是null (注意:不是NULL ,它是null ,即0'\\0' )。 So, the call to strlen() inside print() will return a 0 .因此,在print()strlen()的调用将返回0 So, the immediate printf() will print nothing.因此,立即printf()将不打印任何内容。

That said, I think, the next printf() you meant to write as printf("\\n");也就是说,我认为,您打算将下一个printf()写为printf("\\n");

TL;DR Solution: You're missing a call to ini() in your main() at the start, please add that. TL;DR 解决方案:开始时您在main()中缺少对ini()的调用,请添加。

It looks like I did not call the ini() function from the main().看起来我没有从 main() 调用 ini() 函数。 Calling it will initialize the strings so that the null byte is moved to the end.调用它将初始化字符串,以便将空字节移到末尾。 After that the program works just fine.之后,程序运行得很好。 So question closed.所以问题结束了。

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

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