Monday, July 1, 2019

Programming in C

What is C

        What is C ?   

C programming language is the basic and first level procedural oriented programming language. Those who want to learn programming C language is the perfect platform to begin with. It was designed by Dennis Ritchie in the year 1969 and 1973 . C  language is a mid level programming language which is widely used in developing  applications like system software, application software , embedded software, mobile applications and etc. It was introduced mainly because  to write system level programming and code for operating systems.

The C Character Set

Alphabets                   A,B,………………………Z;a,b,……………………….z
Digits                           0,1,2,3,4,5,6,7,8,9
Special symbols         ! @ # % ^ & * ( ) _ – + = | \{ }, [ ], < > ” , ? / .



‘C ‘ Tokens:
In ‘C’ program the smallest individual units are called as tokens.
There are six types of tokens:
1. Identifiers.
2. Keywords
3. Constants
4. Strings
5. Operators

C Keywords
Keyword are the words whose meaning has already been explained to the C Compiler. There are only 32 keywords available in C.
autobreak Case Char Const continue
defaultdo double else enum extern
floatfor goto if int long
registerreturn short signed sizeofstatic
structswitch typedef union unsignedvoid
volatilewhile
The First C Program
Calculation of simple interest..
#include<stdio.h>

int main()
{
int p,n;
float r,s;
p=1000;
n=3;
r=8.5;
/*formula of simple interest */
s=p*n*r/100;
printf("%f\n",s);
return 0;

Let us understand in detail this program..
There are some certain rules must be use in C .

a) Each instruction in C program indicate how it has to be written as a separate statement .
b) The statement in a program must appear in the same order in which we wish them to be executed.
c) Blank space may be inserted between two words to improve the readability of the statement .
d) All statement should be in lower case latter.
e) C has no specific rules for the position at which a statement is to be written in a given line.
f) Every C statement must be end with a semicolon (;). Thus ; acts as a statement terminator.

   What is main()

When the operating system runs a program in C, it passes control of the computer over to that program. This is like the captain of a huge ocean liner handing you the wheel. Aside from any fears that may induce, the key point is that the operating system needs to know where inside your program the control needs to be passed. In the case of a C language program, it’s the main() function that the operating system is looking for.

What is printf()
printf() i a library function defined under stdio.h header file. this function is used to print something in the console. this function is a integer type function which means this function returns an integer to the caller function. printf() returns the number of characters it has printed in the console.
so, 
              printf("5");
           here printf() will return 1 as printf() is printing only one character (5). we can use different fromat specifier to print different data types in different format,i.e. in exponential format,integer format,character format.
Let 

Sunday, June 2, 2019

C Instructions

"On your mark, get set go..."

Captain of a cricket team is as good as his team. If team is not good enough, captain alone can't  do much. Same is C programming. Unless you know the instructions that it offer, you can hardly write a good program.

Types Of Instruction 

a) Type Declaration Instruction

This instruction is used to declare the type of variables used in a C program . Any variable used in the program must be declared before it is used in any statement. The type declaration statement is written as the beginning of main() function. 

  Example - int, float, char name etc.

b) Arithmetic Instruction
 Am arithmetic instruction in C consists of a variable name on the left hand side of equal and variable name and constant on the right hand side of equal. The variable and constant on the right hand side of equal are connected by arithmetic operators like +,-,* and /
Example : int ad
float kot, deta,alpha, beta, gamma; 
ad=3200;
kot=0.0056;
deta=alpha*beta/gamma+3.2*2/5;

Here;
*, /, -, + are arithmetic operators.
= is the assignment operator.
kot, deta, alpha, beta, gamma are real variables.


Integer And Float Conversions 

In order to to effectively develop C program, it will be necessary to understand the rules that are used for the implicit conversion of floating point and integer value in C. These are mentioned below.

a) An arithmetic operation between an integer and integer always yields an integer result.
b) An operation between a real and real always yields a real result. 
c) An operation between an integer and real always yields a real result. In this operation, the integer is first promoted to a real and then the operation is performed. Hence the result real. 

I think a few practical example shown :
Operation                         Result                         Operation                         Result
     5/2                                  2                                    2/5                                  0
     5.0/2                               2.5                                 2.0/5                               0.4
    5/2.0                                2.5                                 2/5.0                               0.4
    5.0/2.0                             2.5                                 2.0/5.0                            0.4

