C Constants, Variables and Data Types

C is a structured programming language developed at AT&T’s Bell Laboratories of USA in 1972. It was designed and written by Dennis M. Ritchie. In the late seventies, C began to replace the more familiar languages of that time like PL/1, ALGOL etc. Possibly, C seems so popular because it is reliable, simple, and easy to use.

An international committee developed ALGOL 60 language, which is used to program all type of applications such as commercial applications, scientific applications, system applications and so on. However, ALGOL 60 never became popular because it was too abstract and too general. To reduce this abstractness and generality, a new language called Combined Programming Language (CPL) was developed at Cambridge University. However, CPL turned out to be so big, having so many features, that it was hard to learn and difficult to implement.

Basic Combined programming Language (BCPL), developed by Martin Richards at Cambridge University to solve the problems of CPL. But unfortunately it turned out to be too less powerful and too specific. Around the same time a language called “B” was written by Ken Thompson at AT&T’s Bell labs, as a further simplification of CPL. But like BCPL, B is also too specific. Finally, Ritchie inherited the features of B and BCPL, added some of his own stuff, and developed “C”.

FEATURES OF C

  • Robust language, which can be used to write any complex program.
  • Has rich set of built-in functions and operators.
  • Well-suited for writing both system software and business applications.
  • Efficient and faster in execution.
  • Highly portable.
  • Well-suited for structured programming.
  • Dynamic Memory Allocation

CONSTANTS

A constant is an entity (memory location) whose value does not change during the program execution. Constants are either created literally or via the #define statement.

58, 344(Integer literal constants)
‘P’, ‘C’, ‘7’ (Character literal constants)
"The Geek Diary” (String constant)

A string constant is always stored in an array with multiple bytes and ends with a special character ‘\0’ (Backslash Zero). This character also called a null character, acts as a string terminator.

Symbolic constants

Symbolic constants are usually written in uppercase to differentiate them from variables.

#define TRUE 1
#define MAX_LINE 1000
#define NULL ‘\0’

Expressions consisting entirely of constant values are called constant expressions.

128 + 7 – 17

Variables

A variable is an entity used by the program to store values used in the computation. Variable names are the names (labels) given to the memory location where different constants are stored. The type of variable depends on the type of constant that it stores.

Rules for forming variable names:

  • It should begin with a letter or underscore ( _ ).
  • Followed by any combination of letters, underscores or the digits 0-9. For example: sum, piece_flag, _sys_flag (Valid names). 8name, price$, tel# (Invalid names)
  • The uppercase and lowercase letters are distinct in C; the variable names “Sum” and “SUM” refer to different variables.
  • The length of a variable name depends on the compiler.
  • No commas or blanks are allowed within a variable name.

Data types and sizes

Integers

The allowable range for integer (int) in a 16-bit (2 bytes) computer is -32768 to +32767. For a 32-bit (4 bytes) computer, of course, the range would be much larger. In Integer (2 bytes), the 16th bit is used to store the sign of the integer (1 – if the number is negative, 0 – if it is positive).

int i ;
int p = 320, r = -100;

There are a few qualifiers that can be applied to these basic types. short and long, which will vary the size of the variable, signed and unsigned, which varies the range. A long integer (long int) would occupy 4 bytes of memory, which is double the size of int on a 16-bit environment. The value of long integer can vary from -2147483648 to +2147483647. short int will be same as int.

short int i;
long int abc;
long xyz; /* same as long int xyz */

An unsigned integer is one, which cannot store negative values. The most significant bit will be utilized for storing the value and not used for storing the sign. The value will range from 0 to 65535 on a 16-bit environment. A signed int is same as int. A long unsigned int, which has range of 0 to 4294967295, occupies 4 bytes of memory. By default, a long int is a signed long int.

unsigned int ui;
unsigned long ulMemAdd;

Floating Point or Real Numbers

Floating point numbers or Real numbers could be written in two forms, fractional form and exponential form. The value can be positive or negative. Default sign is positive. No commas or blanks are allowed. In the exponential form representation, the real constant is represented in two parts. The part appearing before ‘e’ is called mantissa, whereas the part following ‘e’ is called exponent.

The first type of floating point number is float, which is a single precision real number, occupies 4 bytes.

float p = 3.2e-5;
float j = 4.1e98, k = 34.65F;

A double precision real number, double occupies 8 bytes. If situation demands usage of real numbers that lie even beyond the range offered by double data type, then there exists a long double that occupies 10 bytes.

double d = 5.6e+34;
long double dHigh = 3.4E-65;

Character

A character (char) data type stores a single alphabet, a single digit or a single special symbol enclosed within single inverted commas.

char chOld = ‘A’, chNew = ‘a’;
char flag = ‘\n’, spec = ‘*’;

A character can be either signed or unsigned both occupying 1 byte each, but having different ranges. A signed char is the same as ordinary char and has a range from -128 to +127; whereas unsigned char has a range from 0 to 255.

String

String in “C” is a group or array of characters enclosed in double-quotes. C compiler automatically puts a NULL character, ‘\0’ character, at the end of every string constant. The ‘\0’ is a string terminator. A string containing no characters is a NULL string.

char coName[] = “PCS”      P C S \0

Declarations

All the variables/constants must be declared before use. A declaration specifies a type, and contains a list of one or more variables of that type.

int nCount, nLow, nHigh;
char c;

Escape Characters

These are non-graphic characters including white spaces. These are non-printing characters and are represented by escape sequences consisting of a backslash (\) followed by a letter.

Format Control Strings

Related Post