Warning: Undefined array key 1 in /home/yogindar/www/blog/wp-content/plugins/visitors-online/visitors-online.php on line 505

Warning: Undefined array key 2 in /home/yogindar/www/blog/wp-content/plugins/visitors-online/visitors-online.php on line 505

Warning: Cannot modify header information - headers already sent by (output started at /home/yogindar/www/blog/wp-content/plugins/visitors-online/visitors-online.php:505) in /home/yogindar/www/blog/wp-content/plugins/visitors-online/visitors-online.php on line 557

Warning: Cannot modify header information - headers already sent by (output started at /home/yogindar/www/blog/wp-content/plugins/visitors-online/visitors-online.php:505) in /home/yogindar/www/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1930

Warning: Cannot modify header information - headers already sent by (output started at /home/yogindar/www/blog/wp-content/plugins/visitors-online/visitors-online.php:505) in /home/yogindar/www/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1930

Warning: Cannot modify header information - headers already sent by (output started at /home/yogindar/www/blog/wp-content/plugins/visitors-online/visitors-online.php:505) in /home/yogindar/www/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1930

Warning: Cannot modify header information - headers already sent by (output started at /home/yogindar/www/blog/wp-content/plugins/visitors-online/visitors-online.php:505) in /home/yogindar/www/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1930

Warning: Cannot modify header information - headers already sent by (output started at /home/yogindar/www/blog/wp-content/plugins/visitors-online/visitors-online.php:505) in /home/yogindar/www/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1930

Warning: Cannot modify header information - headers already sent by (output started at /home/yogindar/www/blog/wp-content/plugins/visitors-online/visitors-online.php:505) in /home/yogindar/www/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1930

Warning: Cannot modify header information - headers already sent by (output started at /home/yogindar/www/blog/wp-content/plugins/visitors-online/visitors-online.php:505) in /home/yogindar/www/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1930

