{"id":6516,"date":"2024-08-10T00:01:33","date_gmt":"2024-08-10T07:01:33","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6516"},"modified":"2024-08-03T11:11:49","modified_gmt":"2024-08-03T18:11:49","slug":"hungarian-notation-rhapsody","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6516","title":{"rendered":"Hungarian Notation Rhapsody"},"content":{"rendered":"<p>The rules for naming variables, or identifiers, in C are simple: The names must start with a letter and contain letters and numbers. The _ (underscore) counts as a letter. Upper- and lowercase letters are considered differently. And only the first 31 characters are significant, though some platforms may extend this length.<br \/>\n<!--more--><br \/>\nI&#8217;ve covered the snake_case and camelCase methods for naming identifiers in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5967\">an Exercise post<\/a> from last year. Another naming convention is brought to you from Microsoft: Hungarian notation.<\/p>\n<p>Named for Microsoft programmer Charles Simonyi, whom I assume is Hungarian, this naming convention uses prefixes to identify variable types. Remember that C variable names &mdash; identifiers &mdash; sport no relation to whatever it is they&#8217;re naming. Identifiers can label variables, constants, functions, and just about anything. The name <code>ticks<\/code> could identify a function, a <em>char<\/em>, an <em>int<\/em>, a <em>float<\/em>, and so on. When Hungarian notation is applied, you might use the name <code>lTicks<\/code>, where the prefixed little L indicates that the variable is a <em>long int<\/em>. It&#8217;s these prefixes that define Hungarian notation and identify variable data types.<\/p>\n<p>The following table lists various Hungarian Notation prefixes. This list isn&#8217;t &#8220;official&#8221; and variations exist. I suppose the point is to be consistent in your own code.<\/p>\n<table>\n<tr>\n<th>Prefix<\/th>\n<th>Meaning<\/th>\n<th>Prefix<\/th>\n<th>Meaning<\/th>\n<\/tr>\n<tr>\n<td><code>b<\/code><\/td>\n<td>boolean<\/td>\n<td><code>i<\/code><\/td>\n<td>int<\/td>\n<\/tr>\n<tr>\n<td><code>by<\/code><\/td>\n<td>unsigned char (byte)<\/td>\n<td><code>l<\/code><\/td>\n<td>long<\/td>\n<\/tr>\n<tr>\n<td><code>c<\/code><\/td>\n<td>character<\/td>\n<td><code>n<\/code><\/td>\n<td>integer<\/td>\n<\/tr>\n<tr>\n<td><code>d<\/code><\/td>\n<td>double<\/td>\n<td><code>p<\/code><\/td>\n<td>pointer<\/td>\n<\/tr>\n<tr>\n<td><code>dw<\/code><\/td>\n<td>unsigned long<\/td>\n<td><code>s<\/code><\/td>\n<td>string<\/td>\n<\/tr>\n<tr>\n<td><code>f<\/code><\/td>\n<td>float<\/td>\n<td><code>sz<\/code><\/td>\n<td>null-terminated string<\/td>\n<\/tr>\n<tr>\n<td><code>fp<\/code><\/td>\n<td>floating point<\/td>\n<td><code>u<\/code><\/td>\n<td>unsigned<\/td>\n<\/tr>\n<tr>\n<td><code>g_<\/code><\/td>\n<td>global\/external<\/td>\n<td><code>v<\/code><\/td>\n<td>void<\/td>\n<\/tr>\n<tr>\n<td><code>h<\/code><\/td>\n<td>handle<\/td>\n<td><code>w<\/code><\/td>\n<td>word (<em>unsigned int<\/em>)<\/td>\n<\/tr>\n<\/table>\n<p>It was the <code>sz<\/code> prefix that sent me on my Hungarian notation voyage of discovery. I was examining code online and saw a variable such as <code>szTextInput<\/code>. The &#8220;text input&#8221; part is obvious, but I only guessed that &#8220;sz&#8221; might mean zero-terminated string, &#8220;string zero.&#8221; A quick search confirmed what I knew and identified the naming method as Hungarian notation.<\/p>\n<p>Hungarian Notation isn&#8217;t without its drawbacks. First and foremost, naming a variable with Hungarian Notation does nothing to change the variable&#8217;s underlying type. In fact, you must rely on the programmer being honest when using these prefixes. I also observe a lack of consistency and several variations on Hungarian Notation, probably due to its non-binding nature.<\/p>\n<p>Another disadvantage is that, unlike snake_case and camelCase, Hungarian notation does little to make a variable more readable. In some cases, it truly obfuscates the variable name.<\/p>\n<p>In a way, Hungarian Notation reminds me of the sigils used in other programming languages. BASIC was my first computer language, which used character suffixes (sigils) to identify variable types. For example, <code>TEXT$<\/code> identifies a string variable and <code>COUNT%<\/code> is an integer. These sigils are coded into the language and required with the variable&#8217;s declaration and use.<\/p>\n<p>Most importantly, it&#8217;s good to know what Hungarian Notation is and why it&#8217;s used. Whether you use it in your own code, or whether you develop something similar, is all up to you. C is quite flexible, which is both its weakness and strength.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is yet another way to add descriptive power to a variable name. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6516\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-6516","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6516","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6516"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6516\/revisions"}],"predecessor-version":[{"id":6527,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6516\/revisions\/6527"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6516"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6516"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6516"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}