Learning basics in C program

Learning basics in C program

Hello everyone, today in this blog we would be learning about the basics of C programming.

The C language is one of the powerful programming languages used to write computer programs for variety of applications. It was developed by Dennis Ritchie at Bell Laboratories during the 1970's. Many operating system programs for the latest computers and compilers are written using C language.

Character Used in C

Alphabets: Upper case letters A to Z Lower case letters a to z

Numbers: 0 to 9

Special characters:

  + plus                    , comma              < lesser than
  - minus                   . full stop          > greater than
  * asterisk                ; semicolon          = equal to
  / slash                   : colon              ( open paranthesis
  \ back slash              ' apostrophe         ) close paranthesis
  % percent                 " double quote       [ open bracket
  | vertical bar            & ampersand          ] close bracket
  ~ tilde                   # hash               { open set bracket
  ? question mark           $ dollar             } close set bracket
  ! exclamation mark        ^ caret              _ underscore

Identifiers

An identifier is a name having a few letters, numbers and special character _(underscore). It is used to identify a variable, function, symbolic constant and so on. An identifier can be written with a maximum of 31 characters.

Keywords or Reserved Words

C language uses the following keywords which are not available to users to use them as variables/function names.

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

Tokens

C program has punctuation marks, keywords and operators as the smallest individual units and are referred to as C tokens. Following six types of token are used in C languages:

    C tokens                         Example

(i)    keywords                  auto, break, etc.
(ii)   constants                 -25, 3.15, etc
(iii)  identifiers               calcu_avg, sum, etc.
(iv)  string literals            "Total Amount Rs."
                                 "sum="
(v)   operators                  +, -, * etc
(vi)  separators                 '  ;  :

Constants

Any unchanged value in a program during the program execution is called constant. Following are the constants in C language.

1. Numeric Constants

There are two types of numeric constants.

(i) Integer constant: An integer constant is signed or unsigned whole number.

Example: -25 +350 42

(ii) Real or floating point constant: Any signed or unsigned number with fractional part is called real or floating point constant. A real constant can be written in decimal or exponential form.

Example

(a) decimal form

0.254, +342.0, -3.15

(b) exponential form

0.218e6 means 0.218 10^6 0.42e-32 means 0.42 10^-32 -1.0e12 means -1.0 * 10^12

2. String or Character Constant

Any string of characters enclosed in apostrophes or quotes is called string constant or character constant.

(i) Single character string constant: Any letter or characters enclosed in single apostrophe is called single character string constant.

Example: 'y' '$' '+'

(ii) String of characters constant: Any string of characters consisting of letters, digits and symbols enclosed in double quotes is called string of characters constant.

Example:

"Total value is"

"Chennai - 600 041"

"Average ="

Variables

A variable is an identifier or a name which is used to refer a value and this value varies or changes during the program execution. A variable is written with a combination of letters, numbers and special characters _(underscore) with the first letter being an alphabet. Maximum of 31 letters can be used to write a variable.

Example:

x

fact

c22

total_value

Variable Declaration

Variables used in a C program are declared with appropriate data types. C language permits the user to assign initial values to the variables while declaring them.

Basic Data Types

There are four basic data types in C language. They are given below along with number of bytes occupied by them in the computer memory (RAM).

 Data type       Bytes occupied in RAM       Value range       Value range in decimal
   char                1 byte               -2^7 to 2^7-1           -128 to 127
   int                 2 bytes              -2^15 to 2^15-1         -32768 to 32767
   float               4 bytes              -2^31 to 2^31-1         3.4e-38 to 3.4e+38
   double              8 bytes              -2^63 to 2^63-1         1.7e-308 to 1.7e+308

char refers to character

int refers to integer

float refers to floating point or real number

double also refers to floating point or real number

Additional Data Types

Data type              Bytes occupied in RAM              Value range              Range of values referred
unsigned char                 1 byte                      0 to 2^8-1                0 to 255
short int                     1 byte                      -2^7 to 2^7-1             -128 to 127
unsigned short int            1 byte                      0 to 2^8-1                0 to 255
unsigned int                  2 bytes                     0 to 2^16-1               0 to 65535
long int                      4 bytes                     -2^31 to 2^31-1           -2,147,483,648 to 2,147,483,647
unsigned long int             4 bytes                     0 to 2^32-1               0 to 4,294,967,295
long double                   10 bytes                    -2^79 to 2^79-1           3.4e-4932 to 1.1e+4932

Operators and Expressions

