Showing posts with label C Programming Language. Show all posts
Showing posts with label C Programming Language. Show all posts

Monday, August 18, 2014

A Program of Sum, Subtraction, Production and Division.

#include <stdio.h>

#include <stdlib.h>

int main()

{

int a,b,add,sub,pro;

float div;

printf("Enter 1st number: ");

scanf("%d",&a);

printf("Enter 2nd number: ");

scanf("%d",&b);

add=a+b;

sub=a-b;

pro=a*b;

div=(float)a/(float)b;

printf("\nSum=%d",add);

printf("\nSubstration=%d",sub);

printf("\nProduction=%d",pro);

printf("\nDivision=%.2f",div);

getch();

return 0;

}

Sunday, August 10, 2014

Multi Function In C Programming Language

#include<stdio.h>
add()
{
    int x=10,y=12,res;
    printf("enter the value of x\n");
    scanf("%d",&x);
    printf("enter the value of y\n");
    scanf("%d",&y);
    res=x+y;
    printf("the result is %d",res);
}
substract()
{
    int num1,num2,result;
    printf("enter the value of num1\n");
    scanf("%d",&num1);
    printf("enter the value of num2\n");
    scanf("%d",&num2);
    result=num1-num2;
    printf("the result is %d",result);
}
run_it()
{
    add();
}
main()
{
    char answer[20];
    printf("what do you want to do?? (type add or substract)");
    scanf("%s",&answer);
    if(strcmp(answer, "add")==0)
    {
        run_it();
    }
    else if(strcmp(answer, "substract")==0)
    {
        substract();
    }
    else
    {
        printf("i can not work\n");
    }
    fflush(stdin);
    getchar();
}

Simple C Program Using Function

#include<stdio.h>
add()
{
    int x=10,y=12,res;
    printf("enter the value of x\n");
    scanf("%d",&x);
    printf("enter the value of y\n");
    scanf("%d",&y);
    res=x+y;
    printf("the result is: %d",res);
}
main()
{
    add();
    fflush(stdin);
    getchar();
}

Friday, August 8, 2014

Functions In C Programming Language

Defining Function:

For creating a function the needed items are given below:
1.Write some code statements.
2.Enclose them in parentheses "{...}".
3. Give the result a unique name.
4.Ensure that it is not placed within main or any other function.

The general form of a function definition in C programming language is as follows:

return_type function_name( parameter list )
{
   body of the function
}

For example:
#include<stdio.h>
myfunction()
{
    printf("helloworld");
}
main()
{
    myfunction();
    return 0;
}

A function definition in C programming language consists of a function header and a function body. Here are all the parts of a function:

Return Type: A function may return a value. The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void.

Function Name: This is the actual name of the function. The function name and the parameter list together constitute the function signature.

Parameters: A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters.


Function Body: The function body contains a collection of statements that define what the function does.

Function Declarations:

A function declaration tells the compiler about a function name and how to call the function. The actual body of the function can be defined separately.

A function declaration has the following parts:

return_type function_name( parameter list );
For the above defined function max(), following is the function declaration:

int max(int num1, int num2);
Parameter names are not important in function declaration only their type is required, so following is also valid declaration:
int max(int, int);
Function declaration is required when you define a function in one source file and you call that function in another file. In such case you should declare the function at the top of the file calling the function.

Calling Function:
While creating a C function, you give a definition of what the function has to do. To use a function, you will have to call that function to perform the defined task.

When a program calls a function, program control is transferred to the called function. A called function performs defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns program control back to the main program.

To call a function, you simply need to pass the required parameters along with function name, and if function returns a value, then you can store returned value. For example:

#include<stdio.h>
main()
{
    int a=12,b=16,rate;
    rate=min(a,b);
    printf("%d",rate);
}
int min(int a, int b)
{
    int res;
    if(a>b)
    {
        res=b;
    }
    if(b>a)
    {
        res=a;
    }
    return res;
}

About Functions In C Programming Language

**Functions(known in other languages as "subroutines" and "procedures") are passages of code that have been given a name.
**Printf is a function,so is main.It is called the library function.
**The benefit of the functions---Creating functions breaks a program up into small and simple "chunks".
**Ideally, most the functions are reusable,thus reducing repetitive code in a program.
Here it is the functions and it's benefits.

Structures and Functions In C Programming

A structure is passed to a function just like any other variable,for example:
          print_emp(empl);

Wednesday, August 6, 2014

File Pointer For C Programming

1.Redirection:
One way to get input into a program or to display output from a program is to use standard input and standard output, respectively. All that means is that to read in data, we use scanf() (or a few other functions) and to write out data, we use printf().

When we need to take input from a file (instead of having the user type data at the keyboard) we can use input redirection:

% a.out < inputfile
This allows us to use the same scanf() calls we use to read from the keyboard. With input redirection, the operating system causes input to come from the file (e.g., inputfile above) instead of the keyboard.

Similarly, there is output redirection:

% a.out > outputfile
that allows us to use printf() as before, but that causes the output of the program to go to a file (e.g., outputfile above) instead of the screen.

Of course, the 2 types of redirection can be used at the same time...

% a.out < inputfile > outputfile
2.C File I/O:
While redirection is very useful, it is really part of the operating system (not C). In fact, C has a general mechanism for reading and writing files, which is more flexible than redirection alone.

stdio.h

There are types and functions in the library stdio.h that are used for file I/O. Make sure you always include that header when you use files.
Type

For files you want to read or write, you need a file pointer, e.g.:
FILE *fp;
What is this type "FILE *"? Realistically, you don't need to know. Just think of it as some abstract data structure, whose details are hidden from you. In other words, the only way you can use a FILE * is via the functions that C gives you.

Note: In reality, FILE is some kind of structure that holds information about the file. We must use a FILE * because certain functions will need to change that information, i.e., we need to pass the information around by reference.
Functions

Reading from or writing to a file in C requires 3 basic steps:
Open the file.
Do all the reading or writing.
Close the file.
Following are described the functions needed to accomplish each step.

A complete program that includes the example described below.

3.Opening a file:

In order to open a file, use the function fopen(). Use it as:

fp = fopen(filename, mode);
where:

filename is a string that holds the name of the file on disk (including a path like /cs/course if necessary).
mode is a string representing how you want to open the file. Most often you'll open a file for reading ("r") or writing ("w").
Note that fopen() returns a FILE * that can then be used to access the file. When the file cannot be opened (e.g., we don't have permission or it doesn't exist when opening for reading), fopen() will return NULL.

Here are examples of opening files:

FILE *ifp, *ofp;
char *mode = "r";
char outputFilename[] = "out.list";

ifp = fopen("in.list", mode);

if (ifp == NULL) {
  fprintf(stderr, "Can't open input file in.list!\n");
  exit(1);
}

ofp = fopen(outputFilename, "w");

if (ofp == NULL) {
  fprintf(stderr, "Can't open output file %s!\n",
          outputFilename);
  exit(1);
}
Note that the input file that we are opening for reading ("r") must already exist. In contrast, the output file we are opening for writing ("w") does not have to exist. If it doesn't, it will be created. If this output file does already exist, its previous contents will be thrown away (and will be lost).

Note: There are other modes you can use when opening a file, such as append ("a") to append something to the end of a file without losing its contents...or modes that allow you to both read and write. You can look up these other modes in a good C reference on stdio.h.
4.Reading from or writing to a file:
Once a file has been successfully opened, you can read from it using fscanf() or write to it using fprintf(). These functions work just like scanf() and printf(), except they require an extra first parameter, a FILE * for the file to be read/written.

Note: There are other functions in stdio.h that can be used to read or write files. Look them up in a good C reference.
Continuing our example from above, suppose the input file consists of lines with a username and an integer test score, e.g.:

in.list
------
foo 70
bar 98
...
and that each username is no more than 8 characters long.
We might use the files we opened above by copying each username and score from the input file to the output file. In the process, we'll increase each score by 10 points for the output file:

char username[9];  /* One extra for nul char. */
int score;

...

while (fscanf(ifp, "%s %d", username, &score) != EOF) {
  fprintf(ofp, "%s %d\n", username, score+10);
}

...
The function fscanf(), like scanf(), normally returns the number of values it was able to read in. However, when it hits the end of the file, it returns the special value EOF. So, testing the return value against EOF is one way to stop the loop.

The bad thing about testing against EOF is that if the file is not in the right format (e.g., a letter is found when a number is expected):

in.list
------
foo 70
bar 98
biz A+
...
then fscanf() will not be able to read that line (since there is no integer to read) and it won't advance to the next line in the file. For this error, fscanf() will not return EOF (it's not at the end of the file)....

Errors like that will at least mess up how the rest of the file is read. In some cases, they will cause an infinite loop.

One solution is to test against the number of values we expect to be read by fscanf() each time. Since our format is "%s %d", we expect it to read in 2 values, so our condition could be:

