Technical

Header Ads

What is C programming language

Introduction to c language

Chapter:-1                                                                                           

 Introduction of c Language:                                                              

 c:- C is a computer programming language. That means that you can use C to create lists of instructions for a computer to follow. C is one of thousands of programming languages currently in use. C has been around for several decades and has won widespread acceptance because it gives programmers maximum control and efficiency. C is an easy language to learn. It is a bit more cryptic in its style than some other languages, but you get beyond that fairly quickly.

C is what is called a compiled language. This means that once you write your C program, you must run it through a 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).                     

Structure of c Programme:                                                                                                                                A C Programmes contain a number Of blocks known as Function.

/* comments*/
header files
main function()
{
Declaration part
executable part
}
user define function
{
}   

*Comments line:-

                          To Understand the flow Of the Programme Are Use comments.It Is Useful For Do Commentation comments Are nothing But some Kind Of statements Which are place Between Delimeters.The Compiler Does not execitude comments.
Example:-
              Single line comments../*comments*/
  • This Is a Single Line comments.

Nested comments:-

                                Some Of two Digit/* Multiply of two digit*/*/

Multiline Comments
/*..............
 .....................
  .......................*/

Main Function:-

                         Every Programme Must contain Main function.It the starting point of c Programme.A Programme have more than one function but Main Execution starts with the opening Braces.

Heade File section:-

                                 'C' Programme depends some headers files that are used in programme.The Should be included using '#' include Direct.A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source files. There are two types of header files: the files that the programmer writes and the files that comes with your compiler.
You request to use a header file in your program by including it with the C preprocessing directive #include, like you have seen inclusion of stdio.h header file, which comes along with your compiler.
Including a header file is equal to copying the content of the header file but we do not do it because it will be error-prone and it is not a good idea to copy the content of a header file in the source files, especially if we have multiple source files in a program.
A simple practice in C or C++ programs is that we keep all the constants, macros, system wide global variables, and function prototypes in the header files and include that header file wherever it is required.

Include Syntax

Both the user and the system header files are included using the preprocessing directive #include. It has the following two forms −
#include <file>
This form is used for system header files. It searches for a file named 'file' in a standard list of system directories. You can prepend directories to this list with the -I option while compiling your source code.
#include "file"
This form is used for header files of your own program. It searches for a file named 'file' in the directory containing the current file. You can prepend directories to this list with the -I option while compiling your source code.           
Example:-# include <stdio.h>
                          In this example stdio.h file is included that is all the defination and prototype of function define in this file are available in the current programme.


Programming Rules:-

  1.   All statements should be written in lower case letters.Upper case letters are only used for symbolic constant.

  1.  Blank Spaces may be inserted between Two words but it is not declaring a varriables-keyword,constant and function.

  1. It is not necessary to to Fix the position of statement in the programme that is the programmer can write the statement anywhere in the programme so is often caled Free form language.

  1. The opening and closing braces should be balance.  

User Define Function:-

    A function is a block of code that performs a specific task.
C allows you to define functions according to your need. These functions are known as user-defined functions. For example:
Suppose, you need to create a circle and color it depending upon the radius and color. You can create two functions to solve this problem:
  • createCircle() function
  • color() function

Example: User-defined function

Here is an example to add two integers. To perform this task, an user-defined function addNumbers() is defined.
#include <stdio.h>

int addNumbers(int a, int b); // function prototype

int main()
{
int n1,n2,sum;

printf
("Enters two numbers: ");
scanf
("%d %d",&n1,&n2);

sum
= addNumbers(n1, n2); // function call

printf
("sum = %d",sum);

return 0;
}

int addNumbers(int a,int b) // function definition
{
int result;
result
= a+b;
return result; // return statement
}


Function prototype

A function prototype is simply the declaration of a function that specifies function's name, parameters and return type. It doesn't contain function body.
A function prototype gives information to the compiler that the function may later be used in the program.

Syntax of function prototype

returnType functionName(type1 argument1, type2 argument2,...);
In the above example, int addNumbers(int a, int b); is the function prototype which provides following information to the compiler:
  1. name of the function is addNumbers()
  2. return type of the function is int
  3. two arguments of type int are passed to the function
The function prototype is not needed if the user-defined function is defined before the main() function.

Calling a function

Control of the program is transferred to the user-defined function by calling it.

Syntax of function call

functionName(argument1, argument2, ...);
In the above example, function call is made using addNumbers(n1,n2); statement inside the main().

Function definition

Function definition contains the block of code to perform a specific task i.e. in this case, adding two numbers and returning it.

Syntax of function definition

returnType functionName(type1 argument1, type2 argument2, ...)
{
//body of the function
}
When a function is called, the control of the program is transferred to the function definition. And, the compiler starts executing the codes inside the body of a function.

Passing arguments to a function

In programming, argument refers to the variable passed to the function. In the above example, two variables n1 and n2 are passed during function call.
The parameters a and b accepts the passed arguments in the function definition. These arguments are called formal parameters of the function.
Passing arguments to a function
The type of arguments passed to a function and the formal parameters must match, otherwise the compiler throws error.
If n1 is of char type, a also should be of char type. If n2 is of float type, variable b also should be of float type.
A function can also be called without passing an argument.

Return Statement

The return statement terminates the execution of a function and returns a value to the calling function. The program control is transferred to the calling function after return statement.
In the above example, the value of variable result is returned to the variablesum in the main() function.
Return statement of a function

Syntax of return statement

return (expression);     
For example,
return a;
return (a+b);
The type of value returned from the function and the return type specified in function prototype and function definition must match.
Visit this page to learn more on passing arguments and returning value from a function.

Post a Comment

0 Comments