Siddhartha Gurjala
Siddhartha Gurjala

Reputation: 41

In C what is the difference between null character and a new line character?

What's the conceptual difference and similarity between NULL 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?

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 whell whereas the same logic given with slight difference 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 are program code 2 and program code 3 not working as expected as that of the code 1?

Upvotes: 4

Views: 8943

Answers (4)

Siddhartha Gurjala
Siddhartha Gurjala

Reputation: 41

In the program code 1, if i place '\0' instead of '\n'. j for loop is not getting delimited.

Here is part of the program code 1 modified:

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");
    }
}

If we press enter key twice the first enter key will be read as a new line character and
and second enter keyed in will be read as null character while reading char arrays.

So the char array should stop reading as soon as consecutive second enter key is keyed in. (I mean inner j for loop should get terminated) But its getting read till 256 keys are keyed in.

Upvotes: 0

Keith Thompson
Keith Thompson

Reputation: 263217

The null character '\0' and the newline character '\n' are two different character values, just as 'x' and 'y' are two different character values.

The null character, whose value is 0, is used to mark the end of a string, which is defined by the C standard as "a contiguous sequence of characters terminated by and including the first null character." For example, the strlen() function, which returns the length of a string, works by scanning through the sequence of characters until it finds the terminating null character.

The newline character, '\n', is used to denote the end of a line in a text file. Strings exist in memory while your program is running, and lines exist in a text file external to your program. You can read the contents of a line (in a text file) into a string (in memory); depending on how you read it, the resulting string may or may not include the terminating '\n'. Null characters do not normally occur in text files.

Note carefully that NULL is (a macro that expands to) a null pointer constant. Other than the fact that both a null pointer and a null character can be expressed as 0, they have very little to do with each other. Please do not use the term NULL to refer to the null character.

One minor thing: in C, a character constant such as 'x', '\0', or '\n' is actually of type int, not of type char. (C++ differs in this.) But they're almost always used to denote values of type char. For example, this:

char c;
...
c = '\0';

will store a null character value in c, the int value is implicitly converted from int to char. In most cases, you don't have to worry about this.

char and int are both integer types, and you can freely convert between them. The reasons for character constants being of type int are historical.

Also, I see you're using old-style (K&R) function definitions. Way back in 1989, the ANSI standard added a new way to define functions using prototypes (you actually use some in your code) -- and there have been two new versions of the C standard since then. Old-style function definitions are obsolescent, and should be avoided. This:

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

is an old-style definition. This:

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

is a definition that uses a prototype, and it's preferred. For one thing, it lets the compiler check that a call passes the correct number and types of arguments.

You'll probably have more questions after this. I strongly suggest you take a look at the comp.lang.c FAQ; it will probably answer most of them.

Upvotes: 11

जलजनक
जलजनक

Reputation: 3071

Program #2 and #3 there are syntactical errors.

'\n' with Hex value 0x0a is often used to format text files o/p on screen just for readability.

'\0' with Hex value 0x00 is string delimiter. Although NULL has numeric value 0x0000 it's of type void*.

Upvotes: 3

Aniket Inge
Aniket Inge

Reputation: 25695

Conceptually both are characters, which means internally they're ascii encoded.

'\0' is integer 0 and '\n' is integer 10.

The programs have errors!

Upvotes: 0

Related Questions