Comments In C Programming Language

In this video tutorial lets learn about using comments in C programming language.

There are 2 types of comments in C program

1. Single Line Comment.
2. Multi-line comment.

Why Do We Need To Use Comments In Source Code?

1. Whenever someone looks at the code, they need not spend much time in understanding the logic and what that code snippet does. Instead they can simply read the comment and understand the purpose of the code snippet.

2. If there is comment at the top of program, anybody can read it and understand the purpose of the program itself. Usually developers display author information, title and the date of writing the program.

3. When there are multiple contributes to the source code, it’s best to have comments to credit the code contributor and also it’ll be helpful for the other contributes to look at the comment and understand the purpose of the code immediately, instead of tracking the logic and understanding it.

4. If we look at our own code after some time, comments will surely help us understand why we wrote the code in the first place!

Video Tutorial: Comments In C Programming Language


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

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

Source Code: Comments In C Programming Language

/*
 * Author: SATISH
 * Link: Technotip.com
 * Language: C Programming
 * Title: Comments In C
 * Multi-author: No
*/
#include<stdio.h>
#define PI 3.14  // Value of PI

int main()
{
    float r = 5.0;

    // Logic to calculate area of circle
    printf("Area of Circle is %f\n", (PI * r *r ));

    /*
       We could go further and
       add more features to this
       program in future.
    */    return 0;
}

Output:
Area of Circle is 78.500000

