在C中,空字符和新行字符之间有什么区别?

时间:2012-10-13 18:20:28

标签: c

NULL 空字符和换行符之间的概念差异和相似性是什么? 即'\ 0'和'\ n'之间 你能解释它们对整数和字符数据类型变量和数组的相关性吗?

这里有一个例子供参考 用于读取和写入2d字符数组的程序片段

计划代码1:

int main()
{
    char sort(),stuname(),swap(),(*p)(),(*q)();
    int n;
    p=stuname;
    q=swap;
    printf("Let the number of students in the class be \n");
    scanf("%d",&n);
    fflush(stdin);
    sort(p,q,n);
    return 0;
}

char sort(p1,q1,n1)
char (*p1)(),(*q1)();
int n1;
{
    (*p1)(n1);
    (*q1)();
}

char stuname(int nos)  // number of students
{
    char name[nos][256];
    int i,j;
    printf("Reading names of %d  students started--->\n\n",nos);
    name[0][0]='k'; //initialising as non NULL charecter
    for(i=0;i<nos;i++)  // nos=number of students
    {
        printf("Give name of student %d\n",i);
        for(j=0;j<256;j++)
        {
            scanf("%c",&name[i][j]);
            if(name[i][j]=='\n')
            {
                name[i][j]='\0';
                j=257;
            }
        }
    }
    printf("\n\nWriting student names:\n\n");
    for(i=0;i<nos;i++)
    {
        for(j=0;j<256&&name[i][j]!='\0';j++)
        {
            printf("%c",name[i][j]);
        }
        printf("\n");
    }
}

char swap()
{
    printf("Will swap shortly after getting clarity on scanf and %c");
}

上面的代码正在运行,而略有差异的相同逻辑没有提供适当的输出。这是代码

计划代码2:

#include<stdio.h>
int main()
{
    char sort(),stuname(),swap(),(*p)(),(*q)();
    int n;
    p=stuname;
    q=swap;
    printf("Let the number of students in the class be \n");
    scanf("%d",&n);
    fflush(stdin);
    sort(p,q,n);
    return 0;
}

char sort(p1,q1,n1)
char (*p1)(),(*q1)();
int n1;
{
    (*p1)(n1);
    (*q1)();
}

char stuname(int nos)  // number of students
{
    char name[nos][256];
    int i,j;
    printf("Reading names of %d  students started--->\n\n",nos);
    name[0][0]='k'; //initialising as non NULL charecter
    for(i=0;i<nos;i++)  // nos=number of students
    {
        printf("Give name of student %d\n",i);
        ***for(j=0;j<256&&name[i][j]!='\0';j++)***
        {
            scanf("%c",&name[i][j]);

            /*if(name[i][j]=='\n')
            {
                name[i][j]='\0';
                j=257;
            }*/

        }
    }
    printf("\n\nWriting student names:\n\n");
    for(i=0;i<nos;i++)
    {
        for(j=0;j<256&&name[i][j]!='\0';j++)
        {
            printf("%c",name[i][j]);
        }
        printf("\n");
    }
}

char swap()
{
    printf("Will swap shortly after getting clarity on scanf and %c");
}

这里还有一个相同程序的实例没有给出下面给出的正确输出

计划代码3:

#include<stdio.h>
int main()
{
    char sort(),stuname(),swap(),(*p)(),(*q)();
    int n;
    p=stuname;
    q=swap;
    printf("Let the number of students in the class be \n");
    scanf("%d",&n);
    fflush(stdin);
    sort(p,q,n);
    return 0;
}

char sort(p1,q1,n1)
char (*p1)(),(*q1)();
int n1;
{
    (*p1)(n1);
    (*q1)();
}

char stuname(int nos)  // number of students
{
    char name[nos][256];
    int i,j;
    printf("Reading names of %d  students started--->\n\n",nos);
    name[0][0]='k'; //initialising as non NULL charecter
    for(i=0;i<nos;i++)  // nos=number of students
    {
        printf("Give name of student %d\n",i);
        ***for(j=0;j<256&&name[i][j]!='\n';j++)***
        {
            scanf("%c",&name[i][j]);
            /*if(name[i][j]=='\n')
            {
                name[i][j]='\0';
                j=257;
            }*/
        }
        name[i][i]='\0';
    }
    printf("\n\nWriting student names:\n\n");
    for(i=0;i<nos;i++)
    {
        for(j=0;j<256&&name[i][j]!='\0';j++)
        {
            printf("%c",name[i][j]);
        }
        printf("\n");
    }
}

