{"id":7,"date":"2016-07-25T11:03:51","date_gmt":"2016-07-25T11:03:51","guid":{"rendered":"http:\/\/54.169.157.34\/blog\/?p=7"},"modified":"2016-07-25T11:55:26","modified_gmt":"2016-07-25T11:55:26","slug":"variables-datatypes-elements-in-c","status":"publish","type":"post","link":"https:\/\/codeinsightacademy.com\/blog\/c-programming\/variables-datatypes-elements-in-c\/","title":{"rendered":"Variables, Datatypes and Elements in C"},"content":{"rendered":"<p><strong>Variables<\/strong><\/p>\n<p>A variable is nothing but a name given to a storage area that our programs can manipulate. It is defined by the following syntax.<\/p>\n<p><strong>Storage-class-specifier &lt;space&gt; type-specifier &lt;space&gt; variable-name,&#8230;<\/strong><\/p>\n<p>The Storage-class-specifier can be one of the following:<\/p>\n<table width=\"475\">\n<tbody>\n<tr>\n<td width=\"85\">typedef<\/td>\n<td width=\"390\">The symbol name &#8220;variable-name&#8221; becomes a type-specifier of type &#8220;type-specifier&#8221;.<br \/>\nNo variable is actually created, this is merely for convenience.<\/td>\n<\/tr>\n<tr>\n<td width=\"85\">Extern<\/td>\n<td width=\"390\">Inticates that the variable is defined outside of the current file.<br \/>\nThis brings the variables scope into the current scope. No variable is actually created by this.<\/td>\n<\/tr>\n<tr>\n<td width=\"85\">Static<\/td>\n<td width=\"390\">Causes a variable that is defined within a function to be preserved in subsequent calls to the function.<\/td>\n<\/tr>\n<tr>\n<td width=\"85\">Auto<\/td>\n<td width=\"390\">Causes a local variable to have a local lifetime (default).<\/td>\n<\/tr>\n<tr>\n<td width=\"85\">register<\/td>\n<td width=\"390\">Request that the variable be accessed as quickly as possible. This request is not guaranteed.<br \/>\nNormally, the variable&#8217;s value is kept within a CPU register for maximum speed.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>e.g. int I,j,k;<\/p>\n<p>float x,y,z;<\/p>\n<p>char ch;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Constants<\/strong><\/p>\n<p>ANSI C allows you to declare constants. When you declare a constants it is a bit like a variable declaration except the value cannot be changed.<\/p>\n<p>&nbsp;<\/p>\n<p>The const keyword is to declare a constant, as shows below:<\/p>\n<p>int const a = 1;<\/p>\n<p>const int a = 2;<\/p>\n<p>Note:<\/p>\n<ul>\n<li>You can declare the const before or after the type.<\/li>\n<li>It is usual to initialize a const with a value as it cannot get a value any other way.<\/li>\n<\/ul>\n<p>The preprocessor #define is another more flexible method to define constants in a program.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Datatypes<\/strong><\/p>\n<table width=\"733\">\n<tbody>\n<tr>\n<td width=\"181\"><strong>Datatypes<\/strong><\/td>\n<td width=\"258\"><strong>Details<\/strong><\/td>\n<td width=\"108\"><strong>Format Specifier &amp; Size<\/strong><\/td>\n<td width=\"186\"><strong>Range<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"181\">char, signed char<\/td>\n<td width=\"258\">Variable is large enough to store a basic character in character set.<br \/>\nMay be either signed or nonnegative<\/td>\n<td width=\"108\">%c 8 bits<\/td>\n<td width=\"186\">-128 to 127<\/td>\n<\/tr>\n<tr>\n<td width=\"181\">unsigned char<\/td>\n<td width=\"258\">Same as char, but unsigned only<\/td>\n<td width=\"108\">%c 8 bits<\/td>\n<td width=\"186\">\u00a00 to 255<\/td>\n<\/tr>\n<tr>\n<td width=\"181\">short, signed short, short int, signed short int<\/td>\n<td width=\"258\">Defines a short signed int.<\/td>\n<td width=\"108\">%d 8 bits or 16 bits<\/td>\n<td width=\"186\">\u00a0-128 to 127<\/p>\n<p>Or<\/p>\n<p>-32,768 to 32,767<\/td>\n<\/tr>\n<tr>\n<td width=\"181\">unsigned short, unsigned short int<\/td>\n<td width=\"258\">Defines an unsigned short integer.<\/td>\n<td width=\"108\">%d 8 bits or 16 bits<\/td>\n<td width=\"186\">\u00a00 to 255 or<\/p>\n<p>0 to 65,535<\/td>\n<\/tr>\n<tr>\n<td width=\"181\">int, signed int<\/td>\n<td width=\"258\">Defines a signed integer<\/td>\n<td width=\"108\">%d 16 bits<\/td>\n<td width=\"186\">\u00a0-32,768 to 32,767<\/td>\n<\/tr>\n<tr>\n<td width=\"181\">unsigned int<\/td>\n<td width=\"258\">Same as int, but unsigned values only.<\/td>\n<td width=\"108\">%u 16 bits<\/td>\n<td width=\"186\">\u00a00 to 65,535<\/td>\n<\/tr>\n<tr>\n<td width=\"181\">long, signed long, long int, signed long int<\/td>\n<td width=\"258\">Defines a long signed integer.<\/td>\n<td width=\"108\">%ld 32 bits<\/td>\n<td width=\"186\">\u00a0-2.147,483,648 to 2,147,483,647<\/td>\n<\/tr>\n<tr>\n<td width=\"181\">unsigned long, unsigned long int<\/td>\n<td width=\"258\">Same as long, but unsigned values only.<\/td>\n<td width=\"108\">%lu 32 bits<\/td>\n<td width=\"186\">\u00a00 to 4,294,967,295<\/td>\n<\/tr>\n<tr>\n<td width=\"181\">Float<\/td>\n<td width=\"258\">A floating-point number.<\/td>\n<td width=\"108\">%f 32 bits<\/td>\n<td width=\"186\">\u00a01.17549435 * (10^-38) to 3.40282347*(10^+38)<\/td>\n<\/tr>\n<tr>\n<td width=\"181\">Double<\/td>\n<td width=\"258\">A more accurate floating-point number than float.<\/td>\n<td width=\"108\">%lf 64 bits<\/td>\n<td width=\"186\">\u00a02.2250738585072014 *(10^-308 to 1.7976931348623157 * (10^+308)<\/td>\n<\/tr>\n<tr>\n<td width=\"181\">long double<\/td>\n<td width=\"258\">Increase the size of double.<\/td>\n<td width=\"108\">%Lf 80 bits<\/td>\n<td width=\"186\">\u00a03.4 * (10^-4932) to 1.1 * (10^4932)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p><strong>Note:<\/strong> Size of datatype depends on different implementation of c by different compilers.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>C Elements<\/strong><\/p>\n<p><strong>Tokens<\/strong><\/p>\n<p><strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/strong>In a C source program, the basic element recognized by the compiler is the \u201ctoken\u201d.<\/p>\n<p>A token is source-program text that the compiler does not break down into component elements.<\/p>\n<p>Tokens can be keyword, identifier, constant, string-literal, operator, punctuator.<\/p>\n<p><strong>Identifiers<\/strong><\/p>\n<p>\u201cIdentifiers\u201d or \u00a0\u201csymbols\u201d are the names you supply for variables, types, functions and labels in your program.<br \/>\nIdentifier names must differ in spelling and case from any keywords.<\/p>\n<ul>\n<li>Must start with alphabet (a..z A\u2026Z) or underscore(_).<\/li>\n<li>Next letter can be alphabet (a\u2026z A\u2026Z), underscore(_) or digit.<\/li>\n<li>You cannot use keywords as identifiers.<\/li>\n<\/ul>\n<p><strong>Literals<\/strong><\/p>\n<p>Invariant program elements are called \u201cliterals\u201d or \u201cconstants\u201d. The terms \u201cliteral\u201d and \u201cconstant\u201d are used interchangeably here. Literals fall into four major categories:<\/p>\n<ul>\n<li>integer<\/li>\n<li>character<\/li>\n<li>floating-point<\/li>\n<li>string literals<\/li>\n<\/ul>\n<p>A \u201cstring literal\u201d is a sequence of characters from the source character set enclosed in double quotation marks (\u201c \u201c). String literals are also used to represent a sequence of characters which, taken together, from a null-terminated string. The characters of a literal string are stored in order at continuous memory locations. A null character (represented by the \\0 escape sequence) is automatically appended to and marks the end of each string literal.<\/p>\n<p>e.g.<\/p>\n<ul>\n<li>157 \/\/ integer constant<\/li>\n<li>0xFE \/\/ integer constant<\/li>\n<li>\u2018c\u2019 \/\/ character constant<\/li>\n<li>2 \/\/ floating constant<\/li>\n<li>2E-01 \/\/ floating constant<\/li>\n<li>\u201cdog\u201d string literal<\/li>\n<\/ul>\n<p><strong>Constants<\/strong><\/p>\n<p><strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/strong>\u00a0A \u201cconstant\u201d is a number, character or character string that can be used as a value in a program. Use constants to represent floating-point, integer, enumeration or character values that cannot be modified.<\/p>\n<p><strong>Keywords<\/strong><\/p>\n<p><strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/strong>\u201cKeywords\u201d are words that have special meaning to the C compiler.<\/p>\n<p>An identifier cannot have the same spelling and case as a C keyword.<\/p>\n<p>The C language uses following keywords:<\/p>\n<p>&nbsp;<\/p>\n<table width=\"563\">\n<thead>\n<tr>\n<td>auto<\/td>\n<td>double<\/td>\n<td>int<\/td>\n<td>struct<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>break<\/td>\n<td>else<\/td>\n<td>long<\/td>\n<td>switch<\/td>\n<\/tr>\n<tr>\n<td>case<\/td>\n<td>enum<\/td>\n<td>register<\/td>\n<td>typedef<\/td>\n<\/tr>\n<tr>\n<td>char<\/td>\n<td>extern<\/td>\n<td>return<\/td>\n<td>union<\/td>\n<\/tr>\n<tr>\n<td>const<\/td>\n<td>float<\/td>\n<td>short<\/td>\n<td>unsigned<\/td>\n<\/tr>\n<tr>\n<td>continue<\/td>\n<td>for<\/td>\n<td>signed<\/td>\n<td>void<\/td>\n<\/tr>\n<tr>\n<td>default<\/td>\n<td>goto<\/td>\n<td>sizeof<\/td>\n<td>volatile<\/td>\n<\/tr>\n<tr>\n<td>do<\/td>\n<td>if<\/td>\n<td>static<\/td>\n<td>while<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p><strong>Comments<\/strong><\/p>\n<p>Comments in the source code are ignored by the compiler. They are encapsulated starting with \/* and ending with *\/. According to ANSI standard, nested comments are not allowed, although some implementations allow it.<\/p>\n<p>Single line comments are becoming more common, although not defined in the ANSI standard. Single line comments begin with \/\/ and are automatically terminated at the end of the current line.<\/p>\n<p><strong>Punctuation and Special Characters<\/strong><\/p>\n<p><strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/strong>The punctuation and special characters in the C character set have various uses, from organizing program text to defining the tasks that the compiler or the compiled program carries out. They do not specify an operation to be performed. Some punctuation symbols are also operators. The compiler determines their use from context.<\/p>\n<p><strong>Punctuator: one of [ ] ( ) { } * , : = ; \u2026 #<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Variables A variable is nothing but a name given to a storage area that our programs can manipulate. It is defined by the following syntax. Storage-class-specifier &lt;space&gt; type-specifier &lt;space&gt; variable-name,&#8230; The Storage-class-specifier can be one of the following: typedef The symbol name &#8220;variable-name&#8221; becomes a type-specifier of type &#8220;type-specifier&#8221;. No variable is actually created, this [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/7"}],"collection":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/comments?post=7"}],"version-history":[{"count":8,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/7\/revisions"}],"predecessor-version":[{"id":15,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/7\/revisions\/15"}],"wp:attachment":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/media?parent=7"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/categories?post=7"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/tags?post=7"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}