What does the symbol "!" mean?

Hello Everyone!  

My ex-neighbor wrote the following C-Program to organize and chop a big ASCII text down into a convenient set of files several years ago.   Now, I want to understand the Code completely by myself.   I mostly understand the vocabularies such as *inf, fopen, feof, and strncmp, however, I am not accustomed to use "!" symbol.   Could you tell me the meaning of "!" symbol which appear three times in the code?

   if(!(inf=fopen(argv[1], "rt")){
   while(!feof(inf)){
   if(!strncmp(text, "UI", 2){

Thank you for your help!

Mitsuru (in Los Angeles)

======== THE CODE FOLLOWS =======================
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define BUF_LEN 1024

void main(int argc, char *argv[])
{
     FILE *inf,*outf;
     char text[BUF_LEN], tcopy[BUF_LEN], *t2;

     if (argc<2) {
          printf("Need a filename!\n");
          exit(1);
     }
     if (!(inf=fopen(argv[1],"rt"))) {
          printf("Could not open input file [%s]\n",argv[1]);
          exit(1);
     }
     while (!feof(inf)) {
          fgets(text,BUF_LEN-1,inf);
          if (!strncmp(text,"UI",2)) {
               strncpy(tcopy,text,BUF_LEN-1);
               t2=strtok(tcopy,"-");
               t2=strtok(NULL," ");
               t2[strlen(t2)-1]=0;
               printf("found UI : %s\n",t2);
               t2=strcat(t2,".txt");
               fclose(outf);
               if(!(outf=fopen(t2,"wt"))) {
                    printf("Could not open output file: %s\n",t2);
                    exit(1);
               }
          }
          fputs(text,outf);
     }
     fclose(inf);
     fclose(outf);
}
mkidoAsked:
Who is Participating?
 
posternbConnect With a Mentor Commented:
The "!" is the logical NOT operator, it inverts the truth value of an expression.  Zero (false) becomes one (true) and non-zero (true) becomes zero.  In one example, the statement

while (!feof(inf))

means while inf is NOT at end-of-file.  Similary, fopen() returns a NULL (a.k.a. zero) pointer if it fails, so "!inf" is true when fopen() fails.  Which explains the statement:

if (!(inf=fopen(argv[1],"rt"))) {
     printf("Could not open input file [%s]\n",argv[1]);
     exit(1);
}

Lastly, strncmp() returns zero when the strings it is comparing are exactly equal.  So saying

if ( !strncmp(a,b,n) )

is the same as

if ( strncmp(a,b,n) == 0 )

both of which test when the first n characters of a and b are equal.
0
 
zebadaCommented:
! means logical not
In C false=0 truth=non-zero
0
 
zebadaCommented:
Ahhh crap - you wouldn't believe it took me 3 minutes to type in those two lines.
0
Managing Security Policy in a Changing Environment

The enterprise network environment is evolving rapidly as companies extend their physical data centers to embrace cloud computing and software-defined networking. This new reality means that the challenge of managing the security policy is much more dynamic and complex.

 
antonsigurCommented:
hehe, yes, this is always fun... hehe...
0
 
plusheyCommented:
OK so the ! is logical NOT.

I try to avoid the use of this operator because it is not very readable and can therefore lead to programming errors. My advice would be to remove the ! from the left side of these three expressions and replace with "== 0" on the right hand side.

So

  if(!(inf=fopen(argv[1], "rt")){    would become
  if ((inf = fopen(argv[1], "rt") == 0) {

  while(!feof(inf)){    would become
  while (feof(inf) == 0) {

  if(!strncmp(text, "UI", 2)){    would become
  if (strncmp(text, "UI", 2) == 0) {

Hope you find that easier and clearer to read.
0
 
zebadaCommented:
I've got to agree with plushey on that comment.
0
 
KocilCommented:
(X == 0) maybe more readble,
but some lazy compiler will not optimize it to be as fast as !X.
0
 
plusheyCommented:
Kocil,

I think in the example mkido gave, which is disk intensive, you would be hard pressed to find any performance improvement ! might bring over == 0. Generally if you're not coding some system critical function where speed really matters == 0 is preferable to ! even if you do have a "lazy" compiler.
0
 
rajeev_devinCommented:
Not big explanation. ! symbol is logical not.
0
 
mkidoAuthor Commented:
Thank you! Everyone.
0
All Courses

From novice to tech pro — start learning today.