char swap()
{
    printf("Will swap shortly after getting clarity on scanf and %c");
}

为什么程序代码2和程序代码3的运行不如代码1的那样?

4 个答案:

答案 0 :(得分:8)

空字符'\0'和换行符'\n'是两个不同的字符值,就像'x''y'是两个不同的字符值一样。

空值字符,其值为0,用于标记字符串的结尾,该字符串由C标准定义为“由第一个空值终止并包含第一个空值的连续字符序列字符。”例如,返回字符串长度的strlen()函数通过扫描字符序列来工作,直到找到终止空字符。

换行符'\n'用于表示文本文件中行的结尾。程序运行时,字符串存在于内存中,存在于程序外部的文本文件中。您可以将一行(在文本文件中)的内容读入一个字符串(在内存中);根据您的阅读方式,结果字符串可能包含也可能不包含终止'\n'。文本文件中通常不会出现空字符。

请注意NULL是(扩展为的宏)空指针常量。除了空指针和空字符都可以表示为0这一事实之外,它们彼此之间几乎没有关系。请不要使用术语NULL来引用空字符。

一件小事:在C中,'x''\0''\n'等字符常量实际上是int类型,而不是{{1}类型}。 (C ++与此不同。)但它们几乎总是用于表示char类型的值。例如,这个:

char

会在char c; ... c = '\0'; 中存储空字符值,c值会从int隐式转换为int。在大多数情况下,您不必担心这一点。

charchar都是整数类型,您可以在它们之间自由转换。字符常量为int类型的原因是历史的。

另外,我看到你正在使用旧式(K&amp; R)函数定义。早在1989年,ANSI标准就添加了一种使用原型来定义函数的新方法(实际上你在代码中使用了一些) - 从那以后就有了两个新版本的C标准。旧式函数定义已过时,应避免使用。这样:

int

是一种旧式定义。这样:

int func(x, y)
int x;
char *y;
{
    /* ... */
}

是使用原型的定义,它是首选。首先,它允许编译器检查调用是否传递了正确数量和类型的参数。

此后你可能会有更多问题。我强烈建议你看一下comp.lang.c FAQ;它可能会回答大部分问题。

答案 1 :(得分:3)

程序#2和#3存在语法错误。

为了便于阅读,带有十六进制值'\n'

0x0a通常用于格式化屏幕上的文本文件o / p。

带有十六进制值0x00

'\ 0'是字符串分隔符。虽然NULL的数值为0x0000,但它的类型为void*

答案 2 :(得分:0)

从概念上讲,两者都是字符,这意味着它们在内部被编码为ascii。

'\ 0'是整数0 并且'\ n'是整数10。

程序有错误!

答案 3 :(得分:0)

在程序代码1中,如果我放置'\ 0'而不是'\ n'。 j for循环没有被分隔。

以下是修改后的程序代码1的一部分:

char stuname(int nos)  //nos: number students already read
{
    char name[nos][256];
    int i,j;
    printf("Reading names of %d  students started--->\n\n",nos);
    for(i=0;i<nos;i++)  
    {
        printf("Give name of student %d\n",i);
        ***for(j=0;j<256;j++)***
        {
            scanf("%c",&name[i][j]);

            if(name[i][j]=='\0')    // modified from if(name[i][j]=='\n')
            {

                j=257;
            }

        }
    }
    printf("\n\nWriting student names:\n\n");
    for(i=0;i<nos;i++)
    {
        for(j=0;j<256&&name[i][j]!='\0';j++)
        {
            printf("%c",name[i][j]);
        }
        printf("\n");
    }
}

如果我们按两次输入键,则第一个输入键将被读作新行字符和
在读取字符数组时,第二次输入键入将被读为空字符。

因此,只要连接了第二个输入键,char数组就应该停止读取。 (我的意思是内部j for循环应该终止) 但它被读取到256键是关键。