Macros With Arguments: C Program

In this video tutorial lets see how to write macros with arguments. We’ll illustrate finding biggest of 2 numbers by using “Macros with arguments” concept.

Note: Do not leave space between macro template name and the argument list / parenthesis. Also make sure to write the macro expansion inside parenthesis.

Related Read:
Preprocessors In C Programming Language
C Program To Find Area of Circle Using Macro Expansion

Video Tutorial: Macros With Arguments: C Program (Biggest of 2 Numbers)


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

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

Source Code: Macros With Arguments: C Program

#include<stdio.h>

#define MAX(x, y) ( x > y ? x : y )

int main()
{
    int a, b;

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

    printf("Biggest of %d and %d is %d\n", a, b, MAX(a, b));

    return 0;
}

Output 1:
Enter 2 numbers
14
5
Biggest of 14 and 5 is 14

Output 2:
Enter 2 numbers
5
14
Biggest of 5 and 14 is 14

Functions return result after evaluating the expression, but Macros are different. Before compiler compiles the program, preprocessor replaces all the occurrence of Macro template with Macro expansion. So in macros, it won’t return the result, but instead the entire macro expansion gets placed in place of macro template.

So in above C program, printf() becomes:

printf("Biggest of %d and %d is %d\n", a, b, ( a > b ? a : b ));

Related Read:
Biggest of Two Numbers Using Ternary Operator: C

Check Below 2 Source code and it’s output

No Parenthesis around macro expansion

#include<stdio.h>

#define SQUARE(n) n * n

int main()
{
  printf("%f", 64/SQUARE(4));

  return 0;
}

Output:
64

Parenthesis around macro expansion

#include<stdio.h>

#define SQUARE(n) (n * n)

int main()
{
  printf("%f", 64/SQUARE(4));

  return 0;
}

Output:
4

In C programming and in mathematics in general, each arithmetic operation has different precedence. Hence by using parenthesis around macro expansion, it gets the highest precedence.

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 Find Area of Circle Using Macro Expansion

In this video tutorial lets learn about Macro Expansion and also lets write a simple program to find area of Circle using Macros.

Related Read:
Preprocessors In C Programming Language

Rules To Construct/Write Macro Definition

1. Space between # and define is optional.
2. There must be a space or tab between Macro Template and Macro Expansion.
3. #define MACRO_TEMPLATE MACRO_EXPANSION is called Macro Definition.
4. Macro definition must not end with semi-colon.
5. Using all capital letters for Macro template is convention and not a syntax of C. You can use any name for Macro template, except the C reserve words.

Video Tutorial: C Program To Find Area of Circle Using Macro Expansion


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

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

Source Code: C Program To Find Area of Circle Using Macro Expansion

#include<stdio.h>

#define PI 3.14

int main()
{
    float r, area;

    printf("Enter Radius of Circle\n");
    scanf("%f", &r);

    area = PI * r * r;

    printf("Area of Circle is %f\n", area);

    return 0;
}

Output 1:
Enter Radius of Circle
5
Area of Circle is 78.500000

Source Code: Writing printf statement in Macro expansion

#include<stdio.h>

#define DISPLAY printf("I'm in Love with iPhone UI Elements\n\n")

int main()
{
    DISPLAY;
    return 0;
}

Output 1:
I’m in Love with iPhone UI Elements

Note: Careful not to enter semicolon at the end of Macro definition. i.e., after the end of printf() statement.

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

C Program To Find nPr Factorial

Lets write a C Program to Find nPr Factorial for the user input values of n and r.

In this video tutorial we are showing iterative logic, recursive logic and how to write single line of code for getting factorial using recursion and ternary/conditional operator.

Formula To Calculate nPr Factorial

nPr = n! / (n-r)!;

Definition:

The number of possibilities for choosing an ordered set of r objects(a permutation) from a total of n objects.

Very Important Note: n value should always be greater than value of r. Because, for example, we can get 2 out of 6. But we can’t get 6 out of 2. i.e., the main set should always be greater than the subset.

Example:

Lets assume that there are 3 people in a park. Let their names be A, B and C. There are only 2 seats available to sit. So the possible ways for people to sit over the seat using permutation are {AB, AC, BA, BC, CA, CB}. In our example, we have 3 people, so n = 3. And we have 2 seats available, so r = 2. Finally we get 6 objects/permutation in a set, so nPr is 6, when n = 3 and r = 2.

Factorial Definition: Factorial of a positive integer n, denoted by n!, is the product of all positive integers less than or equal to n.

Important Note: By convention, Factorial of 0 is 1. i.e., 0! = 1.

Example: 5! = 5 x 4 x 3 x 2 x 1 which is equal to 120. i.e., 5! = 120.

Related Read:
C Program To Find nCr Factorial

Video Tutorial: C Program To Find nPr Factorial


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

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

Source Code: C Program To Find nPr Factorial

#include<stdio.h>

int factorial(int);

int main()
{
    int n, r;
    float npr;

    printf("Enter a positive value for n and r\n");
    scanf("%d%d", &n, &r);

    npr = factorial(n) / factorial(n - r);

    printf("\nnPr Factorial of %d and %d is %0.2f.\n", n, r, npr);

    return 0;
}

int factorial(int num)
{
    int fact = 1, count;

    for(count = 1; count <= num; count++)
    {
        fact = fact * count;
    }
    return(fact);
}

Output 1:
Enter positive integer value for n and r
3
2

nPr Factorial of 3 and 2 is 6.00

Output 2:
Enter positive integer value for n and r
6
2

nPr Factorial of 6 and 2 is 30.00

Source Code: C Program To Find nPr Factorial using Recursion

#include<stdio.h>

int factorial(int);

int main()
{
    int n, r;
    float npr;

    printf("Enter positive integer value for n and r\n");
    scanf("%d%d", &n, &r);

    npr = factorial(n) / factorial(n-r);

    printf("\nnPr Factorial of %d and %d is %0.2f\n", n, r, npr);

    return 0;
}

int factorial(int num)
{
    if(num)
        return(num * factorial(num - 1));
    else
        return 1;
}

Output 1:
Enter positive integer value for n and r
3
2

nPr Factorial of 3 and 2 is 6.00

Output 2:
Enter positive integer value for n and r
6
2

nPr Factorial of 6 and 2 is 30.00

Source Code: C Program To Find nPr Factorial using Recursion and Ternary or Conditional Operator

#include<stdio.h>

int factorial(int);

int main()
{
    int n, r;
    float npr;

    printf("Enter positive integer value for n and r\n");
    scanf("%d%d", &n, &r);

    npr = factorial(n) / factorial(n-r);

    printf("\nnPr Factorial of %d and %d is %0.2f\n", n, r, npr);

    return 0;
}

int factorial(int num)
{
  return( (num != 0) ? (num * factorial(num - 1)) : 1);
}

Output 1:
Enter positive integer value for n and r
3
2

nPr Factorial of 3 and 2 is 6.00

Output 2:
Enter positive integer value for n and r
6
2

nPr Factorial of 6 and 2 is 30.00

Logic To Find nPr Factorial of n and r

We ask the user to input positive integer value for n and r. We then make use of nPr formula to get the nPr value.

To find Factorial logic, please visit the links present below in “Related Read” section below:

Related Read:
C Program To Find Factorial of a Number using Function
C Program To Find Factorial of a Number using Recursion

We have separate short videos explaining the iterative logic, recursive logic to find factorial of a user input number.

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 Find nCr Factorial

Lets write a C Program to Find nCr Factorial for the user input values of n and r.

In this video tutorial we are showing iterative logic, recursive logic and how to write single line of code for getting factorial using recursion and ternary/conditional operator.

Formula To Calculate nCr Factorial

