Introduction to c



Download 0.91 Mb.
Page13/13
Date28.05.2018
Size0.91 Mb.
#51782
1   ...   5   6   7   8   9   10   11   12   13

TOKENS


In a C program the smallest individual units are known as C tokens. C has six different types of tokens and they are KEYWORDS (like float, while etc.), IDENTIFIERS (like variable names, function names etc.), CONSTANTS (integer constants, char constants etc.), STRINGS (like “ABC”, “Hello” etc.), SPECIAL SYMBOLS (like {, }, [, ] etc.) and OPERATORS (like +, -, *, &&, % etc.). C programs are written using these tokens and the syntax of the language.

CONSTANTS


Constants in C refer to fixed values that do not change during the execution of a program.

Integer Constants


An integer constant refers to a sequence of digits. There are three types of integers.

Decimal


Decimal integers consists of a set of digits, 0 through 9, preceded by optional – or + sign.

E.g. 345


-23

0

23456 etc.


Octal


Octal integer (base 8) constant consists of any combination of digits, 0 through 7 with leading 0.

E.g. 023


0

0987


01273 etc.

Hexadecimal


A sequence of digits preceded by 0x or 0X is considered as hexadecimal (base 16) integer. They may also include alphabets A through F. the letters A through F represent the numbers 10 through 15.

E.g. 0X78

0x61F

0XF2f etc.


It is also possible to store larger integer constants by applying qualifiers such as U, L and UL to the constants.
Unsigned integer - 564U or 564u

Unsigned long integer - 78345346UL or 78345346ul

Long integer - 56757L or 56757l

Real Constants


The real (floating-point) constants are numbers containing fractional parts.
E.g. 9.345

-0.98


-2.3456

.34


789.

-. 66 etc


Real constants can also be expressed in exponential (scientific) notation. The general format 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 plus or minus sign.


E.g. -1.08e5 is equivalent to -1.08 * 105 i.e., -1.08 * 10,000 i.e., -1,08,000

2.3e-3 is equivalent to 2.3 * 10-3 i.e., 2.3 / 1000 i.e., 0.0023


By default floating point constants are represented as double precision (double). Use the suffixes f or F to force single-precision and l or L to extend double precision.Character Constants

Single Character Constants


A single character constant (character constant) contains a single character enclosed within a pair of single quote marks.
E.g. ‘a’, ‘Z’, ‘8’, ‘;’

String Constants


A string constant is a sequence of characters enclosed in double quotes.
E.g. “Hello Everybody”

“Lotus 1-2-3”

“23+34”

“H”


APPENDIX B

REMOVE()


remove(filename)
remove deletes a file.

RENAME ()


int rename(const char *oldname,

const char *newname);


On successfully renaming the file, rename returns 0. On error, it returns –1.

ABS()


int abs(int x);
Gets the absolute value of an integer

ATOF()


converts a string to a floating point
double atof(const char *s);
Returns the converted value of s or 0 if s cannot be converted.

CEIL ()


rounds up
double ceil(double x);
Returns the smallest integer greater than or equal to x

FLOOR ()


rounds down
double floor(double x);
Returns the largest integer less than or equal to x.

COS ()


cosine
double cos(double x);
x is in radians. Returns a value in the range -1 to 1.

EXP ()


calculates e to the x'th power

double exp(double x);


FABS()


absolute value of a floating-point number
double fabs(double x);

LABS()


long absolute value
long labs(long x);

FMOD()


calculates x modulo y, the remainder of x/y
double fmod(double x, double y);

LOG()


logarithm function ln(x)
double log(double x);

LOG10()


logarithm function log 10(x)
double log10(double x);

POW()


power function, x to the y
double pow(double x, double y);

SIN()


sine function
double sin(double x);
x is in radians. Returns a value in the range -1 to 1.

SQRT()


calculates square root
double sqrt(double x);
Returns the square root of x.

TAN()


tangent function
double tan(double x);
x is in radians. Returns the tangent of x.

ATOI()


Converts a string to an int
int atoi(const char *s);
Returns the converted value of the input

string. If the string cannot be converted,

the return value is 0.

ATOL()


converts a string to a long
long atol(const char *s);
Returns the converted value of the input string. If the string cannot be converted the return value is 0.

ITOA()


converts an integer to a string
char *itoa(int value, char *string, int radix);
Returns a pointer to the target string.

LTOA()


converts a long to a string
char *ltoa(long value, char *string, int radix);
For a decimal representation, use radix=10.

For hexadecimal, use radix=16.

Returns a pointer to the argument string.

RAND()


random number generator
int rand(void);
Returns a random number from 0 to 32767.

RANDOMIZE()


void randomize(void);
Initializes the random number generator with

a random value. It uses the time function,

so you should include time.h when using

this routine.


RANDOM()


int random(int num);
Returns an integer between 0 and (num-1).

SYSTEM()


issues an MS-DOS command
int system(const char *command);
command can execute an internal DOS command such as DIR, a .COM or .EXE program file or a .BAT batch file.

Returns zero on success, -1 on failure.


ULTOA()


converts an unsigned long to a string
char *ultoa(unsigned long value, char *string,

int radix);


Returns a pointer to string. There is no error return.

STRCAT()


appends src to dest
char *strcat(char *dest, const char *src);

STRCHR()


finds c in str
char *strchr(const char *str, int c);
Returns a pointer to the first occurrence of the character c in str; if c does not occur in str, strchr returns NULL.

STRLEN()


gets length of s
size_t strlen(const char *s);
Returns the number of characters in s, not counting the terminating null character.

STRLWR()


converts s to all lowercase
char *strlwr(char *s);
Returns pointer to s.

STRUPR()


converts all characters in s to uppercase.
char *strupr(char *s);

Returns a pointer to s.


TOLOWER()


translates characters to lowercase
int tolower(int ch);
Returns the converted value of ch if it's an

uppercase character; all others are returned

unchanged.

TOUPPER()


translates characters to uppercase
int toupper(int ch);

Returns the converted value of ch if ch was an

lowercase character; all others are returned

unchanged.


ISALNUM()


isalnum(c) - True if c is a letter or digit.

isalpha(c) - True if c is a letter.

isdigit(c) - True if c is a digit.

iscntrl(c) - True if c is a delete character

or ordinary control character.

isascii(c) - True if c is a valid ASCII character.

isprint(c) - True if c is a printable character.

isgraph(c) - Like isprint except that the

space character is excluded.

islower(c) - True if c is a lowercase letter.

isupper(c) - True if c is an uppercase letter.

ispunct(c) - True if c is a punctuation character.

isspace(c) - True if c is a space, tab, carriage

return, newline, vertical tab, or form-feed.



isxdigit(c) - True if c is a hexadecimal.
Appendix C

Precedence of Operators


Operator

Description

Example

Order of evaluation

(Associativity)

( )

Function arguments

Func(arg1, arg2)

left to right

[ ]

Array elements

Array[20]

.

Struct, Union member

Student.name

->

Struct/union pointer member

Ptr->name

!

Logical NOT

!feof(fp)

right to left

~

One’s complement

~x



Unary minus

–x

++

Increment

x++, ++x

--

Decrement

x-- , --x

&

Address of

Ptr = &x

*

indirection

*Ptr

(type)

Type cast

(float) x

Sizeof

Size in bytes

sizeof(int)

*

Multiply

x * 5

left to right

/

Divide

a / b

%

Modulus (remainder)

a % b

+

Add

a + b

left to right



Subtract

a - b

<<

Left shift

x<<2

left to right

>>

Right shift

x>>2

<

Less than

if(x

left to right

<=

Less than or equal to

while( x<=y)

>

Greater than

if(a>b)

>=

Greater than equal to

while(a>=y)

==

Is equal to (equality)

if( a == x)

left to right

!=

Is not equal to (inequality)

if( b !=10)

&

Bitwise AND

x & y

left to right

^

Bitwise exclusive OR

x ^ y

left to right

|

Bitwise OR

x | y

left to right

&&

Logical AND

if(a>b && b>c)

left to right

||

Logical OR

if(ch ==’y’ || ch ==’Y’)

left to right

?:

Conditional

a>b ? a : b

right to left

=

assignment

x=10

right to left

*=

* assignment

x *= 10

/=

/ assignment

x /= 10

%=

% assignment

x %= 10

+=

+ assignment

x += 10

–=

– assignment

x –= 10

<<=

<< assignment

x <<= 2

>>=

>> assignment

x >>= 2

&=

& assignment

x &= b

^=

^ assignment

x ^= z

|=

| assignment

x |= y

,

Comma

for(a=0, b=10; ;a++, b--)

left to right







Download 0.91 Mb.

Share with your friends:
1   ...   5   6   7   8   9   10   11   12   13




The database is protected by copyright ©ininet.org 2024
send message

    Main page