C Language Syntax Rules | Studytonight (2024)

The C programming syntax specifies the rules for writing the code in the C language. In simple words, these rules inform how to form code statements in a C program:

  • How should the line of code start,

  • how it should end,

  • where to use double quotes,

  • where to use curly brackets,

  • where to use parenthesis, etc.

What is Syntax?

  1. Any language, be it English or Hindi, or Spanish, has a grammar.

  2. The grammar defines the rules for using the language, for example how to form a sentence, what different words mean, etc.

  3. In a normal spoken language or a computer programming language, syntax means how to arrange words, characters, special characters, to make a meaningful statement, or expression, etc.

  4. If you don't follow the rules properly then your code will give error.

What is Syntax error?

  • If someone says there is a syntax error in the program, that means you have not written the program correctly.

  • You might have missed some semicolon or some other general mistake in typing the code for the program.

  • Having a syntax error doesn't mean your code's logic is incorrect, it means you have written it incorrectly.

  • Once the syntax is correct, then only the code is compiled and then run.

The C Tokens

Hence the syntax for C language defines how to use these tokens together while writing the C language code.

Let's take an example,

#include <stdio.h>int main(){ printf("Hello,World"); return 0;}

In the above code, if we take the printf statement:

printf("Hello,World");

Then the tokens in this statement are:

  1. printf,

  2. (,

  3. "Hello,World",

  4. )

  5. and ;

So the C tokens are basically the building blocks of a C program.

The picture below shows token in the entire Hello World Program:

C Language Syntax Rules | Studytonight (1)

Now let's see some important syntax rules, which you must remember always while writing code in the C language.

1. Statement ends with Semicolon (;)

  • A semicolon ; is used to mark the end of a statement and the beginning of another statement in the C language.

  • The absence of a semicolon at the end of any statement will mislead the compiler to think that this statement is not yet finished.

  • And it will add the next consecutive statement after it, which may lead to a compilation(syntax) error.

Here is an example of code with a missing semicolon,

#include <stdio.h>int main(){ printf("Hello,World") return 0;}


error: expected ';' before 'return'
return 0;
^
command terminated with exit code 1

In the above program, we dont' have the semicolon after the printf("...") statement, hence the compiler will think that starting from printf up untill the semicolon after return 0is a single statement and this will lead to compilation error.

2. Adding Comments to Code

  1. Comments are plain simple text in a C program that is not compiled in the compilation process.

  2. We can write comments in a program, to explain various parts of the program.

  3. Although writing comments is not compulsory, but it is recommended to make your program more descriptive, and easier for others to understand.

  4. Adding comments makes the code more readable.

  5. There are two ways in which we can write comments.

    1. Using //: This is used to write a single-line comment.

    2. Using /* */: Anything enclosed within /* and */ , will treated as multi-line comments.

  6. You can use both these two techniques in one program.

Example of comments in C language:

Here is a simple program to show how to use comments:

/* This is my first program. I am very excited!*/#include <stdio.h>int main(){ // Printing Hello World printf("Hello,World"); // printf("Useless piece of code."); return 0;}

As you can see in the above code, we have added comments, and have also, commented a printf() statement, which will not get executed.

3. More Syntax rules for the C Language

This is just the beginning, as we will learn more concepts in the C language, we will learn the syntax for using all of them along with examples.

Everything has a way of writing and use when it comes to writing code, and that way is its syntax.

  • C is a case-sensitive language so all C instructions must be written in lower case letters. main is not the same as MAIN.

  • All C statements must end with a semicolon.

  • Whitespace is used in C to add blank space and tabs.

  • You do not have to worry about the indentation of the code.

  • When we write a function, its body is enclosed in curly braces, like for the main() function. We will learn this in detail when we cover functions.

The picture below shows you the structure of the C program.

C Language Syntax Rules | Studytonight (2)

Frequently Asked Questions (FAQ)

