Wednesday, December 18, 2019

Operators in C






Unary Operator:
--> Unary expressions are formed by combining a unary operator with a single operand.
--> All unary operators are of equal precedence and have right-to-left associativity.
Example:
int a = 1;int b = ++a;  // b = 2
Example:
int a = 1;int b = a++;   // b = 1int c = a;     // c = 2

Arithmetic Operator:
--> An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division etc on numerical values.

Example:
C program to demonstrate working of binary arithmetic operators
int main() 

    int a = 10, b = 4, res; 
  
    // printing a and b 
    printf("a is %d and b is %d\n", a, b); 
  
    res = a + b; // addition 
    printf("a+b is %d\n", res); 
  
    res = a - b; // subtraction 
    printf("a-b is %d\n", res); 
  
    res = a * b; // multiplication 
    printf("a*b is %d\n", res); 
  
    res = a / b; // division 
    printf("a/b is %d\n", res); 
  
    res = a % b; // modulus 
    printf("a%b is %d\n", res); 
  
    return 0; 

OUTPUT:
a is 10 and b is: 4
a+b is: 14
a-b is: 6
a*b is: 40
a/b is: 2
a%b is: 2

Relational Operator:
--> Relational operators are used to find the relation between two variables. i.e. to compare the values of two variables in a C program.

Example:

#include <stdio.h>

int main()
{
   int m=40,n=20;
   if (m == n)
   {
       printf("m and n are equal");
   }
   else
   {
       printf("m and n are not equal");
   }
}
OUTPUT:
m and n are not equal


Logical Operator:
--> These operators are used to perform logical operations on the given expressions.
--> There are 3 logical operators in C language. They are, logical AND (&&), logical OR (||) and logical NOT (!).

Example:

#include <stdio.h>

int main()
{
   int m=40,n=20;
   int o=20,p=30;
   if (m>n && m !=0)
   {
      printf("&& Operator : Both conditions are true\n");
   }
   if (o>p || p!=20)
   {
      printf("|| Operator : Only one condition is true\n");
   }
   if (!(m>n && m !=0))
   {
      printf("! Operator : Both conditions are true\n");
   }
   else
   {
      printf("! Operator : Both conditions are true. " \
      "But, status is inverted as false\n");
   }
}

OUTPUT:
&& Operator : Both conditions are true
|| Operator : Only one condition is true
! Operator : Both conditions are true. But, status is inverted as false


Bitwise Operator:
--> Bitwise operators are used for manipulating a data at the bit level
--> Bit-level programming mainly consists of 0 and 1.

Example:
#include <stdio.h>
int main() 
{
int a = 20; /* 20 = 010100 */  
    int b = 21; /* 21 = 010101 */
    int c = 0;           

    c = a & b;       /* 20 = 010100 */ 
    printf("AND - Value of c is %d\n", c );

    c = a | b;       /* 21 = 010101 */
    printf("OR - Value of c is %d\n", c );

    c = a ^ b;       /* 1 = 0001 */
    printf("Exclusive-OR - Value of c is %d\n", c );

    getch();
}


OUTPUT:

AND - Value of c is 20
OR - Value of c is 21
Exclusive-OR - Value of c is 1


Assignment Operator:
--> Assignment Operator is Used to assign value to an variable.
--> Assignment Operator have Two Values – L-Value and R-Value.Operator copies R-Value into L-Value.
--> Assignment Operator is binary operator which operates on two operands.

Example:

#include<stdio.h>

int main() {

   int res;

   res = 11;
   res += 4;
   printf("\nResult of Ex1 = %d", res);

   res = 11;
   res -= 4;
   printf("\nResult of Ex2 = %d", res);

   res = 11;
   res *= 4;
   printf("\nResult of Ex3 = %d", res);

   res = 11;
   res /= 4;
   printf("\nResult of Ex4 = %d", res);

   res = 11;
   res %= 4;
   printf("\nResult of Ex5 = %d", res);

   return 0;
}

OUTPUT:

Result of Ex1 = 15
Result of Ex2 = 7
Result of Ex3 = 44
Result of Ex4 = 2
Result of Ex5 = 3

Ternary or conditional Operator:
--> Programmers use ternary operators in C for decision making inplace of conditional statements if and else.
--> The ternary operator is an operator that takes three arguments. The first argument is a comparison argument, the second is the result upon a true comparison, and the third is the result upon a false comparison.
--> If it helps you can think of the operator as shortened way of writing an if-else statement.
int a = 10, b = 20, c;
c = (a < b) ? a : b;
printf("%d", c);
OUTPUT:
10




Practice Questions 

Question 1
#include<stdio.h>
main()
{
int a;
a=--2;
printf("a=%d",a);
}
 
Question 2
#include<stdio.h>
main()
{
int i=2,j=3,k,l;
float a,b;
k=i/j*j;
l=j/i*i;
a=i/j*j;
b=j/i*i;
printf("k=%d  l=%d  a=%f  b=%f \n",k,l,a,b);
}
 
Question 3
#include<stdio.h>
main()
{
int x=3,y=5,z;
z=x&&y;
printf("\n z=%d",z);
z=x&y;
printf("\n z=%d",z);
}
 
Question 4
#include<stdio.h>
main()
{
int x=7,y=10,a,b;
a=x||y;
printf("\n a=%d",a);
b=x|y;
printf("\n b=%d",b);
}
 