Operator Precedence -

 When an expression contains two or more operators, normal operator precedence rules are applied to determine the order of evaluation. If two operators have different levels of precedence, the operator with the highest precedence is evaluated first. For example, multiplication is of higher precedence than addition, so the expression 2+3*4 is evaluated as 3 * 4 = 12 2 + 12 = 14 The evaluation order can be explicitly controlled using parentheses; e.g., (2+3)*4 is evaluated as 2 + 3 = 5 5 * 4 = 20 Operators in the previous table are presented in groups from highest to lowest precedence.

Operator Associavity - 

If two operators in an expression have the same precedence level, they are evaluated from left to right or right to left depending on their associavity. For example, additions associavity is left-to-right, so the expression 2+3+4 is evaluated as (2+3)+4. In contrast, the assign operators associavity is right-to-left; so the expression x=y=z is evaluated as x=(y=z).

CategoryOperatorAssociavity
Postfix() [] -> . ++ - -Left to right
Unary+ - ! ~ ++ - - (type)* & sizeofRight to left
Multiplicative* / %Left to right
Additive+ -Left to right
Shift<< >>Left to right
Relational< <= > >=Left to right
Equality== !=Left to right
Bitwise AND&Left to right
Bitwise XOR^Left to right
Bitwise OR|Left to right
Logical AND&&Left to right
Logical OR||Left to right
Conditional?:Right to left
Assignment= += -= *= /= %=>>= <<= &= ^= |=Right to left
Comma,Left to right
Note  - Postfix increment/decrement have high precedence, but the actual increment or decrement of the operand is delayed (to be accomplished sometime before the statement completes execution). So in the statement y = x * z++; the current value of z is used to evaluate the expression (i.e., z++ evaluates to z) and z only incremented after all else is done.










                                            
                                     

Saturday, June 1, 2019

Sum of series programs

Sum of series programs/examples in C programming language



  /*This program will print the sum of all natural numbers from 1 to N.*/
 
#include<stdio.h>
     
int main()
{
    int i,N,sum;
     
    /*read value of N*/
    printf("Enter the value of N: ");
    scanf("%d",&N);
     
    /*set sum by 0*/
    sum=0;
     
    /*calculate sum of the series*/
    for(i=1;i<=N;i++)
        sum= sum+ i;
     
    /*print the sum*/
     
    printf("Sum of the series is: %d\n",sum);
     
    return 0; }
Please write comments if you find any of the above code/algorithm incorrect, or find better ways to solve the same problem.

Multiply two integers without using multiplication, division and bitwise operators.

To multiply x and y, recursively add x y times.



#include<stdio.h>
/* function to multiply two numbers x and y*/
int multiply(int x, int y)
{
   /* 0  multiplied with anything gives 0 */
   if(y == 0)
     return 0;
  
   /* Add x one by one */ 
   if(y > 0 )
     return (x + multiply(x, y-1));
   
  /* the case where y is negative */ 
   if(y < 0 )
     return -multiply(x, -y);
}
  
int main()
{
  printf("\n %d", multiply(5, -11));
  getchar();
  return 0;
}

Trigraph characters in C language

  1. C supports the following 9 trigraph characters.

Trigraph sequenceEqual character
??=#
??([
??)]
??/\
??<{
??>}
??!|
??’^
??-~
We need to write a simple hello world program in the following way in the platform which doesn’t support #, {} and \ characters.
1
2
3
4
5
6
??=include<stdio.h>
int main()
??<
printf("Hello??/nWorld");
return 0;
??>
The trigraphs pre-processor changes the above code as
1
2
3
4
5
6
#include<stdio.h>
int main()
{
printf("Hello\nWorld");
return 0;
}
Trigraphs are not commonly supported by all the compilers. Some compilers support an option to turn recognition of trigraphs on. Some issues warnings when they encounter trigraphs in the source files.

Please write comments if you find any of the above code/algorithm incorrect, or find better ways to solve the same problem.

Programming in C

         What is C ?    C programming language is the basic and first level procedural oriented programming language. Those who wa...