?
Solved

What does the symbol "!" mean?

Posted on 2003-03-10
10
Medium Priority
?
700 Views
Last Modified: 2007-12-19
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);
}
0
Comment
Question by:mkido
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 

Accepted Solution

by:
posternb earned 200 total points
ID: 8105457
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
 
LVL 6

Expert Comment

by:zebada
ID: 8105477
! means logical not
In C false=0 truth=non-zero
0
 
LVL 6

Expert Comment

by:zebada
ID: 8105481
Ahhh crap - you wouldn't believe it took me 3 minutes to type in those two lines.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:antonsigur
ID: 8107066
hehe, yes, this is always fun... hehe...
0
 

Expert Comment

by:plushey
ID: 8109842
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
 
LVL 6

Expert Comment

by:zebada
ID: 8109963
I've got to agree with plushey on that comment.
0
 
LVL 5

Expert Comment

by:Kocil
ID: 8112083
(X == 0) maybe more readble,
but some lazy compiler will not optimize it to be as fast as !X.
0
 

Expert Comment

by:plushey
ID: 8112491
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
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 8120002
Not big explanation. ! symbol is logical not.
0
 

Author Comment

by:mkido
ID: 8142319
Thank you! Everyone.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question