Click here to Skip to main content
15,920,217 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So, I was just wondering, why are brackets so Important in C? If you even miss one, the entire code fails. Why is that?

What I have tried:

...I tried asking a question? Am I suppose to ask a compiler question?
Posted
Updated 22-Mar-21 20:21pm

All the elements of the program are important, not just the brackets. Programming languages are precise. They are different from spoken languages.
 
Share this answer
 
Comments
ModerateHacker; 22-Mar-21 10:34am    
Ok thanks
Brackets are part of the syntax of the language, in the same way that English has many syntax elements. If in English you miss out parts of the sentence it can become misleading or uninteligable:
The is red outside

What does that mean?
It could be:
The sky is red outside

The car is red and waiting outside

The pomegranate is red inside, but not outside

Ef you miss off a question mark, it changes the whole sentence from a question to a command!
He needs a banana?

He needs a banana


So why would you assume that any programming language would be able to understand what you want to do unless you tell it explicitly in language it can understand?
Brackets are important because they control what happens (and an English sentence with a missing brackets is ... well.
 
Share this answer
 
Comments
ModerateHacker; 22-Mar-21 11:14am    
Thanks
Richard MacCutchan 22-Mar-21 11:18am    
Guess who perhaps?
OriginalGriff 22-Mar-21 11:26am    
If he behaves ... :D
In addition to the other answers, brackets (not braces) are important when you want to make it very clear in which order you want things to happen.

For example, according to the rules of maths 1 + 10 * 3 = 31 and not 33 as many will say. But (1 + 10) * 3 can only be interpreted in a way that returns 33.

When using Booleans it can be invaluable - this article explains it better than I can
https://www.socialtalent.com/blog/recruitment/the-beginners-guide-to-boolean-search-terms[^]

Finally, this might be a little pedantic but ... looking at this snippet
C
if(x=1)
{
     //some code
);
many people refer to the "curly brackets" { and } as "braces". If they don't use that term, they will (or should) always add the "curly".

Brackets are usually only the ( and ) around the likes of if-statments, function parameters etc.
 
Share this answer
 
Comments
Richard MacCutchan 22-Mar-21 12:09pm    
Those are not brackets, brackets are [ and ]. They are parentheses, just like on line 4 of your code snippet. :))
CHill60 22-Mar-21 12:39pm    
I was trying to find the "great debate" we all had on this a short while ago. But you are correct and I stand corrected. In parentheses :-)
Richard MacCutchan 22-Mar-21 12:48pm    
It was probably started by some pedantic old git ...
The curly braces are telling the computer (compiler or interpreter) how the statements are grouped.

If a bunch of things are within the curly braces they're treated initially as a single unit. A code block is what that's typically called.

Now suppose you start one of these code blocks and leave off the complimentary closing curly brace. That means that everything that follows is part of the block. EVERYTHING. So you certainly must tell it when to stop grouping things together.

Suppose you never open the block but close it - that leaves the compiler/interpreter in a state of 'stop grouping . . . what?'.

In a way it's like a road trip: you need a beginning and an end. If you are missing one of these you either you don't know where you start from or don't know where you're going. You'd just be traveling aimlessly. You could make the same case for reading a story: just starting and stopping arbitrarily won't do. You need a beginning and an end to the thoughts.

Computer code really hates to travel aimlessly - and you should actually be grateful you get an error if that's a possibility. Otherwise the program either doesn't start or never stops.


 
Share this answer
 
Comments
ModerateHacker; 22-Mar-21 19:31pm    
Thanks
Quote:
So, I was just wondering, why are brackets so Important in C?

In any computer languages, you need a mean of telling where a stucture starts and ends.
M. Kernigan and Ritchie the creators of C choose brackets for this purpose.
The C Programming Language - Wikipedia[^]
 
Share this answer
 
Each & every programming language has its own syntax[^]. Language contains things like identifier, keyword, separator, operator, literal, comment, etc.,

Learn more about C (programming language)[^] & C syntax[^] for more details.

Basically this is similar to how Grammar[^] is important in Language[^].

Keep learning!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900