Purchase Linux CDs / DVDs / Flash Drives at OSDisc.com

Welcome to Our Community

While Linux.org has been around for a while, we recently changed management and had to purge most of the content (including users). If you signed up before April 23rd, 2017 please sign up again. Thanks!

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

Discussion in 'General Computing' started by blackneos940, Jul 13, 2016.

  1. blackneos940

    blackneos940 Guest

    So here's this line of 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......? :>


    (Log in to hide this advertisement)

  2. rstanley

    rstanley Guest

    fopen(argv[1], "r")
    The first set of parens are used to send "argv[1]" and "r" to the fopen function.
    (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:
    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! ;^)
    1 person likes this.
  3. blackneos940

    blackneos940 Guest

    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
  4. rstanley

    rstanley Guest

    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.
    1 person likes this.
  5. blackneos940

    blackneos940 Guest

    Ok, then.....! :3 Thanks, good sir!..... ^^ Have a good night, ok.......? :3
  6. blackneos940

    blackneos940 Guest

    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!..... ^^

Share This Page