Even or Odd Number using Macros: C Program

Lets write a C program to find if the user input number is odd or even, using Macros.

Related Read:
Even or Odd Number using Ternary Operator: C Program

Logic To Find Even Or ODD

If user input number is perfectly divisible by 2, then it’s Even number orelse it’s Odd number.

Video Tutorial: Even or Odd Number using Macros: C Program


[youtube https://www.youtube.com/watch?v=QtiHjwNZ7zg]

YouTube Link: https://www.youtube.com/watch?v=QtiHjwNZ7zg [Watch the Video In Full Screen.]

Source Code: Even or Odd Number using Macros: C Program

#include<stdio.h>
#define ODD_EVEN(num) ( (num % 2 == 0) ? printf("Even\n") : printf("ODD\n") )

int main()
{
    int num;

    printf("Enter a positive number\n");
    scanf("%d", &num);

    ODD_EVEN(num);

    return 0;
}

Output 1:
Enter a positive number
5
ODD

Output 2:
Enter a positive number
6
Even

In above program the macro template ODD_EVEN(num) will be replaced by it’s macro expansion ( (num % 2 == 0) ? printf(“Even\n”) : printf(“ODD\n”) ) and hence if the user input number is perfectly divisible by 2, then EVEN will be printed else ODD will be printed.

Related Read
Ternary Operator / Conditional Operator In C

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Determine Leap Year or Not using Macros

In this video tutorial lets learn how to determine user input year is a leap year or not, using Macros and nested ternary / conditional operator.

Related Read:
C Program To Check Leap Year
C Program To Check Leap Year Using Ternary Operator
Using Macro Template In Macro Expansion: C Program

Leap Year Logic

1. If a year is century year(year ending with 00) and is perfectly divisible by 400, then it’s a leap year.

2. If a year is not a century year, and is perfectly divisible by 4, then it’s a leap year.

Video Tutorial: Determine Leap Year or Not using Macros: C Program


[youtube https://www.youtube.com/watch?v=Du7YNk8GGDk]

YouTube Link: https://www.youtube.com/watch?v=Du7YNk8GGDk [Watch the Video In Full Screen.]

Source Code: C Program To Determine Leap Year or Not using Macros

#include<stdio.h>

#define NOT_LEAP(x) printf("%d is not leap year\n", x)
#define LEAP_YEAR(x) printf("%d is leap year\n", x)

#define LEAP(x) ( (x % 100 == 0 && x % 400 == 0) ? LEAP_YEAR(x) : \
                 ( (x % 4 ==0) ? LEAP_YEAR(x) : NOT_LEAP(x)) )

int main()
{
    int year;

    printf("Enter a year\n");
    scanf("%d", &year);

    LEAP(year);

    return 0;
}

Output 1:
Enter a year
2018
2018 is not leap year

Output 2:
Enter a year
2019
2019 is not leap year

Output 3:
Enter a year
2020
2020 is leap year

Output 4:
Enter a year
2023
2023 is not leap year

Output 5:
Enter a year
2024
2024 is leap year

In above program we’re writing NOT_LEAP(x) macro to display a message that the user input year is not a leap year. LEAP_YEAR(x) macro is used to display message that the user input year is a leap year.

We use both NOT_LEAP(x) and LEAP_YEAR(x) macro names or macro templates inside LEAP(x) macro expansion.

We’re also using macro continuation(\) to break the line and continue writing the logic in next line in macro expansion of LEAP(x).

List of some leap years

2000
2004
2008
2012
2016
2020
2024
2028
2032
2036
2040
2044
2048

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Biggest of 3 Numbers using Macros: C Program

In this video lets see how we can make use of Macros and ternary / conditional operator to find biggest of three numbers.

Related Read:
Biggest of 3 Numbers Using Ternary Operator: C
Preprocessors In C Programming Language

What we learn in this video tutorial?

We learn how to make use of nested ternary / conditional operator in macro definition. And how to use macro continuation( \ ) preprocessor operator.

Video Tutorial: Biggest of 3 Numbers using Macros: C Program


[youtube https://www.youtube.com/watch?v=CYWSkQ2-kp4]

YouTube Link: https://www.youtube.com/watch?v=CYWSkQ2-kp4 [Watch the Video In Full Screen.]

Source Code: Biggest of 3 Numbers using Macros: C Program

#include<stdio.h>

#define BIGGEST(x, y, z) ( (x > y && x > z) ? x : ( y > z) ? y : z )

int main()
{
    int a, b, c;

    printf("Enter 3 integer numbers\n");
    scanf("%d%d%d", &a, &b, &c);

    printf("Biggest of 3 numbers is %d\n", BIGGEST(a, b, c));

    return 0;
}

Output:
Enter 3 integer numbers
20
50
60
Biggest of 3 numbers is 60

Here we’re writing logic inside macro expansion. Wherever macro template is found in our source code, preprocessor replaces that macro template with macro expansion and the compiler compiles the code like normal source code.

Nested Ternary / Conditional Operator

Here we are using nested conditional operator. First we check if value of a is greater b and c, if true, value of a will be returned orelse we check if b is greater than c, if true, value of b will be returned orelse value of c will be returned.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Macro Continuation (\) Preprocessor Operator: C Program

In this video lets see how we can have multiple line of code inside macro expansion, by using preprocessor operator – macro continuation( \ ).

Where Is It Used?

While you’re writing complex logic inside macro expansion, you’ll need to break the line and write code in next line. In such cases macro continuation operator is very helpful. And the code looks much cleaner and clearer.

Video Tutorial: Macro Continuation (\) Preprocessor Operator: C Program


[youtube https://www.youtube.com/watch?v=WUcSe_pSv0o]

YouTube Link: https://www.youtube.com/watch?v=WUcSe_pSv0o [Watch the Video In Full Screen.]

Source Code: Macro Continuation (\): C Program

#include<stdio.h>

#define SQUR(x) \
        printf("Square of %d is %d\n", x, (x * x));

int main()
{
    SQUR(5);

    return 0;
}

Output:
Square of 5 is 25

Here we are writing the macro expansion in the next line, so we are making use of macro continuation preprocessor operator (\).

Source Code: Macro Continuation (\) Preprocessor Operator: C Program

#include<stdio.h>

#define COMPANY(x) switch(x) { \
                     case 1: printf("1. Oracle\n"); break; \
                     case 2: printf("2. IBM\n"); break; \
                     case 3: printf("3. Ripple\n"); break; \
                     default: printf("default. Banks\n"); \
                   }

int main()
{
    COMPANY(3);
    COMPANY(2);
    COMPANY(50);

    return 0;
}

Output:
3. Ripple
2. IBM
default. Banks

Here we’ve multiple lines of code inside macro expansion. So at the end of each line we’ve written macro continuation symbol ( \ – backslash). Wherever you write the macro template, preprocessor will replace it with the macro expansion before execution.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

#pragma Directive: C Program

In this video tutorial lets look at using #pragma preprocessor directive.

#pragma Directive

#pragma is a special purpose directive which can be used to turn on and off certain features.

Note: #pragma commands or keywords vary from one compiler to another. So make sure to check the keywords and/or syntax for the compiler you are using.

Video Tutorial: #pragma Preprocessor Directive: C Program


[youtube https://www.youtube.com/watch?v=3jsqavQ5faU]

YouTube Link: https://www.youtube.com/watch?v=3jsqavQ5faU [Watch the Video In Full Screen.]

Source Code: #pragma Directive: C Program

#include<stdio.h>

void init();
void end();

#pragma startup init
#pragma exit end

int main()
{
    printf("We're inside main method\n");

    return 0;
}

void init()
{
    printf("We're inside init method\n");
}

void end()
{
    printf("We're inside end method\n");
}

Output:
We’re inside init method
We’re inside main method
We’re inside end method

#pragma startup and #pragma exit are keywords.

#pragma startup followed by a function name: this makes the function name specified to be executed before main method.

#pragma exit followed by a function name: this makes the function name specified to be executed just before program termination.

Note:
#pragma startup and #pragma exit doesn’t work in GCC compiler. So the equivalent behavior can be obtained by using below syntax:

Source Code: For GCC Compiler: C Program

#include<stdio.h>

void __attribute__((constructor)) init();
void __attribute__((destructor)) end();

int main()
{
    printf("We're inside main method\n");

    return 0;
}

void init()
{
    printf("We're inside init method\n");
}

void end()
{
    printf("We're inside end method\n");
}

Output:
We’re inside init method
We’re inside main method
We’re inside end method

__attribute__((constructor)) acts like #pragma startup and __attribute__((destructor)) acts like #pragma exit.

#include<stdio.h>

void init();
void end();

int main()
{
    printf("We're inside main method\n");

    return 0;
}

void __attribute__((constructor)) init()
{
    printf("We're inside init method\n");
}

void __attribute__((destructor)) end()
{
    printf("We're inside end method\n");
}

Output:
We’re inside init method
We’re inside main method
We’re inside end method

We could even use __attribute__((constructor)) and __attribute__((destructor)) in function definition, as shown in above source code.

Source Code: Suppressing warnings using #pragma Directive: C Program

#include<stdio.h>

#pragma warn -rvl /* No Return Value */#pragma warn -par /* Parameter Not Used */#pragma warn -rch /* Unreachable Code */
int main()
{
    int count = 1;

    printf("%d\n", count);

    return 0;

    count++;

}

int total()
{
    printf("Sum of a and b is c\n");
}

void end(int x)
{
    printf("We're inside end method\n");
}

Output:
1

Here function total has a return type of integer, but it doesn’t return any value from inside, so it must throw “no return value” warning. But that warning is suppressed by #pragma warn -rvl .

Function end takes a integer argument, but the value is nowhere used inside the function definition. Compiler should throw “Parameter Not Used” warning, but it’s suppressed by #pragma warn -par.

Inside main method, there is count++ after return. This code is never executed. So the compiler should throw “Unreachable Code” warning, which is suppressed by #pragma warn -rch.

Note: -(minus or subtraction) symbol before rvl, par and rch means compiler removes the warning. + sign indicated the warning will be shown in the editor after compilation of the program.

Also note that,

#pragma warn -rvl /* No Return Value */#pragma warn -par /* Parameter Not Used */#pragma warn -rch /* Unreachable Code */

doesn’t work on GCC compiler.

Important Note: The functions init() and end() should not receive any arguments and should not return any value, if we want to handle their invocation via #pragma.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert