29
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION

VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION. Identifiers Defined Identifiers are composed of a sequence of letters. Digits, and the special character

Embed Size (px)

Citation preview

VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION

Identifiers Defined

Identifiers are composed of a sequence of letters. Digits, and the special character _ (underscore).

Avoid using names that are too short or too long.

Limit the identifiers from 8 to 15 characters only.

Variables Defined

Variables are identifiers that can store a changeable value. These can be different data types.

Rules for defining or naming identifiers

It must consist only of letters, digits, and underscore.

Example: _duh, num_1 (correct)

Rules for defining or naming identifiers

It should not begin with a digit. Example: 1name, 3to3 (incorrect)

Rules for defining or naming identifiers

An identifier defined in the C standard library should not be redefined.

Example: printf, scanf (incorrect)

Rules for defining or naming identifiers

It is case sensitive; meaning uppercase is not equal to the lowercase.

Example: ans != Ans != aNs

Rules for defining or naming identifiers

Do not include embedded blanks. Example: large num (incorrect)

Rules for defining or naming identifiers

Do not use any of the C language keywords as your variable/ identifier.

Do not call your variable / identifier by the same name as other functions.

Variable Declaration

All variables must be declared before they may be used. The general form of declaration is shown here:

Type variable list;

Example: int i,j, k;

short i,j,k;

Variable Declaration

Note: Before declaring variables, specify first the data type of the variable/s.

Variables must be separated by comma.

All declarations must be terminated by a semicolon (;).

Two kinds of variables

Local Variables Global Variables

Local Variables

Variables that are declared inside a function are called local variables. It can only be referenced by statements that are inside the block in which the variables are declared.

Local Variables

Example:

#include<stdio.h>

main()

{

int a,b,c;

______;

}

Global Variables

Global variables are known throughout the entire program and may be used by any piece of code.

Global variables are created by declaring them outside of any function.

Global Variables

Example:

#include<stdio.h>

int a,b,c;

main()

{

________;

________;

}

Constants Defined

Constants are identifier / variables that can store a value that cannot be changed during program execution.

Example: const int count = 100; Where integer count has a fixed value

of 100.

Arithmetic, Logical, Relational, and Bitwise Operations

Operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.

There are three classes of operators in C: arithmetic, logical and relational, and bitwise.

Arithmetic Operators

Operator Action+ Addition- Subtraction* Multiplication/ Division% Modulus Divisor-- Decrement a value ++ Increment a value

Relational and Logical Operators

In the term relational operator, the word relational refers to the relationship values can have with one another.

In the term logical operator, the word logical refers to the ways these relationships can be connected together using the rules of formal logic.

Relational Operators

Operators Action> Greater than

>= Greater than or equal to

< Less than

<= Less than or equal to

== equal

!= Not equal

Logical Operators Operators Action Truth Table

&& AND true && true = truetrue && false = falsefalse && true = falsefalse && false = false

|| OR true && true = truetrue && false = truefalse && true = truefalse && false = false

! NOT !true = false!false = true

Bitwise Operator

Bitwise operators are the testing, setting or shifting of the actual bits in a byte or a word, which corresponds to C’s standard char and int data types and variants.

Bitwise operators cannot by used on type float, double, long double, void or other more complex types.

The ? Operator

? Operator is a very powerful and convenient operator that can be used to replace certain statements of the if-then-else form.

Example: y= x > 9 ? 100: 200

is equivalent toIf (x>9) y=100;ElseY=200;

Evaluation of Expression

Expression refers to anything that evaluates to a numeric value.

Order of Precedence

Order of Precedence ()!, unary +, -*. /. %binary + , -<, <=, >, >===, !=&&||

Evaluate the following

Given: z= 5; a = 3; b=9; w= 2; y=5 Expression

Z – (a * b / 2) + w + y

Converting Mathematical Formula to C expression

Example: b2 – 4ac = b * b – 4 * a * c

Converting English Conditions to C expression

Example: X and y greater than z can be converted to

Z = x > && y > z