Logical Operators In C

Logical Operators in C programming language return true(non-zero number) or false(0) value. Logical AND(&&) and logical OR(||) works on 2 operands. But logical NOT(!) works on single operand.

Related Read:
Relational Operators In C

Logical Operators

&& – Logical AND Operator.
|| – Logical OR Operator.
! – Logical NOT Operator.

&& – Logical AND Operator.

 
#include < stdio.h >

int main()
{
    int a;

    a = ( 1 && 1 );

    printf("value of a is %d\n", a);

    return 0;
}

Output:
value of a is 1

 
#include < stdio.h >

int main()
{
    int a;

    a = ( 0 && 1 );

    printf("value of a is %d\n", a);

    return 0;
}

Output:
value of a is 0

For logical AND(&&) both operands or expressions must yield to true. If any one condition is false(0), then it’ll return false(0).

|| – Logical OR Operator.

 
#include < stdio.h >
int main()
{
    int a;

    a = ( 1 || 1 );

    printf("value of a is %d\n", a);

    return 0;
}

Output:
value of a is 1

 
#include < stdio.h >
int main()
{
    int a;

    a = ( 1 || 0 );

    printf("value of a is %d\n", a);

    return 0;
}

Output:
value of a is 1

 
#include < stdio.h >
int main()
{
    int a;

    a = ( 0 || 0 );

    printf("value of a is %d\n", a);

    return 0;
}

Output:
value of a is 0

Logical OR(||) returns true(any non-zero number) if either one condition/operand is true. It returns false(0) only when both the conditions / operands are false(0).

! – Logical NOT Operator.

 
#include < stdio.h >
int main()
{
    int a;

    a = ( !1 );

    printf("value of a is %d\n", a);

    return 0;
}

Output:
value of a is 0

 
#include < stdio.h >
int main()
{
    int a;

    a = ( !0 );

    printf("value of a is %d\n", a);

    return 0;
}

Output:
value of a is 1

Logical NOT(!) returns true if the condition is false. It returns false if the condition is true. It just negates the Boolean value given to it.

Logical Operators In C


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

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


Logical Operators combined with Relational Operators

&& – Logical AND Operator.

 
#include < stdio.h >

int main()
{
    int a, b = 100;

    a = ( (b == 0) && (b > 50) );

    printf("Value of a is %d\n", a);

    return 0;
}

Output:
value of a is 1

a is true because both b is equal to 100 is true and b is greater than 50 is true.

 
#include < stdio.h >

int main()
{
    int a, b = 100;

    a = ( (b == 0) && (b > 150) );

    printf("Value of a is %d\n", a);

    return 0;
}

Output:
value of a is 0

a is false(0) because b is equal to 100 is true but b is greater than 150 is false.

|| – Logical OR Operator.

 
#include < stdio.h >

int main()
{
    int a, b = 100;

    a = ( (b == 0) || (b > 50) );

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

    return 0;
}

Output:
value of a is 1

Value of a is true, because b is equal to true. In logical OR(||) if one condition is true, then it returns true. It returns false(0) only when both the conditions / operands are false(0).

! – Logical NOT Operator.

 
#include < stdio.h >

int main()
{
    int a, b = 100;

    a = ( !(b == 0) );

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

    return 0;
}

Output:
value of a is 0

Value of a is false(0). Because b is equal to 100 is true. When true value is given to NOT(!) it’ll return false(0). When false value is supplied to NOT it’ll return true.

Note: = is assignment operator. == is equality operator.

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

The Best IDE For C Programming

There are many IDE(Integrated Development Environment) available for C programming language, but I would prefer using Code::Blocks or Visual Studio Editor.

For this video tutorial series we’ll be using Code::Blocks.

You can visit CodeBlocks website and navigate to Downloads section, and then to Binaries section and depending upon your Operating System, download the IDE and install it. Downloading and installation of Code::Blocks is shown in the video posted below. You can follow along and get started with programming in C language.

 
#include < stdio.h >
int main()
{
    printf("Microsoft\n Apple\n Oracle\n Google\n Yahoo\n");
    return 0;
}

Output:
Microsoft
Apple
Oracle
Google
Yahoo

Above program is just an example. You need not worry if you didn’t understand it. I’ll be explaining it in the next video, in detail. For now, if possible type the program as it is and try to compile and execute it on your own by seeing the video. Try to change the content inside printf function and run compilation(Build) and execution(Run) once again and see the results for yourself.

The Best IDE For C Programming


