Best Industrial Training in C,C++,PHP,Dot Net,Java in Jalandhar

Thursday, 27 March 2014

C Questions With Solutions



1)  What are strings and explain standard library functions?
The string in C programming language is actually a one-dimensional array of characters which is terminated by a null character '\0'. Thus a null-terminated string contains the characters that comprise the string followed by a null.
The following declaration and initialization create a string consisting of the word "Hello". To hold the null character at the end of the array, the size of the character array containing the string is one more than the number of characters in the word "Hello."
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};








Example

#include <stdio.h>
 
int main ()
{
   char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
 
   printf("Greeting message: %s\n", greeting );
 
   return 0;
}
 
S.N.
Function & Purpose
1
strcpy(s1, s2);
Copies string s2 into string s1.
2
strcat(s1, s2);
Concatenates string s2 onto the end of string s1.
3
strlen(s1);
Returns the length of string s1.
4
strcmp(s1, s2);
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
5
strchr(s1, ch);
Returns a pointer to the first occurrence of character ch in string s1.
6
strstr(s1, s2);
Returns a pointer to the first occurrence of string s2 in string s1.
 
 
 
 
 
The Standard Library Functions
Some of the "commands" in C are not really "commands" at all but are functions. For example, we have been using printf and scanf to do input and output, and we have used rand to generate random numbers - all three are functions.
There are a great many standard functions that are included with C compilers and while these are not really part of the language, in the sense that you can re-write them if you really want to, most C programmers think of them as fixtures and fittings. Later in the course we will look into the mysteries of how C gains access to these standard functions and how we can extend the range of the standard library. But for now a list of the most common libraries and a brief description of the most useful functions they contain follows:
1.   stdio.h: I/O functions:
1.   getchar() returns the next character typed on the keyboard.
2.   putchar() outputs a single character to the screen.
3.   printf() as previously described
4.   scanf() as previously described

2.   string.h: String functions
1.   strcat() concatenates a copy of str2 to str1
2.   strcmp() compares two strings
3.   strcpy() copys contents of str2 to str1

3.   ctype.h: Character functions
1.   isdigit() returns non-0 if arg is digit 0 to 9
2.   isalpha() returns non-0 if arg is a letter of the alphabet
3.   isalnum() returns non-0 if arg is a letter or digit
4.   islower() returns non-0 if arg is lowercase letter
5.   isupper() returns non-0 if arg is uppercase letter

4.   math.h: Mathematics functions
1.   acos() returns arc cosine of arg
2.   asin() returns arc sine of arg
3.   atan() returns arc tangent of arg
4.   cos() returns cosine of arg
5.   exp() returns natural logarithim e
6.   fabs() returns absolute value of num
7.   sqrt() returns square root of num

5.   time.h: Time and Date functions
1.   time() returns current calender time of system
2.   difftime() returns difference in secs between two times
3.   clock() returns number of system clock cycles since program execution

6.   stdlib.h:Miscellaneous functions
1.   malloc() provides dynamic memory allocation, covered in future sections
2.   rand() as already described previously
3.   srand() used to set the starting point for rand()

 

No comments:

Post a Comment