nCr = n! / (r! * (n-r)!);

Definition:

The number of different, unordered combination of r objects from a set of n objects is called nCr Factorial of n and r.

Very Important Note: n value should always be greater than value of r. Because, for example, we can get 2 out of 6. But we can’t get 6 out of 2. i.e., the main set should always be greater than the subset.

Example:

Lets assume that there are 3 people in a park. Let their names be A, B and C. There are only 2 seats available to sit. So the possible ways for people to sit over the seat using combination are {AB, AC, BC}. Since nCr is unordered combination AB and BA are considered same, so we use only one of them in our set. So in our example, we have 3 people, so n = 3. And we have 2 seats available, so r = 2. Finally we get 3 objects/combination in a set, so nCr is 3, when n = 3 and r = 2.

Factorial Definition: Factorial of a positive integer n, denoted by n!, is the product of all positive integers less than or equal to n.

Important Note: By convention, Factorial of 0 is 1. i.e., 0! = 1.

Example: 5! = 5 x 4 x 3 x 2 x 1 which is equal to 120. i.e., 5! = 120.

Video Tutorial: C Program To Find nCr Factorial


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

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

Source Code: C Program To Find nCr Factorial

#include<stdio.h>

int factorial(int);

int main()
{
    int n, r;
    float ncr;

    printf("Enter a positive value for n and r\n");
    scanf("%d%d", &n, &r);

    ncr = factorial(n) / ( factorial(r) * factorial(n - r) );

    printf("\nnCr Factorial of %d and %d is %0.2f.\n", n, r, ncr);

    return 0;
}

int factorial(int num)
{
    int fact = 1, count;

    for(count = 1; count <= num; count++)
    {
        fact = fact * count;
    }
    return(fact);
}

Output 1:
Enter a positive value for n and r
3
2

nCr Factor of 3 and 2 is 3.00.

Output 2:
Enter a positive value for n and r
6
2

nCr Factor of 6 and 2 is 15.00.

Source Code: C Program To Find nCr Factorial using Recursion

#include<stdio.h>

int factorial(int);

int main()
{
    int n, r;
    float ncr;

    printf("Enter a positive value for n and r\n");
    scanf("%d%d", &n, &r);

    ncr = factorial(n) / ( factorial(r) * factorial(n - r) );

    printf("\nnCr Factorial of %d and %d is %0.2f.\n", n, r, ncr);

    return 0;
}

int factorial(int num)
{
    if(num)
        return(num * factorial(num-1));
    else
        return 1;
}

Output 1:
Enter a positive value for n and r
3
2

nCr Factor of 3 and 2 is 3.00.

Output 2:
Enter a positive value for n and r
6
2

nCr Factor of 6 and 2 is 15.00.

Source Code: C Program To Find nCr Factorial using Recursion and Ternary or Conditional Operator

#include<stdio.h>

int factorial(int);

int main()
{
    int n, r;
    float ncr;

    printf("Enter a positive value for n and r\n");
    scanf("%d%d", &n, &r);

    ncr = factorial(n) / ( factorial(r) * factorial(n - r) );

    printf("\nnCr Factorial of %d and %d is %0.2f.\n", n, r, ncr);

    return 0;
}

int factorial(int num)
{
  return( (num != 0) ? num * factorial(num-1) : 1);
}

Output 1:
Enter a positive value for n and r
3
2

nCr Factor of 3 and 2 is 3.00.

Output 2:
Enter a positive value for n and r
6
2

nCr Factor of 6 and 2 is 15.00.

Logic To Find nCr Factorial of n and r

We ask the user to input positive integer value for n and r. We then make use of nCr formula to get the nCr value.

To find Factorial logic, please visit the links present below in “Related Read” section.

Related Read:
C Program To Find Factorial of a Number using Function
C Program To Find Factorial of a Number using Recursion

We have separate short videos explaining the iterative logic, recursive logic to find factorial of a user input number.

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