Let us see C

                                               Let us see  C

 

About C:

The C is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system.C is the most widely used computer language.

                                        Program Structure
       A C program basically has the following form:
  • Preprocessor Commands
  • Functions
  • Variables
  • Statements & Expressions
  • Comments

    #include <stdio.h>
    
    int main()
    {
       /* My first program */
       printf("Hello, World! \n");
       
       return 0;
    } 
     
    Preprocessor Commands: 
     
    #include <stdio.h> is a preprocessor command which tells a C compiler to include stdio.h file before going to actual compilation. 

    Functions:
    • are main building blocks of any C Program.  
    • main() function is mandatory 
    Variables: 
             used to hold numbers, strings and complex data for manipulation  
          Comments:
            used to give additional useful information inside a C Program.

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

C - Basic Datatypes:
  C has a concept of 'data types' which are used to define a variable before its use. The definition of a variable will assign storage for the variable and define the type of data that will be held in the location.
C has the following basic built-in datatypes.
  • int
  • float
  • double
  • char

    int - data type

    int is used to define integer numbers.
        {
            int Count;
            Count = 5;
        }
    

    float - data type

    float is used to define floating point numbers.
        {
            float Miles;
            Miles = 5.6;
        }
    

    double - data type

    double is used to define BIG floating point numbers. It reserves twice the storage for the number. On PCs this is likely to be 8 bytes.
        {
            double Atoms;
            Atoms = 2500000;
        }
    

    char - data type

    char defines characters.
        {
            char Letter;
            Letter = 'x';
        }
     

    Modifiers

    The modifiers define the amount of storage allocated to the variable.
    The data types explained above have the following modifiers.
  • short
  • long
  • signed
  • unsigned

            short int  2          -32,768 -> +32,767          (32kb)
   unsigned short int  2                0 -> +65,535          (64Kb)
         unsigned int  4                0 -> +4,294,967,295   ( 4Gb)
                  int  4   -2,147,483,648 -> +2,147,483,647   ( 2Gb)
             long int  4   -2,147,483,648 -> +2,147,483,647   ( 2Gb)
          signed char  1             -128 -> +127
        unsigned char  1                0 -> +255
                float  4 
               double  8  
          long double 12 
 PROGRAM TO FIND THE MEMORY ALLOCATED FOR RESPECTIVE MODIFIERS:
int
main()
{
  printf("sizeof(char) == %d\n", sizeof(char));
  printf("sizeof(short) == %d\n", sizeof(short));
  printf("sizeof(int) == %d\n", sizeof(int));
  printf("sizeof(long) == %d\n", sizeof(long));
  printf("sizeof(float) == %d\n", sizeof(float));
  printf("sizeof(double) == %d\n", sizeof(double));
  printf("sizeof(long double) == %d\n", sizeof(long double));
  printf("sizeof(long long) == %d\n", sizeof(long long));

  return 0;
}


Qualifiers

A type qualifier is used to refine the declaration of a variable, a function, and parameters, by specifying whether:
  • The value of a variable can be changed.
  • The value of a variable must always be read from memory rather than from a register
Standard C language recognizes the following two qualifiers:
  • const
  • volatile
The const qualifier is used to tell C that the variable value can not change after initialisation.
const float pi=3.14159;
VOLATILE :
The volatile qualifier declares a data type that can have its value changed in ways outside the control or detection of the compiler.

What are Arrays:

 In C language it is possible to make arrays whose elements are basic types
int x[10];
The square brackets mean subscripting; parentheses are used only for function references. Array indexes begin at zero, so the elements of x are:
Thus Array are special type of variables which can be used to store multiple values of same data type.
 Those values are stored and accessed using subscript or index.
Arrays occupy consecutive memory slots in the computer's memory.
x[0], x[1], x[2], ..., x[9]
If an array has n elements, the largest subscript is n-1.
Multiple-dimension arrays are provided. The declaration and use look like:
      int name[10] [20];
      n = name[i+j] [1] + name[k] [2];
