Skip to main content

What Are Tokens in C/C++?


 

What Are Tokens?

 In simple terms, tokens represent the smallest meaningful components of any program that the compiler easily comprehends. They can be classified into six sub-categories:

1. Keywords
2. Constants
3. Strings
4. Identifiers
5. Operators
6. Special Symbols

Now, let's explore each category:

Keywords:

Keywords are reserved terms in programming languages that provide specific functionalities to the program. They cannot be used as variable names. C/C++ pre-processor directives, such as header files, can modify keywords before compilation.

 

Constants:

Constants, like variables, are unchanging values. The key distinction is that the program cannot modify the value of a constant after it has been defined. Constants can belong to various data types, including integer, floating-point, octal, hexadecimal, character, and string constants.

 

Strings:

Strings are arrays of characters ending with a null character "\0". They are enclosed in double quotes (" ") and differ from characters within single quotes (' ').

 

Identifiers:

Identifiers are user-defined names for variables, arrays, and functions. They must follow specific rules, such as starting with a letter or underscore, and cannot be keywords.

 

Operators:

Operators are symbols that trigger actions when applied to variables or objects. Unary operators work on a single operand, while binary operators require two operands. Ternary operators involve three operands and include the conditional operator (?:).

 Special Symbols:

These symbols serve specific functions in C/C++:

- Brackets [] for array element reference.

- Braces {} mark the start and end of a code block.

- Comma (,) separates statements, e.g., in for loops.

- Semicolon (;) terminates statements.

- Parenthesis () indicate function parameters and calls.

- Asterisk (*) creates a pointer variable.

- Assignment Operator (=) assigns values.

- Preprocessor (#) is used with header files for program transformation before compilation.



Comments

Popular posts from this blog

C++ Lec-2 Introduction to Object-Oriented Programming (OOP) in C++

  Introduction to Object-Oriented Programming (OOP) in C++ Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to organize software design. It allows for modelling real-world entities and relationships in a program. C++ is an object-oriented programming language that provides features to implement OOP concepts effectively. Key Concepts of OOP   Class and Object o    Class : A blueprint for creating objects. It defines properties and behaviours of objects.   class Car { public:     string brand;     string model;     int year; };   o    Object : An instance of a class.   Car car1; car1.brand = "Mahindra"; car1.model = "THAR"; car1.year = 2024;   Encapsulation o    Encapsulation is the bundling of data and methods that operate on that data within a single unit, or class. It restricts direct acces...

Flow Charts - What Is a Flow Chart? When to Use a Flowchart? Flowchart Symbols & Components

  Flow Charts     Flow charts are a useful tool in many situations, as we make a process easy to understand at a glance. Using just a few words and some simple symbols, they show clearly what happens at each stage and how this affects other decisions and actions.   What Is a Flow Chart?     In 1921, the Frank and Lillian presented what was only a "graphic-based method" in a presentation titled: “ Process Charts: First Steps in Finding the One Best Way to do Work ”, to members of the American Society of Mechanical Engineers (ASME). When to Use a Flowchart?     Flowchart is a very simple yet powerful tool to improve productivity in both our personal and work life. Here are some ways flowchart can be helpful:   ·      Document a process ·      Present solution to a problem ·      Brainstorm ideas in a meeting ·      Design an operation system ·    ...

Difference Between POP and OOP

  What is the Difference Between POP and OOP? Procedure-Oriented Programming (POP) and Object-Oriented Programming (OOP) are two fundamental programming paradigms. Both have their own methodologies, approaches, and use cases. Below, we will define both paradigms, outline their key differences, and provide examples of each for better understanding.   Definition of POP (Procedure-Oriented Programming) Procedure-Oriented Programming (POP) is a programming paradigm where programs are designed around procedures or functions. It focuses on dividing the program into smaller units called functions, which operate on data. Key Features of POP: Program is divided into functions or procedures. Functions are the main building blocks of the program. Emphasizes a step-by-step procedural approach. Data and functions are separate, with functions manipulating data.   Definition of OOP (Object-Oriented Programming) Object-Oriented Programming (OOP) is a p...