null character(s) ignored enabled by default

2020-07-09 07:07发布

问题:

I am trying to implement stack with array! Every time i execute the program runs fine but i am getting warning as null character(s) ignored enabled by default

What does this warning mean?.. what am i doing wrong?

My code is:

#include<stdio.h>
#include<stdlib.h>
# define MAX 10
int top=-1;
int arr[MAX];
void push(int item)
{
    if(top==MAX-1)
    {
        printf("OOps stack overflow:\n");
        exit(1);
    }
    top=top+1;
    arr[top]=item;
}//warning
int popStack()
{
    if(top==0)
    {
        printf("Stack already empty:\n");
        exit(1);
    }
    int x=arr[top];
    top=top-1;
    return x;
}
void display()
{
    int i;
    for(i=top;i>=0;i--)
    {
        printf("%d ",arr[i]);
    }
    return;
}
int peek()
{
    if(top==-1)
    {
        printf("\nEmpty stack");
        exit(1);
    }
    return arr[top];
}
int main()
{
     int i,value;
     printf(" \n1. Push to stack");
     printf(" \n2. Pop from Stack");
     printf(" \n3. Display data of Stack");
     printf(" \n4. Display Top");
     printf(" \n5. Quit\n");
     while(1)
     {
          printf(" \nChoose Option: ");
          scanf("%d",&i);
          switch(i)
          {
               case 1:
               {
               int value;
               printf("\nEnter a value to push into Stack: ");
               scanf("%d",&value);
               push(value);
               break;
               }
               case 2:
               {
                 int p=popStack();
                 printf("Element popped out is:%d\n",p);
                 break;
               }
               case 3:
               {
                 printf("The elements are:\n");
                 display();
                 break;
               }
               case 4:
               {
                 int p=peek();
                 printf("The top position is: %d\n",p);
                 break;
               } 
               case 5:
               {        
                 exit(0);
               }
               default:
               {
                printf("\nwrong choice for operation");
               }
         }
    }
    return 0;
}//warning

I am using Dev C++ IDE.

回答1:

Somewhere in your source code file you have character with the byte value 0 (the ASCII NUL character). Which will be invisible in most text editors.

The compiler (gcc) is just telling you that it ignored that character - which really shouldn't be there in your source code .

You could open your file in a hex editor, figure out where that character is and fix it, or delete your source file and copy paste it back from the code you posted here.



回答2:

If you are seeing large numbers of these null character warnings consider the following as a possibility. The problem could be caused by someone creating the source file using an editor that saved the file as 16bit Unicode.

To fix that (on Linux) no hex editor is needed. Simply open up the file in the geany editor (other editors may also support this too) check the File Properties to see the encoding, if it is a UTF/UCS 16 then in the Document menu you can change it to UTF8. It's probably worth removing the BOM if there is one as well.

In this case the error is to be expected because a UCS16 encoding of characters in the ASCII range will make every second byte a null character.



回答3:

As others have said, the warning means there are null bytes in your source code.

This can happen, for example, when you attempt to compile code in Linux that was originally written as a Visual Studio project in Windows, which by default saves as UTF-16. Since g++ expects source files to be in UTF-8, it ends up reading null bytes.

In my case, the easiest solution was to convert the encoding using iconv (Linux)

iconv myfile -f UTF-16 -t UTF-8 > myfile

You can check a file's encoding with file

file myfile


标签: c stack warnings