DEV Community

Cover image for C - Variables, if, else, while
Haile Melaku
Haile Melaku

Posted on • Updated on

C - Variables, if, else, while

We have seen in the past blog about c compilation process, gcc compiler and some basic c language syntax.

Now we are going to see more on variables, if statement, arithmetic operation and while statement.


# Keywords and identifiers

keywords: are predefined function names that has a specific purpose and they cannot be used as identifiers in the program.

Image description

Identifiers: c identifiers are names used to refer to a number of things like variable and functions.

Identifiers must start with an alphabetic character.

If you want to be sure that your programs are portable in the new c standard, identifiers need to have a characters limit if 31.


# Arithmetic Operators

Are used to used to perform mathematical operations on the given expressions.

Image description

#include <stdio.h>

/**
 * main - Entry point
 * 
 * Description: print result of arithmetic operator
 * 
 * Return: Always 0 (Success)
 */

int main(void)
{
     int a = 5;
     int b = 15;
     int c;

     c = a + b;
     printf("a + b = %d", c);
     c = a % b;
     printf("a % b = %d", c);

     return (0);
}
Enter fullscreen mode Exit fullscreen mode

# relational operators

Are used to perform comparisons on two variable.

Image description

#include <stdio.h>

/**
 * main - Entry point
 * 
 * Description: print result of arithmetic operator
 * 
 * Return: Always 0 (Success)
 */

int main(void)
{
     int a = 5;
     int b = 15;

     if ( a >= b )
     {
           printf("a is greater than or equal to b");
      }

      return (0);
}
Enter fullscreen mode Exit fullscreen mode

# logical operators

Are used to perform logical operations on the given expressions and there are 3 types of logical operator in c, like logical AND (&&), logical OR (||) and logical NOT (!).

Image description

#include <stdio.h>

/**
 * main - Entry point
 * 
 * Description: print result of arithmetic operator
 * 
 * Return: Always 0 (Success)
 */

int main(void)
{
     int a = 5;
     int b = 15;

     if ( a >= b && a = b)
     {
           printf("both statement's are true");
      }

      return (0);
}

Enter fullscreen mode Exit fullscreen mode

# Bitwise Operators

Are used to perform bit-level operations in C programming.

Image description

First you need to revise on how to turn binary into decimal and turn the decimal to binary.

Binary to decimal
Image description
Decimal to binary

Image description

Bitwise AND Operator &

works like the and logical operator the only case we get true is if 2 values are both true.

#include <stdio.h>

/**
 * main - Entry point
 * 
 * Description: Bitwise AND Operator
 * 
 * Return: Always 0 (Success)
 */

int main(void)
{
      int a = 8;
      int b = 21;

      printf("a & b = %d",a&b);

      return (0);
} 
Enter fullscreen mode Exit fullscreen mode

8 = 00001000 (Binary)
21 = 00010101 (Binary)

Bitwise Operation of 8 and 21

00001000
&00010101
00000000 = 0 (Decimal)

Bitwise OR Operator |

works like the or logical operator the only case we get false is if both values are false.

#include <stdio.h>

/**
 * main - Entry point
 * 
 * Description: Bitwise OR Operator
 * 
 * Return: Always 0 (Success)
 */

int main(void)
{
      int a = 8;
      int b = 21;

      printf("a | b = %d",a|b);

      return (0);
} 
Enter fullscreen mode Exit fullscreen mode

8 = 00001000 (Binary)
21 = 00010101 (Binary)

Bitwise Operation of 8 and 21

00001000
|00010101
00011101 = 29 (Decimal)

Bitwise XOR (exclusive OR) Operator ^

The output is 1(true) if the corresponding bits of two operands are opposite.

#include <stdio.h>

/**
 * main - Entry point
 * 
 * Description: Bitwise XOR (exclusive OR) Operator
 * 
 * Return: Always 0 (Success)
 */

int main(void)
{
      int a = 8;
      int b = 21;

      printf("a ^ b = %d",a^b);

      return (0);
} 
Enter fullscreen mode Exit fullscreen mode

8 = 00001000 (Binary)
21 = 00010101 (Binary)

Bitwise Operation of 8 and 21

00001000
^00010101
00011101 = 29 (Decimal)

Bitwise Complement Operator ~

It changes 1 to 0 and 0 to 1, so complement's in c work differently like the bitwise of 10(-10) which is the 2's compliment instead of 245.

#include <stdio.h>

/**
 * main - Entry point
 * 
 * Description: Bitwise Complement Operator
 * 
 * Return: Always 0 (Success)
 */

int main() {

    printf("Output = %d\n",~35);
    printf("Output = %d\n",~-12);
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Right Shift Operator >>

Shift all bit to the right by a specific number

10 = 00001010
10 >> 3 = 00000010
10 >> 0 = 00001010

#include <stdio.h>

/**
 * main - Entry point
 * 
 * Description: Right Shift Operator
 * 
 * Return: Always 0 (Success)
 */

int main(void)
{
     int i = 10;

     printf("right shift by 3 = %d", i>>3);

     return (0);
}
Enter fullscreen mode Exit fullscreen mode

Left Shift Operator <<

Shift all the bit to the left by a specific number

10 = 00001010
10 << 3 = 00001010000
10 << 0 = 00001010

#include <stdio.h>

/**
 * main - Entry point
 * 
 * Description: Left Shift Operator
 * 
 * Return: Always 0 (Success)
 */

int main(void)
{
     int i = 10;

     printf("left shift by 3 = %d", i<<3);

     return (0);
}
Enter fullscreen mode Exit fullscreen mode

# if, if ... else statements

If statement allows you to control if a program enters a section of code or not based on whether a given condition is true or false.

Syntax

if(boolean_expression 1) {
   /* Executes when the boolean expression 1 is true */
} else if( boolean_expression 2) {
   /* Executes when the boolean expression 2 is true */
} else if( boolean_expression 3) {
   /* Executes when the boolean expression 3 is true */
} else {
   /* executes when the none of the above condition is true */
}
Enter fullscreen mode Exit fullscreen mode

Diagram

Image description

Example

#include <stdio.h>

/**
 * main - Entry point
 *
 * Description: if, if ... else statements
 *
 * Return: Always 0 (Success)
 */

int main(void)
{
   int a = 20;

   if( a == 10 ) 
   {
      /* if condition is true then print the following */
      printf("Value of a is 10\n" );
   } 
   else if( a == 20 ) 
   {
      /* if else if condition is true */
      printf("Value of a is 20\n" );
   } 
   else if( a == 30 ) 
   {
      /* if else if condition is true  */
      printf("Value of a is 30\n" );
   } 
   else 
   {
      /* if none of the conditions is true */
      printf("None of the values is matching\n" );
   }

   return (0);
}
Enter fullscreen mode Exit fullscreen mode

# while loop

While loop is used to loop on a statement as long as the given condition is true.

Syntax

while(condition) {
   statement(s);
}
Enter fullscreen mode Exit fullscreen mode

Diagram

Image description

Example

#include <stdio.h>

/**
 * main - Entry point
 *
 * Description: while loop statement 
 *
 * Return: Always 0 (Success)
 */

int main(void)
{
     /* local variable definition */
     int a = 0;

     /* while loop execution */
     while( a < 10 ) 
     {
            printf("value of a: %d\n", a);
            a++;
     }

     return (0);
}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)