GoogelAddUnit1

Friday 26 August 2011

Write a program to determine the ranges of char , short , int , and long variables, both signed and unsigned .


#include <stdio.h>
#include <limits.h>

int 
main ()
{
  printf("Size of Char %d\n", CHAR_BIT);
  printf("Size of Char Max %d\n", CHAR_MAX);
  printf("Size of Char Min %d\n", CHAR_MIN);
  printf("Size of int min %d\n", INT_MIN);
  printf("Size of int max %d\n", INT_MAX);
  printf("Size of long min %ld\n", LONG_MIN);       /* RB */
  printf("Size of long max %ld\n", LONG_MAX);       /* RB */
  printf("Size of short min %d\n", SHRT_MIN);
  printf("Size of short max %d\n", SHRT_MAX);
  printf("Size of unsigned char %u\n", UCHAR_MAX);  /* SF */
  printf("Size of unsigned long %lu\n", ULONG_MAX); /* RB */
  printf("Size of unsigned int %u\n", UINT_MAX);    /* RB */
  printf("Size of unsigned short %u\n", USHRT_MAX); /* SF */

  return 0;
}

Structure of C Program.


Sample C program


#include <stdio.h>
    #define LAST 10
      
    int main()
    {
        int i, sum = 0;
       
        for ( i = 1; i <= LAST; i++ ) {
          sum += i;
        } /*-for-*/
        printf("sum = %d\n", sum);

        return 0;
    }
