Sunday, 28 October 2018

C Identifiers, Keywords, Data Types, and Escape Sequence

Identifiers are simply references (variables) to memory allocations that can hold values. They are formed by combining letters(both uppercase and lowercase), digits, and the underscore(_).
          Rules of an identifier:

i)     It should always start with a letter
ii)    Blank or whitespace and special characters is not allowed
iii)   Use descriptive names that reflect the data items that they are going to store, but variables such as x, y, and z are accepted.
iv)  Do not use reserved words as your identifier
v)    Do not call your identifiers by the same name as other functions.

C identifiers are case-sensitive, meaning the lower- and uppercase letters in identifiers are treated as different characters. For example, the identifiers FIRST_NAME and first_name are all different; they refer to different memory locations.


Variables are identifiers whose value may change over the course of execution of the program. Before a variable can be used in C, it must be declared explicitly with its corresponding data type.

Keywords are also known as reserved words. Have standard, predefined meanings and must be used only for their intended purpose. Therefore, these keywords should not be used as identifiers in the program.


Auto

double

int

struct

break

Else

long

switch

case

enum

Register

typedef

char

extern

return

Union

const

float

short

unsigned

Continue

for

signed

void

default

Goto

sizeof

volatile

do

if

Static

while





Basic Data Types of C

Type

Format Specifier

Meaning

(Example(s))

int

%d or %i

a whole number

(3, 7, +8, +9, 1000000)

float

%f

a number with decimal point

(5.8, 9.0, -5.6789, +0.0032)

char

%c

a single letter, symbol, or number enclosed within two single quotes

('B', 'r', '*', '3')

char

%s

a string in C is considered as a series if characters

("Ms.", "Hi!", "143", "I  Love")

double

%f

for bigger or larger numbers with a decimal point

(123456789.123)


Modifier is used to alter the meaning of the base type to fit the needs of various situations more precisely. The list of modifiers includes the following:

Type

Bidwidth

Range

Char

8

-128 to 127

Unsigned char

8

0 to 255

Signed char

8

-128 to 127

Int

16

-32768 to 32767

Unsigned int

16

0 to 65535

Signed int

16

-32768 to 32767

Short int

16

-32768 to 32767

unsigned short int

16

0 to 65535

Signed short int

16

-32768 to 32767

Long int

32

-2147483648 to 2147483647

Unsigned long int

32

0 to 4294967295

Signed long int

32

-2147483648 to 2147483647

Float

32

3.4E-38 to 3.4E+38

Double

64

1.7E-308 to 1.7E+308

Long double

64

1.7E-308 to 1.7E+308



Escape Sequence always begin with a backslash \ and is followed by one or more special characters.


Control Characters

Meaning

\a

Alert (bell) character

\b

Backspace

\f

Form feed

\n

New line/next line

\r

Carriage return/Enter key

\t

Horizontal tab

\v

Vertical tab

\\

Backslash

\ ‘

single quote is used for single character / letter

\“

double quote is used for two or more character

\0

NULL ASCII 0



{

open curly brace signifies begin

}

close curly brace signifies end

&

Address of an operator

*

Indirection operator / pointer




Variable Declaration

Whatever data type all variables and constants must be declared prior to their use. General declaration is

type variable_name;

Examples:

int age; //declares age as an integer variable

char ch; //declares ch as a character variable

float amount; //declares amount as a floating-point variable


Character Constants : ‘$’ ‘*’ ‘z’ ‘G’

String Constants: “Name: “ “Address:” “Zip Code”


Variables with the same data type can be placed in the same statement, separated by commas.

        

int x, y, z;

float amount, total;


Initializing Variables

//Variable Declaration

int x, y;

float amount, total;


//Variable Initialization

x = 10;

y=0;

total = 0;


Kinds of Variable

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


Global Variables are variable that 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.

0 comments:

Post a Comment

If possible, leave a positive comment. No hate speech or elicit comments. Thank you.