Showing posts with label constants. Show all posts
Showing posts with label constants. Show all posts

Wednesday, May 14, 2008

Constant in C(introduction)

All Constant in C(introduction)
Introduction

There are some values which do not change during the execution of the program. These values are called constants.
Constants are of fixed value that remain unchanged during the execution of a program, and are used in assignment of statements. Constants are stored in variables.

Syntax of constant declaration:

Const datatype var_name = value;

Example of Constant declaration:

Const int a = 5;

In C language there are five types of constants which has been described separately

Character constants

Character constants

A character constant consists of a single digit or a single special symbol enclosed within a pair of single inverted commas. The maximum length of a character constant can be 1 character.

e.g. --> 'a', 'i' , '5', '='.

There are some character sequence constants which are used in C to represented special action, these are called C Escape Sequence.

List of these escape sequence and its tasks are given below:

\a : audible bell

\f : form feed

\r : carriage return

\v : vertical tab

\' : single quote

\? : question mark

\HHH: 1 to 3 digit hex value.

\b : backspace

\n : newline

\t : horizontal tab

\\ : backslash

\" : double quote.

\000 : 1 to 3 digit octal value

Integer constants

Integer constants

An integer constant refers to a sequence of digits. It could be either positive or negative. and must have at least one digit.

It mustn't have a decimal point. No commas or blank are allowed within an integer constant. The allowable range for integer constants is -32767 to 32767.

There are three types of integer constants:

1. decimal :

In decimal notation ,simply we write decimal number. e.g. 24,678

2. octal :

In octal notation, write(0)immediately before the octal represention,e.g.-076,-076

3. hexadecimal :

In hexadecimal notation ,the constant is preceded by 0x,e.g.,0x3e,-0x3e.

Some example of integer constants:

: 426
: +762
: -8000
: -7605

Real constants

Real constants

Real constants are often called Floating Point constants.

It has three parts:

1. A sign (+ or -) preceding the number portion (optional).

2. A number portion (representing the base).

3. An exponent portion following the number portion (optional). This starts with E or E followed by an integer. The integer may be preceded by a sign.

A real constant must have at least one digit. It must have a decimal point. It could be either positive (default) or negative. No commas and blank are allowed within a real constant.

Some example of real constants:

: +.72
: +72
: +7.6E+2
: 24.4e-5

Logical & String constants

Logical & String constants

A logical constant can have either of two values either true or false. In C a non-zero value is always treated as true whereas zero is treated as false.

The logical constants are very useful in evaluating logical expressions and complex condition.

A group of character enclosed within a pair of double inverted commas (" ") is treated as a string constant.

some example of string constant:

: "Hello"
"Welcome to eBiz"
"a"

Saturday, May 10, 2008

Expression

Expression

Evaluation of expression

An expression is a combination of variables, constants
and operators arranged according to the syntax of the language.

C can handle any complex expression with ease.

It is little bit different from algebraic expression.

Algebraic Expressions C Expressions
axb-cxd a*b-c*d
(m+n)(a+b) (m+n)*(a+b)


Evaluation of expression:

We can evaluate an expression by using assignment statement. As

Variable = Expression.

e.g. :

Temp = ((f * cos(x)/sin (y))+(g * sin(x)/cos(y)))

All relevant variables must be assigned values
before the evaluation of the expression.

Type conversion in expression:

To effectively develop C programs, it would be necessary
for you to understand the rules that are used for the
implicit conversion of operands.

If an expression contains an operation between an int
and a float, the int would be automatically promoted to
a float before carrying out of operation.

Copyright

Copyright © 2008 C Tutorial, All rights are reserved