Whats the conceptual difference and similarity between NULL and a newline character
i.e between '\0' and '\n'
Explain their relevance for both integer and character data type variables and arrays?
For reference here is an example
snippets of a program to read and write a 2d char array
PROGRAM CODE 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");
}
The above code is working good where as the same logic given with slight diff is not giving appropriate output. Here's the code
PROGRAM CODE 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");
}
Here one more instance of same program not giving proper output given below
PROGRAM CODE 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");
}
Why the program code 2 and program code 3 are not working as expected as that of the code 1?