0% found this document useful (0 votes)
34 views6 pages

Handbook-C 20

Constants in C are declared with the const keyword and assigned a value. Common conventions are to declare constants in uppercase and variables in lowercase for improved readability. Constants follow the same naming rules as variables but cannot start with a digit. Constants can also be defined using the #define preprocessor directive without specifying a type or using an equals sign. C offers a variety of operators for arithmetic, comparison, logical, compound assignment, and miscellaneous operations on data.

Uploaded by

yospanith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views6 pages

Handbook-C 20

Constants in C are declared with the const keyword and assigned a value. Common conventions are to declare constants in uppercase and variables in lowercase for improved readability. Constants follow the same naming rules as variables but cannot start with a digit. Constants can also be defined using the #define preprocessor directive without specifying a type or using an equals sign. C offers a variety of operators for arithmetic, comparison, logical, compound assignment, and miscellaneous operations on data.

Uploaded by

yospanith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Constants

Let's now talk about constants.

A constant is declared similarly to variables, except it


is prepended with the   const  keyword, and you
always need to specify a value.

Like this:

const int age = 37;

This is perfectly valid C, although it is common to


declare constants uppercase, like this:

const int AGE = 37;

It's just a convention, but one that can greatly help you
while reading or writing a C program as it improves
readability. Uppercase name means constant,
lowercase name means variable.

A constant name follows the same rules for variable


names: can contain any uppercase or lowercase letter,
can contain digits and the underscore character, but it
can't start with a digit.  AGE  and  Age10  are valid
variable names,  1AGE  is not.

Another way to define constants is by using this


syntax:

#define AGE 37

19
In this case, you don't need to add a type, and you
don't also need the  =  equal sign, and you omit the
semicolon at the end.

The C compiler will infer the type from the value


specified, at compile time.

20
Operators
C offers us a wide variety of operators that we can use
to operate on data.

In particular, we can identify various groups of


operators:

arithmetic operators
comparison operators
logical operators
compound assignment operators
bitwise operators
pointer operators
structure operators
miscellaneous operators

In this section I'm going to detail all of them, using 2


imaginary variables  a  and  b  as examples.

I am keeping bitwise operators, structure


operators and pointer operators out of this list, to
keep things simpler

Arithmetic operators
In this macro group I am going to separate binary
operators and unary operators.

Binary operators work using two operands:

21
Operator Name Example
 =  Assignment  a = b 

 +  Addition  a + b 

 -  Subtraction  a - b 

 *  Multiplication  a * b 

 /  Division  a / b 

 %  Modulo  a % b 

Unary operators only take one operand:

Operator Name Example


 +  Unary plus  +a 

 -  Unary minus  -a 

 ++  Increment  a++  or  ++a 

 --  Decrement  a--  or  --a 

The difference between  a++  and  ++a  is that  a++ 

increments the   a  variable after using it.   ++a 

increments the  a  variable before using it.

For example:

int a = 2;
int b;
b = a++ /* b is 2, a is 3 */
b = ++a /* b is 4, a is 4 */

The same applies to the decrement operator.

Comparison operators

22
Operator Name Example
 ==  Equal operator  a == b 

 !=  Not equal operator  a != b 

 >  Bigger than  a > b 

 <  Less than  a < b 

 >=  Bigger than or equal to  a >= b 

 <=  Less than or equal to  a <= b 

Logical operators
 !  NOT (example:  !a )

 &&  AND (example:  a && b )

 ||  OR (example:  a || b )

Those operators are great when working with boolean


values.

Compound assignment
operators
Those operators are useful to perform an assignment
and at the same time perform an arithmetic operation:

23
Operator Name Example
 +=  Addition assignment  a += b 

 -=  Subtraction assignment  a -= b 

Multiplication
 *=   a *= b 
assignment
 /=  Division assignment  a /= b 

 %=  Modulo assignment  a %= b 

Miscellaneous operators
The ternary operator
The ternary operator is the only operator in C that
works with 3 operands, and it’s a short way to express
conditionals.

This is how it looks:

<condition> ? <expression> : <expression>

Example:

a ? b : c

If  a  is evaluated to  true , then the  b  statement is


executed, otherwise  c  is.

The ternary operator is functionality-wise same as an


if/else conditional, except it is shorter to express and it
can be inlined into an expression.

24

You might also like