Variables, Datatypes and Elements in C

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 <space> type-specifier <space> variable-name,…

The Storage-class-specifier can be one of the following:

typedef The symbol name “variable-name” becomes a type-specifier of type “type-specifier”.
No variable is actually created, this is merely for convenience.
Extern Inticates that the variable is defined outside of the current file.
This brings the variables scope into the current scope. No variable is actually created by this.
Static Causes a variable that is defined within a function to be preserved in subsequent calls to the function.
Auto Causes a local variable to have a local lifetime (default).
register Request that the variable be accessed as quickly as possible. This request is not guaranteed.
Normally, the variable’s value is kept within a CPU register for maximum speed.

e.g. int I,j,k;

float x,y,z;

char ch;

 

Constants

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.

 

The const keyword is to declare a constant, as shows below:

int const a = 1;

const int a = 2;

Note:

  • You can declare the const before or after the type.
  • It is usual to initialize a const with a value as it cannot get a value any other way.

The preprocessor #define is another more flexible method to define constants in a program.

 

Datatypes

Datatypes Details Format Specifier & Size Range
char, signed char Variable is large enough to store a basic character in character set.
May be either signed or nonnegative
%c 8 bits -128 to 127
unsigned char Same as char, but unsigned only %c 8 bits  0 to 255
short, signed short, short int, signed short int Defines a short signed int. %d 8 bits or 16 bits  -128 to 127

Or

-32,768 to 32,767

unsigned short, unsigned short int Defines an unsigned short integer. %d 8 bits or 16 bits  0 to 255 or

0 to 65,535

int, signed int Defines a signed integer %d 16 bits  -32,768 to 32,767
unsigned int Same as int, but unsigned values only. %u 16 bits  0 to 65,535
long, signed long, long int, signed long int Defines a long signed integer. %ld 32 bits  -2.147,483,648 to 2,147,483,647
unsigned long, unsigned long int Same as long, but unsigned values only. %lu 32 bits  0 to 4,294,967,295
Float A floating-point number. %f 32 bits  1.17549435 * (10^-38) to 3.40282347*(10^+38)
Double A more accurate floating-point number than float. %lf 64 bits  2.2250738585072014 *(10^-308 to 1.7976931348623157 * (10^+308)
long double Increase the size of double. %Lf 80 bits  3.4 * (10^-4932) to 1.1 * (10^4932)

 

Note: Size of datatype depends on different implementation of c by different compilers.

 

C Elements

Tokens

                In a C source program, the basic element recognized by the compiler is the “token”.

A token is source-program text that the compiler does not break down into component elements.

Tokens can be keyword, identifier, constant, string-literal, operator, punctuator.

Identifiers

“Identifiers” or  “symbols” are the names you supply for variables, types, functions and labels in your program.
Identifier names must differ in spelling and case from any keywords.

  • Must start with alphabet (a..z A…Z) or underscore(_).
  • Next letter can be alphabet (a…z A…Z), underscore(_) or digit.
  • You cannot use keywords as identifiers.

Literals

Invariant program elements are called “literals” or “constants”. The terms “literal” and “constant” are used interchangeably here. Literals fall into four major categories:

  • integer
  • character
  • floating-point
  • string literals

A “string literal” is a sequence of characters from the source character set enclosed in double quotation marks (“ “). 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.

e.g.

  • 157 // integer constant
  • 0xFE // integer constant
  • ‘c’ // character constant
  • 2 // floating constant
  • 2E-01 // floating constant
  • “dog” string literal

Constants

                 A “constant” 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.

Keywords

                “Keywords” are words that have special meaning to the C compiler.

An identifier cannot have the same spelling and case as a C keyword.

The C language uses following keywords:

 

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

 

Comments

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.

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.

Punctuation and Special Characters

                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.

Punctuator: one of [ ] ( ) { } * , : = ; … #

Leave a Reply

Your email address will not be published. Required fields are marked *