NULL空字符和换行符在概念上有什么区别和相似之处
即在 '\0' 和 '\n' 之间
您能解释一下它们与整数和字符数据类型变量和数组的相关性吗?
这里有一个例子供引用 读取和写入二维字符数组的程序片段
程序代码 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 那样正常工作?
最佳答案
空字符'\0'
和换行符'\n'
是两个不同的字符值,就像'x'
和 'y'
是两个不同的字符值。
空字符,其值为0,用于标记字符串的结束,C标准将其定义为“由第一个空字符终止并包括第一个空字符的连续字符序列特点。”例如,返回字符串长度的 strlen()
函数通过扫描字符序列直到找到终止空字符来工作。
换行符 '\n'
用于表示文本文件中一行的结尾。 字符串 在程序运行时存在于内存中,行 存在于程序外部的文本文件中。您可以将一行(在文本文件中)的内容读入一个字符串(在内存中);根据您的阅读方式,生成的字符串可能包含也可能不包含终止符 '\n'
。空字符通常不会出现在文本文件中。
请注意 NULL
是(扩展为的宏)空指针常量。除了空指针和空字符都可以表示为 0
之外,它们之间几乎没有关系。请不要使用术语 NULL
来指代空字符。
一件小事:在 C 中,一个字符常量,例如 'x'
、'\0'
或 '\n'
实际上是 int
类型,而不是 char
类型。 (C++ 在这方面有所不同。)但它们几乎总是用来表示 char
类型的值。例如,这个:
char c;
...
c = '\0';
将在c
中存储一个空字符值,int
值从int
隐式转换为char
.在大多数情况下,您不必担心这一点。
char
和int
都是整型,可以自由转换。字符常量为 int
类型的原因是历史性的。
此外,我发现您使用的是旧式 (K&R) 函数定义。早在 1989 年,ANSI 标准就添加了一种使用原型(prototype) 定义函数的新方法(您实际上在代码中使用了一些原型(prototype))——从那时起,C 标准出现了两个新版本。旧式函数定义已经过时,应该避免使用。这:
int func(x, y)
int x;
char *y;
{
/* ... */
}
是旧式定义。这:
int func(int x, char *y)
{
/* ... */
}
是一个使用原型(prototype)的定义,它是首选。一方面,它让编译器检查调用是否传递了正确数量和类型的参数。
在此之后您可能会有更多问题。我强烈建议你看看 comp.lang.c FAQ ;它可能会回答大部分问题。
关于c - 在 C 中,空字符和换行符有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12876166/