The greatest mistake you can make in life is to be continually fearing you will make one

Monday, 21 June 2010

String

String:
  • Strings are character array.
  • Character array are stored in contiguous memory location.
String Constant:
  • A string constant is a one-Dimensional array of characters terminated by a null character('\0').
  • Each character in the array occupies one byte of memory and the last character is always '\0'
    Example:
        char str[]={'C','P','U','\0'};
  • The terminating null character('\0') is very important in a string because its the only way for the compiler to know where the string ends.
Null Character('\0'):
  • '\0' is called null character.
  • '\0' and 0 are not same because both has different ASCII value. ASCII value of '\0' is 0 but ASCII value of 0 is 48.
String Initialization:
  • A string can be initialized without adding the terminating null character as,

        char str[]="WELCOME";

    Here C inserts the NULL character automatically.
Accessing Character array elements:
  • Similar to integer array we can access array elements  
    #include< stdio.h >
    int main()
    {
        int i=0;
        char str[]="WELCOME";
        while(str[i]!='\0')
        {
            printf("%c",str[i]);
            i++;
        }
        return;
    }
  • This can be done by using pointer also. Mentioning th name of the array we get the base address(zeroth element) of the array.
    #include< stdio.h >
    int main()
    {
        char str[]="WELCOME",*ptr;
        ptr=str;
        while(*ptr!='\0')
        {
            printf("%c",*ptr);
            ptr++;
        }
        return;
    }
Reading a string from a keyboard:
  • For reading and writting a string the format specifier is %s and no & is needed
    #include< stdio.h >
    int main()
    {
        char str[30];
        printf("Enter the string\n");
        scanf("%s",str);
        printf("String=%s",str);
        return;
    }

    The scanf() function fills in the character typed at keyboard into the str[] array until the blank space or enter key is hit. If blank space or enter key is hit scanf() automatically place a '\0' in the array.
  • Normally the %s will read a string upto a blank space. To read a muti word string untill the end of line including blank space use %[^\n]s
    #include< stdio.h >
    int main()
    {
        char str[100];
        printf("ENter the string\n");
        scanf("%[^\n]s", str);
        printf("String=%s",str);
        return;
    }

    Output:
    Enter the string
    welcome honey
    String=Welcome honey
  • Other way of reading multiword string is gets() and puts(). But it is dangerous to use gets() in programs so try to avoid gets()
    #include< stdio.h >
    int main()
    {
        char str[30];
        printf("Enter the string\n");
        gets(str);
        puts(str);
        return;
    }
Pointer and String:
  • A string can be stored in 2 forms

    1. char str[]="Welcome"; //here Welcome is stored in a location called str
    2. char *str="Welcome"; //here Welcome is stored in some other location i memory and assign the address of the string in a char pointer
  • The advantage of pointing a string using a character pointer is we cannot assign a string to another string but we can assign a char pointer to another char pointer
    #include< stdio.h >
    int main()
    {
        char str1[]="Welcome",str2[30];
        char *ptr1="Welcome",*ptr2;
        //str2=str1; //error
        ptr2=ptr1;
        printf("ptr2=%s",ptr2);
        return;
    }
  • Once a string has been defined it cannot be initialized to another set of characters but using char pointer we can redefine the string.
    #include< stdio.h >
    int main()
    {
        char str1[]="Welcome";
        char *ptr1="Welcome";
    //  str1="Good";//error
        ptr1="Good";
        printf("ptr1=%s",ptr1);
        return;
    }

     
Points to consider in Strings:
  1. The size of the string depends on the size of declaration not the number of characters in the string
    #include < stdio.h>
    void main()
    {
        char str[30]="Network programming";
        printf("%d",sizeof(str));
    }

    The output here is 30 not 1

Tuesday, 1 June 2010

Interview Questions