The main parts are:
  • preprocessor directives (notable by the # character and the lack of semicolons)
  • global variable declarations
  • function declarations
  • int main()
  • function definitions
Here's a brief description of those parts:
Comments:
These start with /* and continue, ignoring newlines, until the next */. Notice that this means there is no nesting of comments. We adhere to ANSI-C standard (until C99 becomes more ubiquitous), so you cannot assume that // comments until the end of a line will work. It may turn out that some compilers accept these, but you cannot assume that all compilers (particularly the one used by the marking TA) will.
#include directives:
includes declarations of functions from header files for standard libraries. For example, #include <stdio.h> includes declarations of functions for the standard library that are useful for input/output. Note the distinct absence of semicolon
#define directives:
Performs textual replacement, which is useful to define constants. For example, this is legal C:
#include <stdio.h>
    #define BEGIN {
    #define END }
    int main()
    BEGIN
        printf("Hello World!\n");

        return 0;
    END
int main():
An executable program must have a main method, which is what's called when the program is executed. int indicates that this function returns an integer value to the shell where it is executed. In this course we will normally return 0. 

C Program to compute Pi using Monte Carlo methods.


#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#define SEED 35791246

main(int argc, char* argv)
{
   int niter=0;
   double x,y;
   int i,count=0; /* # of points in the 1st quadrant of unit circle */
   double z;
   double pi;

   printf("Enter the number of iterations used to estimate pi: ");
   scanf("%d",&niter);

   /* initialize random numbers */
   srand(SEED);
   count=0;
   for ( i=0; i<niter; i++) {
      x = (double)rand()/RAND_MAX;
      y = (double)rand()/RAND_MAX;
      z = x*x+y*y;
      if (z<=1) count++;
      }
   pi=(double)count/niter*4;
   printf("# of trials= %d , estimate of pi is %g \n",niter,pi);
}

History of C Programming Language.

Before we embark on a brief tour of C's basic syntax and structure we offer a brief history of C and consider the characteristics of the C language.
In the remainder of the Chapter we will look at the basic aspects of C programs such as C program structure, the declaration of variables, data types and operators. We will assume knowledge of a high level language, such as PASCAL.
It is our intention to provide a quick guide through similar C principles to most high level languages. Here the syntax may be slightly different but the concepts exactly the same.
C does have a few surprises:
  • Many High level languages, like PASCAL, are highly disciplined and structured.
  • However beware -- C is much more flexible and free-wheeling. This freedom gives C much more power that experienced users can employ. The above example below (mystery.c) illustrates how bad things could really get.

History of C

The milestones in C's development as a language are listed below:

  • UNIX developed c. 1969 -- DEC PDP-7 Assembly Language
  • BCPL -- a user friendly OS providing powerful development tools developed from BCPL. Assembler tedious long and error prone.
  • A new language ``B'' a second attempt. c. 1970.
  • A totally new language ``C'' a successor to ``B''. c. 1971
  • By 1973 UNIX OS almost totally written in ``C''.

Characteristics of C

We briefly list some of C's characteristics that define the language and also have lead to its popularity as a programming language. Naturally we will be studying many of these aspects throughout the course.

  • Small size
  • Extensive use of function calls
  • Loose typing -- unlike PASCAL
  • Structured language
  • Low level (BitWise) programming readily available
  • Pointer implementation - extensive use of pointers for memory, array, structures and functions. 

Write a C Program to check whether the given number is Am strong or Not.?


#include<stdio.h>
#include<conio.h>
void main()
{
int dn,rem,sum,n;
printf("Enter the number:");
scanf("%d",&n);
for(dn=n,sum=0;n!=0;rem=n%10,sum=sum+rem*rem*rem,n=n/10);
if(dn==sum)
printf("Amstrong number");
else
printf("Not a amstrong number");
}

Armstrong number:-

The sum of the cubes of digits of a number is equal to the original number.
Ex: n=153 => 13 + 53 +33 = 1+125+27= 153
so 153 is arm strong number.


C program to check whether the given Number is Strong or Not.?

#include<stdio.h>
#include<conio.h>
void main()
{
int sof=0,n,dn,ctr,prod,rem;
printf("Enter the number\n");
scanf("%d",&n);
dn=n;
while(n!=0)
{
prod=1,ctr=1;
rem=n%10;
while(ctr<=rem)
{
prod=prod*ctr;
ctr=ctr+1;
}
sof=sof+prod;
n=n/10;
}
if(sof==dn)
{
printf("The number entered is strong number");
}
else
{
printf("The number entered is not a strong number");
}
}


Strong number:-
The sum of the factorials of digits of a number is equal to the original number.
Ex: n=145=> 1! + 4! + 5! = 1 + 24 + 120 = 145
so it is strong number.



Tuesday 2 August 2011

The form of a C program

If we are used to the block-structured form of, say, Pascal, then at the outer level the layout of a C program may surprise you. If your experience lies in the FORTRAN camp you will find it closer to what you already know, but the inner level will look quite different. C has borrowed shamelessly from both kinds of language, and from a lot of other places too. The input from so many varied sources has spawned a language a bit like a cross-bred terrier: inelegant in places, but a tenacious brute that the family is fond of. Biologists refer to this phenomenon as ‘hybrid vigour’. They might also draw your attention to the ‘chimera’, an artificial crossbreed of creatures such as a sheep and a goat. If it gives wool and milk, fine, but it might equally well just bleat and stink!
At the coarsest level, an obvious feature is the multi-file structure of a program. The language permits separate compilation, where the parts of a complete program can be kept in one or more source files and compiled independently of each other. The idea is that the compilation process will produce files which can then be linked together using whatever link editor or loader that your system provides. The block structure of the Algol-like languages makes this harder by insisting that the whole program comes in one chunk, although there are usually ways of getting around it.
The reason for C's approach is historical and rather interesting. It is supposed to speed things up: the idea is that compiling a program into relocatable object code is slow and expensive in terms of resources; compiling is hard work. Using the loader to bind together a number of object code modules should simply be a matter of sorting out the absolute addresses of each item in the modules when combined into a complete program. This should be relatively inexpensive. The expansion of the idea to arrange for the loader to scan libraries of object modules, and select the ones that are needed, is an obvious one. The benefit is that if you change one small part of a program then the expense of recompiling all of it may be avoided; only the module that was affected has to be recompiled.
All, the same, it's true that the more work put on to the loader, the slower it becomes, in fact sometimes it can be the slowest and most resource consuming part of the whole procedure. It is possible that, for some systems, it would be quicker to recompile everything in one go than to have to use the loader: Ada has sometimes been quoted as an example of this effect occurring. For C, the work that has to be done by the loader is not large and the approach is a sensible one. Figure 1.1 shows the way that this works.
Diagram showing multiple files going from source, through           compilation, to object files, and being combined with libraries by           the loader to produce a program.
Figure 1.1. Separate compilation
This technique is important in C, where it is common to find all but the smallest of programs constructed from a number of separate source files. Furthermore, the extensive use that C makes of libraries means that even trivial programs pass through the loader, although that might not be obvious at the first glance or to the newcomer.

Computer Memory - Stored Program.

Simple C Program by using printf statement.

/***********************************************************/
/* Short Poem                                              */
/***********************************************************/

#include <stdio.h>

/***********************************************************/

   main ()                    /* Poem */

   {
   printf ("Astronomy is %dderful \n",1);
   printf ("And interesting %d \n",2);
   printf ("The ear%d volves around the sun \n",3);
   printf ("And makes a year %d you \n",4);
   printf ("The moon affects the sur %d heard \n",5);
   printf ("By law of phy%d great \n",6);
   printf ("It %d when the the stars so bright \n",7);
   printf ("Do nightly scintill%d \n",8);
   printf ("If watchful providence be%d \n",9);
   printf ("With good intentions fraught \n");
   printf ("Should not keep up her watch divine \n");
   printf ("We soon should come to %d \n",0);
   }
Output
Astronomy is 1derful \n"
And interesting 2
The ear3 volves around the sun
And makes a year 4 you
The moon affects the sur 5 heard
By law of phy6d great
It 7 when the the stars so bright
Do nightly scintill8
If watchful providence be9
With good intentions fraught
Should not keep up her watch divine
We soon should come to 0