Constants, Variables, and Data Types Character Set
The characters in C are grouped into the following categories:
1. Letters --- Uppercase A---Z Lowercase a---z 2. Digits ----All decimal digits 0---9
3. Special characters---- ,? : ; “ % etc
4. White spaces –--- Blank space, Horizontal tab, New line
C token:
In a C program the smallest individual units are known as C token. C has six type of C token.
float ,while
“ABC” “year” + - * / sum, total [ ] {}
-15.5, 100, 3.14
Keywords: Every C word is classified as either a keyword or and an identifier. All keywords have fixed meanings and these meanings cannot be changed. Keywords serve as basic building blocks for program statements. Some compilers may use additional keywords. Keywords are as follows:
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
Identifiers: Identifiers refer to the names of variables, functions and arrays. These are user defined names and consist of a sequence of letters and digits with a letter as a first character.
C TOKENS
Identifiers
Constants
Special symbol Keywords
Operators Strings
Rules for Identifiers:
->First character must be an alphabet (or underscore) ->must consist of only letters, digits or underscore.
->Only first 31 characters are significant ->Cannot use a keyword
->Must not contain white space
Constants:
Constant in C refer to fixed values that do not change during the execution of a program. C supports several types of constants as in fig(2).
Constants
Numeric constants Character constants
Integer constants Real constants Single character String constants Decimal,octal,hexadecimal 0.005, 2.5*10-23 ‘A’ ‘4’ constants “A” “Hello”
Backslash character constants
C supports some special backslash character constants that are used in output functions.
Constant Meaning
\a Audible alert bell
\b Back space
\f Form feed
\n New line
\r Carriage return
\t Horizontal tab
\v Vertical tab
\’ Single quote
\” Double quote
\? Question mark
\\ Backslash
\0 Null
Variables:
A variable is a data name that may be used to store a data value. Unlike constants that remain unchanged during the execution of a program, a variable may take values at different times during execution.
Rules for variables:
->First character must be an alphabet (or underscore) ->Must consist of only letters, digits or underscore.
->Only first 31 characters are significant ->Cannot use a keyword
->Must not contain white space
->Uppercase and lowercase are significant
Data types:
A data type in a programming language is a set of data with values having predefined characteristics.
A particular kind of data item, as defined by the values it can take, the programming language used, or the operations that can be performed on it.
The data type of a value (or variable in some contexts) is an attribute that tells what kind of data that value can have.
ANSI C supports three classes of data types.
->Primary data types ->Derived data types ->User defined data types
Primary data types
Integer type Character type floating point type void type signed unsigned
char signed char
unsigned char int
short int long int
unsigned int
unsigned short int unsigned long int
float double long double
void
The details about basic data types are as follows:
Data types Size (bits) Range of values
void 0 0
char 8 bits -128 to 127
int 16 bits -32768 to 32,767
float 32 bits 3.4e-38 to 3.4e+38
double 64 bits 1.7e-308 to 1.7e+308
Void Types: The void type has no values. This is usually used to specify the type of functions.
The type of a function is said to be void when it does not return any value to the calling function.
Character Types (char) :A single character can be defined as a character (char) type data.
Characters are usually stored in 8 bits of internal storage. The qualifier signed and unsigned may be explicitly applied to char. While unsigned char has values between 0 to 255, signed char has values from -128 to 127.
Integer Types (int): Integers are whole numbers with a range of values supported by particular machine. If we use a 16-bits word length, the size of the integer value is limited to the range -32768 to + 32767( that is,- 215 to +215-1)A signed integer uses one bit for sign and 15 bits for magnitude of the number. Programming language C has three classes of integer storage, namely short int, int and long int, in both signed and unsigned form. Modified data type long int can store an integer ranging from -2,147,483,648 to 2,147,483,647.
Floating Point Types (float): Floating point numbers are stored in 32 bits (on all 16 or 32 bits machine, with 6 digits of precision. When the accuracy provided by a float number is not sufficient, the type double can be used to define the number. A double data type number uses 64 bits giving precision of 14 digits. These are known as double precision numbers. Remember that double type represents the same data type that float represents, but with a greater precession. To extend the precision further, we may use long double which uses 80 bits.
Size and Range of basic and modified data types of 16 bits Machine are as follows:
Type Size (bits) Range
char or signed char 8 -128 to 127
unsigned char 8 0 to 255
int or signed int 16 -32768 to 32767
unsigned int 16 0 to 65535
short int or signed short int 8 -128 to 127
unsigned short int 8 0 to 255
long int or signed long int 32 -2,147,483,648 to 2,147,483,647
unsigned long int 32 0 to 4,294,967,295
float 32 3.4e -38 to 3.4e +38
double 64 1.7e- 308 to 1.7e +308
long double 80 3.4e - 4932 to 1.1e + 4932
User defined type declaration:
C supports a feature known as “type definition” that allows users to define an identifier that would represent an existing data type. The user defined data type identifier can later be used to declare variables. It takes the general form:
typedef type identifier;
For example:
typedef int unit;
typedef float unit;
The main advantage of typedef is that we can create meaningful data type names for increasing readability of the program.
Storage classes and their meaning:
Storage class meaning
auto Local variable known only to the function in
which it is declared. Default is auto.
static Local variable which exists and retains its
value even after the control is transferred to the calling function
extern Global variable known to all functions in the
file
register Local variable which is stored in the register
Static and external (extern) variables are automatically initialized to zero. Automatic (auto) variables contain undefined values (known as garbage) unless they are initialized explicitly.
Defining symbolic constant:
If we would like to use the same constant value in several place, would face to the following problems:
->Problem in modification of the program ->Problem in understanding of the program Symbolic constant is defined as follows:
#define symbolic-name value of constant For examples:
#define PI 3.14
#define NUM_STU 50
Declare variable as constant:
We may like the value of certain variables to remain constant during the execution of a program.
Example:
const int a=40;
After declared, it is not possible for the following expressions.
a++;
++a;
Declare variable as volatile:
A variable’s value may be changed at any time by some external sources.
Example:
volatile int a=40;
After declared, it is possible for the following expression.
a++;
++a;