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
Ice Dodo Unblocked 76
Volvo P1800 de collection à vendre
Goodbye Horses: The Many Lives of Q Lazzarus
Sandrail Options and Accessories
Plaza Nails Clifton
Prosper TX Visitors Guide - Dallas Fort Worth Guide
Dr Doe's Chemistry Quiz Answer Key
The Idol - watch tv show streaming online
Phenix Food Locker Weekly Ad
Bbc 5Live Schedule
Morgan Wallen Pnc Park Seating Chart
Globe Position Fault Litter Robot
Ap Chem Unit 8 Progress Check Mcq
Persona 4 Golden Taotie Fusion Calculator
Snow Rider 3D Unblocked Wtf
Lake Nockamixon Fishing Report
50 Shades Darker Movie 123Movies
Gdp E124
Arre St Wv Srj
Vintage Stock Edmond Ok
Kountry Pumpkin 29
Our History
Egizi Funeral Home Turnersville Nj
Thick Ebony Trans
How to Make Ghee - How We Flourish
California Online Traffic School
Violent Night Showtimes Near Amc Dine-In Menlo Park 12
Walgreens On Bingle And Long Point
What Sells at Flea Markets: 20 Profitable Items
Sinfuldeed Leaked
Craigs List Jax Fl
Korg Forums :: View topic
Nacogdoches, Texas: Step Back in Time in Texas' Oldest Town
Bratislava | Location, Map, History, Culture, & Facts
Weekly Math Review Q4 3
Diana Lolalytics
AP Microeconomics Score Calculator for 2023
Domino's Delivery Pizza
Buhsd Studentvue
3302577704
Hometown Pizza Sheridan Menu
Thelemagick Library - The New Comment to Liber AL vel Legis
Сталь aisi 310s российский аналог
Great Clips Virginia Center Commons
How To Get To Ultra Space Pixelmon
Rubmaps H
Osrs Vorkath Combat Achievements
Bones And All Showtimes Near Emagine Canton
Vrca File Converter
라이키 유출
Access One Ummc
Latest Posts
Article information

Author: Carlyn Walter

Last Updated:

Views: 6285

Rating: 5 / 5 (50 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Carlyn Walter

Birthday: 1996-01-03

Address: Suite 452 40815 Denyse Extensions, Sengermouth, OR 42374

Phone: +8501809515404

Job: Manufacturing Technician

Hobby: Table tennis, Archery, Vacation, Metal detecting, Yo-yoing, Crocheting, Creative writing

Introduction: My name is Carlyn Walter, I am a lively, glamorous, healthy, clean, powerful, calm, combative person who loves writing and wants to share my knowledge and understanding with you.