Wednesday, June 5, 2013

Pointer

A pointer is derived data type in C. It is built one of the fundamental data types available in C. Pointer contains memory addresses as their values. Since these memory addresses are the computer memory where program instructions and data are stored, pointers can be used to access and manipulate data stored in the memory.



eg:-

//program

#include<stdio.h>
#include<conio.h>\
void main()
{
char a;
int x;
float p, q;
printf("%c is stored at addr %u.\n",a,&a);
printf("%d is stored at addr%u.\n",x,&x);
printf("%f is stored at addr%u.\n",p,&p);
printf("%f is stored at addr%u.\n",q,&q);
getch();
}

Thursday, May 23, 2013

What is structure??

A structure is an object that consist of named members, that may be of different data types. The members have public access


Concept:  A structure provides a means of grouping variables under a single name for ease of use and handling of the same. The structure can be nested to develop complex hierarchies. Structure may be copied to and assigned value like other variables. They are also  useful in passing groups of logically related data into function.


Syntax:-

struct stucture_name
{
        ......
        .......     // list of data types and variable  name
       .......
};



Monday, May 20, 2013

Array program

Array programs:------------------------------------


#include<stdio.h>
#include<conio.h>
3define DIM 5

void main()
{
    int arr[DIM];
    int i=0;

while(i<DIM)
{
  printf("element %d:",i);

  scanf("%d",&arr[i]);
  ++i;
}

printf("Array Elements");
i=0;

while(i<DIM)
{
printf("%d\t",arr[i]);
++i;
}
   printf("\n");
getch();
}




\\next program


#include<stdio.h>
#include<conio.h>
#define DIM 5
void main()
{
    int arr[DIM];
    int i;
    for(i=0;i<DIM;++i)
        {
            printf("elementsv%d:",i);
            scanf("%d",&arr[i]);
        }
    printf("array elements:");
for(i=0;i<DIM;++i)
    printf("%d",arr[i]);
   printf("\n");
printf("Array elements in reverse order");
    for(i=DIM-1;i>=0;--i);
        printf("%d",arr[i]);
    printf("\n");
    getch();
}





\\next program


#include<stdio.h>
#include<conio.h>
#define N 5
void main()
{
    int arr[N];
    int i, max;
    for(i=0;i<N;i++)
    {
       printf("element %d:",i);
           sacnf("%d",&arr[i]);

    }
   max=arr[0];
     for(i=1;i<N;i++);
    if(max<arr[i])
       max=arr[i];
            printf("MAX Array Element is %d:\n",max);
  getch();
}









Sunday, May 19, 2013

Array

Array can be defined as a collection of variables of the same type that are referred through a common name. A specific element in an array is accessed by an index. In C, all arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the heighest address to the last element. We can say that an array is a variable that is capable of holding many values. On the other hand, an ordinary variable can hold a single value at a time.




ARRAY DECLARATION AND DEFINITION

An array needs to be declared and defineed at the beginning so that the computer will know what is the data type of array elements and how large the array size, i.e., the maximum number of elements that array can hold. The general form for declaring an array is

storage_class type var_name[size];



STORING ELEMENTS IN ARRAY

An array must be both, declared and defined before it can be used. Array declaration only reserves memory for the elements in the array. No value will be stored. The value of array elements are stored when array is defined. This is also called array initialization.



Examples


#include<stdio.h>
#include<conio.h>
void main()
{
   int i;
   float marks[5];
   float sum=0,avg;
   for(i=0;i<=4;i++)

{
  int i;
  float marks[5];
  floatsum=0,avg;
  for(i=0;i<=4;i++)
{
  printf("\n enter marks[%d]=",i);
  scanf("%f",&marks[i]);
}
for(i=0;i<=4;i++)

{
   sum=sum+marks[i];
}

avg=sum/5;
printf("average marks=%6.2f",avg);
}
getch();
}















Saturday, May 18, 2013

Rom types

ROM is non-volatile memory chip in which data is stored permanently.usual programs cannot alter this data. In fact, storing data pemanently into this kind of memory is called "burning in a data" because data in such memory is stored by using fuse links. Once we burn fuse-links for some data, it is called permanent. We can only read data stored in rom chip.

its types

