Tuesday 30 September 2014

Structure of C

Structure of C language

Before stating in c programming,we must to know structure of c. it is very important. 
                 C program is a collection of one or more functions. Every function is a collection of statement and perform some specific task. The general structure of C.


Comments

preprocessor 
global variable
main() function
{
local variable
statements
...............
..............
................
}


function( )

{
local variables
statements
................
...............
}


function 2 ( )

{
local variables
statements
........
........
...........
}



Comment can be placed anywhere in a program and are enclosed between the delimiter /* and */ . this is called single line comment. // this is called multiple line comment. comment are generally used for documentation purposes.


prepossor directive are processed through prepocessor before the C source code passes through compiler. The commonly used preposser directive are #include and #define. # include is used for including header files. #define is used to define symbolic constants and macros.

Every C program has one or more functions.if a program has only one function then it must be main( ). Execution of every C program starts with main( ) function. it has two parts, declaration of local variables and statements. The scope of the local variable is local to that function only. Statement in the main () function are executed one by one. Other function are the user defined functions, which also have local variable and C statements.they can be defined before or after main(). It may be possible that some variable have to be used in many function, so it is necessary to declare them globally. These variable are called global variables.

Sunday 28 September 2014

Variable in C language

What is variable ?


Variable is the name or user define name that can be used to store values. Variable can take different type of values but it stores only one value at a time. These values can be changed during the execution of program. A data type is associated with each variable. The data type of the variable decides what values it can take or store.

Rule for naming variable-
1     
       The name should be consist of only alphabets (both upper and lower          case),digits and underscore sign.
     
        First character should be alphabets and underscore.
     
        The name should not be keyword.
   
        Since c is the case sensitive, the upper case and lower case are considered         different for e.g. Apple, APPLE both different.

Declaration of variable


We know that what is variable, it must to declare a variable before it is used in the program. Declaration of a variable specifies its name and data type. The type and range of values that a variable can store depends upon its data type.
Initialization of Variable-
When a variable is declare it contain undefined value known as garbage value.If we want we can assign some initial value to the variable during the declaration of variable. this is called initialization of the variable. for example: 
float x=8.9, y=98.0

Syntax for declaration of variable-

Data type <variable name ;>
Or
Data type <variable name1, variable name2 … n ;> 

For example-
                  
                   Int x;                       // declare variable named ‘x’

                   Float salary               // declare variable ‘salary’
                  
                   Char grade               // ‘grade’ is the variable

Here x is a variable of type int. this mean that x will take  the value of integer such as 12, 3, 4,etc. 
salary is a variable of type float, it take a value type of float like 3421.543 etc. and grade is a variable of type char. char means character it takes the value if character such as A, a, B, b, C,c etc.

declaration of mare than one variable-

int x,y,z,total; 

Here x, y,z and total  are all variable and type of variable is int / integer because all variable declare with int data type after declare variable we must terminate the variable with semicolon. in the upper example x, y,z,total are 4 variable x y z and total separate by the comma and variable declaration end by (;)semicolon. if we are not terminate the variable with semicolon our program generate a error. 



int a=5;
here a=5 means that 5 is assign to the variable a, if we write 'a' in all the program, 'a' means we write 5. 'a' and 5 both are equal after assigning the value. other initialization....

here x value is 8.9 and y value is 98.0 if we write x then its equal to 8.9 and if we write y then it is equal to 98.0.
char ch='y'
double num =0.4324;
int l,m,u,totle=0;
in the declaration only one variable is initialize.



What is Data type

What is Data types?

In the C Programming language a data type is classification identifying one of various type of data. such as Int, float, char, double, long etc. in other words we can say that  a data type in a programming language is a set of data with value having predefined characteristics. A data type is the identification of data which is input by user, data type describe the program, which type of data input by end user and which type of the operation perform on the input data.Result are depend on data type, such as  if user want to add two number 5 and 6, declare variable in the float data type then result will be in floating 11.0 if in the integer then only 11. data type play an important role in the program. it is a responsible for storage on the memory. storage representation of these data types is different in memory. there are four fundamental data types in c. which are int char , float , and double.
            char is used to store any single character, int is used to store integer value , float is used for storing single precision floating point number and double is used for storing double precision floating point number. we can use type qualifier with these basic types to get some more type.

There are two type of type qualifiers-
1. Size qualifier  ------- short , long
2. sign qualifier -------- signed, unsigned

when qualifier unsigned is used, the number is always positive, and when signed is used, number may be positive or negative. if the sign qualifier is not mentioned , then by default signed qualifier is assumed. the range of values for signed data type is less than that of unsigned type. this is because in sign type, the leftmost bit is used to represent the sign, while in unsigned type this bit is also used to represent the value.
 Range of data type given below--

