C/Language Basics/Operator General

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Bitwise operator

#include<stdio.h>
 
 main() {
   char c1,c2,c3;
   
   printf("enter value for c1 and c2");
   scanf("%c,%c",&c1,&c2);
  
   c3 = c1 & c2; 
   printf("\n c1 & c2 = %c",c3);
  
   c3 = c1 | c2; 
   printf("\n c1 | c2 = %c",c3);
  
   c3 = c1 ^ c2; 
   printf("\n i.e. c1 ^ c2 = %c",c3);
  
   c3 = ~c1; 
   printf("\n compliment  of  c1 = %c",c3);
  
   c3 =  c1<<2;
   printf("\n left shift by 2 bits c1 << 2 = %c",c3);
  
   c3 =  c1>>2;
   printf("\n right shift by 2 bits c1 >> 2 = %c",c3);
 }


Calculation: divide and mod, / %

/* Calculation: divide and mod, / % */
#include <stdio.h>
void main()
{
  int total = 45;                    
  int divider = 7;
  int a = 0;    
  int b = 0; 
  
  a = total/divider;  
  
  printf(" total is %d  and  divider is %d", total, divider);
  printf("\n a is  %d.", a);
  /*  left over */
  b = total%divider;          
  printf("\nThere are %d  left over.\n", b);
}


Calculation: divide, product and PI

/* calculations on a table */
#include <stdio.h>
void main()
{
   float r = 0.0f;              /* The radius of the table        */
   float d = 2.0f;              /* The diameter of the table      */
   float c = 0.0f;              /* The circumference of the table */
   float area = 0.0f;           /* The area of a circle           */
   float Pi = 3.14159265f; 

   r = d/2.0f;           /* Calculate the radius                */
   c = 2.0f*Pi*r;        /* Calculate the circumference         */
   area = Pi*r*r;           /* Calculate the area                 */
   printf("\nThe circumference is %.2f", c);  
   printf("\nThe area is %.2f\n", area);  
}


Calculations: plus

/*Calculations: plus */
#include <stdio.h>
void main()
{
  int total;       /* The total number */
  int cats;        /* The number of cats */
  int dogs;        /* The number of dogs */
  /* Set the number of each kind of pet */
  cats = 2;             
  dogs = 1;
  /* Calculate the total number of pets */
  total = cats + dogs;     
  printf("We have %d pets in total", total);          /* Output the result */
}


Calculations: plus and minus

/* Calculations: plus and minus*/
#include <stdio.h>
void main()
{
   int cookies = 5;
   int cookie_calories = 125;       /* Calories per cookie */
   int total_eaten = 0;             /* Total of cookies eaten */
   int eaten = 2;                   /* Number to be eaten */
   cookies = cookies - eaten;       /* Subtract number eaten from cookies */
   total_eaten = total_eaten + eaten;
   printf("\nI have eaten %d cookies.  There are %d cookies left",
                                                            eaten, cookies);
   printf("\nTotal energy consumed is %d calories.\n",
                                           total_eaten*cookie_calories);
}


compute the area of a triangle, given its width and height

#include <stdio.h>
int main()
{
    char line[100];
    int  height;
    int  width;
    int  area;
    printf("Enter width height? ");
    fgets(line, sizeof(line), stdin);
    sscanf(line, "%d %d", &width, &height);
    area = (width * height) / 2;
    printf("The area is %d\n", area);
    return (0);
}


Division with float values and output it

/* Division with float values */
#include <stdio.h>
void main()
{
   float a = 10.0f;    
   float b = 4.0f;      
   float result = 0.0f; 
  
   result = a/b;
   printf("result is %f .",result);
}


( ) operator

 #include<stdio.h>
 
 main() {
   int i,j,k;
   
   k = (i = 4, j = 5);
   
   printf("k = %d",k);
 }


Operator for int value

#include<stdio.h>
main() {
  int a,b,c,d;
  int sum,sub,mul,rem;
  float div;
 
  printf("enter value for b, c, d:");
  scanf("%d%d%d",&b,&c,&d);
  
  sum = b+c;
  sub = b-c;
  mul = b*c;
  div = b/c;
  rem = b%d;
  a = b/c * d;
  
  printf("\n sum = %d, sub = %d, mul = %d, div = %f",sum,sub,mul,div);
  printf("\n remainder is %d",rem);
  printf("\n a = %d",a);
}


Output float: Result of the divide

#include <stdio.h>
float result;  /* Result of the divide */
int main()
{
    result = 7.0 / 22.0;
    printf("The result is %f\n", result);
    return (0);
}


Plus operator

  
#include<stdio.h>
main() {
  int a,b,c;
  printf("enter value for a,b, c");
  
  scanf("%d%d%d",&a,&b,&c);
  a += b*c+a;
  
  printf("\n a = %d",a);
}


This program produces a Celsius to Fahrenheit conversion chart for the numbers 0 to 100

#include <stdio.h>

int main() {
    int celsius;
    
    for (celsius = 0; celsius <= 100; ++celsius);
        printf("Celsius:%d Fahrenheit:%d\n", celsius, (celsius * 9) / 5 + 32);
    return (0);
}


Use and

#include <stdio.h>
int main()
{
    int a, b;  /* two integers */
    a = 4;
    b = 2;
    if ((a != 0) && (b != 0))
      printf("Both are not zero\n");
    return (0);
}


Using the ;amp operator: get the address

#include<stdio.h>
void main()
{
   /* integer variables */
   long a = 4L;
   long b = 5L;
   long c = 6L;                        
   /* Floating point variables */
   double d = 4.0;
   double e = 5.0;
   double f = 6.0;
  
   printf("A variable of type long occupies %d bytes.", sizeof(long));
   printf("\nThe address of a is: %p  The Address of b is: %p", &a, &b);
   printf("\nThe address of c is: %p", &c);
   printf("\n\nA variable of type double occupies %d bytes.", sizeof(double));
   printf("\nHere are the addresses of some variables of type double:");
   printf("\nThe address of d is: %p  The Address of e is: %p", &d, &e);
   printf("\nThe Address of f is: %p\n", &f);
}