An expression consists of variables and constants separated by operators. C language uses many types of operators as listed below.

  1. Arithmetic operators
  2. Relational operators
  3. Logical operators
  4. Increment and decrement operators
  5. Assignment operators
  6. Conditional operators
  7. Bitwise operators

Arithmetic Operators

Arithmetic operators are used to perform arithmetic operations. Following are the arithmetic operators used in C language.

Operators       Meaning                          Example       Result
  +             addition                          5+2           7
  -             subtraction                       5-2           3
  *             multiplication                    5*2           10
  /             division                          5/2           2
  %             modulus operator to get           5%2           1
                remainder in integer division 

Relation Operators

Relation operators are used to compare the values of operands (expressions) to produce a logical value. A logical value is either true or false. Following are the relational operators used in C language.

   Operators      Meaning                       Example       Result
       <          less than                     5<2           false
       >          greater than                  5>2           true
       <=         less than or equal to         5<=2          false
       >=         greater than or equal to      5>=2          true
       ==         equal to                      5==2          false
       !=         not equal to                  5!=2          true

Note that in C language the logical value true is represented by integer 1 and false by 0.

Logical Operators

Logical operators are used to connect more relational operations to form a complex expression called logical expression. A value obtained by evaluating a logical expression is always logical, i.e. either true or false. Following are the logical operators in C.

 Operator       Meaning         Example         Result
    &&          logical and     (5<2)&&(5>3)    false
    ||          logical or      (5<2)||(5>3)    true
    !           logical not     !(5<2)          true
                (negation)

Note that logical not(!) is a unary operator which requires only one operand.

Assignment Operators

Assignment operators are used to perform arithmetic operations while assigning a value to a variable. Following are the arithmetic assignment operators used in C language.

 Operators         Example          Equivalent expression     Result
                                    (consider m = 15)
     +=            m += 10          m = m + 10                25
     -=            m -= 10          m = m - 10                5
     *=            m *= 10          m = m * 10                150
     /=            m /= 10          m = m / 10                1
     %=            m %= 10          m = m % 10                5

Conditional operators (?:) or Ternary Operator

Conditional operator is used to check a condition and select a value depending on the value of the condition. Normally the selected value will be assigned to a variable which has the following form.

  Variable = (condition) ? value 1 :value 2;

Bitwise Operators

Bitwise operators are used to perform operations at binary level. These operators are not commonly used and are used only in special applications where optimised use of storage is required. Following are the bitwise operators used in C language.

Operator              Meaning
    <<                shifts the bits to left
    >>                shifts the bits to right
    ~                 bitwise inversion (one's complement)
    &                 bitwise logical and
    |                 bitwise logical or
    ^                 bitwise exclusive or

Casts or Explicit Conversion

Cast operation is used to overcome automatic conversion. The variable declared in specific data type can be converted into the required type as shown below:

(type) expression or (type) (expression)

Consider the following example .

int m = 5;

float y;

y = (float)m/2;

In the above expression, the int variable m is converted into float to get the result 2.5; otherwise the result will be 2.

Operator Precedence and Associativity

The computer scans an expression which contains the C operators from left to right and performs only one operation at a time. The expression will be scanned many times to produce the result. The order in which various operations are performed is known as hierarchy of operations or operation precedence.

 Precedence order            Operator                Associativity 

         1                   ()  []  ->              left to right 
         2                   ++ -- -(unary)        
                             !~ * & sizeof           right to left
         3                   * / %                   left to right 
         4                   + -                     left to right  
         5                   <<  >>                  left to right 
         6                   < <= > >=               left to right 
         7                   == !=                   left to right 
         8                   &(bitwise AND)          left to right 
         9                   ^(bitwise XOR)          left to right 
         10                  | (bitwise OR)          left to right 
         11                  &&(logical AND)         left to right 
         12                  ||(logical OR)          left to right 
         13                  ?:(conditional)         right to left
         14                  = += -= *= /= %=
                             (assignment operator)   right to left
         15                  ,(comma operator)       left to right

Structure of a C Program

C language does not actually follow any specific method of writing a program. It is a case sensitive language. All the statements must be written in lower case letters (small letters). The structure of a C program is as follows.

 <Header files>
 <Global declaration of variables>
 main()
 {
         <Local declaration of variables>
         -   -   -   -   -   -   -   -   - 
         <Statements>
         -   -   -   -   -   -   -   -   -
 }
 <sub programs - function blocks>

The first program that we all write as a beginner in C programming is "Hello World!". The code for which looks something like this:

  #include<stdio.h>
  int main()
  {
        printf("Hello World!");
        return 0;
  }

Thank you folks for spending your precious time reading my blog.

Have a nice day.