0% found this document useful (0 votes)
11 views

C BASIC SYNTAX

Uploaded by

abbamusa6112
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

C BASIC SYNTAX

Uploaded by

abbamusa6112
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

C BASIC SYNTAX, TOKENS, IDENTIFIERS AND KEYWORDS

What is a Character set?

Like every other language, ‘C’ also has its own character set. A program is a set
of instructions that, when executed, generate an output. The data that is
processed by a program consists of various characters and symbols. The output
generated is also a combination of characters and symbols.

A character set in ‘C’ programming is divided into,

• Letters
- Uppercase characters (A-Z)
- Lowercase characters (a-z)
• Numbers
- All the digits from 0 to 9
• Special characters
- White spaces (blank spaces)
- Blank space
- New line
- Carriage return
- Horizontal tab
• Special characters

Special characters in ‘C’ are shown in the given table,


, (comma) { } (opening/closing curly bracket)
. (period) [ ] (left/right bracket)
; (semi-colon) ( ) (opening/closing parenthesis)
: (colon) & (ampersand)
? (question mark) ^ (caret)
‘ (apostrophe) + (addition)
1

” (double quotation mark) – (subtraction)


! (exclamation mark) * (multiplication)
| (vertical bar) / (division)
/ (forward slash) > (greater than or closing angle bracket)
\ (backward slash) < (less than or opening angle bracket)
~ (tilde) # (hash sign)
_ (underscore) % (percentage sign)
$ (dollar sign)

What is Token in C?

Token is the smallest unit in a ‘C’ program, are the basically the building blocks
of a C program. It is each and every meaningful character, word and symbol that
you come across in your C program.

The compiler breaks a program into the smallest possible units (Tokens) and
proceeds to the various stages of the compilation.

C Token is divided into six different types, Keywords, Operators, Strings,


Constants, Special Characters, and Identifiers.
2
Keywords and Identifiers

In ‘C’ every word can be either a keyword or an identifier.

A keyword is reserved words by C language. Keywords are a set of words used


for a special purpose in the program.

Keywords have fixed meanings, and the meaning cannot be changed. There are
a total of 32 keywords in ‘C’. Keywords are written in lowercase letters.

There are total of 32 keywords.


Auto double Int struct

Break else Long switch

Case enum Register typedef

Char extern Return union

Const short Float unsigned

Continue for Signed void

Default goto Sizeof volatile

do if Static while
An identifier is nothing but a name assigned to an element in a program.
Example, name of a variable, function, etc.

Identifiers in C language are the user-defined names consisting of ‘C’ standard


character set.

As the name says, identifiers are used to identify a particular element in a


program. Each identifier must have a unique name.

Following rules must be followed for identifiers:

1. The first character must always be an alphabet or an underscore.


2. It should be formed using only letters, numbers, or underscore.
3. A keyword cannot be used as an identifier.

3
4. It should not contain any whitespace character.
5. The name must be meaningful.

C is a case-sensitive programming language. Thus, Student and student are two


different identifiers in C. Here are some examples of acceptable identifiers,

mohd zara abc move_name a_123 myname50 _temp j a23b9 retVal

How C Programming Language Works?

C is a compiled language. A compiler is a special tool that compiles the program


and converts it into the object file which is machine readable.

After the compilation process, the linker will combine different object files and
creates a single executable file to run the program. The following diagram
shows the execution of a ‘C’ program
Nowadays, various compilers are available online, and you can use any of those
compilers. The functionality will never differ and most of the compilers will
provide the features required to execute both ‘C’ and ‘C++’ programs and most
of them are IDEs.
Following is the list of popular compilers available online:

i. Clang compiler
ii. Dev C
iii. MinGW compiler (Minimalist GNU for Windows)
iv. Portable ‘C’ compiler v. Turbo C vi. Code::Blocks

4
Basic Syntax of C Program

The basic syntax of the C program consists of the header, main() function, body,
and return type of the program. The C program can also be called a collection of
various tokens.

// Basic Syntax of C Program

1. #include <stdio.h> // header


2.
3. int main() { // main function
4. printf("First C program."); // body
5. return 0; // return statement
6. }

Line 1: #include is a pre-processor directive in ‘C.’ <stdio.h> is a header file


that lets us work with input and output functions, such as printf() (used in line
4).
What are header files?

Header files (a.k.a Library files) contain a bunch of files that help the user to
include the predefined functions in C program according to his requirements.
You can add header files using the preprocessor directive #include.

Line 2: A blank line. C ignores white space. But we use it to make the code
more readable.

Line 3: Another thing that always appear in a C program, is main(). This is


called a function. In the C language, the main() function defines the starting
point of program execution. Any code inside its curly brackets {} will be
executed.

We can represent the main function in various forms, such as main(), int
main(), void main(), main(void), void main(void) and int main(void)

Line 4: printf() is a function used to output/print text to the screen. In our


example it will output "First C Program!".

Line 5: return 0 ends the main() function.


5
Line 6: Do not forget to add the closing curly bracket } to actually end the main
function.

C Statement

C Statements consist of tokens, expressions and other statements, and can be


either a simple or compound statement.

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, sometimes may lead to
a compilation(syntax) error.

Here is an example of code with a missing semicolon,

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

Output

error: expected ';' before 'return'


return 0;
^
command terminated with exit code 1

In the above program, no semicolon after the printf("...") statement, hence


the compiler will think that starting from printf up till the semicolon after
return 0 is a single statement and this will lead to compilation error.

Block
A block in C is a set of statements written within the opening and closing curly
brackets {……………}.

6
Adding Comments to Code

A comment is an explanation or description of the source code of the program. It


helps a developer explain logic of the code and improves program readability.
At run-time, a comment is ignored by the compiler.

Although writing comments is not compulsory, but it is recommended to make


your program more descriptive, and easier for others to understand.

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.

Example of comments in C language:

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

1. /*
2. This is my first program.
3. I am very excited!
4. */
5. #include <stdio.h>
6. int main(){
7. // Printing Hello World
8. printf("Hello,World");
9. // printf("Comment C Program");
10. return 0;
11. }

Chapter FAQs

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

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


Q2. What will happen if we forgot to use a semicolon at the end of the statement
in C?
Q3. What is a Compilation Error?
Q4. How to add comments in C?
Q5. Can a program run without the main() function in C?

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy