Tuesday, October 27, 2009

C Programming - Constants and Variables

In this tutorial you will learn about Character Set, C Character-Set Table, Special Characters, White Space, Keywords and Identifiers, Constants, Integer Constants, Decimal Integers, Octal Integers, Hexadecimal integer, Real Constants, Single Character Constants, String Constants, Backslash Character Constants [Escape Sequences] and Variables.

Instructions in C language are formed using syntax and keywords. It is necessary to strictly follow C language Syntax rules. Any instructions that mis-matches with C language Syntax generates an error while compiling the program. All programs must confirm to rules pre-defined in C Language. Keywords as special words which are exclusively used by C language, each keyword has its own meaning and relevance hence, Keywords should not be used either as Variable or Constant names.

Character Set

The character set in C Language can be grouped into the following categories.

1. Letters
2. Digits
3. Special Characters
4. White Spaces

White Spaces are ignored by the compiler until they are a part of string constant. White Space may be used to separate words, but are strictly prohibited while using between characters of keywords or identifiers.

C Character-Set Table

Letters


Digits


Upper Case A to Z


0 to 9


Lower Case a to z


.

Special Characters


,


.Comma


&


.Ampersand


.


.Period


^


.Caret

;


.Semicolon


*


.Asterisk


:


.Colon


-


.Minus Sign


?


.Question Mark


+


.Plus Sign


'


.Aphostrophe


<


.Opening Angle (Less than sign)


"


.Quotation Marks


>


.Closing Angle (Greater than sign)


!


.Exclaimation Mark


(


.Left Parenthesis


|


.Vertical Bar


)


.Right Parenthesis


/


.Slash


[


.Left Bracket


\


.Backslash


]


.Right Bracket


~


.Tilde


{


.Left Brace


-


.Underscore


}


.Right Bracket


$


.Dollar Sign


#


.Number Sign


%


.Percentage Sign . .











.

.
.


White Space

1. Blank Space
2. Horizontal Tab
3. Carriage Return
4. New Line
5. Form Feed

Keywords and Identifiers

Every word in C language is a keyword or an identifier. Keywords in C language cannot be used as a variable name. They are specifically used by the compiler for its own purpose and they serve as building blocks of a c program.

The following are the Keyword set of C language.

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

Some compilers may have additional keywords listed in C manual.Identifiers refers to the name of user-defined variables, array and functions. A variable should be essentially a sequence of letters and or digits and the variable name should begin with a character.Both uppercase and lowercase letters are permitted. The underscore character is also permitted in identifiers.

The identifiers must conform to the following rules.

1. First character must be an alphabet (or underscore)
2. Identifier names must consists of only letters, digits and underscore.
3. A identifier name should have less than 31 characters.
4. Any standard C language keyword cannot be used as a variable name.
5. A identifier should not contain a space.

Constants

A constant value is the one which does not change during the execution of a program. C supports several types of constants.


1. Integer Constants
2. Real Constants
3. Single Character Constants
4. String Constants

Integer Constants

An integer constant is a sequence of digits. There are 3 types of integers namely decimal integer, octal integers and hexadecimal integer.

Decimal Integers consists of a set of digits 0 to 9 preceded by an optional + or - sign. Spaces, commas and non digit characters are not permitted between digits. Example for valid decimal integer constants are

123
-31
0
562321
+ 78

Some examples for invalid integer constants are15 750
20,000
Rs. 1000

Octal Integers
constant consists of any combination of digits from 0 through 7 with a O at the beginning. Some examples of octal integers areO26
O
O347
O676

Hexadecimal integer constant is preceded by OX or Ox, they may contain alphabets from A to F or a to f. The alphabets A to F refers to 10 to 15 in decimal digits. Example of valid hexadecimal integers are

OX2
OX8C
OXbcd
Ox

Real Constants

Real Constants consists of a fractional part in their representation. Integer constants are inadequate to represent quantities that vary continuously. These quantities are represented by numbers containing fractional parts like 26.082. Example of real constants are

0.0026
-0.97
435.29
+487.0

Real Numbers can also be represented by exponential notation. The general form for exponential notation is mantissa exponent. The mantissa is either a real number expressed in decimal notation or an integer. The exponent is an integer number with an optional plus or minus sign.


Backslash Character Constants [Escape Sequences]

Backslash character constants are special characters used in output functions. Although they contain two characters they represent only one character. Given below is the table of escape sequence and their meanings.


Constant


Meaning


'\a'


.Audible Alert (Bell)

'\b'


.Backspace


'\f'


.Formfeed

'\n'


.New Line

'\r'


.Carriage Return

'\t'


.Horizontal tab

'\v'


.Vertical Tab

'\''


.Single Quote

'\"'


.Double Quote

'\?'


.Question Mark

'\\'


.Back Slash

'\0'


.Null

Variables

A variable is a value that can change any time. It is a memory location used to store a data value. A variable name should be carefully chosen by the programmer so that its use is reflected in a useful way in the entire program. Variable names are case sensitive. Example of variable names are

Sun
number
Salary
Emp_name
average1


Any variable declared in a program should confirm to the following

1. They must always begin with a letter, although some systems permit underscore as the first character.
2. The length of a variable must not be more than 8 characters.
3. White space is not allowed and
4. A variable should not be a Keyword
5. It should not contain any special characters.

Examples of Invalid Variable names are

123
(area)
6th
%abc

C Programming

C Language

Developed originally at Bell Labs by Ken Thompson and Dennis Ritchie in the second half of the 1980’s, the C Language has become a high-level programming language responsible for almost all operating systems of today. Together with the object-oriented successor of C, C++, these two languages have become commercial software’s first choice in programming language. UNIX runs on C Language and is becoming commercially acceptable on a mass scale.

Venture capital seems to be financing C Language-based software development as it is gaining interest in the job market and receiving support from large corporations and big business markets. Communications and Information Technology are some of the employment opportunities available for the expert C Language programmer.

Like any language learning exercise, the C Language begins with Variables and Constants. These Variables and Constants of basic data types create words and sentences of C, forming the C programming language. A set of instructions and rules for writing in the C Language exists, as is part of any computer programming language. These instructions are explained in online tutorials defining Statements, Expressions, Operators, Managing Input/Output Operations, Strings, Arrays, Functions, Pointers, Dynamic Memory allocation and more.

Using Preprocessor directives, Macros, define identifier string, Simple macro substitution, Macros as arguments, Nesting of macros, Un-defining a macro and File inclusion, the C Language programmer becomes familiar with the terms and functions of this complex programming language. The preprocessor in the C Language is examined in tutorials that describe modifying and reading C Language and discuss efficiency and portability.

C Programming Language - An Overview

In this tutorial you will learn about C Programming Lanuage, Overview of C, Sample program - Printing a message, Executing a C Program and Basic structure of C programs.

Overview of C

C is a programming language. It is most popular computer language today because it is a structured high level, machine independent language. Programmers need not worry about the hardware platform where they will be implemented.

Dennis Ritchie invented C language. Ken Thompson created a language which was based upon a language known as BCPL and it was called as B. B language was created in 1970, basically for unix operating system Dennis Ritchie used ALGOL, BCPL and B as the basic reference language from which he created C.

C has many qualities which any programmer may desire. It contains the capability of assembly language with the features of high level language which can be used for creating software packages, system software etc. It supports the programmer with a rich set of built-in functions and operators. C is highly portable. C programs written on one computer can run on other computer without making any changes in the program. Structured programming concept is well supported in C, this helps in dividing the programs into function modules or code blocks.

Sample program-1
Printing a message
Consider the following message

#include
main()
{
...../* Printing begins here */
.....printf (“C is a very good programming language.”);
...../* Printing ends here */
}

The first line is a preprocessor command which adds the stdio header file into our program. Actually stdio stands for standard input out, this header file supports the input-output functions in a program.

In a program, we need to provide input data and display processed data on standard output – Screen. The stdio.h header file supports these two activities. There are many header files which will be discussed in future.

The second line main() tell the compiler that it is the starting point of the program, every program should essentially have the main function only once in the program. The opening and closing braces indicates the beginning and ending of the program. All the statements between these two braces form the function body. These statements are actually the C code which tells the computer to do something. Each statement is a instruction for the computer to perform specific task.

The /* .... */ is a comment and will not be executed, the compiler simply ignores this statement. These are essential since it enhances the readability and understandability of the program. It is a very good practice to include comments in all the programs to make the users understand what is being done in the program.

The next statement printf() statement is the only executable line in the above sample program. The printf() function is a standard inbuild function for printing a given line which appears inside the double quotes. Therefore in the standard output device we can see the following line

C is a very good programming language.

The next line is again a comment statement as explained earlier. The closing brace indicates the end of the program.

Executing a C Program

The following basic steps is carried out in executing a C Program.

1. Type the C lanuage program.

2. Store the program by giving a suitable name and following it with an extension .c

3. Compile the program

4. Debug the errors if any, that is displayed during compile.

5. Run the program.