Conditional Operator (?:) is ternary operator (demands 3 operands), and is used in certain situations, replacing if-else condition phrases. Conditional operator’s shape is:
Condition_phrase ? phrase1 : phrase2;
If conditional_phrase is true, phrase1 is executed and whole phrase is assigned with value of this phrase1. If the conditional phrase is false, thenphrase2 is executed, and whole phrase is assigned with phrase2 value.
Example:
int a, b, c;
...
c = a > b ? a : b; // if a>b "execute" a, else b
PRE-PROCESSOR’S COMMANDS
Before source code is translated (compiled), to a language “machine” understands, pre-processor is started. Depending on pre-processor’s commands, pre-processor changes the source code, and this changed code is now ready to be translated. Changes pre-processor made are saved into temp file, and the source code file is left untouched. Some of pre-processors commands are:
#include includes other files and links it with program
#define defines symbolic constant or so called macro
#include <stdio.h>
#include "MyFunctions.h"
#define PI 3.14159
#define PROGRAM NAME "Program"
With little help of pre-processor’s "define" command, macros can be produced (they remind of function but they aren’t one).
Example:
What’s the value assigned to b variable after following commands?
int a, b;
#define MAX(x,y) ((x) > (y)) ? (x) : (y)
a = 3;
b = MAX (3, a++);
/* Pre-processor physically changes this line with following:
b = ((3) > (a++)) ? (3) : (a++);
In the end values are
a = 5, b = 4 */
a = 3;
b = MAX (a++, 3);
/* Pre-processor physically changes this line with following:
b = ((a++) > (3)) ? (a++) : (3);
In the end values are
a = 4, b = 3 (3++ > 3) ? 3++ : 3*/
LOGIC OPERATORS
Simple relation phrases can be combined into more complex ones with help of logic operators. C language includes 3 logic operators.
Pay attention to difference between logic operators and logic operators above bits!
Example:
if ((x>20) && (x<100)) printf("x is inside open interval 20-100");
if ((x<5) || (x>20)) printf("x is not inside closed interval 5-20");
if (!(x>20)) printf("x is smaller or equal to 20");
Example:
What does following program block do?
int a, b, c, d;
a = 0;
b = 4;
c = (a++) + b; /*(a++) +b 0+4 4*/
printf ("a = %d, b = %d,c = %d " , a, b, c); /* 1, 4, 4 */
d = c && b + 3 * a;
printf ("d = %d", d);
Because priority of arithmetic operators is bigger then priority of logical operators:
d = c && b + 3 * a /* this is the same as expression in next row */
d = c && (b + (3 * a))
d = 4 && (4+ (3 * 1))
d = 4 && 7
d = 1
Complex phrases are often wrongly stated because people tend to “literally rewrite” stated conditions:
Statement “if x is higher than 20 and lower than 100” (pay attention that “lower than 100” reefers to x smaller than 100), is often wrongly (literally) written:
If (x>20 && <100) instead of correct one: If (x>20 && x<100)
This produces error translation. Another thing, people often mistake putting “,” instead of operator “AND”, so previous statement is again wrongly interpreted:
if(x>20 , x<100)
which produces “logic” error (which is much harder to notice, since compiler - translator program, can’t find it and report as error) .
Phrase "x>20,x<100" only matches the statement "x<100".
OPERATORS PRIORITY AND ASSIGNMENT
Term “assignment” reefers to assigning two operators of the same priority, one after another (from left to right and sideways). Table of priorities and assignment of all previously mentioned operators:
Pay attention that all comparing operators don’t use same priority!
Example:
Write your own program which calculates results (roots) of square equation(image placeholder).
Additional Info:
First #include <stdio.h> command "includes" header file with functions and constants description which reefer to work with input/output (inputting from keyboard/printing on screen).
Second #include <math.h> command "includes" header file with description of mathematical functions and constants.
When translating the code (linking), MS Visual C++ 6.0 includes data files with implementation of mathematical functions, by definition. When translating it isn’t necessary to give any additional parameters. When working with some Unix OS programming tools, C translator doesn’t include that data file by definition, so you have to add "-lm" parameter (option) manually (when starting translation).
#include <stdio.h>
#include <math.h>
main() {
float a, b, c, x1, x2, q, d, x1r, x2r, x1i, x2i;
printf("Input coefficients of square equation a,b,c:");
scanf("%f %f %f", &a, &b, &c);
d=b*b -4.0*a*c;
if (d > 0) {
/* results are real numbers */
q = pow (d, 1./2);
x1 = (-b + q)/(2*a);
x2 = (-b - q)/(2*a);
printf ("X1=%f X2=%f\n", x1, x2);
} else if (d == 0) {
/* there’s only one result */
x1 = -b/(2*a);
printf ("X1=X2=%f\n", x1);
} else {
/* results are conjugated complex numbers */
q = pow(-d, 1./2);
x1r = -b/(2*a) ;
x2r = x1r;
x1i = q/(2*a);
x2i = -x1i;
printf ("X1 = (%f,%f)\n", x1r, x1i);
printf ("X2 = (%f,%f)\n", x2r, x2i);
}
}