Subscripts can be arbitrary integer expressions. Multi-dimension arrays are stored by row so the rightmost subscript varies fastest. In above example name has 10 rows and 20 columns.
Same way, arrays can be defined for any data type. Text is usually kept as an array of characters. By convention in C, the last character in a character array should be a `\0' because most programs that manipulate character arrays expect it. For example, printf uses the `\0' to detect the end of a character array when printing it out with a `%s'.
Here is a program which reads a line, stores it in a buffer, and prints its length (excluding the newline at the end).
       main( ) {
               int n, c;
               char line[100];
               n = 0;
               while( (c=getchar( )) != '\n' ) {
                       if( n < 100 )
                               line[n] = c;
                       n++;
               }
               printf("length = %d\n", n);
       }

Array Initialization

  • As with other declarations, array declarations can include an optional initialization
  • Scalar variables are initialized with a single value
  • Arrays are initialized with a list of values
  • The list is enclosed in curly braces
int array [8] = {2, 4, 6, 8, 10, 12, 14, 16};
The number of initializers cannot be more than the number of elements in the array but it can be less in which case, the remaining elements are initialized to 0.if you like, the array size can be inferred from the number of initializers by leaving the square brackets empty so these are identical declarations:
int array1 [8] = {2, 4, 6, 8, 10, 12, 14, 16};
int array2 [] = {2, 4, 6, 8, 10, 12, 14, 16};
An array of characters ie string can be initialized as follows:
char string[10] = "Hello";

Inspirational and Motivational Stories

                                                               The Carpenter  


An elderly carpenter was ready to retire. He told his employer/contractor of his plans to leave the house building business and live a more leisurely life with his wife enjoying his extended family. He would miss the paycheck, but he needed to retire. They could get by.
 
The contractor was sorry to see his good worker go and asked if he could build just one more house as a personal favor. The carpenter said yes, but in time it was easy to see that his heart was not in his work. He resorted to shoddy workmanship and used inferior materials. It was an unfortunate way to end his career.

When the carpenter finished his work and the builder came to inspect the house, the contractor handed the front-door key to the carpenter. "This is your house," he said, "my gift to you."

What a shock! What a shame! If he had only known he was building his own house, he would have done it all so differently. Now he had to live in the home he had built none too well.

So it is with us. We build our lives in a distracted way, reacting rather than acting, willing to put up less than the best. At important points we do not give the job our best effort. Then with a shock we look at the situation we have created and find that we are now living in the house we have built. If we had realized, we would have done it differently.

Think of yourself as the carpenter. Think about your house. Each day you hammer a nail, place a board, or erect a wall. Build wisely. It is the only life you will ever build. Even if you live it for only one day more, that day deserves to be lived graciously and with dignity. The plaque on the wall says, "Life is a do-it-yourself project."

Who could say it more clearly? Your life today is the result of your attitudes and choices in the past. Your life tomorrow will be the result of your attitudes and the choices you make today.
                                                     Never Give Up
There was an old mule. One day accidentally he fell into the farmer’s well. The farmer has evaluated the situation and thought to himself, that neither the well nor the old mule was worth the efforts to save them. Thus he decided to haul dirt to bury the old mule in the well.

So the farmer called his neighbours and together they started shoveling dirt into the well. The old mule was terrified and hysterical in the beginning. But soon one hopeful idea came to his mind – every time when a shovel of dirt landed on his back, he would shake it off and step up!

He repeated these words to himself again and again: „Shake it off and step up“. This way he could struggle the panic and encourage himself. After some time, the mule had stepped over the well‘s wall. Although terribly tired, he was the winner, he saved his own life. He decided to face his adversity positively and not to give up, and thus he won.

MORAL:
What seemed like it would bury him, actually saved him, owing to his confidence and unresting efforts.




                                                      Life is Challenge

Once a small gap appeared in the cocoon, through which the butterfly had to appear. A man, who accidentally passed by, stopped and watched how the butterfly was trying to get out of the cocoon. It took a lot of time, the butterfly was trying very hard, and the gap was as little as before. It seemed that the power would leave the butterfly soon.

Then a man decided to help the butterfly. He took a penknife and cut the cocoon. The butterfly immediately got out, but her body was weak and feeble, and the wings were barely moving.

A man continued to watch at the butterfly, thinking that now her wings would spread and she would fly. However, that did not happen.

The rest of her life the butterfly had to drag her weak body and wings that weren’t spread. She was unable to fly, because a man who wanted to help her did not realize that an effort to enter through the narrow gap of the cocoon was necessary for the butterfly, so that the life-giving fluid would move from the body to the butterfly’s wings and that the butterfly could fly. Life forced the butterfly to leave her shell hardly, so that it would become stronger and would be able to grow and develop.