1. PROM
2. EPROM
3. EEPROM
4. UVEPROM

Types of Memory Chips

1.RAM(Random Access Memory)

this memory consists of some integrated circuit(IC) chips either on motherboard or on a small circuit board attached to motherboard. A computer's motherboard usually has flexibility to easily add more memory chis for enhancing the memory capacity of the system. Hence, if a user decides to have more memory than his/her computer currently has, he/she can bye more memory chips and plug them in empty memory slots on the motherboard. Normally, service engineers do this job.


RAM is of two types:-

1. Dynamic RAM :- In this use an external circuitry to periodically "regenerate" or refresh storgage charge to retain the store data.

2. Static RAM :- does not need any special regenrator circuit to retain the storage data. It takes more transistors and other devices to store a bit in a static RAM.

Tuesday, May 7, 2013

To cheak leap year or not

#include<stdio.h>
#include<conio.h>
void main()
{
  int year;
  clrscr();
  printf("enter year");
  scanf("%d",&year);
  if(year%100==0)
      {
        if(year%400==0)
        {
           printf("this is leap year");
         }
         else
           { printf("year is not a leap year");
           } 
        }
   else if(year%4==0)
      {
        printf("year is a leap year");
      }
  getch();
}

Cheak whether a given charrector is vowel or consonants in c

\\Source Code

#include<stdio.h>
#include<conio.h>
void main()
{
  char p;
  clrscr();
  printf("\n enter a alphabet:");
  p=getchar();
  if(isalpha(p)!=0)
   {
     switch(tolower(p))
      {
        Case 'a';
        case 'e';
        case 'i';
        case 'o';
        case 'u';
        printf("it is a vowel");
        break;
         default:
         printf("it is a consonant");

        }
      } 

 getch();
}

To print largest number in c

\\Source code


#include<stdio.h>
#include<conio.h>
void main()
{
  int a, b, c;
  clrscr();
  printf("enter three number");
  scanf("%d%d%d",&a,&b,&c);
  if(a>b)
    {  
      if(a>c)
       printf("\n a is largest");
      else
       printf("\n c is largest"); 
     }
       if(b>c)
         printf("\n b is largest");
        else
          printf("\n c is largest");
  getch();
}

Compilation Process

Compilation:

A "C" program consistbof source code inone or more files.

In each source files is run through the preprocesser and compiler resulting in a file containing object code.

Object files are tide together by the linker to form a single executable program.



 

Sunday, May 5, 2013

Prime number or not

#include<conio.h>

#include<stdio.h>

void main()

{

   int z,i;
   printf("enter a number");
   scanf("%d",&z);
   while
     {
       if(z%i==0)
         {
           break;
         }
         i=i+1;
     }
      if(i==z)
         {
            printf("%d is a prime number",z);
         }
      else
        {
          printf("%d is not prime",z);
        }
getch();
}    

Example for loop with Output

.
\\Prints a Fahrenheit-to-Celsius conversion table. This is easily accomplished with a for loop or a while loop:




#include<stdio.h>
#include<conio.h>
void main()
{
int a=0;
while(a<=100)
{
printf("%4d degree F=%4d degree C",a,(a-32)*5/9);
a=a+10;
}
getch();
}



If you run this program, it will produce a table of values starting at 0 degrees F and ending at 100 degrees F. The output will look like this:
 
 
   0 degrees F =  -17 degrees C
  10 degrees F =  -12 degrees C
  20 degrees F =   -6 degrees C
  30 degrees F =   -1 degrees C
  40 degrees F =    4 degrees C
  50 degrees F =   10 degrees C
  60 degrees F =   15 degrees C
  70 degrees F =   21 degrees C
  80 degrees F =   26 degrees C
  90 degrees F =   32 degrees C
 100 degrees F =   37 degrees C
 
 

C Errors to Avoid

  • Putting = when you mean == in an if or while statement
  • Forgetting to increment the counter inside the while loop - If you forget to increment the counter, you get an infinite loop (the loop never ends).
  • Accidentally putting a ; at the end of a for loop or if statement so that the statement has no effect - For example: for (x=1; x<10; x++); printf("%d\n",x); only prints out one value because the semicolon after the for statement acts as the one line the for loop executes.

Boolean operator

