mkido
asked on
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);
}
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
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);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Ahhh crap - you wouldn't believe it took me 3 minutes to type in those two lines.
hehe, yes, this is always fun... hehe...
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.
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.
I've got to agree with plushey on that comment.
(X == 0) maybe more readble,
but some lazy compiler will not optimize it to be as fast as !X.
but some lazy compiler will not optimize it to be as fast as !X.
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.
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.
Not big explanation. ! symbol is logical not.
ASKER
Thank you! Everyone.
In C false=0 truth=non-zero