(s.p) Unit-2 Constant, Variables & Data Types

    

Constant, Variables & Data Types

Character Set

  •   The character that can be used to form words, numbers, and expressions depends upon the computer, on which the program is run.
  •   The characters in C are grouped into the following categories: -

1.      Letters: - a … z , A … Z

2.      Digits: - 0 to 9

3.      Special Characters: - , . / : ; ?

4.      White Spaces: - blank spaces, horizontal tab, new line

Special Characters

,    .    :    ;    ?

‘    “    !    |

/    \    ~    _

$    %    &    ^

*    -    +    #

<    >    (    )

{    }    [     ] 

Trigraph Characters

  • Many non-English keyboards do not support all the characters.
  • C introduces the concept of trigraph sequences to provide a way to enter certain characters that are not available on some keyboards.
  • Each trigraph sequence consists of three characters, which means two question marks (??) followed by another character.

Trigraph Sequence

Translation

??=

# Number sign

??(

[ left bracket

??)

] right bracket

??<

{ left brace

??>

} right brace

??!

| vertical bar

??/

\ back slash

??-

~ tilde


C Tokens



  • In C program, the smallest individual units are known as C tokens.

Keywords and Identifiers

  • All keywords have fixed meanings and these meanings cannot be changed.
  • Keywords served as basic building blocks for program statements.
  • All keywords must be written in lowercase.

auto

else

goto

double

enum

if

char

break

int

const

case

long

continue

extern

register

default

float

return

do

for

short

signed

sizeof

static

struct

switch

typedef

union

unsigned

void

volatile

while

 

  • Identifiers refers to the names of the variables, functions and arrays.
  • These are user defined names and consists of a sequences of letters and digits, with a letter as a first character.
  • Both uppercase and lowercase letters are permitted, although lowercase letters are commonly used.
  • The underscore character is also permitted in identifiers.

Rules for identifiers

  • First character must be an alphabet or underscore.
  • Must consist of only letters, digits or underscore.
  • Only first thirty-one (31) characters are significant.
  • Cannot use a keyword.
  • Must not contain white space.

Constants




Integer Constants

  • An integer constant refers to a sequence of digits.
  • There are three types of integers namely decimal integer, octal integer and hexadecimal integer.
  • Decimal integer consists of a s et of digits 0 to 9, preceded by an optional – or + sign.
  • Embedded spaces, commas and non-digit characters are not permitted between the digits

Valid

Not-Valid

123

15      750

-321

20,000

0

$100

654321

 

78

 


  • An octal integer constant consists of any combination of digits from the set 0 to 7 with a leading 0.

Eg.,  037

        021

        024

        0435

        0551

  • A sequence of digit preceded by 0x or 0X is considered as hexadecimal integer.
  • They may also include alphabets a - f; the letter a - f represents the number 10 – 15.

for eg.:- 0x2

              0x9F 

  • The largest integer value that can be stored is machine dependent; it is also possible to store larger integer constant on this machines by appending qualifiers such as U (the capital U), L (the capital L), UL to the Constance.

Real Constants

  • The quantities like distance, heights temperatures, price are represented by numbers containing fractional parts such numbers are called real or floating point constants.

For eg.:- 0.0083

                -0.75

               +2.740

               +247.0

  • These numbers are shown in decimal notations having a whole number, followed by a decimal point and the fractional part.
  • It is possible to omit digits before the decimal point or digits after the decimal point, i.e.,

For eg.:- 215.

                .95

               -.71

               +.5

  • A real number may also be expressed in exponential or scientific notations
  • For eg. The value 215.65 may written as 2.1565e2 in exponential notation.
  • The general form is “ mantissa e exponent ”.
  • The mantissa is either a real number expressed in decimal notation or an integer.
  • The exponent is an integer number with an optional + or – sign.
  • The letter e separating the mantissa and the exponent can be written either lowercase or uppercase.

Is it valid or not

Valid or not

Remarks

698354L

Yes

 

25,000

No

, (comma) special character used

+5.0E3

Yes

 

3.5e-5

Yes

 

7.1e  4

Not

White space included

-4.5e-2

Yes

 

1.5E+2.5

No

Exponent is not an integer

$255

No

‘$’ special character used

0x7B

Yes

 


Character Constants

Single Character Constant

  • A single character constant contains a single character enclosed within a pair of single quote marks (‘  ’).

For eg.:- ‘5’

                ‘X’

                ‘;’

               ‘  ’

  • The character constants have integer values knows as ASCII values ( American Standard Code for Information Exchange )

For eg.:- printf(“%d”, c);

  • The above statement would print number 99, i.e.; the ASCII value for letter small C

For eg.:- printf(“%c”, ‘100’);

  • The above statement would print the letter d.

String Character constant

  • A string constant is a sequence of character enclosed in double quotes (“ ”).
  • The characters may be letters, numbers, special characters and blank spaces.

For eg.:- “Hello!”

                “1987”

                “Well DONE”

                “?...!”

                “5+3”

                “X”

Variables

  • A variable is a data name that may be used to store a data value.
  • A variable name can be chosen by the programmer in a meaningful way; So as to reflect its function or nature in the programmer.

Is it valid or not

Valid or not

Remarks

First_try

Yes

 

Char

No

Keyword used

Price$

No

Special character used

Group one

No

Blank space used

average_number

Yes

 

int_type

Yes

 

  •  Variable names may consists of letters, digits and the underscore character subject to the following conditions.

Conditions

  • They must begin with a letter; some systems permits underscore as the first character.
  • Length should not be normally more than 8 characters, since, only the first 8 characters are treated as significant by many compilers.
  • Uppercase and lowercase letters are significant, i.e, the variable Total is not the same as total or TOTAL.
  • It should not be a keyword.
  • White space is not allowed.

Valid

Invalid

Delhi

123

Mark

%

value

(area)

X1

25th

Sum1

 

T_raise

 

Ph_value

 

Distance

 










Post a Comment

Previous Post Next Post