The C language programming. Constant variable & data type.

Introduction 

                    A programming language is designed to help process certain kinds of data consisting of numbers of characters and strings and to provide useful output known as information. The task of processing of data is accomplished by executing a sequence of precise instructions called a program. Thes instructions are formed using certain symbols and words according to some rigid rules known syntax rules(or grammar). Every program instruction must conform precisely to the syntax rule of the language 


The character set 

                    The characters that can be used to form words, numbers, and expressions depend upon the computer on which the program is run. However, a subset of characters is available that can be used on most personal, micro, mini, and mainframe computers. The characters in Care grouped into the following categories:
1. Letters
2. Digits
3. Special characters
4. White spaces

C TOKENS

                    In a passage of text, individual words and punctuation marks are called tokens. Similarly, in a C Program, the smallest individual units are known as C tokens. Chas six types of tokens as shown below. C programs are written using these tokens and the syntax of the language

C TOKENS

Keywords

 Constants

 Strings

 Operators

 Special  

Symbols

 Identifiers

 float

 -15.5

 "ABC"

 + -

 { }

 main 

 while

 100

 "year"

* '

[ ]

 amount

KEYWORDS AND IDENTIFIERS

                    Every C-word is classified as either a keyword or an identifier All keywords have fixed meanings and these meanings cannot be changed. Keywords serve as basic building blocks for program statements. all keyword is written in lower case.
                    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. Both uppercase and lowercase letters are permitted, although lowercase letters are commonly used. The underscore character is also permitted in identifiers. It is usually used as a link between two words in long identifiers.

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

CONSTANTS

                    Constants in C Refer to fixed values that do not change during the execution of a program.

Integer Constants

                    An integer constant refers to a sequence of digits. There are three types of integers, namely. decimal, octal, and hexadecimal. 
123
-321
0
654321 
+78
                    Embedded spaces, commas, and non-digit characters are not permitted between digits. For example.
15 750 
20,000 
$1000 
are illegal numbers.

Real Constants

                    Integer numbers are inadequate to represent quantities that vary continuously, such as distances heights, temperatures. prices, and so on. These quantities are represented by numbers containing fractional parts like 17.548 Such examples of real constants are numbers are called real or floating-point constants. A further example of real constants are : 
0.0083
-0.75 435 36
-2470
                    These numbers are shown in decimal notation, having a whole number followed by a decimal point and the fractional part. It is possible to omit digits before the decimal point, or digits after the decimal point that is
215
95
- 71 
are all valid real numbers. 

                    A real number may also be expressed in exponential (or scientific) notation, For example, the aloe 215 65 may be written as 2565e2 in exponential notation, e2 means multiply by 102. The general form is mantissa e exponent 
7500000000 may be written as 7.5E9 or 75E8. Similarly - 0000000368 is equivalent to -3.68E-7,

Constant         Valid                      Remarks

698354L

Yes

Represents a long integer

25,000

No

Comma is not allowed 

+5.0E3

Yes

(ANSI C supports unary plus)

3.5e-5

Yes

 -

7.1e 4

No

No white space is permitted

-4.5e-2

Yes

 -

1.5E+2.5  

No

Exponent must be an integer

$255   

No

$ the symbol is not permitted

0X7B

Yes

Hexadecimal integer

Single Character Constants

                    A single character constant (or simply character constant) contains a single character enclosed within a pair of single quote marks. Examples of character constants are:
                    '5', 'X', ';', ' ' 
                    Note that the character constant '5 is not the same as number 5. The last constant is a blank space
                    Character constants have integer values known as ASCII values Tthe statement would print the number 97 the ASCII value of the letter a. Similarly, the statement
                    printf("%d", 'a');
                    Printf("%c", 97);
                    Since each character constantly represents an integer value it is also possible to perform arithmetic operations on character constants.

String Constants

                    A string constant is a sequence of characters enclosed in double-quotes. The characters may be letters, numbers, special characters, and blank space. Examples are:
"Hello!"
"1987
"WELL DONE
"2..."
"5+3"
                    Remember that a character constant (e.g., X) is not equivalent to the single-character string constant (eg, "X"). Further, a single character string constant does not have an equivalent integer value while a character constant has an integer value. Character strings are often used in programs to build meaningful programs. 

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 different values at different times during execution.

                    As mentioned earlier, variable names may consist of letters, digits, and the underscore character, subject to the following conditions:
1. They must begin with a letter. Some systems permit underscore as the first character.
2.ANSI standard recognizes a length of 31 characters. However, the length should not be normally more than eight characters, since only the first eight characters are treated as significant by many compilers.
3. Uppercase and lowercase are significant. That is the variable Total is not the same as total or TOTAL
4. The variable name should not be a keyword.
5. White space is not allowed.

Variable name                     Valid?                    Remark

First_Tag                    

Valid

-

char                            

Not Valid

Char is a keyword

Price$                         

Not Valid

Dollar sign is illegal

group one                  

Not Valid

Blank space is not permitted

average_number         

Valid

First eight characters are significant

int_type                      

Valid

Keyword maybe part of a name

DATA TYPES

                    C language is rich in its data types. Storage representations and machine instructions to handle constants differ from machine to machine. The variety of data types available allows the programmer to select the type appropriate to the needs of the application as well as the machine.

ANSI C supports four classes of data types:

1. Primary (or fundamental) data types
2. User-defined data types
3. Derived data types
4. Empty data set

Primary data type 

                    Predefine data type in the programming languages even knows as a fundamental data type or in-build data type.
                    All C compilers support four fundamental data types, namely integer (int), character (char).
floating-point (float), and double-precision floating-point (double).

User-Defined Type Declaration

                    Supports a feature is 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;
                    Where type refers to an existing data type and "identifier" refers to the "new" name given to the data type. The existing data type may belong to any class of type, including the user-defined ones. Remember that the new type is 'new' only in name, but not the data type. typedef cannot create a new type. 
Some examples of type definition are:
typedef int units; 
typedef float marks:
                    Here, units symbolize int, and marks symbolize float. They can be later used to declare variables as follows:
units batch 1, batch 2; 
marks name 1.50], name 2.50]:

Derived data types

                    Derived data types are nothing but primary datatypes but a little twisted or grouped together like an array, structure, union, and pointer.

Empty data set

                    The void is an empty data type that has no value. This can be used in functions and pointers

Declaration of Storage Class

                    Variables in C can have not only data type but also storage class that provides information about their location and Visibility. The storage class decides the portion of the program within which the variables are recognized.
Example of storage classes
int x;
main()
    {
        int m;
        float i;
        .....
        .....
        function1():
    }
function 1()
    {
        int n:
        float j;
        .....
        .....
    }
                The variable x which has been declared before the main is called a global variable. It can be used in all the functions in the program. It need not be declared in another function. A global variable also is known as an external variable.
                The variables i, j, m, and n are called local variables because they are declared inside a function. Local variables are visible and meaningful only inside the functions in which they are declared. They are not known to other functions. Note that the variable i has been declared in both the functions. Any change in the value of in one function does not affect its value in the other

Storage class                        Meaning

Auto

A local variable is known only to the function in which it is declared. The default is auto.

Static                                

Local variable exists and retains its value even after the control is transferred to the calling function. 

Extern                              

Not Valid

Register                            

Not Valid

Assignment Statement

                    Values can be assigned to variables using the assignment operator = as follows:
variable_name = constant;
Further examples are.
initial_value =0; 
final_value = 100;
balance = 75.84;
yes = 'x';
                    C permits multiple assignments in one line are valid statements. For example 
Initial_value = 0; final_value = 100; 
                    An assignment statement implies that the value of the variable on the left of the equal sign is set equal to the value of the quantity (or the expression) on the right.
x = x + 1; 
means that the new value of bear is equal to the old value of bear plus !.
                    During assignment operation. C converts the type of value on the right-hand side to the type on the left. This may involve truncation when the real value is converted to an integer. possible to assign a value to a variable at the time the variable is declared. This takes It is also the following form:
data-type variable_name = constant;
Some examples are:
int final_marks = 100; 
char yes  = 'true';
double balance = 98.45;

Declaring a Variable as Constant

                    We may like the value of certain variables to remain constant during the execution of a program We can achieve this by declaring the variable with the qualifier const at the time of initialization, Example:
const int class_size = 40;
                    const is a new data type qualifier defined by ANSI standard. This tells the compiler that the value of the int variable class_size must not be modified by the program. However, it can be used on the right-hand side of an assignment statement like any other variable.

Declaring a Variable as Volatile

                    ANSI standard defines another qualifier volatile that could be used to tell explicitly the compiler that a variable's value may be changed at any time by some external sources (from outside the program). For example:
volatile Int date;
                    The value of data may be altered by some external factors even if it does not appear on the left-hand side of an assignment statement.
                    When we declare a variable as volatile, the compiler will examine the value of the variable each time it is encountered to see whether any external alteration has changed the value. Remember that the value of a variable declared as volatile can be modified by its own program as well If we wish that the value must not be modified by the program while it may be altered by some other process, then we may declare the variable as both const and volatile as shown below:
volatile const Int location = 100;

Defining symbolic constant 

                    The #define directive allows the definition of macros within your source code. These macro definitions allow constant values to be declared for use throughout your code.
#define
symbolic-name value of constant
Valid examples of constant definitions are:
#define STRENGTH     100
#define PASS MARK      36
#define MAX      360
#define PI     3.14
                    Rules for using define in c language 
1. Symbolic names have the same form as variable names. 
2. No blank space between the pound sign #and the word define is permitted.
3. # must be the first character in the line.
4. A blank space is required between #define and symbolic name and between the symbolic name and the constant. 
5. #define statements must not end with a semicolon.
6. After definition, the symbolic name should not be assigned any other value within the program by using an assignment statement. For example, STRENGTH = 200; is illegal.
7. Symbolic names are NOT declared for data types. Its data type depends on the type of constant. 8.#define statements may appear anywhere in the program but before it is referenced in the program.

Statement                          Validity             Remark

#define X = 2.5

Invalid

'=' sign is not allowed

#define MAX 10

Invalid

No white space between # and define

#define N 25:

Invalid

No semicolon at the end

#define N 5, M 10

Invalid

A statement can define only one name.

#Define ARRAY 11

Invalid

define should be in lowercase letters

#define PRICE$ 100

Invalid

$ symbol is not permitted in the name


Hope this helps you in finding your answer. 
Thank you.

~Aaditya Rasala

Comments

Post a Comment

Popular Posts