Sunday, August 10, 2014

BASICS OF C PART 2

BASICS OF C PART 2




Statements: C programs are made of functions. Functions contain statements. Statement performs a specific action and terminated by semicolon ‘;’ . In the above program
printf(“Welcome to C programming”);  is only statement.

You can write printf statement in multiple line . for example. :
 The statement:
 printf(“ The increasing popularity of C is probably”
             “  Due to its many desirable quantities/.”
             “It is a robust language whose rich set is “);
is a single statement.
             
The Second program:

1.   /* program2.c */
2.   /*This program accepts a character two number and displays them in screen */
3.   #include<stdio.h>
4.   main()
5.   {
6.     char c; int i;
7.     float f;
8.     printf(“\nEnter a character:”);
9.     Scanf(“%c”,&c);
10.   printf(“\nEnter an integer;”);
11.   scanf(“%d”,&i);
12.   printf(“\nEnter a floating pt number:”);
13.   scanf(“%f”,&f);
14.   printf(“character: %c\n integeris:%d,float: %f”,c,i,f);
15.  }  /*end of main() */

Analysis:Variable and types:

Line number 6 and 7 in program  are:
 char c; int i; and float f;
In those lines, c,i, f are variables and int, char, float are data types  key words for declaration of variables in C program.
In C there are four primitive types of variables.
  1. char : it can store one ASCII character(i.e. one byte only )
  2. int: it can store integers( i.e. without decimal precision values). Its size is generally 2 bytes. However it differs from machine to machine. In some machines it is equal to 4 bytes. If it is two bytes it can store numbers from -32768 to +32767
  3. float : Single precision number with decimal value. Its size is generally equal to 4 bytes.
  4. double: Double precision number with decimal value. Its size is generally equal to 8 bytes.

How these types are indicated in printf and scanf  functions ?
  1. char : by  %c
  2. int : by  %d(decimal) , %i (integer) , %x(hex) , %o (octal)
  3. float : by %f
  4. double : by %f

in C each necessary variable is  declared before its use. If variable is declared outside the function, it is called global variable and visible to all the functions in that program. If variable is declared inside the function it is called local variable and visible only for that particular function only.

How to give the variable name ?
There some restrictions on naming the variables in C.
  1. Variable name should begin with a alphabetic character. But  underscore character ‘_’ is also legal character as beginning character for variable name.
  2. Variable name should contain only letters ,digits and underscore characters. No other characters are allowed for name.
  3. Variable name should be unambiguous within its first eight characters since first eight characters are treated as significant characters by many compilers.
  4. Uppercase and lowercase  are significant. i.e. name, Name,NAME or nAme all are different variables.
  5. The C key word can not be a variable name.
  6. White spaces are not allowed

Here are some examples of valid and invalid variable names in C.
Valid variable names:
Abc,  name ,  _name,   first_name  alpha_1,  alpha99  alp97bc etc

     Invalid varieable name: (Why ?)

   123_res,            first name,          ret?b     first-name
    ab:xy,                               %                            int           return

The declaration of variables is done as:
<data type >  <variable name> ;
we can declare more than one variable of same type with single type declaration as:
char var1,var2,var3,……,varn;
float e,f,g;                      and so on.

The scanf() function:

Scanf() is an input function defined in stdio.h header file. Scanf accepts the  input from keyboard. As above programs the syntax of input function scanf is
   Scanf(“specifier”,&variablename);
The specifier may be any one of %d,%c,%f and so on.

The & is an address operator in scanf function . &variablename specifies the memory address for the variable and the value entered from keyboard is stored in that specified location.

Recall the line 14 of program:
   printf(“character: %c\n integer:%d,float: %f”,c,i,f);

if our input is : c=’A’, i=10,and f=23.45
This produces the output as:
character: A
integer : 10
float : 23.450000

The specifier %c,%d,%f etc inside the double quote of the printf statement displays the actual value of variable listed after the closing of double quote and comma ‘,’ sequentially. If there are no listing of variables for those specifier, then the some garbage value is printed.

Using Pression specifiers in the display of float numbers.

You may have noticed that the floating point number example  in above program 2  have displayed with more decimal places  in them than were in original number. You can exercise greater control over how a float number is displayed using precision specifier to limit the number the number of decimal places displayed.
For example  the variable x contains the number 23.456789 and you only want two  of its decimal places for display  the printf statement looks like

 Printf(“%5.2f”,x);              which will produce out put as 23.45


Field width             precision

Similary you can print the integer numbers with interleaving spaces using specifiers.
Example: int x=10,y=40,z=30;
               If the printf statement in program is :
 printf(“%d%d%d”,x,y,z); then the output is : 104030 which is ambiguous.
 You can print it in meaningful format using formatted printf statement as:
     printf (“%4d%4d%5d”,x,y,z); which gives output as:  10  40   30
 

   The Third Program:
1.  /* progm3.c */
2.  /*this program illustrates the simple arithmetic and assignment operators */
3.  #include<stdio.h>
4.   main()
5.   {
6.      int num1,num2,sum,dif,prod, rem;
7.      float div;
8.      printf(“\nEnter the firsrt number:”);
9.      scanf(“%d”,&num1);
10.      printf(“\nEnter the second number: “);
11.      scanf(“%d”,&num2);
12.      sum=num1+num2;
13.      dif=num1-num2;
14.      prod=num1*num2;
15.     div=(float)num1/(float)num2;
16.     printf(“The sum is %d”, sum);
17.     printf(“\nThe difference of %d and %d is %d “,
              num1,num2,dif);
18.     printf(“\n%d x %d = %d “ ,num1,num2,prod);
19.     printf(“\nThe quotient after division is  %f”, div);
20.     printf(“\nThe remainder dividing %d with %d is : %d”,  
       num1,num2,num1%num2);
21.     }   /*end of program */

Arithmetic operators in C:
+ for addition
-          for subtraction
* for multiplication
/  for division
% for remainder  ( only for integer division )

Assignment Operator:
 = for assign the value to a variable.
  e. g.  int x = 10;
           float  f = 56.34;
           char c = ‘n’;             /* character constant  is included inside single quote */
short-cut assignment:
  +=, -=, *= , /= 
eg.  x= x+10  equivalent to x+=10
       var1 = var1 * var2     equivalent to var1*=var2             ………….and so on

Constants:
Constants are those that don’t changes during the whole program execution.
Constants are defined in C by the following two ways.
  1. using #define directive:
syntax : #define identifier value  /* note no semicolon */
e.g. #define PI 3.14
    #define ADULT_AGE 18
    #define GREETING “Wishing you Happy Dashain “
      2. Using const key word:
           syntax: <const> <data type>  identifier = value ;
                    eg. const float PI = 3.14;
           const char ch = ‘#’;
          

Expressions:

Expressions are combinations of variables ,constants and operators arranged as per the grammar of the language. C can handle  any complex types of expressions . some example of mathematical expressions in C are as shown.
Algrabic expression                                         C expression
ab – c                                                                 a*b-c
(m+n) (x+y)                                                        (m+n) * (x+y)

ab
 c

 
                                                                                a*b/c
 



 3x2+2x-1                                                             3*x*x +2*x – 1

+
 
  x           c
  y

Note: There is no any operator in C for exponentiation.



HackerBoy

Compiled By Sabin Khanal

0 comments:

Post a Comment

 

Copyright @ 2013 Learn Programming.

Designed by Templateiy & CollegeTalks