Showing posts with label built-in facilities. Show all posts
Showing posts with label built-in facilities. Show all posts

Wednesday, May 14, 2008

Conditional & ternary operators

Conditional & ternary operators

The conditional operator? and: are sometimes called ternary operators.

A ternary operator is one which contains three operands.

The general form of ternary operator is:

exp 1 ? exp 2 : exp 3

The operator works as, if exp 1 is evaluated first. If the result is true then exp 2 is executed, otherwise exp 3 is executed.

Saturday, May 10, 2008

String

String



Strings in C are represented by arrays of characters.
The end of the string is marked with a special character,
the null character, which is simply the character with the value 0.

Because C has no built-in facilities for manipulating entire
arrays (copying them, comparing them, etc.), it also has
very few built-in facilities for manipulating strings.

In fact, C's only truly built-in string-handling is that
it allows us to use string constants (also called string
literals) in our code.

Whenever we write a string, enclosed in double quotes,
C automatically creates an array of characters for us,
containing that string, terminated by the \0 character.

For example, we can declare and define an array of
characters, and initialize it with a string constant:

char string[] = "Hello, world!";

Two ways to initilize string >




In this case, we can leave out the dimension of the array,
since the compiler can compute it for us based on the size
of the initializer.

This is the only case where the compiler sizes a string
array for us, however; in other cases, it will be necessary
that we decide how big the arrays we use to hold strings.

An example program showing the character data type array:



Out put of the program


In the above example, a character based array named word
is declared, and each element of array is assigned a character.

The last element is filled with a zero value, to signify
the end of the character string (in C, there is no string
type, so character based arrays are used to hold strings).

A printf statement is then used to print out all elements
of the array.

Pointer(Pointer And Array)

Pointer & Array

Pointers and arrays are very closely linked in C.

Hint: Think of array elements arranged in consecutive memory locations.

Consider the following:

int a[10], x;
int *pa;
pa = &a[0]; /* pa pointer to address of a[0] */
x = *pa;

/* x = contents of pa (a[0] in this case) */




Warning: There is no bound checking of arrays and pointers
so you can easily go beyond array memory and overwrite other things.

C however is much more subtle in its link between arrays and pointers.

For example we can just type:

pa = a;
instead of
pa = &a[0]
and
a[i] can be written as *(a + i).
i.e. &a[i] =a + i.

We also express pointer addressing like this:

pa[i] =*(pa + i).

However pointers and arrays are different:

A pointer is a variable. We can do pa = a and pa++.

An Array is not a variable. a = pa and a++ ARE ILLEGAL

This stuff is very important. Make sure you understand
it. We will see a lot more of this. We can now understand
how arrays are passed to functions.

When an array is passed to a function what is actually
passed is its initial element location in memory

So: strlen(s) strlen(&s[0])

This is why we declare the function:

int strlen(char s[]);

An equivalent declaration is:

int strlen(char *s);

since char s[] is equivalent to char *s.

strlen () is a standard library function that returns the length of a string.

Let's look at how we may write a function:

int strlength(char *s)
{
char *p = s;
while (*p != '\0');
p++;
return p-s;
}

Now let’s write a function to copy a string to another string.
strcpy () is a standard library function that does this:

void strcopy (char *s, char *t)
{ while ( (*s++ = *t++) != `\0' );}

This uses pointers and assignment by value.

Note: Uses of Null statements with while.

Malloc Library Function

Function: Allocates main memory
Syntax: void*malloc(size_t size);
Prototype in: stdlib.h, alloc.h

Remarks: malloc allocates a block of size bytes from the
C heap memory. It allows a program to allocate memory
explicitly, as it is needed and in the exact amounts needed.

Calloc Library Function

Function: Allocates main memory
Syntax: void*calloc(size_t n size);
Prototype in: stdlib.h, alloc.h

Remarks: Calloc provides access to the C heap memory .
Calloc allocates a block of size n items of x size.
The block is cleared to 0.

Copyright

Copyright © 2008 C Tutorial, All rights are reserved