Relational and logical operators

In notation relation operator and logical operator term relations means a relationship that can exist between two meanings, and the term logical- the relationship between the logical values ​​"true" and "false". And since relational operators give true or false results, they are often used together with logical operators. It is for this reason that they are considered together.

The following are the relational operators:

Boolean operators include the following:

The result of executing a relational or logical operator is a boolean value of type bool .

In general, objects can be compared for equality or inequality using the relational operators == and !=. And the comparison operators, =, can only be applied to data types that support an order relation. Therefore, relational operators can be applied to all numeric data types. But bool values ​​can only be compared for equality or inequality, since true (true) and false (false) values ​​are not ordered. For example, comparing true > false in C# doesn't make sense.

Consider an example program that demonstrates the use of relational and logical operators:

Using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 ( class Program ( static void Main(string args) ( short d = 10, f = 12; bool var1 = true, var2 = false; if (d f) Console.WriteLine("d > f"); // Comparing variables var1 and var2 if (var1 & var2) Console.WriteLine("This text will not be output"); if (!(var1 & var2)) Console.WriteLine("!(var1 & var2) = true"); if (var1 | var2) Console.WriteLine("var1 | var2 = true"); if (var1 ^ var2) Console.WriteLine("var1 ^ var2 = true"); Console.ReadLine(); ) ) )

Logical operators in C# perform the most common logical operations. Nevertheless, there are a number of operations performed according to the rules of formal logic. These logical operations can be built using the logical operators supported in C#. Therefore, C# provides a set of logical operators that is sufficient to construct almost any logical operation, including implication. implication is a binary operation that evaluates to false only if its left operand is true and its right operand is false. (The operation of implication reflects the principle that true cannot imply false.)

The implication operation can be built on the basis of a combination of logical operators! and |:

Shortcut Boolean Operators

C# also provides special shortened, variants of the logical AND and OR operators designed to produce more efficient code. Let us explain this with the following examples of logical operations. If the first operand of a logical AND operation is false, then its result will be false, regardless of the value of the second operand. If the first operand of the logical OR operation is true, then its result will be true regardless of the value of the second operand. Due to the fact that the value of the second operand in these operations does not need to be calculated, saves time and improves code efficiency.

shortened logical operation And it is done with operator &&, and the shortened logical operation OR - using operator ||. These shortened logical operators correspond to the regular logical operators & and |. The only difference between a shortened logical operator and a regular one is that its second operand is evaluated only as needed.

Any language expression consists of operands (variables, constants, etc.) connected by operation signs. An operator sign is a character or group of characters that tells the compiler to perform certain arithmetic, logical, or other operations.

Operations are performed in strict sequence. The value that determines the pre-emptive right to perform a particular operation is called priority. In table. 2 lists the various operations of the C language (C). Their priorities for each group are the same (groups are highlighted in color). The more advantage the corresponding group of operations enjoys, the higher it is located in the table. The order in which operations are performed can be controlled using parentheses.

Table 2 - operations

Operation sign

Purpose of the operation

Function call

Selecting an array element

Highlighting a Record Element

Highlighting a Record Element

Logical negation

Bitwise negation

Sign change

Unit increase

Decrease by one

Taking an address

Contact by address

Type conversion (i.e. (float) a)

Determining the size in bytes

Multiplication

Definition of the remainder of the division

Addition

Subtraction

Shift left

shift right

Less than

Less or equal

More than

More or equal

Bitwise logical "AND"

Bitwise XOR

Bitwise logical "OR"

Logic "AND"

Logical "OR"

Conditional (ternary) operation

Assignment

+=, - =, *=, /=, %=, <<=,
>>=, &=, |=, ^=

Binary operations (for example, a *= b
(i.e. a = a * b), etc.)

Operation comma

Operator in C language (C)

To eliminate confusion in the concepts of "operation" and "operator", we note that the operator is the smallest executable unit of the program. There are expression operators whose action is to evaluate given expressions (for example: a = sin(b)+c; j++;), declaration operators, compound operators, empty operators, label operators, cycle operators, etc. A semicolon is used to mark the end of a statement in C (C). As for a compound statement (or block), which is a set of logically related statements placed between opening (() and closing ()) curly braces ("operator brackets"), it is not followed by a semicolon. Note that a block differs from a compound statement by the presence of definitions in the body of the block.

Characteristics of the basic operations of the C language (C)

Let us characterize the main operations of the SI (C) language.

assignment operation

First, consider one of them - the assignment operator (=). Expression of the form

assigns the value of x to y. The "=" operator can be used multiple times in one expression, for example:

x=y=z=100;

There are unary and binary operations. The first of them has one operand, and the second has two. Let's start their consideration with operations assigned to the first of the following traditional groups:

Arithmetic operations.

Logical and relational operations.

Operations with bits.

Arithmetic operations are specified by the following symbols (Table 2): +, -, *, /, % . The last of them cannot be applied to variables of real type. For example:

a = b + c;
x = y - z;
r = t*v;
s = k / l;
p = q % w;

Boolean operations

The logical operations of a relation are specified by the following symbols (see Table 2): && ("AND"), || ("OR"), ! ("NOT"), >, >=,<, <= , = = (равно), != (не равно). Традиционно эти операции должны давать одно из двух значений: истину или ложь. В языке СИ (C)принято следующее правило: истина - это любое ненулевое значение; ложь - это нулевое значение. Выражения, использующие логические операции и операции отношения, возвращают 0 для ложного значения и 1 для истинного. Ниже приводится таблица истинности для логических операций.

Bit operations can be applied to variables of types int, char, and their variants (for example, long int). They cannot be applied to variables of types float, double, void (or more complex types). These operations are specified by the following symbols: ~ (bitwise negation),<< (сдвиг влево), >> (right shift), & (bitwise AND), ^ (bitwise XOR), | (bitwise "OR").

Examples: if a=0000 1111 and b=1000 1000 then

~a = 1111 0000,
a<< 1 = 0001 1110,
a >> 1 = 0000 0111,
a & b = 0000 1000,
a^b=10000111,
a | b = 1000 1111.

The language provides two unconventional increment (++) and decrement (--) operators. They are designed to increase and decrease by one the value of the operand. The ++ and -- operators can be written before or after the operand. In the first case (++n or --n), the value of the operand (n) is changed before it is used in the corresponding expression, and in the second case (n++ or n--) after it is used. Consider the following two lines of program:

a = b + c++;
a1 = b1 + ++c1;

Suppose that b = b1 = 2, c = c1 = 4. Then after performing the operations: a = 6, b = 2, c = 5, a1 = 7, b1 = 2, c1 = 5.

Expressions with another non-traditional ternary or conditional operation ?: are also widely used. In the formula

y = a if x is not zero (i.e. true), and y = b if x is zero (false). The following expression

y = (a>b) ? a:b;

allows you to assign the value of a larger variable (a or b) to the variable y, i.e. y = max(a, b).

Another difference of the language is that an expression of the form a = a + 5; can be written in another form: a += 5;. Instead of the + sign, symbols of other binary operations can also be used (see Table 2).

Other operations from the table. 2 will be described in the following paragraphs.

Loops are organized to execute some statement or group of statements a certain number of times. There are three loop statements in C (C) language: for, while and do - while. The first of them is formally written as follows:

for (expression_1; expression_2; expression_3) loop_body

The loop body is either one statement or several statements enclosed in braces( ... ) (there is no semicolon after the block). Expressions 1, 2, 3 contain a special variable called the control variable. By its value, the need to repeat the cycle or exit from it is set.

Expression_1 assigns an initial value to the control variable, expression_3 changes it at each step, and expression_2 checks to see if it has reached the boundary value that sets the need to exit the loop.

for (i = 1; i< 10; i++)

for (ch = "a"; ch != "p";) scanf ("%c", &ch);

/* The loop will be executed until the keyboard

character "p" will not be entered */

Any of the three expressions in for loop may be omitted, but the semicolon must remain. So for (; ;) (...) is an infinite loop that can only be exited in other ways.

The following rule is adopted in the SI (C) language. Any assignment expression enclosed in parentheses has the same value as the one being assigned. For example, the expression (a=7+2) has a value of 9. After that, you can write another expression, for example: ((a=7+2)<10), которое в данном случае будет всегда давать истинное значение. Следующая конструкция:

((ch = getch()) == "i")

allows you to enter the value of the variable ch and give a true result only when the entered value is the letter "i". In brackets, you can write several formulas that make up a complex expression. For these purposes, the comma operation is used. Formulas will be evaluated from left to right, and the entire expression will take on the value of the last formula evaluated. For example, if there are two variables of type char, then the expression

z = (x = y, y = getch());

defines the following actions: the value of the variable y is assigned to the variable x; a character is entered from the keyboard and assigned to the variable y; z gets the value of y. The parentheses are necessary here because the comma operator has lower precedence than the assignment operator written after the variable z. The comma operator is widely used for constructing for loop expressions and allows you to change the values ​​of several control variables in parallel.

Nested constructions are allowed, i.e. the body of a loop may contain other for statements.

The while statement is formally written like this:

while (expression) loop_body

The parenthesized expression can take a non-zero (true) or zero (false) value. If it is true, then the body of the loop is executed and the expression is evaluated again. If the expression is false, then the while loop ends.

The do-while statement is formally written as follows:

do (loop_body) while (expression);

The main difference between while and do-while loops is that the body in a do-while loop is executed at least once. The body of the loop will be executed until the expression in parentheses evaluates to false. If it is false when entering the loop, then its body is executed exactly once.

Nesting of some loops into others is allowed, i.e. statements for, while and do-while may appear in the body of any loop.

The new break and continue statements can be used in the loop body. The break statement provides an immediate exit from the loop, the continue statement causes the termination of the next iteration and the beginning of the next iteration.

Conditional and unconditional jump operators

To organize conditional and unconditional jumps in a C (C) program, the following statements are used: if - else, switch and goto. The first one is written as follows:

if (check_condition) statement_1; else statement_2;

If the condition in parentheses evaluates to true, statement_1 is executed; if false, statement_2 is executed. If multiple statements need to be executed instead of one, they are enclosed in curly braces. The if statement may not contain the else word.

In an if - else statement, other statements must immediately follow the if and else keywords. If at least one of them is an if statement, it is called nested. According to C language convention, the word else always refers to the nearest if that precedes it.

The switch statement allows you to choose one of several alternatives. It is written in the following formal form:

switch (expression)

case constant_1: statements_1;

case constant_2: statements_2;

........ ........

default: operators_default;

This evaluates the value of the whole expression in parentheses (sometimes called a selector) and compares it against all constants (constant expressions). All constants must be different. If there is a match, the corresponding statement variant (one or more statements) will be executed. The variant with the default keyword is implemented if none of the others matched (the word default may be absent). If default is absent and all comparison results are negative, then none of the options are executed.

To stop subsequent checks after a successful selection of some option, the break statement is used, which provides an immediate exit from the switch switch.

Nested switch constructs are allowed.

Consider the rules for performing an unconditional jump, which can be represented in the following form:

goto label;

A label is any identifier followed by a colon. The goto statement specifies that program execution should continue from the statement preceded by the label. A label can be placed before any statement in the function where the corresponding goto statement is found. It doesn't need to be announced.

Turbo debugger fully supports C language (C) expression syntax. The expression consists of a mixture of operations, strings, variables

Since in the previous article, I first used a logical operation, I will tell you what they are, how many of them and how to use them.

There are three logical operations in C++:

  1. The logical AND operation && , we already know;
  2. Logical operation OR || ;
  3. Logical operation NOT ! or logical negation.

Logical operations form a complex (composite) condition from several simple (two or more) conditions. These operations simplify the structure of the program code by several times. Yes, you can do without them, but then the number of ifs increases several times, depending on the condition. The following table briefly characterizes all logical operations in the C++ programming language for building logical conditions.

Now you should understand the difference between the logical operation AND and the logical operation OR, so as not to get confused in the future. It's time to get acquainted with the data type bool-logical. This data type can take two values: true (true) and false (false). The condition to be checked in the select statements has the data type bool . Consider the principle of the following program, and everything will be clear with all these logical operations.

// or_and_not.cpp: Specifies the entry point for the console application. #include "stdafx.h" #include using namespace std; int main(int argc, char* argv) ( bool a1 = true, a2 = false; // declaration of boolean variables bool a3 = true, a4 = false; cout<< "Tablica istinnosti log operacii &&" << endl; cout << "true && false: " << (a1 && a2) << endl // логическое И << "false && true: " << (a2 && a1) << endl << "true && true: " << (a1 && a3) << endl << "false && false: " << (a2 && a4) << endl; cout << "Tablica istinnosti log operacii ||" << endl; cout << "true || false: " << (a1 || a2) << endl // логическое ИЛИ << "false || true: " << (a2 || a1) << endl << "true || true: " << (a1 || a3) << endl << "false || false: " << (a2 || a4) << endl; cout << "Tablica istinnosti log operacii !" << endl; cout << "!true: " << (! a1) << endl // логическое НЕ << "!false: "<< (! a2) << endl; system("pause"); return 0; }

Lines 9 and 10you should be clear, since variables of the type are initialized here bool . And each variable is assigned a value true or false . Beginning with 9th line and ending 20th, showing the use of logical operations. The result of the program (see Figure 1).

Tablica istinnosti log operacii && true && false: 0 false && true: 0 true && true: 1 false && false: 0 Tablica istinnosti log operacii || true || false: 1 false || true: 1 true || true: 1 false || false: 0 !true: 0 !false: 1 Press any key to continue. . .

Figure 1 - C++ Logical Operations

Probably, you have a question, “What are these zeros and ones?”. If there is a question, then it must be answered. I answer: "Zero is a representation of the logical value false (false), but ones are the logical value true (true)." Let me briefly explain some points. Compound condition using Boolean And is true only if both simple conditions are true. In all other cases, the compound condition is false. A compound condition using logical OR is false only if both simple conditions are false. In all other cases, the compound condition is true. Logical negation NOT is a unary operation, and it does not combine two conditions, unlike logical operations And and OR, which are binary operations. Logical negation allows you to reverse the meaning of the condition, which is very convenient in some cases. A condition with logical negation is true if the same condition is false without negation, and vice versa.

Various operations can be performed on objects in the C language:

  • assignment operations;
  • relation operations;
  • arithmetic;
  • brain teaser;
  • shift operations.

The result of the operation is a number.

Operations can be binary or unary.
Binary operations are performed on two objects, unary operations on one.

assignment operation

The assignment operation is denoted by the symbol = and is performed in 2 stages:

  • the expression on the right side is evaluated;
  • the result is assigned to the operand on the left side:

object = expression;

Example:

int a = 4; // variable a is assigned the value 4
intb;
b = a + 2; // variable b is assigned the value 6 calculated on the right side

If the objects in the left and right parts of the assignment operation have different types, an explicit type conversion operation is used.
object = (type)expression;

Example:

float a = 241.5;
// Before calculating the remainder of the division, a is converted to an integer type
int b = (int )a % 2; // b = 1

relation operations

Basic relation operations:

  • == equivalent - check for equality;
  • != not equal - check for inequality;
  • < less;
  • > more;
  • <= less or equal;
  • >= more or equal.

Relational operations are used to organize conditions and branches. The result of these operations is 1 bit, the value of which is 1 if the result of the operation is true, and is 0 if the result of the operation is false.

Arithmetic operations

Basic binary operations, arranged in descending order of precedence:

  • * - multiplication;
  • / - division;
  • + - addition;
  • - subtraction;
  • % is the remainder of an integer division.

Basic unary operations:

  • ++ - incrementing (increase by 1);
  • -- - decrementing (decrement by 1);
  • - sign change.

The result of evaluating an expression containing increment or decrement operations depends on where the sign of the operation is located (before or after the object). If the operation is located before the object, then the value of the variable is first changed to 1, and then this value is used to perform the following operations. If the operation ++ or is located after the variable, then the operation is performed first, and then the value of the variable is changed to 1.

Example :

Binary arithmetic operations can be combined with the assignment operator:

  • object *= expression; // object = object * expression
  • object /= expression; // object = object / expression
  • object += expression; // object = object + expression
  • object -= expression; // object = object - expression
  • object %= expression; // object = object % expression

Boolean operations

Logic operations are divided into two groups:

  • conditional;
  • bitwise.

Conditional logical operations are most often used in if condition check operations and can be performed on any object. Result of the conditional boolean operation:

  • 1 if the expression is true;
  • 0 if the expression is false.

In general, all non-zero values ​​are interpreted by the conditional boolean operations as true.

Basic conditional logical operations:

  • && - And (binary) - simultaneous execution of all operations of the relation is required;
  • || - OR (binary) - at least one relation operation is required;
  • ! - NOT (unary) - non-execution of the relation operation is required.

Bitwise logical operations operate on bits, each of which can take only two values: 0 or 1.

Basic bitwise logical operations in C language:

  • & conjunction (logical AND) - a binary operation, the result of which is 1 only when both operands are single (in the general case, when all operands are single);
  • | disjunction (logical OR) - a binary operation, the result of which is 1 when at least one of the operands is 1;
  • ~ inversion (logical NOT) is a unary operation, the result of which is 0 if the operand is single, and is 1 if the operand is zero;
  • ^ XOR is a binary operation whose result is 1 if only one of the two operands is 1 (generally, if the input set of operands has an odd number of 1s).

For each bit, the result of the operation will be obtained in accordance with the table.

a b a&b a | b ~a a^b
0 0 0 0 1 0
0 1 0 1 1 1
1 0 0 1 0 1
1 1 1 1 0 0

Example :

1
2
3
4
5
6
7

unsigned char a = 14; // a = 0000 1110
unsigned char b = 9; // b = 0000 1001
unsigned char c, d, e, f;
c = a&b; // c = 8 = 0000 1000
d = a | b; // d = 15 = 0000 1111
e = ~a; // e = 241 = 1111 0001
f = a^b; // f = 7 = 0000 0111


Bitwise operations allow you to set and reset individual bits of a number. For this purpose, it is used bit masking. The masks corresponding to the setting of each bit in a byte are presented in the table

Bit Mask
0 0x01
1 0x02
2 0x04
3 0x08
4 0x10
5 0x20
6 0x40
7 0x80

To set a certain bit, you need to set the corresponding bit of the mask to 1 and perform a bitwise logical OR operation with a constant representing the mask.

The syntax of the SI assignment operator is:
LValue = RValue;
LValue is where the value will be written. Only a variable can act as such an object in SI.
RValue is the value of which we will write to LValue. And in this role, objects such as:
variable,
constant,
function call operator,
mathematical or logical expression.
Assignment examples
int a, b, c;
double x, y;
a = 5; b = 4; c = a + b;
x=5.0; y = exp(x);

Improved assignment operators in SI

There are so-called improved assignment operators in SI, they look like this:
LValue X= RValue; where X is one operation from the set: + - * / % ^ & | >. this is the analogy of the assignment operator:
LValue = LValue X RValue;
For example:
a += b; ≡ a = a + b;
In the C language, all mathematical operations can be divided into 2 groups:
1. mathematical operations for real and integer calculations;
2. mathematical operations for integer calculations only.

Mathematical operations for real and integer calculations of the SI language include ordinary arithmetic operations:
addition (+),
subtraction (-),
multiplication (*), division (/).

Matching result type from operand types

Features of the SI language

Consider one of the features on an example:
int a,b;
double c;
a = 10;
b = 4;
c = a / b; // c will be equal to 2, since the operation is performed not by division, but by integer division, or:
double x = 1 / 3; // x will be 0, for the same reason as in the previous example

Operations for integer calculations

Integer calculations include:
the operation of taking the remainder of a division,
bitwise operations,
shift operations,
increment and decrement operations.
The operation of taking the remainder of division (mod) is a binary operation and in the SI language is denoted by the percent symbol (%). Calculation example:
int a = 10, b = 3, c;
c = a % b; // c will be equal to 1

Bitwise operations in SI

The bitwise operations of the SI language are represented by three binary and one unary operation. Binary bitwise operations include:
operation "AND" (&),
operation "OR" (|)
operation "Exclusive OR" (^).

Here is the truth table for these operations:

first operand second operand operation
and or exclusive or
0 0 0 0 0
1 0 0 1 1
0 1 0 1 1
1 1 1 1 0

Bitwise Operations

The unary bitwise operator is the negation operator, denoted by the tilde character (~). Example:
unsigned char a = 10, b; //a: 00001010 = 10
b = ~a; //b: 11110101 = 245

Shift operations

Shift operations perform a bitwise shift of the integer value specified in the first operand, to the right (symbol >>) or left (symbol<<) на указанное во втором операнде целое число бит. Пример:
unsigned char a = 10, b, c; //a: 00001010 = 10
b = a<< 2; //b: 00101000 = 40
c = a >> 1; // c: 00000101 = 5

Increment and decrement operations

The increment (++ sign) and decrement (- sign) operators are unary and increase and decrease an integer value by one, respectively.
int a = 10, b, c;
b = ++a //pre-increment b == 11
c = a++; //post-increment c == 11

In modern programming languages ​​(including the SI language of the C99 standard), these operations can also be used for real values. Example:
double x = 12.5;
x++;
printf("%lf\n",x); //output: 13.5

Relational operations (comparisons)

In programming languages, relational (comparison) operators are binary operators that compare two operands and return the result of the comparison as a boolean value. In the C language, it is customary to interpret the logical values ​​TRUE and FALSE as integer values:
0 - FALSE, 1 - TRUE.
Designation Name
> More
< Less
>= More or equal
<= Less or equal
== Equals
!= Not equal

Examples
Some examples of using comparison operators:
int a=5, b=4, c=10, x, y;
x = a > b; //x == 1
y=c==a; //y == 0

Boolean operations in SI

Boolean operations are unary or binary operations that operate on boolean values ​​and return a boolean value. The set of logical operations for different programming languages ​​may be different.

Boolean operations


Examples of logical operations:
int a=1, b=0, c, d; //a - TRUE, b - FALSE
c = a || b; // c == 1
d = !b && a; //d == 1

Operation Priorities

++, -- Post-increment and decrement operations
() Calling a function, grouping operations
Accessing an array element
-> Accessing a structure or union field through a pointer
. Accessing a structure or union field
++, -- Pre-increment and decrement operations
! Logical "NOT"
~ Binary negation (inversion)
+, - Unary plus and minus
& Address take operation
* Pointer dereferencing
sizeof Sizing operator
(type) Type conversion operator
* Multiplication
/ Division
% Taking the remainder of a division
+ Addition
- Subtraction
<<, >> Bit shifts left and right
<, <=, >, >= Comparison operations
==, != Comparison operations
& Bitwise "and"
^ Bitwise XOR
| Bitwise "OR"
Logic "AND"
|| Logical "OR"
?: Conditional operation
= Simple assignment operator
*=, /=, %=, +=, -=, <<=, >>=, &=, ^=,|= Enhanced Assignment Operators
, Comma

Features of translators


The order in which the function arguments are evaluated when it is called is not defined. Therefore, the following statement may produce different results when translated by different compilers:
printf("%d %lf\n", ++n, pow(2.0,n));
The result will depend on whether n is incremented before or after the call to pow. To solve the problem, just write it like this: n++;
printf("%d %lf\n", n,pow(2.0,n));

Automatic Type Coercion Schema


1.If any of the operators is of type long double, then the other is reduced to long double.
2.Otherwise, if any of the operators has the type double, then the other is reduced to double.
3.Otherwise, if any of the operators has the type float, then the other is reduced to float.
4.Otherwise, integer type expansion is performed for both operands; then, if one of the operands is of type unsigned long int, then the other is converted to unsigned long int.
5.Otherwise, if one of the operands is of type long int, and the other is unsigned int, then the result depends on whether long int all values unsigned int; if so, then an operand of type unsigned int
6.cast to type long int; if not, both operands are converted to unsigned long int.
7.Otherwise, if one of the operands is of type long int, then the other is reduced to long int.
8.Otherwise, both operands are of type int.

Cast operator

int a = 15, b = 2; double r = 0.0;
r = a / b; //r == 7.0

Type casting operator: (type)expression.
r = (double) a / b; //Correctly
r = (double) (a / b); //Not properly

Conditional operation


There is a so-called conditional operator in C language, which has the following syntax:
condition? expression #1: expression #2;
An example of a conditional operation. It is necessary to enter two real values ​​​​from the keyboard and display the maximum of these values ​​on the screen:
#include


  {
double x,y;
scanf("%lf %lf",&x,&y);
double max = (x > y) ? x: y;
return 0;
  }

It is necessary to enter three real values ​​​​from the keyboard and display the maximum of these values:
#include

int main(int argc, char *argv)
  {
double x, y, z;
printf("Enter values: ");
scanf("%lf %lf %lf",&x,&y,&z);
double max = (x > y) ? ((x > z) ? x: z): ((y > z) ? y: z);
printf("Max value: %lf\n",max);
return 0;
  }

A real number is entered from the keyboard. Raise a number to the fourth power using only two multiplications.
#include

int main(int argc, char *argv)
  {
double a;
printf("Enter value: ");
scanf("%lf",&a);
a*=(a*=a);
printf("Result: %lf\n",a);
return 0;
  }

A quadratic equation of the form is given by the coefficients A, B and C. Determine how many roots this equation has.
#include

int main(int argc, char *argv)
  {
double a,b,c;
printf("Enter coefficients A, B and C: ");
scanf("%lf %lf %lf",&a,&b,&c);
double d = b*b-4*a*c;
int n = (d< 0.0)?0:(d > 0.0)?2:1;
printf("Number of roots: %d\n",n);
return 0;
  }