Friday, 8 March 2013

const modifier in c:


const modifier in c:

In c all variables are by default not constant. Hence, you can modify the value of variable by program. You can convert any variable as a constant variable by using modifier const which is keyword of c language.
Properties of constant variable:

1. You can assign the value to the constant variables only at the time of declaration. For example:

const int i=10;
float const f=0.0f;
unsigned const long double ld=3.14L;

2. Uninitialized constant variable is not cause of any compilation error. But you cannot assign any value after the declaration. For example:

const int i;

If you have declared the uninitialized variable globally then default initial value will be zero in case of integral data type and null in case of non-integral data type. If you have declared the uninitialized const variable locally then default initial value will be garbage.

3. Constant variables executes faster than non constant variables.

4. You can modify constant variable with the help of pointers. For example:

#include<stdio.h>
int main(){
    const int i=10;
    int *ptr=&i;
    *ptr=(int *)20;
    printf("%d",i);
    return 0;
}

Output: 20 

Constants



Constants: A constant is a value that does not change during the program execution. As its name implies it is a constant or fixed value. Constants are declared with the ‘const’ keyword. Constants in every language remain same. In some languages, constants are also known as literals. The constants can be classified as integer constant, floating constant, character constant and string constant. For example: const data_type var; 

Integer Constant: An integer is a whole number without decimal point. An integer may be either 0-9 digits or the combination of 0-9. Integer can be further divided into three different number systems: decimal (base 10), octal (base 8) and hexadecimal (base 16).
Octal: 1) It consists of numeric range 0-7 or combination of 0-7 numbers.
2) The first digit must be 0 in order to identify the constant as an octal.
3) For example: 00, 010, and 0200.
Decimal: 1) It consists of any combination of 0-9 digits. It is the most commonly used number system.
2) It is normal counting number of mathematics. The first digit needs not to be zero.
3) For example: 0, 8, 156, 25378 etc.
Hexadecimal: It consists of 0-9 digits and alphabets ‘a’ to ‘f’ or ‘A’ to ‘F’. Here ‘a’ to ‘f’ represents the decimal values 10 to 15. ‘A’ or ‘a’ is 10, ‘B’ or ‘b’ is 11 and so on.
2) A hexadecimal number must begin with 0x or OX.
3) For example: 0x0, 0x10, 0x265 etc.
Rules for integer constants: 1) it may be either positive or negative.
2) It must not have any decimal point.
3) No commas or blank spaces are allowed in an integer.
4) It must have at least one digit.


Floating Constant: A floating point constant has a real value. It is a numeric value with decimal point. It may be either of single precision or of double precision type. It may be written in two forms called the fraction form and exponent form.
Fraction form: In the fraction form, the value is represented as a whole number followed by a decimal point. For example: 130.35, -0.67
Rules for fraction form: 1) a decimal point should be present.
2) No commas or spaces are allowed.
3) It may be +ve or -ve but default is +ve.
Exponential form: In the exponential form a floating point constant has a mantissa part and exponent. The mantissa part and exponent part should be separated by e or E. For example: a number 234.56 can be written as 2.3456e2 or 2.3456e+2 i.e. 2.3456*10^2 Here 2.3456 represents mantissa part and the part after 'e' is exponent part, which has a base 10.
Rules: 1) the mantissa part can be written in positive or negative.
2) Spaces and commas are not allowed.
3) Letter e can be written in uppercase or lowercase.


Character constant: A character constant is a single alphabet, single digit or single symbol which is enclosed in single quotes (' '). Every character constant has a unique ASCII value. For example ASCII value of 'A' is 65. The maximum length of a character constant is only one character. For example: 'a', '&', '2' etc.
A character can be any ASCII character, printable or not printable from values -128 to 127. (But only 0 to 127 are used.) Control characters i.e. non printable characters are put into programs by using a backslash \ and a special character or number. The characters and their meanings are:
\b
backspace BS 
\f
form feed FF (also clear screen) 
\n
new line NL (like pressing return) 
\r
carriage return CR (cursor to start of line) 
\t
horizontal tab HT 
\v
vertical tab (not all versions) 
\"
double quotes (not all versions) 
\'
single quote character ' 
\\
backslash character \ 

These are some certain character constants that start with backslash (\). Such characters are known as escape sequence character. There are predefined in compile and used with output statements. These characters are non printable and have some specific tasks.
String constant: A string constant is a group of characters, which is enclosed in double quotes. So it is also called double quotes character constant. The characters enclosed in double quotes may be alphabet, digits, special symbols, escape sequence and spaces. For example: "This is my pen", "hi...\ this is Vijraj Bhargav", "My mobile number is 9872871471".

Variables


Variables: A variable is a symbolic name of a location in the computer's memory. In this variable, a value can be stored and this can be used in the program. The value of a variable may change during the program execution. Only one value can be stored at a time in it. In a variable you can store integer value or float value, character value or string names according to the requirement. As we know that a variable is an identifier of certain data. Every variable must have a name and a data type. A data type is the type of data which is to be stored in a variable. It can be an integer, floating, character or string name values. These values can be assigned to the variable using assignment operator: = (equal to)
Example of variable: sum=5+10; Here sum is a variable and 5 & 10 are two integer constant values. + And = are two operators. Semicolon (;) is used in the end of every C statement. This tells that it is end of statement.

Rules for naming variables: 1) a variable must be unique in a program’s block.
2) A variable name can contain only alphabets, digits and underscore. No any other symbol can be used.
3) The variable name must begin with a letter or an underscore.
4) A variable name must not start with a digit.
5) C language is a case sensitive language. It means upper case and lower case letters are different variable.
6) A variable name must not contain any white space. The white space includes tab, spacebar and new line.
7) It can’t be a C language keyword or reserve word. As we know that C is a case sensitive language and we can use different case of keywords as variable name.
8) A variable name cannot use a symbol in naming.
9) Some valid examples are: a, box, tab10, roll_no, Roll_no, _abc, For.
10) Some invalid examples are: 10tab, for, ab$c, roll no.
11) The length of variable name should be of 31 characters. If it is more than 31 characters then compiler will not generate any error but it will recognize only first 31 characters.


Where to declare Variables: There are two kinds of place in which declarations can be made.

  1. One place is outside all of the functions. That is, in the space between function definitions. (After the #include lines, for example.) Variables declared here are called global variables. They are also called static and external variables in special cases.)
#include <stdio.h>

int globalinteger;              /* Here! outside {} */

float global_floating_point;

main ()

{
}
2.      The other place where declarations can be made is following the opening brace, {}, of a block. Variables of this kind only work inside their braces {} and are often called local variables. Another name for them is automatic variables.
main ()

 { int a;
   float x,y,z;

 /* statements */

 }

Identifiers


Identifiers: A name or identifier in C can be anything from a single letter to a word. Identifiers are the names given to program elements such as variables, function names, array names and structure names. Once a type or value is assigned to an identifier, it cannot be changed during the execution of the program. For example: if an integer variable named as 'x' then 'x' will always remain an integer variable which can hold only integer or numeric value.

Rules of identifier: 1) an identifier must be unique in a program.
2) An identifier can contain only alphabets, digits and underscore. No any other symbol can be used.
3) The identifier must begin with a letter or an underscore.
4) An identifier must not start with a digit.
5) C language is a case sensitive language. It means upper case and lower case letters are different identifier.
6) An identifier must not contain any white space. The white space includes tab, spacebar and new line.
7) It can’t be a C language keyword or reserve word. As we know that C is a case sensitive language and we can use different case of keywords as identifier.
8) An identifier cannot use a symbol in naming.
9) Some valid examples are: a, box, tab10, roll_no, Roll_no, _abc, For.
10) Some invalid examples are: 10tab, for, ab$c, roll no.

Wednesday, 6 March 2013

Keywords


Keywords: Some words in C programming are reserved for some special tasks. Such words are known as keywords. Their meaning is predefined in the compiler and compiler knows what to do with these keywords. Keywords are also known as the reserved words because these are reserved for C compiler. The programmer cannot use these words as variable or identifier in program. As we know that C is a case sensitive language and we can use different case of the keywords as identifier. The color of the keywords in C language is white. C language has total 40 keywords. 32 keywords are used in routine programming but remaining 8 characters are used in low level programming and rarely used. These 32 keywords are:
auto                else                  long                 switch
break               enum               register                        typedef
case                 extern              return               union
char                 float                 short                unsigned
const                for                   signed              void
continue          goto                 sizeof               volatile
default             if                      static                while
do                    int
               struct               double 

Character set and C-Tokens


Character Set: C is a powerful programming language. Not only C, every language follows some rules. These rules are required for writing a program. In order to write a program some alphabets, digits and special symbols are used. All this is known as Character set. Character set is used to write program blocks, variables and identifiers etc. Character set consists of the upper and lower case alphabets, digits, special characters and white space.
Alphabets: It consists of both upper and lower case alphabets. It can be ‘A’ to ‘Z’ or ‘a’ to ‘z’ or combination of the alphabets. These are used for naming purpose. C language is a case sensitive language. It means that lower and upper case alphabets are different. For example: 'A' is different from 'a'. Both can be used in the program and both can contain different values. But one thing should be noted that same variable or name cannot be used for two purposes. If we do so then it will create a big confusion.
Digits: Digits represent to the numeric values. These are used by the programmer for assigning the values to the variables. Digits are same as the mathematical digits i.e. 0 to 9 or combination of the digits. It consists both of positive and negative numbers. Also it includes decimal point values. The programmer can assign any numeric value depending on the data type. For example: a=5, b=109, c=-69, d=48.56
Special Symbols: On the keyboard all keys are special symbols except alphabets, digits and function keys or hot keys. Such as: &, $, @. All these symbols are used for special purposes.
Tokens: Tokens are various C program elements. Tokens are identified by the compiler during compilation. Tokens are useful for compiler to detect errors. When tokens are not arranged in a particular sequence then compiler generates an error message. These tokens are: Keywords, Identifiers, Variables, Constants and Delimiters or separators

Types of Errors


Types of error: An error is considered as a speed breaker in the process of program developing. This error may be of any type. There are four types of errors.
1) Syntax error, 2) Linking error, 3) Run Time error, 4) Logical error
Syntax error: This error occurs when the rule or syntax of the language is broken. Such errors may be of missing semi colon, missing of any punctuation, misuse of any term etc. For example: c = (a+b/2; Here in example, closing bracket is missing after b.
All syntax errors must be found and corrected before a program is made to run. Almost all the language compilers and interpreters are designed to detect syntax errors. The language processors print error messages on the screen that indicates the number of the statement with errors and gives hints of nature of the error.
Linking error: As we know that each language comes with built in libraries and basic functions. To execute a program we use these basic functions along with the libraries. It is the responsibility of the linker to link the function with the library. If the linker does not find the library then an error is generated. This error is known as linking error. For example: if main() method's library is not defined i.e. stdio.h
Runtime error: It is not sure that after successfully compiling and linking a program, it will display the desired result. The result may be wrong due to errors in logic such as division by zero, square root of a negative number. Runtime errors are harder to find and to correct them. It is because compiler does not tell us about the error. Compiler does not tell the number of line in which the occurred and also not the nature of the error.
Logic Errors: Logic errors are those that appear once the application is in use. They are most often unwanted or unexpected results in response to user actions. In this case the language processor successfully translates the source code into machine code. Also a computer actually does not know that an error has occurred. It follows the instructions and produces the results, but this output may be wrong. For example, a mistyped key or other outside influence might cause your application to stop working. Logic errors are generally the hardest type to fix, since it is not always clear where they originate.