In C, both if statements and while loops rely on the idea of Boolean expressions

This program accepts a number from the user. It then tests the number using an if statement to see if it is less than 0. If it is, the program prints a message. Otherwise, the program is silent. The (b < 0) portion of the program is the Boolean expression. C evaluates this expression to decide whether or not to print the message. If the Boolean expression evaluates to True, then C executes the single line immediately following the if statement. If the Boolean expression is False, then C skips the line or block of lines immediately following the if statement.
 Short example:

#include <stdio.h>
#include<conio.h>

void main()
{
    int b;
    printf("Enter a value:");
    scanf("%d", &b);
    if (b < 0)
        printf("The value is negative\n");
    getch();
}


Here are all of the Boolean operators in C:
 
 
  equality          ==
  less than         <
  Greater than      >
  <=                <=
  >=                >=
  inequality        !=
  and               &&
  or                ||
  not               !
 
 
 
 
 
 
 
 
 
 
 
 

Loops in C

There are three types of loops, they are For Loop, While Loop and Do...While Loop.

Loops are used to repeat a block of code. Being able to have your program repeatedly execute a block of code is one of the most basic but useful tasks in programming 


The syntax for a for loop is


for ( variable initialization; condition; variable update )
 {
  Code to execute while the condition is true
 } 

Example:
 
#include<stdio.h>
#include<conio.h>
void main()
{
    int x;
    for ( x = 0; x < 10; x++ )          /* The loop goes while x < 10, and x increases by one every loop*/
     
{
                                        /* Keep in mind that the loop condition checks 
        printf( "%d", x );                 the conditional statement before it loops again.
                                           consequently, when x equals 10 the loop breaks.
                                           x is updated before the condition is checked. */   
        
 }
    getch();
} 



While loop are very simple syntax is

while ( condition )
 {
  Code to execute while the condition is true
 }



Example:
 
 
#include<stdio.h>
#include<conio.h>
void main()
{ 
  int x = 0;                   /* Don't forget to declare variables */
  
  while ( x < 10 ) 
{                              /* While x is less than 10 */
      printf( "%d", x );
      x++;                     /* Update x so the condition can be met eventually */
 }
  getch();
} 






DO WHILE LOOP syntax is


do 
{
} 
while ( condition ); 
 
 
The condition is tested at the end of the block instead of the
beginning, so the block will be executed at least once. If the condition is
true, we jump back to the beginning of the block and execute it again. A
do..while loop is almost the same as a while loop except that the loop body is
guaranteed to execute at least once. A while loop says "Loop while the
condition is true, and execute this block of code", a do..while loop says
"Execute this block of code, and then continue to loop while the condition is
true".
 
 
Example:
 
 
#include <stdio.h>
#include<conio.h>
void main()
{
  int x;
  x = 0; 
  
do 
{
    printf( "Hello, world!\n" );        /* "Hello, world!" is printed at least one time
                                            even though the condition is false */
      
 } while ( x != 0 );
  getch();
} 

Simple Example ...

Examples:

\\1.The hello program

#include<stdio.h>
#include<conio.h>
void main()
{
printf("Hello World");
getch();
}


\\2.Adding two intergers

#include<stdio.h>
#include<conio.h>
void main()
{
int a, b,c;
printf("enter two no.");
scanf("%d%d",&a,&b);
c=a+b;
printf("SUM is =%d",c);
getch();
}


\\3.  Print out powers of 2: 1, 2, 4, 8, .. up to 2^N


#include <stdio.h>
#include<conio.h>
#define N 16

void main()

