Discovering C !

As we all know that C is mother of all Computer Languages. You can Create another C language using C language itself. The sea of C is too massive. I just want to swim out this sea of C. Its not that easy but I will keep trying.

My Photo
Name:
Location: Nagpur, Maharashtra, India

Just another Perl Programmer, Linux Lover and a Curious User...and almost always self motivated, passionately helpful, travel enthusiast, workaholic, mostly busy, bad photographer, good teacher, somewhat romantic, creative writer, not so compulsive friend, quick learner, silent poet, determined survivor and an insatiable dreamer.

Thursday, July 13, 2006

C @@ my Notes

A string constant is a character array terminated by a null ('\0').

For EG:

char name[]={'R', 'T', 'L', 'i', 'n', 'u', 'X', '\0'};

Each character in the array occupies one byte of memory and last character is always '\0'. It looks like two characters but actually is just one. It is different from 0. The ascii value of 0 is 48 where as ascii value of \0 is 0.

Three ways write a C program for strings:

METHOD 1

#include

main()

{

char os[]="Suse";

int i=0;

while (i<=5)

{

printf("%c",os[i]);

i++;

}

printf("\n");

return 0;

}

METHOD 2

#include

main()

{

char os[]="Redhat";

int i=0;

while(os[i]!='\0')

{

printf("%c",os[i]);

i++;

}

printf("\n");

return 0;

}

METHOD 3

#include

main()

{

char os[]="Debian";

char *i=os;

//i=os;

while(*i!='\0')

{

printf("%c",*i);

i++;

}

printf("\n");

return 0;

}

METHOD 4

#include

main()

{

char os[]="Ubuntu";

printf("%s",os);

printf("\n");

return 0;

}


METHOD 5

#include

main()

{

char os[15];

printf("\nPlease enter the name of the Operating System: ");

scanf("%s",os);

printf("%s",os);

printf("\n");

return 0;

}


Note the declaration char os[15] sets aside 15 bytes under the the array os[], where as the scanf function fills in the characters typed at keyboard into this array until the enter key is hit. Once the enter key is hit, the scanf() places a '\0' in the array.


The length of string should not exceed the character array dimension because C compiler doesn't perform bounds checking on character array.


scanf is not capable of receiving multi-word strings. Hence to use multi word strings use gets(). Counter part of gets() is puts(). puts() can display only one string at a time.

EG:

#include

main()

{

char os[15];

printf("Please enter the name of os: ");

gets(os);

puts("Use ");

puts(os);

puts("\n");

return 0;

}


scanf can be made to accept multi word strings as shown in following example:

EG:

#include

main()

{

char os[15];

printf("Please enter the oss which u like: ");

scanf("%[^\n]s",os);

printf("You love %s", os);

printf("\n",os);

return 0;

}


Suppose we wish to store “Hello”. We may either store it in string or we may ask the C compiler to store it at some location in memory and assign the address of the string in a char pointer. This is shown bellow.

#include

main()

{

char os1[]="Xandros";

char os2[10];

os2=os1;

printf("%s",os2);

printf("\n");

return 0;

}


#include

main()

{

char lang1[]="vb.net";

char *lang2;

lang2=lang1;

printf("%s",lang2);

printf("\n");

return 0;

}


STANDARD LIBRARY FUNCTIONS:

Function

Use

strlen

Finds length of a string.

strupr

Converts a string to upper case.

strlwr

Converts a string to lower case.

strcat

Appends one string at the end of other string.

strncat

Appends n characters at the end of other string.

strcpy

Copies one string at the end of other string.

strncpy

Copies first n characters at the end of other string.

strcmp

Compares two strings without ignoring the case.

strncmp

Compares first n characters of two strings.

stricmp

Compares two strings ignoring the the case.

strnicmp

Compares first n characters of two strings ignoring the case.

strdup

Duplicates the string.

strrev

Reverses the string.

strstr

Finds first occurance of given string in other string.

strchr

Finds first occurance of given characters in other string.

strrchr

Finds last occurance of given characters in other string.

strset

Sets all characters of a given string in another string.

strnset

Sets first n characters of a string to a given character.


#include

main()

{

char os1[]="goose";

char os2[]="Solaris";

int l1;

int l2;

l1=strlen(os1);

l2=strlen(os2);

printf("%s has a length of %d", os1,l1);

printf("\n");

printf("%s has a length of %d", os2,l2);

printf("\n");

return 0;

}


#include

main()

{

char os1[]="server2003";

int ln1,ln2;

ln1=cstrlen(os1);

ln2=cstrlen("serverNT");

printf("%s has a length of %d",os1,ln1);

printf("\n");

printf("%s has a length of %d","serverNT",ln2);

printf("\n");

return 0;

}

cstrlen(char *s)

{

int l3=0;

while(*s!='\0')

{

l3++;

s++;

}

return(l3);

}


#include

main()

{

char mp[]="xmms";

strupr(mp);

printf("%s",mp);

printf("\n");

return 0;

}


#include

main()

{

char edi1[]=" Open Office ";

char edi2[50]="Word is not better than";

strcat(edi2,edi1);

printf("%s", edi2);

printf("\n");

return 0;

}


#include

main()

{

char mp[]="AMORK";

strlwr(mp);

printf("%s",mp);

printf("\n");

return 0;

}


#include

main()

{

char edi1[]=" Open Office ";

char edi2[50]="Word is not better than";

strcat(edi2,edi1);

printf("%s", edi2);

printf("\n");

return 0;

}


#include

main()

{

char unixfather[50];

char linuxfather[]="Linux Torvalds owes many things to Denis Ritchie";

strcpy(unixfather,linuxfather);

printf("%s \n", unixfather);

printf("%s \n", linuxfather);

return 0;

}


#include

main()

{

char intking1[50];

char intking2[50]="Google is giving a run for money to Yahoo";

cstrcpy(intking1,intking2);

printf("%s \n",intking1);

printf("%s \n",intking2);

return 0;

}


cstrcpy(char *add1,char *add2)

{

//*int i=0;

while(*add2!='\0')

{

*add1=*add2;

add1++;

add2++;

}

//*add1=*add2;

//return(add1);

}


By declaring char as constant we are declaring that the source string should remain constant(should not change). Thus the const qualifier ensures that your program does not inadvertently alter a variable that you intended to be a constant.


strcmp()

This is a function which compares two strings to find out whether they are same or different. The two strings are compared character by character until there is a mismatch or end of one of the strings is reached, which ever occurs first. If two strings are identical, strcmp returns a value zero. If they are not it returns numeric difference of ascii values of the first non matching pairs of characters.


ARRAYS

EG1:

#include

main()

{

int i;

int a[]={2,4,5,6,7,8,8};

for(i=0;i<7;i++)

display(a[i]);

printf("\n");

return 0;

}


display(int m)

{

printf(" %d",m);

}


EG2:

#include

main()

{

int i;

int ary[]={1,2,3,4,5,6};

for(i=0;i<6;i++)

display(&ary[i]);

printf("\n");

}


display(int *n)

{

show(&n);

//printf("%c",n);

}


show(int *m)

{

int *l;

l=*m;

printf("%d",*l);

printf("\n");

}


0 Comments:

Post a Comment

<< Home