Brackets used in C and their purpose

I am sorry if this has been asked before, but I am starting in C and I was wondering if anyone could help explain the use of brackets ()

in the following code, I took it from an online tutorial and it works great, but there is one part of it I do not understand.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int age;
    printf("Please enter the age");
    scanf("%d", &age);
    if (age > 18 /*condition */ ) {
        printf("You may enter the club");
        if (age < 21)
       {
        printf("The age is greater than 18 but less than 21");

      

}

else (printf("The age is greater than 18 and greater than 21"));

} else if (age == 18)
{
    printf("The age is equal to 18");
}
else {
    printf("The age is not greater than 18");
    }

return 0;
}

      

From what I've gathered, parentheses are used to contain the conditions of an if statement, and curly braces are used to indicate the operation that follows that condition. Except for the nested else clause in bold, where the else statement is followed by parentheses containing the print command.

Can anyone explain why this is the case?

+3


source to share


7 replies


Parentheses ()

(as well as curly braces {}

) can appear in several different contexts, serving different roles.

Sliding parentheses if

are mainly required in cases where you need to combine multiple statements into a single compound statement to serve as a true or false branch if

. In your case, for example, each branch if (age < 21)

is only one statement, which means that the curly braces in their branches are really unnecessary. You can just write that if

as

if (age < 21)
  printf("The age is greater than 18 but less than 21");
else
  printf("The age is greater than 18 and greater than 21");

      

At the same time, true branch if (age > 18)

contains multiple statements, so you need curly braces.

Meanwhile, the parentheses that surround the condition if

are merely syntactic element of the operator if

- for grammar requires a pair of parentheses around the condition if

, as well as for the required parentheses for

, while

calling functions and other syntactic constructs.

But parentheses can also be used in very different ways: in expressions, where their main purpose is to group operators with their operands, as in (2 + 2) * (5 - 3)

. However, you can use redundant parentheses in expressions, eg. you can write (((2))) + (3)

instead 2 + 3

. Or you can add extra parentheses around the whole expression, eg. (((2 + 3)))

instead of (2 + 3)

etc.

This is exactly what you observe in your example. Your



printf("The age is greater than 18 and greater than 21");

      

is just an expression operator, i.e. an operator consisting of one expression. This expression is a call printf

. If you so desire, you can wrap this call in any number of spare parentheses

(((printf("The age is greater than 18 and greater than 21"))));

      

without changing its meaning.

The combination of these two factors: the fact that you don't need to {}

around it printf

, and the fact that you can put as much ()

around it printf

as you want, creates the illusion that in the case the printf

pair has {}

been replaced by a pair in some way ()

. In fact, these are two completely unrelated actions.

In any case, this is just syntactic curious, not any practical sense. How it happened in the code from the textbook - everyone's guess.

+5


source


The condition else

must be followed by curly braces to indicate which statements are to be executed as part of the sentence else

. If else

there is a single statement in a sentence , then those parentheses can be omitted, although some consider this to be bad coding style. In this case, the curly braces are omitted. In addition, a fallback pair of normal parentheses has been added, which makes the code more confusing.

I would highly recommend replacing those parentheses with curly braces, although the code is technically correct. In addition, the code should be written with a consistent indentation pattern. This makes the logical flow of the program easier:



int main()
{
    int age;
    printf("Please enter the age");
    scanf("%d", &age);

    if (age > 18 /*condition */ ) {
        printf("You may enter the club");
        if (age < 21) {
            printf("The age is greater than 18 but less than 21");
        }
        else {
           printf("The age is greater than 18 and greater than 21");
        }

    }
    else if (age == 18) {
        printf("The age is equal to 18");
    }
    else {
        printf("The age is not greater than 18");
    }

    return 0;
}

      

+2


source


I suspect this is a typo. Any expression, including one in parentheses, can be entered at any time. The parentheses make more sense here and will match the rest of the code if formatted correctly.

0


source


Looks like a typo: there is no legitimate programming reason to use these parentheses. But note that it compiles just fine because it doesn't do anything: (x) is the same as x, so including in printch (...) in more parentheses is acceptable, but doesn't change anything.

0


source


It's legal, but completely useless, courtesy of the flexible C syntax system.

This expression is legal:

int i =  printf("The age is greater than 18 and greater than 21");

      

may be

int i =  (printf("The age is greater than 18 and greater than 21"));

      

and maybe this

(printf("The age is greater than 18 and greater than 21"));

      

It will not be legal if printf is invalidated. Think of it as an expression that was wrapped in oranteza, but not used. Maybe it's a typo that went through because the code is still legal.

0


source


An expression ending with a semicolon ;

becomes an expression.

printf

is a function that returns int

.

Thus, the result printf

is an expression (therefore it printf()

is an expression).

An expression enclosed in curly braces is an expression.

So printf()

, enclosed in curly braces and followed by a semicolon is an expression.

The statement should follow else

. So there is no syntactic problem here.

0


source


You will be familiar with the common use of parentheses in mathematical expressions.

for example 3*(1+1)

will evaluate to 6 because the addition happens before the multiplication and the multiplication uses that value on the right side.

What you might not know is function calls in C are expressions. So while nothing is done with the return value printf()

, it is legal (albeit eccentric to put it in redundant parentheses.

So the following makes sense:

  int count=printf("Hello");

      

Assuming the error count

will initialize to 5 (number of characters printed). But this is just as correct (albeit redundant):

  int count=(printf("Hello"));

      

But the assignment does not affect the syntax, and the following are legal and equivalent.

printf("Hello");
(printf("Hello"));

      

0


source







All Articles