while (fscanf(ifp, "%s %d", username, &score) == 2) {
  ...
Now, if we get 2 values, the loop continues. If we don't get 2 values, either because we are at the end of the file or some other problem occurred (e.g., it sees a letter when it is trying to read in a number with %d), then the loop will end.

Another way to test for end of file is with the library function feof(). It just takes a file pointer and returns a true/false value based on whether we are at the end of the file.

To use it in the above example, you would do:

while (!feof(ifp)) {
  if (fscanf(ifp, "%s %d", username, &score) != 2)
    break;
  fprintf(ofp, "%s %d", username, score+10);
}
Note that, like testing != EOF, it might cause an infinite loop if the format of the input file was not as expected. However, we can add code to make sure it reads in 2 values (as we've done above).

Note: When you use fscanf(...) != EOF or feof(...), they will not detect the end of the file until they try to read past it. In other words, they won't report end-of-file on the last valid read, only on the one after it.
5.Closing a file:
When done with a file, it must be closed using the function fclose().

To finish our example, we'd want to close our input and output files:

fclose(ifp);
fclose(ofp);
Closing a file is very important, especially with output files. The reason is that output is often buffered. This means that when you tell C to write something out, e.g.,

fprintf(ofp, "Whatever!\n");
it doesn't necessary get written to disk right away, but may end up in a buffer in memory. This output buffer would hold the text temporarily:

Sample output buffer:
----------------------------------------------
| a | b  | c | W | h | a | t | e | v | e | r |
----------------------------------------------
| ! | \n |   |   |   |   |   |   |   |   |   |
----------------------------------------------
|   |    |   |   |   |   |   |   |   |   |   |
----------------------------------------------
|   |    |   |   |   |   |   |   |   |   |   |
----------------------------------------------
...
(The buffer is really just 1-dimensional despite this drawing.)

When the buffer fills up (or when the file is closed), the data is finally written to disk.

So, if you forget to close an output file then whatever is still in the buffer may not be written out.

Note: There are other kinds of buffering than the one we describe here.
6.Special file pointers:
There are 3 special FILE *'s that are always defined for a program. They are stdin (standard input), stdout (standard output) and stderr (standard error).

Standard Input

Standard input is where things come from when you use scanf(). In other words,

scanf("%d", &val);
is equivalent to the following fscanf():

fscanf(stdin, "%d", &val);
Standard Output

Similarly, standard output is exactly where things go when you use printf(). In other words,

printf("Value = %d\n", val):
is equivalent to the following fprintf():

fprintf(stdout, "Value = %d\n", val):
Remember that standard input is normally associated with the keyboard and standard output with the screen, unless redirection is used.

Standard Error

Standard error is where you should display error messages. We've already done that above:
fprintf(stderr, "Can't open input file in.list!\n");
Standard error is normally associated with the same place as standard output; however, redirecting standard output does not redirect standard error.

For example,

% a.out > outfile
only redirects stuff going to standard output to the file outfile... anything written to standard error goes to the screen.

Using the Special File Pointers

We've already seen that stderr is useful for printing error messages, but you may be asking, "When would I ever use the special file pointers stdin and stdout?" Well, suppose you create a function that writes a bunch of data to an opened file that is specified as a parameter:
void WriteData(FILE *fp)
{
  fprintf(fp, "data1\n");
  fprintf(fp, "data2\n");
  ...
}
Certainly, you can use it to write the data to an output file (like the one above):

WriteData(ofp);
But, you can also write the data to standard output:

WriteData(stdout);
Without the special file pointer stdout, you'd have to write a second version of WriteData() that wrote stuff to standard output.

Tuesday, July 15, 2014

Download And Install Code::Blocks

Code::Blocks: It is free open source.C,C++ and fortran IDE built to meet the most demanding needs of its users.The code::blocks is designed to be very extensible and fully configurable.

Finally an IDE with all the features you need having a consistent look, feel and operation across platforms

It is built on a plugin framework.Code::Blocks can be extended with plugins.Any kind of functionality can be added by installing /coding a plugin.For instance, compiling and debugging functionality is already provided by plugins!

Special credit go to dramar for his great work on the fortranproject plugin.

This is a compiler.So,you can run any C or C++ program.I hope  you enjoy IT.

Now I am going to show you "how to download and install Code::Blocks".So, Follow me.

1.Go to this download link.
2.Then go to downloads menu.
3.After that go to "download the binary release".
4.For windows you have to download the file which shown for windows(32 bit or 64 bit) and others for others.
5.After download the code blocks.then install it.

Here is some screenshots for your help.So,follow the screenshots below.

DOWNLOAD LINK

                                                                    Step One


Step Two

Step Three
                                        

Saturday, July 12, 2014

Print Hello World

#include<stdio.h>
int main()
{
        printf("hello world");
        return 0;
}