{"id":81746,"date":"2021-07-13T09:00:30","date_gmt":"2021-07-13T03:30:30","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=81746"},"modified":"2021-07-13T09:00:30","modified_gmt":"2021-07-13T03:30:30","slug":"tokens-in-cpp","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/tokens-in-cpp\/","title":{"rendered":"Tokens in C++"},"content":{"rendered":"<p>In this article, we will learn about tokens and its types in C++. Let&#8217;s start!!<\/p>\n<h3>What is Token in C++?<\/h3>\n<p>A token is the smallest unit of a program that the compiler understands. In C++, Tokens are divided into<\/p>\n<ul>\n<li>Keywords<\/li>\n<li>Identifiers<\/li>\n<li>Constants<\/li>\n<li>Strings<\/li>\n<li>Special Symbols<\/li>\n<li>Operators<\/li>\n<\/ul>\n<p>Before moving ahead to the types, let\u2019s first talk about character set in C++.<\/p>\n<h3>Character Set in C++<\/h3>\n<p>The input and output of a program are made up of characters and symbols. C++ Character Set is a collection of characters and symbols that have a specified meaning. It consists of:<\/p>\n<ul>\n<li><strong>Letters<\/strong>: uppercase (A-Z) and lowercase (a-z) alphabets.<\/li>\n<li><strong>Digits<\/strong>: Numbers from 0-9.<\/li>\n<li><strong>Special characters<\/strong>: There are various special characters like , ; \\ _ ? : etc.<\/li>\n<li><strong>White spaces:<\/strong> blank space, new line, horizontal tab and carriage return.<\/li>\n<\/ul>\n<h3>Types of Tokens in C++<\/h3>\n<h4>1. Keywords in C++<\/h4>\n<p>In a programming language, there are certain reserved words that have fixed meaning. These words are known as keywords. We cannot use a keyword for purposes other than what it&#8217;s reserved for.<\/p>\n<p>There are 32 keywords supported in C language.<\/p>\n<p><b>auto\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 double\u00a0 \u00a0int\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0struct<\/b><\/p>\n<p><b>break\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 else\u00a0 \u00a0 \u00a0 \u00a0 long\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 switch<\/b><\/p>\n<p><b>case\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0enum\u00a0 \u00a0 \u00a0register\u00a0 typedef<\/b><\/p>\n<p><b>char\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 extern\u00a0 \u00a0 return \u00a0 \u00a0 union<\/b><\/p>\n<p><b>const\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0float \u00a0 \u00a0 \u00a0 short\u00a0 \u00a0 \u00a0 unsigned<\/b><\/p>\n<p><b>continue\u00a0 \u00a0 for\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 signed \u00a0 \u00a0 void<\/b><\/p>\n<p><b>default\u00a0 \u00a0 \u00a0 \u00a0 goto\u00a0 \u00a0 \u00a0 \u00a0sizeof\u00a0 \u00a0 \u00a0 volatile<\/b><\/p>\n<p><b>do\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0if\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0static\u00a0 \u00a0 \u00a0 while<\/b><\/p>\n<p>C++ supports these 32 keywords and 31 additional keywords that are not available in C.<\/p>\n<p><b>asm\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 bool\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 catch\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0class<\/b><\/p>\n<p><b>const_cast \u00a0 delete\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 dynamic_cast \u00a0 explicit\u00a0<\/b><\/p>\n<p><b>export\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0false\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0friend\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0inline\u00a0<\/b><\/p>\n<p><b>mutable\u00a0 \u00a0 \u00a0 \u00a0namespace \u00a0 new\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 operator\u00a0<\/b><\/p>\n<p><b>private\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 protected\u00a0 \u00a0 \u00a0public\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 reinterpret_cast<\/b><\/p>\n<p><b>static_cast\u00a0 \u00a0template\u00a0 \u00a0 \u00a0 this\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0throw<\/b><\/p>\n<p><b>true\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 try\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 typeid\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0typename\u00a0<\/b><\/p>\n<p><b>using\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 virtual\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 wchar_t<\/b><\/p>\n<h4>2. Identifiers in C++<\/h4>\n<p>We can give names to various elements of a program like variables, functions, structures, etc. These user-defined names are called identifiers. Identifiers must be unique as we use them in the program execution.<\/p>\n<p>We need to follow some rules for naming identifiers:<\/p>\n<ul>\n<li>An identifier name should begin with a letter or an underscore ( _ ).<\/li>\n<li>An identifier name should be made up of letters, digits and underscores only.<\/li>\n<li>Special characters and white spaces are not allowed.<\/li>\n<li>No keyword can be used as an identifier.<\/li>\n<li>Identifier names are case sensitive.<\/li>\n<li>Length of an identifier name should not exceed 31 characters, beyond which it becomes insignificant.<\/li>\n<\/ul>\n<p>Examples of some valid identifiers are: Name, age, add_numbers, _students, etc.<br \/>\nExamples of some invalid identifiers are: Name@, 6age, new, etc.<\/p>\n<h4>3. Constants in C++<\/h4>\n<p>Constants are expressions whose values remain fixed. Once defined, we cannot change the value of a constant. These can also be referred to as literals.<\/p>\n<p>Constants can be of integer, floating-point, character, string or boolean data types.<\/p>\n<p><strong>For example,<\/strong><br \/>\nconst float pi = 3.14;<\/p>\n<p>Here, the value of constant pi cannot be changed in the entire program.<\/p>\n<h4>4. Strings in C++<\/h4>\n<p>A string stores a sequence of characters. It terminates with a null character \u2018\\0\u2019. Unlike characters, strings in C++ are always enclosed within double quotes (&#8221; &#8220;).<\/p>\n<p>In C++, there are two types of strings:<\/p>\n<p>a. C-style strings<br \/>\n<strong>Example<\/strong> &#8211; char name[ ] = &#8220;TechVidvan&#8221;;<\/p>\n<p>b. Objects of the string class in the Standard C++ Library<br \/>\n<strong>Example<\/strong> &#8211; string name = &#8220;TechVidvan&#8221;;<\/p>\n<h4>5. Special Symbols in C++<\/h4>\n<p>There are some special symbols in C++ that have special meaning to the compiler. We cannot alter their meaning. List of special symbols in C++ are:<\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Special Symbol<\/b><\/td>\n<td><b>Name<\/b><\/td>\n<td><b>Use<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">[ ]<\/span><\/td>\n<td><span style=\"font-weight: 400\">Square Brackets<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used for single dimensional and multidimensional subscripts of arrays.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">()<\/span><\/td>\n<td><span style=\"font-weight: 400\">Parentheses<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used for function calls and parameters.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">{ }<\/span><\/td>\n<td><span style=\"font-weight: 400\">Curly braces<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used to indicate the beginning and end of a code block.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">,<\/span><\/td>\n<td><span style=\"font-weight: 400\">Comma<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used to separate multiple statements like parameters in a function.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">:<\/span><\/td>\n<td><span style=\"font-weight: 400\">Colon<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used to invoke an initialization list.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">;<\/span><\/td>\n<td><span style=\"font-weight: 400\">Semicolon<\/span><\/td>\n<td><span style=\"font-weight: 400\">Also called statement terminator, it is used to mark the end of statements.\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">*<\/span><\/td>\n<td><span style=\"font-weight: 400\">Asterisk<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used to create pointers.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">#<\/span><\/td>\n<td><span style=\"font-weight: 400\">Hash\/ Preprocessor<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used as a preprocessor directive to include header files and define constants.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">.<\/span><\/td>\n<td><span style=\"font-weight: 400\">Dot<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used to access a structure member.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">~<\/span><\/td>\n<td><span style=\"font-weight: 400\">Tilde<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used as a destructor.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>6. Operators in C++<\/h4>\n<p>Operators are symbols that operate on operands. These operands can be variables or values. Operators help us to perform mathematical and logical computations.<\/p>\n<p>Operators in C++ are classified into following types based on the number of operands they operate on:<\/p>\n<p><strong>a. Unary Operators:<\/strong> These act upon one operand. For example, increment operator (++).<br \/>\n<strong>b. Binary Operators:<\/strong> They operate on two operands. For example, addition operator (+).<br \/>\n<strong>c. Ternary Operator:<\/strong> There is a ternary operator in C++ that acts on three operands. It is the ?: conditional operator.<\/p>\n<p>On the basis of nature of operation, operators in C++ are classified into following six types:<\/p>\n<ul>\n<li>Arithmetic<\/li>\n<li>Assignment<\/li>\n<li>Relational<\/li>\n<li>Logical<\/li>\n<li>Bitwise<\/li>\n<li>Other Operators<\/li>\n<\/ul>\n<h3>Summary<\/h3>\n<p>Now, we understand what tokens are in C++. It is the smallest unit of a program that the compiler understands. Tokens are classified into Keywords, Identifiers, Constants, Strings, Special symbols and Operators.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will learn about tokens and its types in C++. Let&#8217;s start!! What is Token in C++? A token is the smallest unit of a program that the compiler understands. In&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":81812,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3405],"tags":[3441,3408,3605,3778,3624],"class_list":["post-81746","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cpp","tag-identifiers-in-c","tag-keywords-in-c","tag-operators-in-c","tag-special-symbols-in-c","tag-tokens-in-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Tokens in C++ - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn about tokens in C++ which is the smallest unit of a program that the compiler understands and its types.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/tokens-in-cpp\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Tokens in C++ - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn about tokens in C++ which is the smallest unit of a program that the compiler understands and its types.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/tokens-in-cpp\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-13T03:30:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/C-Tokens.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Tokens in C++ - TechVidvan","description":"Learn about tokens in C++ which is the smallest unit of a program that the compiler understands and its types.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/tokens-in-cpp\/","og_locale":"en_US","og_type":"article","og_title":"Tokens in C++ - TechVidvan","og_description":"Learn about tokens in C++ which is the smallest unit of a program that the compiler understands and its types.","og_url":"https:\/\/techvidvan.com\/tutorials\/tokens-in-cpp\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2021-07-13T03:30:30+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/C-Tokens.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/tokens-in-cpp\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/tokens-in-cpp\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Tokens in C++","datePublished":"2021-07-13T03:30:30+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/tokens-in-cpp\/"},"wordCount":775,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/tokens-in-cpp\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/C-Tokens.jpg","keywords":["Identifiers in C","Keywords in C","Operators in C","Special Symbols in C++","tokens in c++"],"articleSection":["C++ Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/tokens-in-cpp\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/tokens-in-cpp\/","url":"https:\/\/techvidvan.com\/tutorials\/tokens-in-cpp\/","name":"Tokens in C++ - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/tokens-in-cpp\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/tokens-in-cpp\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/C-Tokens.jpg","datePublished":"2021-07-13T03:30:30+00:00","description":"Learn about tokens in C++ which is the smallest unit of a program that the compiler understands and its types.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/tokens-in-cpp\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/tokens-in-cpp\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/tokens-in-cpp\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/C-Tokens.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/C-Tokens.jpg","width":1200,"height":628,"caption":"Tokens in C++"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/tokens-in-cpp\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Tokens in C++"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/81746","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=81746"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/81746\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/81812"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=81746"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=81746"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=81746"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}