MORAL:If we were allowed to live without meeting difficulties, we would be via-ble. Life gives us challenges to make us stronger. 




                                                       Determination
As he was a kid, his father as a horse trainer was moving from stable to stable, from ranch to ranch, training horses. Thus, the boy‘s school career was constantly interrupted. One day, when he was a senior, teacher asked him to write about what he wanted to be when he grew up. He did not hesitate a minute and wrote seven-page paper about his aim to be an owner of a horse ranch, he wrote many details and drew a location of buildings and stables and even a detailed house plan.

Two days later he received his paper back with letter „F“ on the front page. After class he came to teacher and asked: „Why did I receive an F?“. The teacher responded: „This dreams is so unrealistic for a boy like you, who has no money, no resources and who comes from itinerant family. There is no possibility that you will reach your great goals one day.“ Then the teacher offered to rewrite the paper with more realistic attitude.

The boy went home and asked his father, how should he act. The father answered: „This decision is very important for you. So you have to make your own mind on this“.

After several days the boy brought the same paper to his teacher. No changes were made. He said: „Keep the F and I will keep my dream“.

Now Monty Roberts owns 4,000-square-foot house in the middle of 200-acre horse ranch and he still has that school paper, which now is framed over the fireplace.


MORAL:
Remember, you have to follow your heart and never let anyone to steal your dreams.


                                                                Values 
Many years ago all feeling and emotions have gathered to spent their vacation on a coastal island. Each of them was having a good time, but one day there was announced a warning of a storm and everyone had to leave the island.

This caused a panic, all rushed to their boats and only Love did not wish to be in a hurry. There was so much to do, so Love was the last, who realized that it was time to leave. However, no free boats were left and Love looked around with hope.

As Prosperity was passing by in its classy boat, Love asked: „Please, take me in your boat“. But Prosperity replied: „My boat is full of gold and other precious possessions, there is no place for you“.

Then Vanity came by in a lovely boat. Love asked: „Vanity, could you take me in your boat? Please, help me.“ Vanity said: „No, your feet are muddy, and I don‘t want my boat get dirty.“

Some time later Sorrow was passing by and Love called for help. But Sorrow answered: „I am so sad, I want to be by myself“.

Then Happiness came by, Love asked for help, but Happiness was too happy, it hardly concerned about anyone.

Suddenly somebody called out: „Love, I will take you with me“. Love did not recognized its savior, just gratefully jumped on to the boat.

When everyone had reached safe place, Love get off the boat and met Knowledge. Love asked: „Knowledge, do you know who helped me when everyone else turned away?“ Knowledge smiled: „That was Time, because only Time knows Love‘s true value and what Love is capable of. Only Love can bring peace and happiness.“
 

“The messageof this story is that when we are prosperous, we underrate Love. When we feel important, we do not appreciate love. And even in happiness and sorrow we overlook love. Only with time we realize the true value of love. Why wait and not cherish and share Love every day of your life?” 

                                                          Making a Difference 
Once, during a flood, there were a lot of sea-stars brought to the shore. The low tide came and a big amount of them started drying up in the sun. A boy, walking down the shore, started throwing the sea-stars into the sea, so that they could continue their lives.

A person came up to him and asked:
Why are you doing this? Look around! There are millions of sea-stars; the shore is covered with them. Your attempts won’t change anything! The boy picked up the following sea-star, thought for a moment, threw it into the sea and said:
 No, my attempt will change a lot…for this sea-star.

 

C Basics






/*program to display without using semicolon in a C program*/

#include<stdio.h>   /*header files*/
#include<conio.h>
void main()             /*main function*/
{
if(printf("Hai !My name is Thilagaraj ")){}  /*body of function*/
}  /*end of function*/

               /*Program to list the n number of prime number*/

#include<stdio.h>

#include<conio.h>


void main()
{


   int i,j,n,p=0;


   clrscr();


   printf("\nEnter the limit to which the prime no. to be printed"); 


  scanf("%d",&n);


   for(i=2;i<n;i++)
   {
     for(j=1;j<=i;j++)
        {

          if(i%j==0)


            p++;


          if(p>2)


           break;
         }
              if(p==2)


              printf("\n%d",i);
              p=0;
      }

          getch();
  }