Here are some frequently asked questions related to the C language syntax.

Q1. What do you understand by tokens in C language?

The smallest individual unit in the C program is known as C Token. Tokens are either keywords or identifiers, constants, variables, or any other symbol which has some meaning in C language. The C program can also be called a collection of various tokens.

Q2. What will happen if we forgot to use a semicolon at the end of the statement in C?

If we forgot to put the semicolon at the end of any statement in the C language then it will lead to a syntax error. In that case, an error message is issued by the compiler that a semicolon is expected.

Q3. What is a Compilation Error?

A compilation error, or compile-time error, is the error returned by the compiler if the syntax of the C program is incorrect. When you try to compile a program with some syntax mistake, then the compiler will give an error, which is called a Compile-time error.

Q4. How to add comments in C?

We can add single-line comments using the // at the starting of the comment text, and multi-line comment by enclosing the text within /* and */ in the C program.

Q5. Can a program run without the main() function in C?

In the C language, the main() function defines the starting point of execution. If a C program doesn't have a main() function then no code statement will be executed in that program. In most cases the compiler gives an error, if it cannot find the main() function.

  • ← Prev
  • Next →
C Language Syntax Rules | Studytonight (2024)
Top Articles
Live Report - MASTODON + BIG BUSINESS + KROKODIL - Fabrique, Milano (MI), 10/12/2014 - Metallized.it
The 38 Laws of Powder, 38 Spesh - Qobuz
Brokensilenze Website
Extranet Landing Page Delta
Nerdwallet Chase
Craigslist Apartments For Rent Cheap
NO CLUE: deutsche Übersetzung von NCT 127
Delta Air Lines - Login
Minecraft Jenny Mod Dragon Staff
Sarah Lindstrom Telegram
Google Sites 1V1.Lol
Evo Unblocked
Ups Store Fax Cost
Getwush Com
Unforeseen Guest Ep 3
Sutter Health Candidate Login
Busted Newspaper Williams County
Lecture Tutorials For Introductory Astronomy Answer Guide
R/Skinwalker
Stone Eater Bike Park
Ttw Cut Content
Clemson Sorority Rankings 2022
The Guardian Crossword Answers - solve the daily Crossword
Offsale Roblox Items are Going Limited… What’s Next? | Rolimon's
Mileage To Walmart
Mcallen Craiglist
Best Internists In Ft-Lauderdale
Charm City Kings 123Movies
Chi Trib Weather
Lookwhogotbusted New Braunfels
Cronología De Chelsea Contra Fulham
Knock At The Cabin Showtimes Near Alamo Drafthouse Raleigh
Prisoners Metacritic
Wolf Of Wallstreet 123 Movies
Movies123 Avatar 2
Dimbleby Funeral Home
Coverwood Terriers For Sale
Bakkesmod Preset
Jasminx Fansly
Pick N Pull Near Me [Locator Map + Guide + FAQ]
Transformers Movie Wiki
Avalon Hope Joi
Latest News & Breaking News Coverage | Flipboard
Sayuri Pilkey
Information on Fluorinated Waxes – FasterSkier.com
Mtb Com Online
Effingham Radio News
Warranty Killer Performance Reviews
Sbc Workspace
Computer Repair Arboretum North Carolina
Uncc Class Schedule
Craigslist High Springs Fl
Latest Posts
Article information

Author: Kelle Weber

Last Updated:

Views: 6331

Rating: 4.2 / 5 (73 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Kelle Weber

Birthday: 2000-08-05

Address: 6796 Juan Square, Markfort, MN 58988

Phone: +8215934114615

Job: Hospitality Director

Hobby: tabletop games, Foreign language learning, Leather crafting, Horseback riding, Swimming, Knapping, Handball

Introduction: My name is Kelle Weber, I am a magnificent, enchanting, fair, joyous, light, determined, joyous person who loves writing and wants to share my knowledge and understanding with you.