C Program To Print Multiplication Table Using Macros

In this video lets see how we can print multiplication table(from 1 to 10) for any positive user input value, using Macros.

Related Read:
C Program To Print Multiplication Table Using While Loop

Video Tutorial: C Program To Print Multiplication Table Using Macros


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

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

Source Code: C Program To Print Multiplication Table Using Macros

#include<stdio.h>

#define MULTI(num, count) printf("%d x %d = %d\n", num, count, (num*count))

int main()
{
    int num, count = 1;

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

    printf("\nMultiplication Table for %d is: \n\n", num);

    while(count <= 10)
    {
        MULTI(num, count);
        count++;
    }

    return 0;
}

Output 1:
Enter a positive number
5

Multiplication Table for 5 is:

5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

Output 2:
Enter a positive number
4

Multiplication Table for 4 is:

4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40

Here we are initializing variable count to 1 and iterating the while loop until count is less than or equal to 10. For each iteration we’re executing macro MULTI(num, count). But before compilation itself preprocessor will have replaced MULTI(num, count) by its macro expansion, which is printf(“%d x %d = %d\n”, num, count, (num*count)).

Note: If you see \ inside macro expansion – it is called as Macro continuation(\) operator. It is used to continue the code in next line, in macro expansion.

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

Positive or Negative or Zero Using Macros: C Program

C Program to check whether the user input integer number is positive, negative or zero using Macros and ternary / Conditional operator.

Related Read:
Positive or Negative or Zero Using Ternary Operator: C Program

Logic

If user input number is greater than 0, then the number is positive. If user input number is less than 0, then the number is negative. If neither of the above 2 conditions are true, then the number is zero.

Video Tutorial: Positive or Negative or Zero Using Macros: C Program


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

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

Source Code: Positive or Negative or Zero Using Macros: C Program

#include<stdio.h>

#define SIGN(num) ( (num > 0) ? \
                   printf("POSITIVE") : \
                   (num < 0) ? printf("NEGATIVE") : \
                   printf("ZERO"))

int main()
{
    int num;

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

    SIGN(num);

    return 0;
}

Output 1:
Enter a number
5
POSITIVE

Output 2:
Enter a number
-2
NEGATIVE

Output 3:
Enter a number
0
ZERO

Here the macro template SIGN(num) will be replaced by its corresponding macro expansion ( (num > 0) printf(“POSITIVE”) : (num < 0) ? printf(“NEGATIVE”) : printf(“ZERO”)) by preprocessor. And if user input number is greater than 0, it’ll print POSITIVE else it’ll print NEGATIVE. If neither of those 2 conditions are true, then it’ll print ZERO.

In our program we’re using Macro Continuation (\) Preprocessor Operator: C Program in macro expansion to break from the line and continue the logic in new/next line.

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

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

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

Preprocessors In C Programming Language

In this video tutorial we’ll cover some basics about preprocessors and give you overview of what we’ll be covering in upcoming video tutorials.

Note

1. Preprocessor is a program that processes our source code before it is passed to the compiler. Hence its name pre-processor.

2. Preprocessor Commands are often called as DIRECTIVES.

3. Preprocessor Directives begin with # symbol.

Coming soon ..

1. Macro expansion.
2. File inclusion.
3. Conditional compilation.
4. Miscellaneous directives.
5. We’ll also be covering about predefined macros.

Previously we had covered a little bit about File inclusion, you can take a look at it here include directive in C Program

Video Tutorial: Preprocessors In C Programming Language


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

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

Computer Science Students and Professionals

If you’re a computer science student enrolled in University, your syllabus may not include in-depth learning of preprocessors. But when it comes to competitive exams and real-time application programming you’ll have to use preprocessors a lot. So better learn it now.

In upcoming videos we’ll cover preprocessor concepts in detail with simple example programs to explain the concepts/topics.

So stay tuned, stay subscribed to our YouTube channel and blog.

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