Data type
Type of qualifier
size
Range




char
char or signed char
1
-128 to 127

char
unsigned char
1
0 to 255
int
int or signed int
2
-32768 to 32767

unsigned int
2
0 to 65535

short int  or signed short int
1
-128 to 127

unsigned short int
1
0 to 255

long int or signed long int
4
-2147483648 to 2147483647

unsigned long int
4
0 to 4294967295
float
float
4
3.4E-38 to 3.4E+38

double
double
8
1.7E-308 to1.7E+308

Long double
10
3.4E-4932 to1.1E+4932

Saturday 27 September 2014

What is Constants

What is Constants in C?

Constant is a value that cannot be changed during execution of the program. Constant is user define value, which is input by the user and cannot be changed during the program of execution.

There are three type of constants-
  1.        Numeric Constants : Numeric Constants are those constants which consist of digits, they may or may not have decimal point(.).
  •   Numeric constant should have at least one digits.
  •  No comma or space is allowed within the numeric constant.
  •  Numeric constants can either be positive or negative but default sign is always        positive.
  •   Decimal constants: e.g. 0,1,2,3,4,5,6,7,8,9. its base is 10. 
  •   Octal constants:   e.g. 0,1,2,3,4,5,6,7. its base is 8.
  •   Hex decimal : e.g. 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,a,b,c,d,e,f. its base is 16.
    3. String constant : A string constant has zero, one or more than one character. A string constant is enclosed within double quotes (" "). At the end of string,\0 is automatically placed by the compiler. Some example of String constants are-
           "blog"
       Rules for defining numeric constants-


  There are two type of numeric constants-

          Integer constant : Integer constants are whole numbers which have no decimal                   point (.). There are three type of integer constants based on different number   systems. 

           Some valid decimal integer constants are -
              0
              123
              1234
              908654
              34456778

           Some invalid decimal integer constants are-
              2.3                illegal character(.).
              4#33             illegal character #.
              0444             first digits cannot be zero.only zero should be.
              123 4            space not allow.
              4,545            Comma is not allowed.
           in Octal integer constants, first digits must be 0. e.g.
               0
               01
               03
               0453

           in hexadecimal integer constants, first two characters should be 0x or 0X . eg.
                0x
                0x23
                0x333
                0xA4F1
            By default the type of an integer constants is int.But if the value of integer constants               exceeds the range of value represents by int type, the type is taken to be unsigned               int or long int. 
              e.g.  45235                      Integer constants of type int
                      45456566UL            integer constants of type unsigned long int.
                      6655U                     type of unsigned of int.
               
                I will explain about number System our next blog....

           Real Constants: it is also called floating constants. Floating constants are numeric               constants that contain decimal point. if decimal constant number have  point(.) that                called floating number. some valid floating point constants are-
                0.5
                4.4
                3000.0
                34.89
            for expressing very large or very small real constants, scientific(exponent) form is                  used.in example, number is written in the mantissa and exponent form,which are                  separated by 'e' or 'E'. The mantissa can be an integer or real number, while the                   exponent  can be only an integer (positive or negative). for example 1800000 can                  be written as 1.8e6, here 1.8 is mantissa and 6 is exponent.
             some other example: 2500000000 we write 2.5e9 or 2.5*1000000000
             by default the type of a floating point constant is double.the symbol for recognizing                 is f or F (for float type) l or L( for Long double).

                 2.3e5                floating point constant of type double

                2.4e-9L              floating point of type long double.

                3.5f                    floating point of type float..

    2. Character constants:  A  character constant is a single character that is enclosed                within single quotes.Some valid character constants are-
         
          '3'          'A'          '$'          ' '          '#'

      Some invalid character constants are-
      
      'four'              there should be only one character within quotes.

      "A"                 double quote are not allowed 

       a                   single quote is missing.
   
       ''                   no character between single quotes at least space should be.
  
    Every character constant has a unique integer value associated with it. This integer is the numeric value of the character in the machine's character code. if the machine is using ASCII code then the character 'G' represents integer value 71 its means that if we write 71 it is a equal to 'G' and character 5 represent 53.
   
              ASCII value is 
      
         A - Z                        ASCII value (65 -90)
         
         a - z                         ASCII value (97 -122)

         0 -9                          ASCII value (48 - 57)

         ;                                ASCII value (59)



           "342"

           "3"
           " "
           "i"

           


Element of C language

What is Element of C language?