Question 5
#include<stdio.h>
main()
{
int x=3,y,z;
y=x=10;
z=x<10;
printf("\n x=%d y=%d z=%d",x,y,z);
}
 
Question 6
#include<stdio.h>
main()
{
int k=35;
printf("\n%d%d%d",k==5,k=50,k>40);
k=- -2;
printf("\n %d",k);
}
 
Question 7
#include<stdio.h>
main()
{
int x,y;
scanf("%d",&x);
y=(x<0)?-x:x;
printf("absolute value of %d  is %d \n",x,y);
}
 
Question 8
#include<stdio.h>
main()
{
int x=2,y=8,z=6;
x=y==z;
printf("\n%d\n",x);
}
 
Question 9
#include<stdio.h>
main()
{
int x=4,y,z;
y=--x;
z=x--;
printf("\n %d %d %d",x,y,z);
}
 
Question 10
#include<stdio.h>
main()
{
int x=4,y=3,z;
z=x-- - y;
printf("%d %d %d",x,y,z);

}
 
Question 11
#include<stdio.h>
main()
{
int a=3,b=4,c=1;
b=(a>b?(a>c?a:c):(b>c?b:c));
printf("%d",b);
}
 
Question 12
#include<stdio.h>
main()
{
int x=5;
printf("%d %d %d\n",x,x<<2,x>>2);
}
 
Question 13
#include<stdio.h>
main()
{
int x=20,y=35;
x=y++ + x++;
y=++y + ++x;
printf("%d %d\n",x,y);
}
 
Question 14
#include<stdio.h>
main()
{
printf("\n %d %d %d\n",sizeof('4'),sizeof("4"),sizeof(4));
}


Tuesday, December 17, 2019

Data Types in C Programming

There are three types of data Types in c programming:



1. Primary Data Types:
char
--> Character data types are used to define variables taking one character as its value.
short
--> The short integer can be used in places where small values and little storage space is required.
int
--> Integer data types are used to define the variables taking integer values with or without constant values given.
--> There are other data types like ‘short’ and ‘long’ used to define integer values but they have different ranges
long
--> long integer gives us a long range or a bigger size compared to ‘short’
--> It can cause our program to take more time for execution because of the storage size it offers.

float and double
--> Float and double data types are used to define variables that take up a decimal value or an exponential value.
--> Float has a range of –3.4e38 to +3.4e38 and its size is 4 bytes.
--> Double has a range of -1.7e308 to +1.7e308 and its size is 8 bytes.

2. Derived Data Types:
Array
--> array is a collection of data of the same data type.
--> we declare an integer array, all the values in the array have to be an integer.
--> These are declared under the same variable and are accessed using it.
Pointers
--> A pointer contains the address of a variable in the program.

3. User-defined Data Types

Structures :
--> It is a collection of variables of different data types represented by the same name.
Union :
--> Store data of different data type in the same memory location.
--> A union can have multiple members but it can store only one member at a particular time.
Enum
--> Enumeration is used to declare the variables and consists of integral constants.




EXAMPLE 1: Integer Variable in C
#include <stdio.h>
void main()
{
    int i = 15;
    printf("integer: %d \n", i);
}
OUTPUT:
The %d is to tell printf() function to format the integer i as a decimal number.

The output from this program would be integer: 15.


EXAMPLE 2: Float Variable in C
#include <stdio.h>
void main()
{
    float f = 3.145
    printf("My float: %f \n", f);
}

OUTPUT:
The %f is to tell printf() function to format the float variable f as a decimal floating number. My float: 3.14

EXAMPLE 3: Character Variable in C
#include <stdio.h>
void main()
{
    char c;
    c = 'b';
    printf("This is my character: %c \n", c);
OUTPUT:
The %c is to tell printf() function to format the variable “c” as a character.

 The output from this program would be This is my character: b.

Monday, December 16, 2019

Errors in C Programming




Errors in C Programming :

There are three types of errors in c programming:

Compile Time Errors : Compile errors are those errors that occur at the time of compilation of the program. C compile errors may be further classified as:

1. Preprocessor error
#include<stdio.H> // small h
2. Translator error
int i;printf(%d, i);      // syntax error
3 Linkage error
int i=1;print("%d", i);   
// undefined reference error like using print instead of printf

Run-time Errors : Run time error are those error which occurs at the time of running of the program.
C run time errors may be further classified as:

1. Segmentation fault
int i;scanf("%d", i);    // segmentation error it should be scanf("%d",&i);  

2. Bus error:
--> Bus error is a fault raised by hardware, notifying an operating system (OS) that a process is trying to access memory that the CPU cannot physically address: an invalid address for the address bus.


Tuesday, December 3, 2019

Compilation stages in c

The Four Stages of Compiling a C Program



1. Preprocessing
--> Removal of Comments
--> Include the header files
--> Replacement of Macros
Command to generate the Preprocessing file.
$cc -E test.c -o test.i

2. Translator
--> Checking the Syntax errors
--> Translate c program into Assembly language.
Command to generate the Translator file.
$cc -S test.i -o test.s

3. Assembly
--> Assembly convert the program into machine understanding program.
Command to generate the Assembly file.
$cc -c test.s -o test.o

4. Linking
--> Link the depended libraries
--> Adding additional OS related information
--> Create the executable file
Command to generate the final file.
$cc test.c -o test