Introduction to C Programming
- 'C' is a programming language, used to write programs that are executed by the computer.
- 'C' is the most preferred language among programmers today, because of its flexibility, efficiency and well structured programs with the simple syntax.
Creating and Running Programs
writing a simple C program
#include<stdio.>
int manin()
{
printf("This is My First C program\n");
return 0;
}
OUTPUT
This is My First C program
Documentations
The documentation section, consist of a set of comment lines giving the name of the program, authors name and other details, which the programmer would like to use later. There are two types of comments such as:
Single line comments
Multi-line comments or block comments
Single line Comments: These are represented using'//'
//The value 5 is assigned to 'a'
a=5;
or
a=5; //The value 5 is assigned to 'a'
Multi-line Comments or Block Comments: These are used to write multiple lines of comments.
/* Title: Good Morning
Author: Kiran Kumar
Date: 18/07/2014 */
#include<stdio.h>
int main()
{
printf("Good Morning\n");
}
Structure of a C Program
Preprocessor Directives
- The preprocessor statement begins with a # symbol and is also called the preprocessor directive
- These statements instruct the compiler to include C preprocessors such as header files and symbolic constants before compiling the C program
- A header file is a file, with the extension ".h", which contains C function declarations.
For Example: When we include "stdio.h" in the program, all input/output functions will be added.
Some of the preprocessor statements are listed below:
Header files:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
Symbolic Constants
#define PI 3.14
#define TRUE 1
#define FALSE 0
Global Declaration:Global variables can be accessed by all user defined functions including the main() function.
All the variables declared before the main () function, as well as user defined functions are called global variables.
The main( ) function
Each and every C program should contain only one main () function.
The C program execution starts with the main() function. 'C' program cannot be executed without the main function.
The main( ) function should be written in small letters and it should not be terminated by a semicolon i.e.;
The main( ) function executes user defined program statements library functions and user defined functions
These statements should be enclosed within the left and right braces.
Braces
Every C program should have a pair of curly braces { }.
The left brace indicates the beginning of the main ( ) function and the right brace indicates the end of the main ( ) function.
These braces can also be used to indicate beginning and ending of a user-defined function.
These two braces can also be used in compound statements.
Local Declarations
Local variables are declared inside the main( ) function
Not only variables, we can also declare arrays, functions and pointers etc.
These variables can also be initialized with basic data types
Example:
main( )
{ //beginning of the main( )
int sum = 0; //local declarations
int x;
int y;
float y;
} //ending of main
Here, the variable sum is declared as an integer variable and it is initialized to zero. Other variables declared as 'int' and 'float' , inside the function are called local variables.
Statements
- An instruction may contain input/output statements, arithmetic statements, assignment statements, control statements and any other statements.
- An instruction may also include comments that are enclosed within"/" these statements are not compiled and executed.
- Other functions help to decompose a large into small segments, which makes it easy for programmers to understand.
//program to demonstrate the working of user defined function
#include <stdio.h>
int sum (int, int);
int main (void)
{
int total;
total = sum (2, 3);
printf ("Total is %d\n", total);
return 0;
}
int sum(int a, int b)
{
return a+b;
}
Post a Comment
Post a Comment