[youtube https://www.youtube.com/watch?v=xsWy-sMzIj0]

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


Note:
1. Code::Blocks is available for all the popular Operating Systems. So whatever I show in the video tutorial is applicable for everyone irrespective of the OS they’re using.

2. Code::Blocks comes with C/C++ compiler, so you need not install compiler or any other plugins separately.

3. Make sure to download the latest version of the IDE. And make sure the file you download has ( codeblocks-17.12mingw-setup ) mingw in it. That’s how we can know it comes shipped with c/c++ compiler by default.

Why Learn ‘C’ As Your First Programming Language?

Let’s know a little bit of history of C programming. Many of you may not be interested in knowing the history of C – you maybe here just to learn how to get started with Coding in C Program. I understand that. You’ll want to know the history soon, once you learn the basics of C programming. History is very important. But for now I’ll let you know about it in 1 line!

History of C Programming Language

C Programming language was developed at AT&T’s Bell Lab in 1972. It was designed and developed by a man called Dennis Ritchie (September 9, 1941 – October 12, 2011).

Why name it as C?
Before designing C, there was a programming language called “B”. So Dennis Ritchie named it as “C”. Little did he know “C Programming” language would get so popular among programmers – even after 3 decades of its inception.


C programming language

Why Learn C As A First Programming Language?

Beginners tend to get into arguments/discussion as to how C++, C#, Java has improved upon C and how “Object Oriented Programming” has so many advantages over C. But let me tell you, Operating Systems like Windows, iOS, OSx, Unix/Linux(Kernel), android are all written in C. Looks like there is something which is good in C because of which companies like Microsoft, Apple, Unix/Linux Foundation, Google are choosing C Programming language to develop and maintain their Operating Systems. These are only a few popular companies I’ve listed. There are many many companies solely dependent upon C programming for their software development. We’ll learn more along the course of these video tutorials.

Why Learn C As A First Programming Language?


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

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


Advantages of learning C

1. Object Oriented Programming(OOP) method present in C++, C#, Java provides a huge advantage for programmers. But the basic programming elements like looping, variables, declaration, arithmetic operations, logical operations etc are almost similar to what you learn in C. Learning C definitely gives you an extra edge before jumping on to learn C++ or C# or Java.

2. Programs written in C are very efficient. It’s performance and speed of execution is unique. Even today device drivers are written in C for this reason. The code is compact and has high performance even in low memory availability.

3. Your smartphone, digital camera, washing machine, microwave oven, refrigerator, smart bulbs, smart fans etc you name it, all these have microprocessors attached to it, which has program embedded in it. Since these micro-processor in these devices have limited memory and need to respond to user inputs instantly, the program of choice is inevitably C, in most cases.

4. Most 3D gaming frameworks like “DirectX” are built using the C Programming language. Because for good gaming experience speed of execution matters a lot. Since C programs are robust, reliable and fast these 3D gaming framework choose C programming over others. Even animation special effects in movies are done using C programs.

5. Its flexible and portable: Since C program allows user to handle memory directly we can write programs very flexibly according to our needs. We can manage memory flexibly. “Portable” because the same program source code works on all operating systems without the need for much modification. All Operating System have C Compiler and they compile and give object code and executable which suits and works for that particular OS.

6. Did you know, many compilers and interpreters for other languages like FORTRAN, Pascal, Perl, Python etc are written in C. Why is it so? As I told you before, C is reliable, portable and fast.

7. Using C programming language we can directly access hardware. This is very powerful feature. We’ll discuss this in detail in coming video tutorials.

8. C program enables programmer to manipulate individual bits of memory. But “with power comes responsibility” – we need to handle memory carefully or else we may end up writing code which might behave abruptly. For this reason Java has its own builtin memory management feature called Garbage Collection.

9. C implementation has a large library of useful functions. Ex: scanf, printf, functions to write data to file etc.

10. C programming language is easy to learn and understand.

We can go on and keep writing advantages of learning C Program. But let me stop here and assure you that you’re in the right place and C is the best choice as your first programming language to learn.

Make sure to practice all the programs we post here and quickly you’ll know the advantage for yourself. And soon you’ll be an advanced user and if you keep learning you’ll be an expert.

There are many Job opportunities for people who are experts in C Programming Language. We’ll even post Job Board shortly and try to list job offers for C program experts. Stay tuned for all the good stuff we’ll post on this page: C Programming: Beginner To Advance To Expert

Request

Please let us know some advantages and disadvantages or anything informative about C programming language in the comment section below. This will be helpful for others joining you on later date to start learning ‘C Programming language’. Sharing is caring. Also please share this article with your friends and followers on Social Media.

Linear Search: C

In this video tutorial we shall see how we can search for a number in an array in linear fashion.

First ask the user to enter the number to be searched. Store it inside a variable called key.
Now start comparing key value with a[i] using a for loop. If the key is found in the array, then assign 1 to flag orelse flag will be 0.

Depending upon the value of flag, print the message.

If flag is 1, search is successful.
If flag is 0, search failed. in the sense, the number is not present in the given array.

Video Tutorial: Linear Search: C


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

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



Full Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include < stdio.h >
#include < conio.h >
 
void main()
{
int a[20], key, N, flag = 0, i;
clrscr();
 
printf("Enter array limit\n");
scanf("%d", &N);
 
printf("Enter %d elements\n", N);
for(i=0; i < N; i++)
 scanf("%d",&a[i]);
 
printf("Enter the number to be searched\n");
scanf("%d", &key);
 
for( i=0; i < N; i++)
 if( a[i] == key )
 {
flag = 1;
break;
 }
 
if(flag)
printf("\nSearch Successful\n");
else
printf("\nSearch Failed\n");
getch();
}

Output
Enter array limit
5
Enter 5 elements
77
0
13
9
55
Enter the number to be searched
9
Search Successful

Find Biggest In An Array, Without Sorting It: C

First we have to assume that a[0] is biggest. Now store the value of a[0] inside a variable called big. Now compare value of big with other values in the array. If big is less than any other value, then store the bigger value inside variable big.

Video Tutorial: Biggest In An Array, Without Sorting It: C


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

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



Note that, the previous value inside the variable big is discarded.

Full source code

#include < stdio.h >
#include < conio.h >

void main()
{
int big, a[20], N, pos, i;
clrscr();

printf("Enter the array size\n");
scanf("%d", &N);

printf("Enter %d elements\n", N);
for(i = 0; i < N; i++) 
           scanf("%d", &a[i]);

         big = a[0];
         pos = 0;

         for(i = 1;  i < N; i++)
          if(a[i] > big)
  {
    big = a[i];
    pos = i+1;
    }

 printf("Big is %d and its position is %d", big, pos);

 getch();
}

Output
Enter the array size
5
Enter 5 elements
2
0
100
108
55
Big is 108 and its position is 4