Every language has basic element and its own grammatical rules. Before starting programming we must to know basic of programming and its element. Elements C are-

1.       Character Set: The character set that are used in C program according to user.

                                                        i.            Alphabets :

 Capital A, B, C, D, E, F, G……………..Z
Small a, b, c, d, e, f, g, h, I……………….z

                                                      ii.            Digits
All the number 0 to 9 are digits e.g. 1, 2, 3, 4,5, 6, 7, 8, 9,0
                                                  
                                                     iii.            Special Characters :  Special characters are used in the program for specific task each character has its own meaning in the program just like & used for address, ++  used for increment, [] used  for array, * sign for pointer etc.

Character
Meaning
+
-
*
< 
> 
(
)
\
{
}
[
]
;
:
?
&
@
$
 Plus sign
                     Minus sign(hyphen)
Asterisk
           Less than sign
                Greater than sign
               Left parenthesis
                Right parenthesis
            Backward slash
    Left braces
      Right braces
      Left bracket
        Right bracket
  Semicolon
                             Colon
           Double quotes
        Single quotes
            Question marks
     Ampersand
   At the rate
  Dollar sign
2.      
      Escape Sequences: Execution character or escape character represented by two characters. The first character is “\”and second character is from the C character set.  Escape character always start with “\” symbol for e.g. \n . these character combination is known as escape character and it is very use full in the c program. C supports some Escape sequence that has its own meaning. Blank, horizontal, vertical tab newline, carriage return, form feed also known as whitespace.

Escape
Sequence
Meaning
ASCII Value
Why use
\b
Back Space
008
Moves the cursor to the previous position of the current line.
\a
Bell alert
007
Generate a beep sound for alert.
\n
New line
010
Move the cursor at beginning of the next line.
\r
Carriage return
013
Move the cursor to beginning of the current line.
\0
null
000
for null
\f
Form feed
012
Moves the cursor to the initial position of the next logical page.
\t
Horizontal Tab
009
moves the cursor to the next horizontal tab. 5 space at a time.
\v
Vertical tab
011
Moves the cursor to the next vertical tab.
\\
backslash
092
Print the character with (\).

3.       Trigraph Character: There is a possibility that the keyboard doesn’t print some character.C support  the facility of “trigraph sequence” to print these characters. This trigraph has three characters. First two are ‘??’ and third character is any character of C character set .

Trigraph
Symbol
??<
??>
??(
??)
??!
??/
??=
??-
??’
{
}
[
]
|
\
#
~
caret

4.     
              Delimiters: Delimiters are used for Syntactic meaning in C.that have a specific task in the                         program.

:                                                       Colon                                                    used for label

;                                                       Semicolon                                             end of statement

( )                                                    Parenthesis                                           used in expression

[ ]                                                    square brackets                                    used for array

{ }                                                    Curly Braces                                        used for block of                                                                                                                                   statement

#                                                       Hash                                                     preprocessor directive

‘                                                       Comma                                                   variable  delimiter

5.       
       Reserved Word /Keyword: Reserved Words are that word which are reserved for doing specific tasks. These words are known as keywords and they have standard, predefined meaning in C.keywords always written in lowercase. there are 32 keywords in c language.
    
    for example. auto, break, case, char, const, continue, default, do, double, else, enum,                          extern, float, for, goto, if, int,long,register, return, short, sizeof, static, struct,                      switch, typedef, union, void, volatile, while.


Identifiers : All the words that we will use in our C programs either keyword or identifiers.keywords are predefined and can't be changed by user, while identifiers are user defined words and are used to give name to entities like variables, array , functions,structure etc. Rules for naming identifier are-

  1. The name should consist of only alphabets (both upper and lower case), digits and underscore sign(_).
  2. first  character should be an alphabet or underscore.
  3. The name should not be a keyword.
  4. Since C is case sensitive, the uppercase and lowercase letters are considered different. e.g CODE , code both are different.
  5. An identifier name may be arbitrarily long. some implementation of c recognize only the first eight character , though most implementation recognize 31 characters. ANSI standard compilers recognize 31 character.
    Some Valid identifier is :  1.  Value
                                                2.  a
                                                3.  net_save
                                                4.  _save
                                                5.  SAVE

  Some Invalid identifier is:  1.  9ab
                                                2. float
                                                3. rd#
                                                4. a b

Note:

  • in the first 9ab, first character should be alphabet or underscore sign so it is invalid identifier.

  •    in second float, float is a keyword so it is invalid.

  • in the third rd#, # is the special character so its also invalid.  

  •  in fourth a b, space is not allow in identifier.