ankitweblogic logo
W e b t u t o r i a l s

Programing Approach


Flow Chart

Define C

Define Programming

C - Syntax

C - Variables

C - Keywords

C - Data Types

C - Comments

Turbo C - Shortcut keys

C - Operators

Hierarchy of Operators

Ex: Arithmetic Operator

Constant Variable

C - Escape Sequence

Control Structure - if

Ex: If condition

Ex: Inc / Dec Operator

C - loops

Ex: Loops

C - Nesting Of loops

Ex: Nested Loops

Jumping Statements

C - Arrays 1D

C - Arrays 2D

C - Sorting


Character I/O Function

C - Strings

Ex: Strings

Array of Strings

C - Math Functions

User-defined Function

Exercise Function

C - Types of Functions

Storage Class

C - Header Files

C - Preprocessor

C - Pointers

C - Structures

C - Typedef

C - File Handling

Ex: File Handling

Command Line Argument


C PreProcessor

C PreProcessor process our source code before it is passed to the compiler. Each preprocessor statement begins with a # symbol.

Following are the lists of important preprocessor directives:

Directive Description
#define Substitutes a preprocessor macro.
#include Inserts a particular header from another file.
#undef Undefines a preprocessor macro.
#ifdef Returns true if this macro is defined.
#ifndef Returns true if this macro is not defined.
#if Tests if a compile time condition is true.
#else The alternative for #if
#elif #else an #if in one statement
#endif Ends preprocessor conditional
#error Prints error message on stderr
#pragma Issues special commands to the compiler, using a standardized method

Macro Expansion: It is also known as simple macro. Syntax to define Macro Expansion is:
#define pi 3.14 declare constant variable pi with 3.14 value.
#define and && declare && operator with english word and.
#define or || declare || operator with english word or.

Macro with Argument: Macro's can have argument just like functions. Syntax to define Macro's with argument is:
#define area(x) 3.14*r*r declare a macro area with one argument and also define its defination in one line.
#define sq(a) a*a declare a marcro sq with one argument to calculate square of a number.
#define sum(a,b) a+b declare a macro sum with two arguments to calculate sum of two numbers.

Macro versus Function

Sno Macro Function
1. In macro call the preprocessor replaces the macro template with its macro expansion. In function call the control is passed to a function along with its arguments and returned back from the function.
2. Execution is faster than Functions. Execution is slower than Macros.
3. Make the program size bigger. Make the program compact.
4. Macro’s are brief. Function can be lengthy.
5. Macro is simple Function is complex
6. Make the source code small Function make the source code lengthy

#undef FILE_SIZE
#define FILE_SIZE 42
This tells the CPP to undefine existing FILE_SIZE and define it as 42.

#ifndef MESSAGE
#define MESSAGE "You wish!"
This tells the CPP to define MESSAGE only if MESSAGE isn't already defined.

Predefined Macros
ANSI C defines a number of macros. Although each one is available for your use in programming, the predefined macros should not be directly modified.
Macro Description
__DATE__ The current date as a character literal in "MMM DD YYYY" format
__TIME__ The current time as a character literal in "HH:MM:SS" format
__FILE__ This contains the current filename as a string literal.
__LINE__ This contains the current line number as a decimal constant.
__STDC__ Defined as 1 when the compiler complies with the ANSI standard.

Let's try the following example:
#include <stdio.h>

printf("File :%s\n", __FILE__ );
printf("Date :%s\n", __DATE__ );
printf("Time :%s\n", __TIME__ );
printf("Line :%d\n", __LINE__ );
printf("ANSI :%d\n", __STDC__ );

Miscellaneous Directives
#pragma directive
a) #pragma startup This directive allow us to execute function before main () function.
syntax: #pragma startup fun

b) #pragma exit This directive allow us to execute function after main () function.
syntax: #pragma exit fun

Both startup and exit functions should neither receive nor return any value. We can use more than one function to be executed at startup but they will be executed in reverse order.

#pragma warn This directive tells the compiler whether the warning will be shown or not.
a) #pragma warn –rvl return value warning does not display even if return keyword is not used in a function.
b) #pragma warn – par parameter not used warning does not display even if variable not used in a parameter.
c) #pragma warn – rch unreachable code warning does not display even if any code of statement is unreachable.

Updated: 01-Feb-19