Note that there are some explanatory texts on larger screens.

plurals
  1. POIn C what is the difference between null character and a new line character?
    primarykey
    data
    text
    <p>What's the conceptual difference and similarity between <strike>NULL</strike> a null character and a newline character i.e between '\0' and '\n' Can you explain their relevance for both integer and character data type variables and arrays?</p> <p>For reference here is an example snippets of a program to read and write a 2d char array</p> <p><em><strong>PROGRAM CODE 1:</em></strong></p> <pre><code>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",&amp;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---&gt;\n\n",nos); name[0][0]='k'; //initialising as non NULL charecter for(i=0;i&lt;nos;i++) // nos=number of students { printf("Give name of student %d\n",i); for(j=0;j&lt;256;j++) { scanf("%c",&amp;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&lt;nos;i++) { for(j=0;j&lt;256&amp;&amp;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"); } </code></pre> <p>The above code is working whell whereas the same logic given with slight difference is not giving appropriate output. Here's the code</p> <p><em><strong>PROGRAM CODE 2:</em></strong></p> <pre><code>#include&lt;stdio.h&gt; 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",&amp;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---&gt;\n\n",nos); name[0][0]='k'; //initialising as non NULL charecter for(i=0;i&lt;nos;i++) // nos=number of students { printf("Give name of student %d\n",i); ***for(j=0;j&lt;256&amp;&amp;name[i][j]!='\0';j++)*** { scanf("%c",&amp;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&lt;nos;i++) { for(j=0;j&lt;256&amp;&amp;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"); } </code></pre> <p>Here one more instance of same program not giving proper output given below</p> <p><em><strong>PROGRAM CODE 3:</em></strong></p> <pre><code>#include&lt;stdio.h&gt; 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",&amp;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---&gt;\n\n",nos); name[0][0]='k'; //initialising as non NULL charecter for(i=0;i&lt;nos;i++) // nos=number of students { printf("Give name of student %d\n",i); ***for(j=0;j&lt;256&amp;&amp;name[i][j]!='\n';j++)*** { scanf("%c",&amp;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&lt;nos;i++) { for(j=0;j&lt;256&amp;&amp;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"); } </code></pre> <p>Why are program code 2 and program code 3 not working as expected as that of the code 1?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload