Why So Many Parenthesis In C.....? :<

B

blackneos940

Guest
So here's this line of Code:

Code:
 if ((fp = fopen(argv[1], "r")) == NULL)

It's confusing, trying to read all of that..... I mean, I'm getting better at it, but..... What's the purpose of all those Parenthesis.....? :< Thank you for any help, and have a good day, alright......? :>

~blackneos940
 


Code:
fopen(argv[1], "r")
The first set of parens are used to send "argv[1]" and "r" to the fopen function.
Code:
(fp = fopen(argv[1], "r"))
You need to capture the return value from fopen(), the FILE * assigned to "fp".

The reason you need parens around this code is that the '==' operator has a higher precedence than the '=' operator.

You need to capture the pointer BEFORE you compare it to "NULL".

All of that is now wrapped in the parens of the if() statement. Alternatively, you could split this one statement into two:
Code:
fp = fopen(argv[1], "r");  // attempt to open the file first

if(fp == NULL) // Now test if the file failed to open
{
    // Some code to execute if the file failed to open
}

Hope this clears it up for you!

No, I have not forgotten your other question! ;^)
 
if ((fp = fopen(argv[1], "r")) == NULL)
Code:
fopen(argv[1], "r")
The first set of parens are used to send "argv[1]" and "r" to the fopen function.
Code:
(fp = fopen(argv[1], "r"))
You need to capture the return value from fopen(), the FILE * assigned to "fp".

The reason you need parens around this code is that the '==' operator has a higher precedence than the '=' operator.

You need to capture the pointer BEFORE you compare it to "NULL".

All of that is now wrapped in the parens of the if() statement. Alternatively, you could split this one statement into two:
Code:
fp = fopen(argv[1], "r");  // attempt to open the file first

if(fp == NULL) // Now test if the file failed to open
{
    // Some code to execute if the file failed to open
}

Hope this clears it up for you!

No, I have not forgotten your other question! ;^)


I.... THINK I get it..... :) So, it's like PEMDAS then....? :3 I suck at Math, but PEMDAS is one thing I remember about that sort of Math....... :) Honestly, I just thought you received many Emails a day like me, and simply lost it in the mess..... :D Have a good night, ok Mr. Penguin.....? :3
 
Yes, PEMDAS, but as it relates to C.

Your book on C that you have studied has a chapter or part of a chapter on all the C operators. You should go back and review this information.
 
Yes, PEMDAS, but as it relates to C.

Your book on C that you have studied has a chapter or part of a chapter on all the C operators. You should go back and review this information.
Ok, then.....! :3 Thanks, good sir!..... ^^ Have a good night, ok.......? :3
 
Code:
fopen(argv[1], "r")
The first set of parens are used to send "argv[1]" and "r" to the fopen function.
Code:
(fp = fopen(argv[1], "r"))
You need to capture the return value from fopen(), the FILE * assigned to "fp".

The reason you need parens around this code is that the '==' operator has a higher precedence than the '=' operator.

You need to capture the pointer BEFORE you compare it to "NULL".

All of that is now wrapped in the parens of the if() statement. Alternatively, you could split this one statement into two:
Code:
fp = fopen(argv[1], "r");  // attempt to open the file first

if(fp == NULL) // Now test if the file failed to open
{
    // Some code to execute if the file failed to open
}

Hope this clears it up for you!

No, I have not forgotten your other question! ;^)

Oh yes........ :) I also realized that this our respective versions of the Code.... Well, one is longer, and the other is shorter..... :) And THAT tightens the Code up!..... ^^
 

Members online


Top