C Variables and Data Types

Read this article about data types. Although this article discusses C, the data types in C++ are exactly the same. The only difference is that C does not include a boolean variable. For now, just note that a boolean variable holds a value of either True or False.

What are variables?

Variables in C have the same meaning as variables in algebra. A variable in C is a storage unit, which sets a space in memory to hold a value and can take different values at different times during program execution.


Rules to construct a valid variable name

1. A variable name may consists of letters, digits and the underscore ( _ ) characters.

2. A variable name must begin with a letter. Some system allows to starts the variable name with an underscore as the first character.

3. ANSI standard recognizes a length of 31 characters for a variable name. However, the length should not be normally more than any combination of eight alphabets, digits, and underscores.

4. Uppercase and lowercase are significant. That is the variable Totamt is not the same as totamt and TOTAMT.

5. The variable name may not be a C reserved word (keyword).


Some valid variable names

Total TAmount Tctr Tname1
Tn1 TM_age TAMOUNT  


Some invalid variable names

13th (name) 111 %nm


Naming Conventions

Generally, C programmers maintain the following conventions for naming variables.

  • Start a variable name with lowercase letters.
  • Try to use meaningful identifiers
  • Separate "words" within identifiers with mixed upper and lowercase (for example empCode) or underscores (for example emp_code).
  • For symbolic constants use all uppercase letters (for example #define LENGTH 100, #define MRP 45).


Keywords and Identifiers

Every C word is classified as either a keyword or an identifier. Each keyword has a specific meaning and these meanings cannot be changed. Keywords serve as basic building blocks for program statements. There are only 32 keywords in C. The list of all keywords in ANSI C is listed in the following table. All keywords must be written in lowercase.

auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while


Constants

Constants in C refer to a specific quantity that doesn't change during the execution of a program.


Types of Constants

  • Integer Constants
  • Real Constants
  • Single Character Constants
  • String Constants


Rules to construct Integer Constant

  • An integer constant refers to a sequence of digits. The three types of integers are decimal, octal and hexadecimal
  • No embedded blanks, commas, and non-numeric character are permitted within an integer constant.
  • An integer constant must contain one digit
  • Decimal integers consist set of digits, 0 to 9 without any decimal point and can be preceded by an optional +ve or -ve sign.
  • An octal integer constant contains any combination of digits between 0 and 7 with a leading 0.
  • A hexadecimal constant contains any combination of digits between 0 and 9 and can also hold alphabets between A and F or a and f with prefix 0x or 0X. Alphabets A or a represents number 10 and F or f represents 15.
  • The largest integer value for the 16-bit machine is 32767 and 2147483647 for a 32-bit machine.


Example of various valid numeric constants

Constant Type Constant Type
241 Decimal Integer 047 Octal Integer
-973 Decimal Integer 053 Octal Integer
0 Decimal Integer 0X59 Hexadecimal Integer
+4177 Decimal Integer 0x47F Hexadecimal Integer


Example of some invalid numeric constants

Invalid Constant
05 241
7,412
$120


Rules to construct Real Constant

  • A real constant is a number that may have a fractional part.
  • It could be either +ve or -ve.
  • No embedded blanks, commas, and non-numeric character are permitted within a real constant.
  • A real number may also be expressed in exponential notation. An exponent is an integer number with an optional plus or minus sign. Exponential is useful for representing the number which is very large or very small in magnitude.


Example of various valid real constants

0.0045 -.71
+45.203 0.45e3
-0.547 0.78e-4
337. 2.79E4
.478. -4.69E-4


Initialization of Variables

Variables are given initial values, or initialized, when declared. See the following examples :

char abc = 'X';
int marks =77;
float amount = 45.23;

Write initialized variables on a separate line and a comment beside the variable name is a good idea. See the following examples:
int qty; /* quantity of an item */
float value = 12.10; /* purchase value of an item */
int marks; /* marks of a subject */


C Programming: Tips of the Day

String literals: Where do they go?

A common technique is for string literals to be put in "read-only-data" section which gets mapped into the process space as read-only (which is why you can't change it).

It does vary by platform. For example, simpler chip architectures may not support read-only memory segments so the data segment will be writable.

Rather than try to figure out a trick to make string literals changeable (it will be highly dependent on your platform and could change over time), just use arrays: char foo[] = "...";. The compiler will arrange for the array to get initialized from the literal and you can modify the array.



Source: W3resource, https://www.w3resource.com/c-programming/c-variable.php
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License.

Last modified: Thursday, July 15, 2021, 3:42 PM