Single Line Comment
Whatever is written after double forward slash ( // ) in that same line, will be treated as comment.

Multi-Line Comment
Anything written inside a /* and */ is considered multiple comment. Text inside /* and */ can be written in multiple lines and all those things will be considered as comments.

Note: Comments are not executed as part of the program. Comments are not even compiled. They’re present only for the user to read and understand the purpose of the code.

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

Comparing Floating Point Variable With a Value In C Programming

In this video tutorial lets see how we can compare a floating point variable with a constant value, and lets see the result.

Related Read:
Sizeof Operator in C Programming Language
C Program To Convert Decimal Number To Binary Number, using While Loop

The Problem

If you assign 0.7 to a floating point variable num and then inside if condition you check if num == 0.7, it returns false. Because here variable num is of data type float and the constant value 0.7 is considered as double type data.

Comparison

Video Tutorial: Comparing Floating Point Variable With a Value In C Programming


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

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

Source Code: Comparing Floating Point Variable With a Value In C Programming

#include<stdio.h>

int main()
{
    float num = 0.7;

    if(num == 0.7)
        printf("Yes, true\n");
    else
        printf("Not True\n");

    return 0;
}

Output:
Not True

Some of you might get surprised with the result. But there is nothing to be surprised. It’s working the way it is intended to work.

Reason

We need to keep the basics in mind. When we input decimal numbers, it gets converted into Binary number system and then the further calculation occurs.

Now getting back to our problem: value present in floating point variable num is converted to its binary equivalent. But float has 4 bytes of memory storage.

#include<stdio.h>

int main()
{
    float num = 0.7;

    printf("%d\n", sizeof(num));
    printf("%d\n", sizeof(0.7));

    return 0;
}

Output:
4
8

Next the constant value 0.7 is converted into its binary equivalent. Since its data type is double, it has 8 Bytes of memory for storage. Hence it can store higher precision number.

Now the comparison between these two binary number occurs. Inside double, the binary number has more precision compared to floating point variable – due to available memory for each data type.

For Example:

#include<stdio.h>
int main()
{
    float PI = 3.14;
    double M_PI = 3.14159265358979323846;

    if(PI == M_PI)
        printf("YES\n");
    else
        printf("NO\n");

    return 0;
}

Output:
NO

Even though both variable PI and M_PI has value of PI in it, they’re not equal.

Quick Fix

There are 2 quick fixes for this bug:

1. Typecast constant value to float.
2. Declare double type variable instead of floating point variable.

1. Typecast constant value to float.

#include<stdio.h>
int main()
{
    float num = 0.7;

    if(num == 0.7f)
        printf("Yes, true\n");
    else
        printf("Not True\n");

    return 0;
}

Output:
Yes, true

Inside if condition, we’ve converted double value 0.7 into floating point data, by appending f to it.

2. Declare double type variable instead of floating point variable.

#include<stdio.h>

int main()
{
    double num = 0.7;

    if(num == 0.7)
        printf("Yes, true\n");
    else
        printf("Not True\n");

    return 0;
}

Output:
Yes, true

Best Example:

We had written a C program to Find Grade of Steel. One of our reader reported this comparison bug, and we fixed it using above method mentioned in this video.

Purposefully we’ve not altered the old code. We’ve preserved both old and new code, so that people can learn from our mistake. Take a look at: C Program To Find Grade of Steel

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

File Inclusion In C Programming Language

In today’s video tutorial lets learn more about include directive or preprocessor statement.

What We Learn?

1. Two ways of including/importing files.
2. Why write related code in separate file.
3. Example program to illustrate including and working with multiple files.

Related Read:
include directive in C Program

Video Tutorial: File Inclusion In C Programming Language


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

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

Source Code: File Inclusion In C Programming Language

Main.c

#include<stdio.h>
#include "Circle.h"

int main()
{
    float r;

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

    printf("Area of Circle is %f\n", circle_area(r, PI));

    return 0;
}

Output:
Enter radius of Circle
5
Area of Circle is 78.539819

In your Code::Blocks editor, go to File -> New -> Empty File.

Circle.c

double circle_area(float r, float PI)
{
    return( PI * r * r );
}

Circle.h

#define PI 3.14159265358979323846

double circle_area(float, float);

Note: Make sure Main.c, Circle.c and Circle.h are all in the same directory/folder.

Circle.c
This file has definition of the function/methods.

Circle.h
This file has all the macro definitions and function prototypes.

Main.c
This is our main C program where we include Circle.h file. By including this file we also include macro definition for PI and also the function to calculate area of circle.

Preprocessor includes all the content of Circle.h into Main.c source code before passing the code to the compiler.

Since all the code in Circle.h is included, we can directly use PI value as well as we can call circle_area() method from within Main.c file.

Advantages of Separating Related Files

1. If we’re using certain functions and macros repeatedly in our program, its best practice to write it in a separate file and include it at the top of our program and use the functions and macros wherever necessary.

Example: We keep using printf() and scanf() methods in all our programs, so it has been written in a separate library file called stdio.

2. In big projects, if we separate related files, we can handover related tasks to separate teams. This way we can maintain modularity of the project effectively.

Example: Math.h file has macro definition and prototype of all the functions related to Mathematics.

3. It’s easy to find bugs, debug and unit test our project.

4. Our source code looks less clumsy and more readable.

Two Ways To Include Files

There are 2 ways to include files in C.

1. Using angular brackets: Angular Brackets are used to import or include standard library files. In this case the linker looks for the included file in the standard library file storage directory / folder.

2. Using Double Quotes: When filename is wrapped with double quotes, the linker looks for the file inside current working directory. It also means that the included file is written by the user and is not a part of standard library file.

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 With Argument v/s Function: C Program

In today’s video tutorial lets see the difference between a Macro with argument and a simple function.

Related Read:
Macros With Arguments: C Program
Function / Methods In C Programming Language

Video Tutorial: Macro With Argument v/s Function Call: C Program


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

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

Source Code: Macro With Argument v/s Function Call: C Program

#include<stdio.h>

#define PI 3.14159265358979323846
#define AREA(r) ( PI * r * r )

double circle_area(float);

int main()
{
    float r;

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

    printf("Area of Circle, using Macro is %f\n", AREA(r));
    printf("Area of Circle, using Function is %f\n", circle_area(r));

    return 0;
}

double circle_area(float r)
{
    return( 3.14159265358979323846 * r * r );
}

Output:
Enter radius of Circle
5
Area of Circle, using Macro is 78.539816
Area of Circle, using Function is 78.539816

As you can see in above output the result is same for Macro and function call. Now the question is, when to use macros and when to use functions?

When to use macros and when to use functions?

Macro: If a macro template is used 100 times inside a C program, all these macro template will be replaced with its macro expansion before its passed to the compiler. This process is called as preprocessing, and it’s done by preprocessor. Because of this, the size of the program increases.

Function: Function definition is written only once and even if its called 100 times inside a program it won’t increase the program size. Whenever there is a function call, the control is passed to the function definition where some logical operation is performed and then a meaningful result is returned back to the calling function. This takes some time.

So the trade off is between memory space and time.

If memory is not an issue, then you can simply use Macros – as it brings in speed of execution as an advantage. But if memory space is an issue – if you’re writing for a mobile device, then it’s better to go with functions. Though its slower, it uses less memory space.

While coding for bigger real-time projects you’ll surely need the knowledge of macros and macros with argument, so know the syntax and it’s advantages. In this fast moving world speed of execution really matters. And C programming specifically is famous and used often for its speed and its easy integration(and interaction) with the hardware devices.

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

Using Macro Template In Macro Expansion: C Program

In this video tutorial lets see how we can make use of a macro template in a macro expansion.

Objective

We define PI in a macro, and then we use PI in expansion of another macro.

We shall also modify the same program to make use of constant M_PI present in math.h library file instead of macro template PI.

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

Video Tutorial: Using Macro Template In Macro Expansion: C Program


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

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

Source Code: Using Macro Template In Macro Expansion: C Program

#include<stdio.h>

#define PI 3.14159265358979323846

#define AREA(r) ( PI * r * r )

int main()
{
    float r;

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

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

    return 0;
}

Output:
Enter radius of Circle
5
Area of Circle is 78.539816

In above source code, preprocessor replaces every occurrence of PI with 3.14159265358979323846 and then checks for every occurrence of AREA(r) and replaces with ( 3.14159265358979323846 * r * r ).

With this simple example I’m trying to illustrate that we can make use of Macro template inside macro expansion of another macro definition.

Using M_PI constant present in Math.h header file

#include<stdio.h>
#include<math.h>

#define AREA(r) ( M_PI * r * r )

int main()
{
    float r;

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

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

    return 0;
}

Output:
Enter radius of Circle
5
Area of Circle is 78.539816

Note: M_PI is also a macro inside math.h library file.

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