C structure : C Language has a basic structure as follow-
In C all statement are executed one by one. If error occurs in one statement then the next statement will not execute and program halt. So care should be taken in each step.
#definition Directive
This instruction are usually placed at the beginning before the main() function. It is usually written in uppercase and should not end with a semicolon.
Example :-
#define PI 3.1416
#define MAX 180
#define PRINCPAL=1000.00;(illegal)
#include Directive
#include<filename>
Filename is the name of the library file that contents the required function definition. Preprocessor directives are placed at the beginning of a program.
#include “filename.h”
Filename is your own library function or the library function of the preprocessor. We will demonstrate it later.
main function
The main is a part of every C program. C permits different forms of main statement. The basic structure of main is-
Note that void can be representing as void or empty first bracket i.e. ()
Example:- int main(void) {
Executable part;
return 0;
}
Rules—Every statement in C program must end with semicolon except #include or #define directive and main statement.
Variable installation
Data_type variable_name=initial_value;
Primary data type
Note that every signed variable includes positive or negative number but unsigned variable include only positive number.
Example:-
int a;
int a,b,c;
unsigned char sample;
float pi=3.1416;
double a,b,c=2.1;
If we don’t include initial value in variable declaration by default its value should be zero.
In C not only do all variable have a data type, they also have a storage class. The following variable storage classes are most relevant to function-
Automatic variable
External variable
Static variable
Register variable
Automatic Variable :- Automatic variable are declared inside a function in which they are to be utilized. They are created when the function is called and destroyed automatically when the function is exited.
Example:-
int main(void) {
char classroom;
int c,d,e=123;
return 0;
}
External Variable :- Variable that are both alive and active thought the entire program are known as External variable. They are also known as global variable. External variable are declared outside a function.
Example:-
float length=7.5;
int main(void){
return 0;
}
Static Variable :- static variable may be internal or external depending on there declarations i.e. static int count = 0;
Register variable :- it is a register type local variable that store in cpu/mcu storage cells which is often called register. i.e. register int a=30;
Interger type variables have the following format that used bit wise operation and math calculation.
Arithmetic operators :
Note that Module division only applies to integer data type.
Example:-
#include<stdio.h> ——->Header file
int main(void)
{
int a,b,c; ——–> Declaration of variables
a=10; ——–> Initialization of variable
b=25; ——–> Initialization of variable
c=a+b; ——–> c=35
c=b-a; ——–> c=15
c=a*b; ——–> c=250
c=a/b; ——–> c=2
c=a%b; ——-> c=5(remainder)
return 0;
}
Note that stdio.h is the standard I/O library function.
Relational operators
Relational operators are used in condition or looping operation that will discuss later.
Shorthanded Assignment operators
Comments
For a single line comments use a double back space
i.e. //EEE RUET
For multiline comments begin with /* and ends with */
i.e. /***************** EEE RUET ****************************/
Increment and decrement operator
++m; // increment at run time
–m; //decrement at run time
m++; //increment by one at the next line
m++; //decrement by one at the next line