{  int n;           /* The current exponent */ 


int val = 1;     /* The current power of 2  */ 

printf("\t  n  \t    2^n\n");


printf("\t================\n");

   
 for (n=0; n<=N; n++)

{
 printf("\t%3d \t %6d\n", n, val);    

 val = 2*val;
 


getch();
}




\\4.Read a positive number N. Then read N integers and print them out together with their sum.

#include <stdio.h>
#include<conio.h>
void main()
{
int n;       /* The number of numbers to be read */ 

int sum;     /* The sum of numbers already read  */
  int current; /* The number just read             */ 
int lcv;     /* Loop control variable, it counts the number of numbers already read */ 

printf("Enter a positive number n > ");

scanf("%d",&n); /* We should check that n is really positive*/

sum = 0;

for (lcv=0; lcv < n; lcv++)
{

   printf("\nEnter an integer > ");

    scanf("%d",&current);    /*    printf("\nThe number was %d\n", current); */
    sum = sum + current;  } 
   printf("The sum is %d\n", sum);

getch();
}

Scanf working

The scanf function allows you to accept input from standard in, which for us is generally the keyboard. The scanf function can do a lot of different things, but it is generally unreliable unless used in the simplest ways. It is unreliable because it does not handle human errors very well. But for simple programs it is good enough and easy-to-use.
Scanf syntax:
scanf("%d", &b);
The program will read in an integer value that the user enters on the keyboard (%d is for integers, as is printf, so b must be declared as an int) and place that value into b.
The scanf function uses the same placeholders as printf:
  • int uses %d
  • float uses %f
  • char uses %c
  • character strings use %s
You MUST put & in front of the variable used in scanf because it shows the adress of variable.

In general, it is best to use scanf() as shown here -- to read a single value from the keyboard. Use multiple calls to scanf to read multiple values. In any real program, you will use the gets() or fgets() functions instead to read text a line at a time. Then you will "parse" the line to read its values. The reason that you do that is so you can detect errors in the input and handle them as you see fit.
The printf and scanf functions will take a bit of practice to be completely understood, but once mastered they are extremely useful.

                                    Try this

 

Modify this program so that it accepts three values instead of two and multiply all three together:

#include <stdio.h>
#include<conio.h>
void main()
{
    int a, b, c;
    printf("Enter the first value:");
    scanf("%d", &a);
    printf("Enter the second value:");
    scanf("%d", &b);
    c = a * b;
    printf("%d * %d = %d\n", a, b, c);
    getch();
}
You can also delete the b variable in the first line of the above program and see what the compiler does when you forget to declare a variable. Delete a semicolon and see what happens. Leave out one of the braces. Remove one of the parentheses next to the main function. Make each error by itself and then run the program through the compiler to see what happens.

Printf working

printf()  Working

#include<stdio.h>
#include<conio.h>
void main()
{
    int a, b, c;
    a = 10;
    b = 10;
    c = a + b;
    printf("%d + %d = %d\n", a, b, c);
    getch();
}


Type this program into a file and save it as add.c. Compile it with the line gcc add.c -o add and then run it by typing add (or ./add). You will see the line "5 + 7 = 12" as output.
Here is an explanation of the different lines in this program:
  • The line int a, b, c; declares three integer variables named a, b and c. Integer variables hold whole numbers.
  • The next line initializes the variable named a to the value 10.
  • The next line sets b to 10.
  • The next line adds a and b and "assigns" the result to c. The computer adds the value in a (10) to the value in b (10) to form the result 20, and then places that new value (20) into the variable c. The variable c is assigned the value 20. For this reason, the = in this line is called "the assignment operator."
  • The printf statement then prints the line "10 + 10 = 20." The %d placeholders in the printf statement act as placeholders for values. There are three %d placeholders, and at the end of the printf line there are the three variable names: a, b and c. C matches up the first %d with a and substitutes 10 there. It matches the second %d with b and substitutes 10. It matches the third %d with c and substitutes 20. Then it prints the completed line to the screen: 10 + 10 = 20. The +, the = and the spacing are a part of the format line and get embedded automatically between the %d operators as specified by the programmer.

Why variable are important?

You will frequently want your program to "remember" a value. For example, if your program requests a value from the user, or if it calculates a value, you will want to remember it somewhere so you can use it later. The way your program remembers things is by using variables. For example:
int b;
This line says, "I want to create a space called b that is able to hold one integer value." A variable has a name (in this case, b) and a type (in this case, int, an integer). You can store a value in b by saying something like:
b = 5;
You can use the value in b by saying something like:
printf("%d", b);
In C, there are several standard types for variables:
  • int - integer (whole number) values
  • float - floating point values
  • char - single character values (such as "m" or "Z")

Example

#include<stdio.h>
#include<conio.h>
void main()
{
    int a, b, c;      //these are variables 
    printf("enter two value");
    scanf("%d%d",&a,&b); 
    c = a + b;
    printf("%d + %d = %d\n", a, b, c);
    getch();
}

How C language Works

  • This C program starts with "#include <stdio.h>". This line includes the "standard I/O library" into your program. The standard I/O library lets you read input from the keyboard (called "standard in"), write output to the screen (called "standard out"), process text files stored on the disk, and so on. It is an extremely useful library. C has a large number of standard libraries like stdio, including string, time and math libraries. A library is simply a package of code that someone else has written to make your life easier (we'll discuss libraries a bit later).
  •  The line "void main()" declares the main function. Every C program must have a function named main somewhere in the code. We will learn more about functions shortly. At run time, program execution starts at the first line of the main function.
  • In C, the "{ "and "}" symbols mark the beginning and end of a block of code. In this case, the block of code making up the main function contains two lines.
  • The "printf" statement in C allows you to send output to standard out (for us, the screen). The portion in quotes is called the format string and describes how the data is to be formatted when printed. The format string can contain string literals such as "This is output from my first program!," symbols for carriage returns (\n), and operators as placeholders for variables (see below). If you are using UNIX, you can type man 3 printf to get complete documentation for the printf function. If not, see the documentation included with your compiler for details about the printf function.
  • "getch()" is a funtion which freez the screen to show the results..

What is C?????

                                        What is C???


Some of common computer languages and their translators will be described in this computer. A set of instruction to be executed by a computer is known as a program of c. A combination of different programs satisfying a common objective/task is called software.


Application software

A sdet of programs, which is necessary to perform various general-purpose operation or tasks, is called application software. for examples: Inventory control, pay roll etc..


System software

System software is a set of one or more programs that provides the facility to develop application programs or software for specfic task. for example: Compiler, Interpreter,Operating system and other Utility etc..



Computer languages

programming provides new tools to express ours ideas. Expressingb methodology in a computer language forces it to be unambiguous and  computationally effective. The task of formulating a method as a computer-executable program and debugging that program is a powerfull exercise in the learning process.


(1) computer circuits are designed on the basis of 2 bits 0 and 1. the output of such digital electronic circuit, at any time is either high or low. 1 and 0 can be re present these states respectively.


(2) This binary system simplifies the design of the circuits, reduece the cost and improves the reliability.


(3) Every opertion that can be performed that can be performed in decimal system can also be perform in binary.


(4) In  punched cards, a hole represents binary 1 and the absence of hole represents binary 0. This concept was used in earlier computers..



The C programming language is also a high-level programming language developed by Dennis Ritchie and Brian Kernighan at Bell Labs in 1972 from an almost unknown language named B. The first major program written in C was the Unix operating system, and for many years, C was considered to be inextricably linked with Unix. Below is an example of a C program that prints Hello World! after it's been compiled. If you need a free C compiler consider GCC.



C programming is one of thousands of computer programming languages that allow users to create instructions for a computer to follow. While C has a slightly more cryptic style than some other programming languages, it's fairly easy to learn and allows you to read and write code for many different platforms. Because it's so efficient and gives the user a lot of control, C is very popular with programmers.


C compiler to turn your program into an executable that the computer can run (execute). The C program is the human-readable form, while the executable that comes out of the compiler is the machine-readable and executable form. What this means is that to write and run a C program, you must have access to a C compiler. If you are using a UNIX machine (for example, if you are writing CGI scripts in C on your host's UNIX computer, or if you are a student working on a lab's UNIX machine), the C compiler is available for free. It is called either "cc" or "gcc" and is available on the command line. If you are a student, then the school will likely provide you with a compiler -- find out what the school is using and learn about it. If you are working at home on a Windows machine, you are going to need to download a free C compiler or purchase a commercial compiler. A widely used commercial compiler is Microsoft's Visual C++ environment (it compiles both C and C++ programs). Unfortunately, this program costs several hundred dollars. If you do not have hundreds of dollars to spend on a commercial compiler....


You will probably get some sort of error when you compile it, so make sure you remember the .c. Also, make sure that your editor does not automatically append some extra characters (such as .txt) to the name of the file. Here's the first program:
 
 
#include <stdio.h>
#include <conio.h> 
 void main()
{
    printf("This is output from my first program!\n");
    getch();
}