Warning: Cannot modify header information - headers already sent by (output started at /home/yogindar/www/blog/wp-content/plugins/visitors-online/visitors-online.php:505) in /home/yogindar/www/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1930
{"id":2207,"date":"2020-10-18T08:26:04","date_gmt":"2020-10-18T08:26:04","guid":{"rendered":"http:\/\/www.yogindar.com\/news\/?p=2207"},"modified":"2020-10-18T08:59:39","modified_gmt":"2020-10-18T08:59:39","slug":"variables-data-types-and-variable-types-in-c","status":"publish","type":"post","link":"http:\/\/blog.yogindar.org\/?p=2207","title":{"rendered":"Variables, data types and variable types in C"},"content":{"rendered":"

Variables are used to store data.\u00a0 The following are the charateristics for the variable.<\/p>\n

    \n
  1. Variables will have unique names under a specific scope.<\/li>\n
  2. Variable data type defines the storage requirement for the variable.<\/li>\n
  3. Storage class of the variable defines the visibility and lifetime of the variable in a program or function or block.<\/li>\n<\/ol>\n

    The following are the rules for a variable naming.<\/p>\n

      \n
    1. Variable can have underscore, letter or alphabet.<\/li>\n
    2. Variable can start only with underscore or alphabet.<\/li>\n
    3. No whitespaces allowed in a variable name.<\/li>\n
    4. Variable name cannot be a reserved keyword.<\/li>\n<\/ol>\n

      Data types<\/p>\n

        \n
      1. char or unsigned char\n
          \n
        1. single byte storage.<\/li>\n
        2. signed will have a value -128 to 127<\/li>\n
        3. unsigned will have a value 0 to 256<\/li>\n<\/ol>\n<\/li>\n
        4. short or unsigned short\n
            \n
          1. double byte storage.<\/li>\n
          2. signed will have a value -32,768 to 32,767<\/li>\n
          3. unsigned will have value 0 to 65535.<\/li>\n<\/ol>\n<\/li>\n
          4. int or unsigned int\n
              \n
            1. 4 byte storage.<\/li>\n
            2. signed will have a value -2GB to (2GB -1)<\/li>\n
            3. unsigned will have a value 0 to 4GB<\/li>\n<\/ol>\n<\/li>\n
            4. long or unsigned long\n
                \n
              1. 4 byte storage on 32 bit machines and 8 byte storage on 64 bit machine.<\/li>\n
              2. for 4 bit\n
                  \n
                1. signed will have a value -2GB to (2GB -1)<\/li>\n
                2. unsigned will have a value 0 to 4GB<\/li>\n<\/ol>\n<\/li>\n
                3. for 8 bit\n
                    \n
                  1. signed will have a value of -2 power 63 to (2 power63 -1)<\/li>\n
                  2. unsigned will have a value 0 to 2 power 64.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<\/li>\n
                  3. long long and unsigned long long\n
                      \n
                    1. Both 32 and 64 bit machines certain compilers support long long data type to hold 64 byte values.<\/li>\n
                    2. signed will have a value of -2 power 63 to (2 power63 -1)<\/li>\n
                    3. unsigned will have a value 0 to 2 power 64.<\/li>\n<\/ol>\n<\/li>\n
                    4. void\n
                        \n
                      1. Data type which can be point to no storage.<\/li>\n
                      2. Used mainly for pointers to point to any storage types.<\/li>\n<\/ol>\n<\/li>\n
                      3. pointer type\n
                          \n
                        1. Qualified with a * to address the variable points to an address which holds the data.<\/li>\n<\/ol>\n<\/li>\n
                        2. union\n
                            \n
                          1. combination of multiple types of variable and hold the maximum size of the variable used.<\/li>\n<\/ol>\n<\/li>\n
                          2. struct\n
                              \n
                            1. Combination of multiple types of variable and storage will be the combination of all variable datatypes.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n

                              Storage class<\/p>\n

                              Storage class defines the scope of the variable and the life time of the variable.<\/p>\n

                                \n
                              1. local variables\n
                                  \n
                                1. Variables defined inside a function or block without any storage class specified.<\/li>\n<\/ol>\n<\/li>\n
                                2. auto variables\n
                                    \n
                                  1. auto variables are defined with the keyword auto. By default all local variables are auto type.<\/li>\n<\/ol>\n<\/li>\n
                                  2. static variables\n
                                      \n
                                    1. static variables are defined with the keyword static.<\/li>\n
                                    2. static variables can have either scope defined inside a function or a file.<\/li>\n
                                    3. static variables will retain the data even if they goes out of scope and can be accessed when the code access the block\/function and file again.<\/li>\n<\/ol>\n<\/li>\n
                                    4. const variables\n
                                        \n
                                      1. Constant variables are variable data which cannot be modified later in the code.<\/li>\n
                                      2. Usually they are set to make sure the called API or code executed later cannot modify them.<\/li>\n<\/ol>\n<\/li>\n
                                      3. volatile variables\n
                                          \n
                                        1. volatile keyword is used to indicate compiler not to perform any optimization in the code.<\/li>\n<\/ol>\n<\/li>\n
                                        2. extern variable\n
                                            \n
                                          1. extern variable is used to indicate the variable is defined somewhere in another file or library.<\/li>\n
                                          2. extern keyword can be used to indicate the variable storage type to compiler so we can modify the data.<\/li>\n<\/ol>\n<\/li>\n
                                          3. register variables\n
                                              \n
                                            1. register variables are used to indicate the compiler to use any free register to cache the value.<\/li>\n
                                            2. However the compiler can ignore the register access and treat it as auto variable if not register available or cannot cache the value.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n

                                              Source: Variables and Keywords in C – GeeksforGeeks<\/a><\/em><\/p>\n

                                              Example code<\/p>\n

                                              var.c<\/strong><\/p>\n

                                              \u00a0<\/p>\n\n\n

                                              #include <stdio.h>\n\nint a=1; \/\/ initialized global variable.\nint b; \/\/ uninitialized global variable.\n\nstatic int c; \/\/global static variable.\nextern int d;\n\ntypedef struct _abc{\nchar a;\nshort b;\nint c;\n}ABC;\n\ntypedef union _def{\nchar a;\nshort b;\nint c;\n}DEF;\n\nint increment()\n{\nstatic int p = 5;\na=a+1;\np=p+1;\n\nprintf(\"dataof(p)=%d\\n\", p);\nreturn 0;\n}\n\nint main(int argc, char * argv[])\r\n{\r\nauto int e=5; \/\/auto variable\r\nint f=6; \/\/local variable\r\nconst int g=7; \/\/ constant variable\r\nlong j=10; \/\/ long type variable\r\nlong long k=11; \/\/ long long type variable\r\nABC h; \/\/ structure type variable\r\nDEF i; \/\/ union type variable\r\nchar l='a'; \/\/ character type variable\r\nshort m=10; \/\/ short type variable\r\nchar *n=&l; \/\/ pointer variable.\r\nvoid *o=(void *)n; \/\/ void pointer variable.\n\/\/ Size check\r\nprintf(\"Size of char is %d bytes\\n\", sizeof(l));\r\nprintf(\"Size of short is %d bytes\\n\", sizeof(m));\r\nprintf(\"Size of int is %d bytes\\n\", sizeof(f));\r\nprintf(\"Size of long is %d bytes\\n\", sizeof(j));\r\nprintf(\"Size of long long is %d bytes\\n\", sizeof(k));\r\nprintf(\"Size of pointer is %d bytes\\n\", sizeof(n));\r\nprintf(\"Size of structure ABC is %d bytes\\n\", sizeof(h));\r\nprintf(\"Size of union DEF is %d bytes\\n\", sizeof(i));\r\n\r\n\/\/Access check\r\nincrement();\r\nincrement();\r\nprintf(\"dataof(a)=%d, addressof(a)=0x%x\\n\", a, &a);\r\nprintf(\"dataof(b)=%d, addressof(b)=0x%x\\n\", b, &b);\r\nprintf(\"dataof(c)=%d, addressof(c)=0x%x\\n\", c, &c);\r\nprintf(\"dataof(d)=%d, addressof(d)=0x%x\\n\", d, &d);\r\nprintf(\"dataof(e)=%d\\n\", e);\r\nprintf(\"dataof(f)=%d\\n\", f);\r\n\/\/ g = g+10; in correct as its a constant \r\nprintf(\"dataof(g)=%d\\n\", g);\r\nprintf(\"dataof(j)=%d\\n\", j);\r\nprintf(\"dataof(k)=%d\\n\", k);\n\nreturn 0;\n}<\/code><\/pre>\n\n\n\n


                                              <\/p>\n\n\n\n

                                              ext.c<\/strong><\/p>\n\n\n\n

                                              int d = 4;<\/code><\/pre>\n\n\n\n

                                              <\/p>\n\n\n\n

                                              compilation<\/strong><\/p>\n\n\n\n

                                              <\/p>\n\n\n\n

                                              gcc -o test var.c ext.c<\/code><\/pre>\n\n\n\n

                                              Output<\/strong><\/p>\n\n\n\n

                                              yogi@localhost devel]$ .\/test\r\nSize of char is 1 bytes\r\nSize of short is 2 bytes\r\nSize of int is 4 bytes\r\nSize of long is 8 bytes\r\nSize of long long is 8 bytes\r\nSize of pointer is 8 bytes\r\nSize of structure ABC is 8 bytes\r\nSize of union DEF is 4 bytes\r\ndataof(p)=6\r\ndataof(p)=7\r\ndataof(a)=3, addressof(a)=0x601024\r\ndataof(b)=0, addressof(b)=0x601038\r\ndataof(c)=0, addressof(c)=0x601034\r\ndataof(d)=4, addressof(d)=0x60102c\r\ndataof(e)=5\r\ndataof(f)=6\r\ndataof(g)=7\r\ndataof(j)=10\r\ndataof(k)=11<\/code><\/pre>\n\n\n\n

                                              <\/p>\n","protected":false},"excerpt":{"rendered":"

                                              Variables are used to store data.\u00a0 The following are the charateristics for the variable. Variables will have unique names under a specific scope. Variable data type defines the storage requirement for the variable. Storage class of the variable defines the visibility and lifetime of the variable in a program or function or block. The following […]<\/p>\n","protected":false},"author":29,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1337],"tags":[1333],"class_list":["post-2207","post","type-post","status-publish","format-standard","hentry","category-my-article","tag-programming-2"],"_links":{"self":[{"href":"http:\/\/blog.yogindar.org\/index.php?rest_route=\/wp\/v2\/posts\/2207","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.yogindar.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.yogindar.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.yogindar.org\/index.php?rest_route=\/wp\/v2\/users\/29"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.yogindar.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2207"}],"version-history":[{"count":3,"href":"http:\/\/blog.yogindar.org\/index.php?rest_route=\/wp\/v2\/posts\/2207\/revisions"}],"predecessor-version":[{"id":2210,"href":"http:\/\/blog.yogindar.org\/index.php?rest_route=\/wp\/v2\/posts\/2207\/revisions\/2210"}],"wp:attachment":[{"href":"http:\/\/blog.yogindar.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.yogindar.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2207"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.yogindar.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}