If you want to know the c program structure then read the complete article.
- Components of c program structure
- Pre-processor in c program structure
- Predefined macros in c
- preprocessor operators
- Variable in c program structure
- Functions in c program structure
- C program statements and expressions
- Comment lines in c program structure
- Example program for c program structure
- c programming tutorial
Components of c program structure
C progam contains 5 main components are listed below.
- Pre-processor.
- variables.
- Functions.
- C program statements and expressions.
- Comment lines.
Pre-processor in c program structure
The preprocessor is a simple instruction to compiler. It instructs that pre-processors have to be processed before compiling the program.
Compiler substiutes pre-processor text with its value.
Pre-processors begins with # symbols.
Various pre-processors are as follows:
Name | Description |
#define | It defines a pre-processor-macro. |
#undef | It undefines a pre-processor macro. |
#include | It uincludes a header file. |
#pragma | It is to give special commands to the compiler. |
#ifdef | It returns true if defined. |
#ifndef | It returns true if not defined. |
#if | pre-processor if block. |
#else | pre-processor else block. |
#elif | #else and #if in on estatement. |
#endif | It ends the if pre-processor. |
#error | It prints error message to stderr. |
No lets look at some pre-processor examples.
define MY_MACRO 100
#ifndef MY_MACRO
printf("My macro is not defined");
#endif
#ifdef MY_MACRO
printf("My macro is defined");
#endif
Try other macoros yourself.
Predefined macros in c
Macro-name | Description |
__FILE__ | It is the file name of the c program. |
__DATE__ | It returns the date in MM DD YYYY format. |
__LINE__ | It returns current line number as decimal constant. |
__TIME__ | It returns Time in HH:MM:SS format. |
__STDC__ | It is defined as 1 if the compiler compiles in ANSI Standard |
preprocessor operators
Name | Operator | Description | Example |
Macro continuation operator | \ | It is used to continue a macro if it doesnot fit in one line. | #define my_error(g) \ printf(#g " Error occurred\n") |
String size operator | # | It converts parameter of macro to string constant. | #define my_error(a, b) \ printf(#a " and " #b ":Are the errors\n") |
Token pasting operator | ## | It combines two arguments | #define token_x(n) printf ("token" #n " = %d", token##n) |
Defined operator | defined | It is used to determine a macro is previously defined or not. | #if !defined (MY_MESSAGE) #define MY_MESSAGE "Hello world!" #endif |
Parameterized macros: macros which allows to pass parameter like we pass to functions.
Example:
#define square_of_a_number(x) ((x) * (x))
Variable in c program structure
Variables are nothing but name given to storing area of different data.
There are two types of variables: 1) Global variable 2) Local variable.
Below is the example to declare a variable:
datatype variable name;
Example: int example;
You can directly initialize variable like below
int example=100;
Refer to explanation to variables in hello world program.
Functions in c program structure
Function is a set of code which is intended to perform a specific task.
Main function in c where the program begins to exeute.
Function prototype in c is as below:
return_type function name ( parameters )
{
.
.
.
}
Please refer to function example in hello world c program.
C program statements and expressions
Statements are the commands given to the computer to perform specific task.
Types of statements
- Expression Statements.
- Labeled Statements.
- Compound Statements.
- Jump Statements.
- Iteration Statements.
- Selection Statements.
Expression consists one more operand and operator which finally results in a value.
Expressions are used in control statements.
Comment lines in c program structure
Comment lines are for programmers to understand whats going on in the code.
These comment lines are not compiled and has no effect on the output of the c program.
Comment lines in your code is a good programming practice.
In c programming there are two types of comment lines:
- Single line comments. It begins with
//
- Multiple line comments. It begins with
/*
and ends with*/
Example of comment line in c
//This is simgle line comment in c programming.
/*This is multiple line comment in c programming.
Example program for c program structure
#include<stdio.h>//This is #include pre-processor macro
int global_variable=1;//This is a global variable.
void my_function()
{
printf("My function is called.\n");
}
int main(int argc, char const *argv[])
{
//this is a single line comment.
/*
This is a multiple line comments.
Line1
Line2
.
.
.
.
.
*/
int local_variable=100;//This is a local variable.
my_function();
if( local_variable==100 ) //if is a statement and localvariable==100 is an expression
{
printf("Local variable value is 100");
}
else
{
printf("Local variable value is not 100");
}
return 0;
}
c programming tutorial
- Datatypes in c
- String in c
- C programming history WiKi
Disclaimer: A tweet screenshot is being used is not the property of scholarsoul.com.