1. Difference between arrays and pointers?
2. What is the purpose of realloc( )?
3. What is static memory allocation and dynamic memory allocation?
4. How are pointer variables initialized?
5. Are pointers integers?
6. What is a pointer variable?
7. What is a pointer value and address?
8. What is a method?
9. What are the advantages of the functions?
10. What is the purpose of main( ) function?
11. What is an argument ? differentiate between formal arguments and actual arguments?
12. What is a function and built-in function?
13. What is modular programming?
14. When does the compiler not implicitly generate the address of the first element of an array?
15. What are the characteristics of arrays in C?
16. Differentiate between a linker and linkage?
17. What are advantages and disadvantages of external storage class?
18. Diffenentiate between an internal static and external static variable?
19. What are the advantages of auto variables?
20. What is storage class and what are storage variable ?
21. Which expression always return true? Which always return false?
22. Write the equivalent expression for x%8?
23. why n++ executes faster than n+1?
24. what is a modulus operator? What are the restrictions of a modulus operator?
25. What is the difference between a string and an array?
26. Is it better to use a pointer to navigate an array of values,or is it better to use a subscripted array name?
27. Can the sizeof operator be used to tell the size of an array passed to a function?
28. Is using exit() the same as using return?
29. Is it possible to execute code even after the program exits the main() function?
30. What is a static function?
31. Why should I prototype a function?
32. How do you print an address?
33. Can math operations be performed on a void pointer?
34. How can you determine the size of an allocated portion of memory?
35. What is a “null pointer assignment” error? What are bus errors, memory faults, and core dumps?
36. What is the difference between NULL and NUL?
37. What is the heap?
38. Can the size of an array be declared at runtime?
39. What is the stack?
40. When should a far pointer be used?
41. What is the difference between far and near?
42. Is it better to use malloc() or calloc()?
43. Why should we assign NULL to the elements (pointer) after freeing them?
44. When would you use a pointer to a function?
45. How do you use a pointer to a function?
46. Can you add pointers together? Why would you?
47. What does it mean when a pointer is used in an if statement?
48. Is NULL always defined as 0?
49. What is a void pointer?
50. What is a null pointer?
51. How many levels of pointers can you have?
52. What is indirection?
53. How do you print only part of a string?
54. How can I convert a string to a number?
55. How can I convert a number to a string?
56. What is the difference between a string copy (strcpy) and a memory copy (memcpy)? When should each be used?
57. How can you check to see whether a symbol is defined?
58. How do you override a defined macro?
59. What is #line used for?
60. What is a pragma?
61. What are the standard predefined macros?
62. How can type-insensitive macros be created?
63. How many levels deep can include files be nested?
64. Can include files be nested?
65. Can you define which header file to include at compile time?
66. What is the difference between #include and #include “file”?
67. Is it better to use a macro or a function?
68. How are portions of a program disabled in demo versions?
69. What is the benefit of using an enum rather than a #define constant?
70. What is the benefit of using #define to declare a constant?
71. Can a file other than a .h file be included with #include?
72. How can you avoid including a header more than once?
73. What will the preprocessor do for a program?
74. What is a macro, and how do you use it?
75. What is Preprocessor?
76. How can I make sure that my program is the only one accessing a file?
77. How can I open a file so that other programs can update it at the same time?
78. How do you determine whether to use a stream function or a low-level function?
79. What is the difference between text and binary modes?
80. How can you restore a redirected standard stream?
81. How do you redirect a standard stream?
82. How can I search for data in a linked list?
83. How can I sort a linked list?
84. What is hashing?
85. What is the quickest searching method to use?
86. What is the easiest searching method to use?
87. How can I sort things that are too large to bring into memory?
88. What is the quickest sorting method to use?
89. What is the easiest sorting method to use?
90. What is the benefit of using const for declaring constants?
91. Can static variables be declared in a header file?
92. What is the difference between declaring a variable and defining a variable?
93. Is it acceptable to declare/define a variable in a C header?
94. When should a type cast not be used?
95. When should a type cast be used?
96. How can you determine the maximum value that a numeric variable can hold?
97. How reliable are floating-point comparisons?
98. Can a variable be both const and volatile?
99. when should the volatile modifier be used?
100. When should the register modifier be used? Does it really help?