Professional Documents
Culture Documents
2. What is a pointer?
Pointer :
A pointer is a variable used to store address of another variable or any memory
location. A pointer provides a way of accessing a variable without referring to the
variable directly.
Syntax: datatype *pvar1,*pvar2 ;
3. What is a structure?
Structure:
Structures are data structures whose individual elements can differ in type. A
single data structure may contain integers, floating points, pointers, arrays as well as
other structures. The individual structure elements are referred to as members.
The advantage of using macro is the execution speed of the program fragment.
When the actual code snippet is to be used, it can be substituted by the name of the
macro. The same block of statements, on the other hand, need to be repeatedly hard
coded as and when required.
The disadvantage of the macro is the size of the program. The reason is, the
pre-processor will replace all the macros in the program by its real definition prior to the
compilation process of the program.
Pass By Reference :
- In Pass by reference address of the variable is passed to a function. Whatever changes
made to the formal parameter will affect to the actual parameters
- Same memory location is used for both variables.(Formal and Actual)-
- it is useful when you required to return more than 1 values
Pass By Value:
- In this method value of the variable is passed. Changes made to formal will not affect
the actual parameters.
- Different memory locations will be created for both variables.
Static Identifier :
The static identifier is used for initializing only once, and the value retains during the life
time of the program / application. A separate memory is allocated for static variables. This
value can be used between function calls. The default value of an uninitialized static variable is
zero. A function can also be defined as a static function, which has the same scope of the static
variable.
Main memory and CPU registers are the two memory locations where auto variables are
stored. Auto variables are defined under automatic storage class. They are stored in main
memory. Memory is allocated to an automatic variable when the block which contains it is called
and it is de-allocated at the completion of its block execution.
Auto variables:
Storage : main memory.
Default value : garbage value.
Scope : local to the block in which the variable is defined.
Lifetime : till the control remains within the block in which the variable is defined.
11. Where does global, static, local, register variables, free memory and C Program
instructions get stored?
Local/auto variables, which is declared in stack area of memory. A variable prefixed with
register will be stored in registers. Global variables stored in uninitialized data segment
static variables stored in initialized data segment. Free memory (dynamic memory allocations)
will be stored in heap memory (i.e. in data segments). Const variables also stores in text
segments
Arrays and Linked list both are list data structures used for maintaining a
list of values. Arrays use sequential allocation while Linked list uses linked allocation.
> Linked list uses some extra memory i.e. link pointer.
> Indexing an element, e.g. accessing kth element is cheaper in arrays and costly in Linked list.
> Insertion and Deletion of elements is a cheaper operation in Linked lists.
> Since nodes in Linked list are dynamically allocated, it has no limitations on growth (apart
from memory constraints).
> Merging Lists is easier in case of Linked lists.
> Breaking a List into two or more lists is easier in case of Linked lists.so Linked list is a better
data structure in most cases. Arrays are goes mostly for static data structures.
Syntax:
enum identifier opt { enumerator-list }
enum identifier
14. Describe about storage allocation and scope of global, extern, static, local and register
variables?
Storage allocation and scope of global ,extern ,static ,local and register variables :
The storage allocation / class determine the memory part where the storage space
is allocated for variables, functions and how long the allocation of storage continues to exist.
- auto variables stores in the memory storage. Majority of the variables in a program
/ application are of type auto. Their scope might be local or global.
- extern variables are of global variables and can be declared in another source file
which could be external / outside the current program scope,
- register variables are allocated in the CPU registers. These variables storage and
accessibility is much faster than other variables, being they are stored in CPU itself. The
variables of repeated usage or access time is critical, can be declared as register variables.
- static variables provide a lifetime over the program, and provide a way for
limiting the scope of such variables. These variables are automatically initialized to zero and
could be specified for auto and extern variables. The values are retained, even though they are
declared in local scope, between the repeated function calls to the same function.
15. What are register variables? What are the advantage of using register variables?
register variables :
The variables of register type modifier will inform the compiler for storing the
variables in a register of CPU.
The advantage of this type modifier is the access optimization and speed of program
execution. The operations of these variables are faster by orders of magnitude
typedef :
A typedef declaration is a declaration with typedef as the storage class. The declarator
becomes a new type. You can use typedef declarations to construct shorter or more meaningful
names for types already defined by C or for types that you have declared. Typedef names allow
you to encapsulate implementation details that may change.
typedef is used to define new data type names to make a program more readable to the
programmer.
17. Can we specify variable field width in a scanf() format string? If possible how?
It is possible to specify variable field width in a scanf() format string. By using %s control string.
This character reads a string of variable field width up to the first white space character.
18. Out of fgets() and gets() which function is safe to use and why?
The function fgets() function is safer to use. It checks the bounds, i.e., the size of the buffer and
does not cause overflow on the stack to occur. gets() does not check the bounds.
The function strcpy() will not allocate the memory space to copy. A pointer to the string
to copy and a pointer to place to copy it to should be given.
The function strdup() will occupy / grab itself the memory space for copying the string to.
This memory space needs to be freed up later when it is of no use anymore.
21. Differentiate between a for loop and a while loop? What are it uses?
Storage classes in C :
Storage class defined for a variable determines the accessibility and longevity of the
variable. The accessibility of the variable relates to the portion of the program that has
access to the variable. The longevity of the variable refers to the length of time the
variable exists within the program.
Auto
Automatic variable, also called as local variable and it has scope only within the function
block where it is defined.
External
External variable are defined outside any function and memory is set aside for this type of
variable once it is declared and remained until the end of the program. These variables are
also called global variables.
Static
The static automatic variables, as with local variables, are accessible only within the
function in which it is defined. Static automatic variables exist until the program ends in the
same manner as external variables. In order to maintain value between function calls, the
static variable takes its presence.
23. Write down the equivalent pointer expression for referring the same element a[i][j][k][l]?
equivalent pointer expression for referring the same element aijkl :
(*(*(*(*(a+i)+j))+k)+l)
1. union allocates the memory equal to the maximum memory required by the
member of the union but structure allocates the memory equal to the total memory required by
the members.
2. In union, one block is used by all the member of the union but in case of structure,
each member have their own memory space.
3. union is best in the environment where memory is less as it shares the memory
allocated.But structure can not implemented in shared memory.
4. As memory is shared,ambiguity are more in union,but less in structure.
5. self referencial union can not be implemented in any datastructure ,but self
referencial structure can be implemented.
In case of structure each variables have their own storage location whereas for
unions all the variables share a common memory location. In structure all the variables can be
handled at a time but for union only a member can be handled at a time.
30 . How will you declare an array of three function pointers where each function receives
two ints and returns a float?
Declaration for an array of three function pointers where each function receives two ints
and returns a float :
float (*fn[3])(int,int)
32. What is a NULL Macro? What is the difference between a NULL Pointer and a NULL
Macro?
Null Macro:
33. What does the error 'Null Pointer Assignment' mean and what causes this error?
Null Pointer Assignment Error :
A NULL pointer assignment is a runtime error It occurs due to various reasons one is that ur
program has tried to access an illegal memory location. Illegal location means either the location
is in the operating systems address space or in the other processes memory space. In stdio.h
NULL is defined as 0
So whenever your program tries to access 0th location the operating system kills your
program with runtime assignment error because the 0th location is in the operating systems
address space and operating system doesnt allow access to its address space by user program .
34. What is near, far and huge pointers? How many bytes are occupied by them?
Near ,Far and huge Pointers :
Near pointer: Near pointer is 16 bit wide and holds only offset address. This can
point up to 64kb or 0xFFFF offset in a given segment.
int i = 0;
int * i_pointer = &i;
Far pointer: Far pointers are 32 bit wide. It holds both 16bit segment and 16bit
offset address. Thus a far pointer can point any segment and any offset inside that segment.
MK_FP is a compiler macro which takes segment and offset value and constructs a far pointer.
Huge pointer: Huge pointers are also far pointers i.e. 32 bit pointer. The difference
is compiler rounds of the offset to zero of a far pointer when offset reaches to 0xFFFF but for
huge pointer it increments the segment thus huge pointer can be increased or decreased
uniformly between any segments and thus can have any value from 0 to 1MB.
In case of far pointer increment of i_pointer will make it round off to 0xA0000
again. But for huge pointer the value will point to next segment i.e. 0xAFFFF + 1 = 0xB0000.
35. How would you obtain segment and offset addresses from a far address of a memory
location?
Pointers to far objects are stored using four bytes (32 bits). The bytes are stored little
endian or low to high order. The first word contains the 14-bit memory offset (bits 14 and 15 are
always 0). The second word contains the page number (or segment number for function
pointers). The memory address is calculated as follows:
36. Are the expressions arr and &arr same for an array of integers?
No arr refers to address of array &arr refers address of address of array but compiler treats arr
and & arr, so even you place arr and & arr no error only warnings will be displayed.
37. Does mentioning the array name gives the base address in all the contexts?
yes by mentioning the name of array we get the base address of the array
Both structures and unions are used to group a number of variables together. But while a
structure enables us to treat as a unite a number of different variables stored at different places in
memory , a union enables us to treat the same space in memory as a number of different
variables . That is , a union is a way for a section of memory to be treated as a variable of one
type on one occasion, and as a different variable, of a different type on other occasion.
An enumerated type (also called enumeration or enum) is a data type consisting of a set of
named values called elements, members or enumerators of the type. The enumerator names are
usually identifiersthat behave as constants in the language. A variable that has been declared as
having an enumerated type can be assigned any of the enumerators as a value.
For example, the four suits in a deck of playing cards may be four enumerators
named CLUB, DIAMOND, HEART, SPADE, belonging to an enumerated type named suit. If a
variable V is declared having suit as its data type, one can assign any of those four values to it.
You may need to use the code below to declare the structure:
Code:
struct testing
{
public int y,z;
}
You may need to use the code below to declare the class:
Code:
class test
{
int num = 10;
...
}
You may need to use the code below to declare the emum:
Code:
enum colors {red, green, blue, yellow};
41. How can we check whether the contents of two structure variables are same or not?
There is no way you can compare the two structure variables.
for Ex:
stucture test a b;
if (a == b) // is wrong we can't compair structure as whole.
if((a.rno==b.rno)&&(a.name==b.name)&&(a.total==b.total))
printf("
two structure variables are equal");
else
printf("
two structure variables are not equal");
getch();
}
42. How are Structure passing and returning implemented by the complier?
Here is the program that illustrates the solution to the above question.
i)accept input through GetEmployeeDetails()
ii)Print the same details through PrintEmployeeDetails()
iii)Modify / Change the details through ModifyEmployeeDetails()
iv)Print the modified values using PrintEmployeeDetails()
#include<stdio.h>
struct Employee
{
int ID;
char *name;
};
void GetEmployeeDetails(struct Employee *Emp)
{
printf("Enter the ID");
scanf(" d" &Emp->ID);
Emp->name = (char*)malloc(20 * sizeof(char));
fflush(stdin);
printf("nEnter the Name");
gets(Emp->name);
}
void PrintEmployeeDetails(struct Employee Emp)
{
printf("nEmployee ID :: d" Emp.ID);
printf("nEmployee Name :: snn" Emp.name);
}
}
43. How can we read/write Structures from/to data files?
There 2 function given in C++. They are fread() & fwrite(). Using them we can read & write
structures
from/to data files respectively.
Basic representation:
fread(&structure, sizeof structure,1,fp);
fread function recieves a pointer to structure and reads the image(memory) of the
structure as a stream of bytes. Sizeof gives the bytes which was occupied by structure.
fwrite function recieves a pointer to structure and writes the image(memory) of the
structure as a stream of bytes. Sizeof gives the bytes which was occupied by structure.
44. What is the difference between an enumeration and a set of pre-processor # defines?
Difference between an enumeration and a set of pre-processor # defines :
It is always better to use enums over #define for this kind of cases. One thing is type safety.
Another one is that when you have a sequence of values you only have to give the beginning of
the sequence in the enum, the other values get consecutive values.
enum {
ONE = 1,
TWO,
THREE,
FOUR
};
instead of
#define ONE 1
#define TWO 2
#define THREE 3
#define FOUR 4
As a side-note, there is still some cases where you may have to use #define (typically for some
kind of macros, if you need to be able to construct an identifier that contains the constant), but
that's kind of macro black magic, and very very rare to be the way to go. If you go to these
extremities you probably should use a C++ template (but if you're stuck with C...).
45. What do the 'c' and 'v' in argc and argv stand for?
In C, we can supply arguments to main function. The arguments that we pass to main ( ) at
command prompt are called command line arguments. These arguments are supplied at the
time of invoking the program.The main ( ) function can take arguments as:
main(int argc, char *argv[]) { }
The first argument argc is known as argument counter. It represents the number of
arguments in the command line. The second argument argv is known as argument vector. It is
an array of char type pointers that points to the command line arguments. Size of this array will
be equal to the value of argc.
46. Are the variables argc and argv are local to main?
Reason :
Yes why because these variables written in main function only not outside main. These
belong to main block but not to other. So according to my knowledge these command line
arguments are local to main.
47. What is the maximum combined length of command line arguments including the space
between adjacent arguments?
The maximum combined length of command line arguments including the space between
adjacent arguments :
Presumably this would be (INT_MAX-1) * SIZE_MAX. That is each argument can presumably
be up to SIZE_MAX chars and there can be INT_MAX - 1 arguments (one being needed for
argv[0] the program name).
Since the actual values of INT_MAX and SIZE_MAX are implementation defined no maximum
can be definitively given ahead of time.
That said one might suspect the hosting OS and/or startup code would provide somewhat more
stringent limits.
48. If we want that any wildcard characters in the command line arguments should be
appropriately expanded, are we required to make any special provision? If yes, which?
Yes you have to compile a program like
tcc myprog wildargs.obj
49. Does there exist any way to make the command line arguments available to other
functions without passing them as arguments to the function?
You can copy them into global variables in the main() function, then have your other functions
access those global variables. Global variables should generally be avoided, however.
(Or)
Trap the arguments in variables which can be passed to other function.
50. What are bit fields? What is the use of bit fields in a Structure declaration?
Bit fields and its usuage in structure declaration :
When the storage is at premium it may be necessary to pack several objects into single machine
word one common use is a set of single bit flags in application like compiler symbol tables. A bit
field or field for short is a set of adjacent bits with a single implementation - defined storage unit.
struct {
unsugned int is_keyword : 1;
unsigned is_extern :1;
unsigned is_static : 1 ;
} flags ;
52. Which bit wise operator is suitable for checking whether a particular bit is on or off?
Checking whether a particulat bit is on or off :
Explaination:
x = 24 i.e. 0001 1000
tester = 1 i.e. 0000 0001
tester = tester << 3 does:
tester = 8 i.e. 0000 1000
if x = 5 (0000 0101)
then x&tester i.e.
00000101 &
00001000
------------
00000000
which is ZERO hence decides that 3rd bit is UNSET / OFF.
In Brief:
Bitwise AND is used to CHECK / TEST a particular bit.
Bitwise OR is used to SET (Make it 1) a particular bit.
Bitwise XOR is used to TOGGLE (1to0 or 0to1) a particular bit.
Bitwise AND is also used to RESET a particular bit (if ANDed with 11110111 say I have to
reset 4th bit).
53. Which bit wise operator is suitable for turning off a particular bit in a number?
the bit wise AND operator.
In the following code snippet, the bit number 24 is reset to zero.
54. Which bit wise operator is suitable for putting on a particular bit in a number?
Shift to the position first and use XOR (^) to switch the bit on/off. AND (&) to check if it's on
or off. In both cases the 2nd operand will be 1. (Ex. int switch = x ^ 1; int check = x & 1; This
is only if you are checking for the least significant bit, the furthest one to the right.)
55. Which bit wise operator is suitable for checking whether a particular bit is on or off?
Kindly refer to Q.52
56. which one is equivalent to multiplying by 2:Left shifting a number by 1 or Left shifting an
unsigned int or char by 1?
57. Write a program to compare two strings without using the strcmp() function.
#include <stdio.h>
#include <conio.h>
main()
{
while(str1[i]!='/0' &&str2[i]!='/0')
if(str1[i]!=str2[i])
flag=1;
if(flag==1)
printf("equal");
}
#include
main()
{
while(str1[i]!='/0' &&str2[i]!='/0')
if(str1[i]!=str2[i])
flag=1;
if(flag==1)
printf("equal");
}
#include<stdio.h>
#include<conio.h>
main()
{
char s1[20],s2[20];
printf("enter first string");
scanf("%s",s1);
printf("enter second string");
scanf("%s",s2);
string_concatenate(&s1,&s2);
}
count_characters(char *x)
{
int i=0, count=0;
for(i=0;*(x+i)!='\0';i++)
count++;
return count;
}
string_concatenate(char *s1,char *s2)
{
int i,j;
char s3[50];
for(i=0;i
s3[i]=s1[i];
for(j=0;j
s3[i+j]=s2[j];
s3[i+j]=s2[j];
printf("%s",s3);
getch();
}
59. Write a program to interchange 2 variables without using the third one.
#include<stdio.h>
#include<conio.h>
void main
{
int a,b;
printf("enter the two no");
scanf("%d%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("a=%d,b=%d",a,b);
getch();
}
REVERSE A STRING
#include<stdio.h>
#include<string.h>
main()
{
char str[50] revstr[50];
int i=0 j=0;
printf("Enter the string to be reversed : ");
scanf(" s" str);
for(i=strlen(str)-1;i>=0;i--)
{
revstr[j]=str[i];
j++;
}
revstr[j]=' ';
printf("Input String : s" str);
printf("nOutput String : s" revstr);
getch();
PALINDROME CHECK
int main()
{
isPalindrome("avon sees nova");
isPalindrome("a");
isPalindrome("avon sies nova");
isPalindrome("aa");
isPalindrome("abc");
isPalindrome("aba");
isPalindrome("3a2");
return(0);
}
N = strlen(string);
end = N-1;
#include<iostream>
using namespace std;
Fibonacci series :
#include<stdio.h>
#include<conio.h>
int fib(int a);
main()
{
int a;
clrscr();
scanf("%d",&a);
for(int i=0;i<a;i++)
printf("%d\n",fib(i));
}
int fib(int a)
{
if(a==0)
return 0;
if(a==1)
return 1;
else
return (fib(a-1)+fib(a-2));
}
Recursion :
#include<stdio.h>
#include<conio.h>
void main()
{
int factorial(int);
int n;
clrscr();
printf("Enter a number: ");
scanf("%d",&n);
printf("Factorial of %d is: %d",n,factorial(n));
getch();
}
int factorial(int f)
{
int fact;
if(f==1)
return(1);
else
fact=f*factorial(f-1);
return(fact);
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
if ((argc < 4) ||
(sscanf(argv[1],"%d",&x) != 1) ||
(sscanf(argv[2],"%d",&y) != 1) ||
(sscanf(argv[3],"%d",&z) != 1) ||
(x <= 0) || (y <= 0) || (z <= 0) ||
(z < y) || ((z - y) > 10)) {
printf("usage: %s <x> <y> <z>\n",argv[0]);
printf(" where: x, y, and z are integers > 0\n");
printf(" z >= y, and z - y <= 10\n");
return -1;
}
a = malloc((z-y+1)*sizeof(double));
for (i = 0, j = y; i < z-y+1; i++, j++) {
a[i] = pow(x,j);
printf("%g ",a[i]);
}
puts("");
free(a);
return 0;
}
65. Write a program which uses functions like strcmp(), strcpy()? etc
#include <stdio.h>
#include <string.h>
int main()
{
char name1[12], name2[12], mixed[25];
char title[20];
strcpy(name1, "Rosalinda");
strcpy(name2, "Zeke");
strcpy(title, "This is the title.");
printf(" %s\n\n", title);
printf("Name 1 is %s\n", name1);
printf("Name 2 is %s\n", name2);
if(strcmp(name1, name2) > 0) /* returns 1 if name1 > name2 */
strcpy(mixed, name1);
else
strcpy(mixed, name2);
printf("The biggest name alphabetically is %s\n", mixed);
strcpy(mixed, name1);
strcat(mixed, " ");
strcat(mixed, name2);
printf("Both names are %s\n", mixed);
return 0;
}
/* Output of Program.
Name1 is Rosalinda
Name2 is Zeke
The biggest name alphabetically is Zeke
Both names are Rosalinda Zeke
*/
67. How would you dynamically allocate a one-dimensional and two-dimensional array of
integers?
To dynamically allocate an array, use 'void *realloc(void *array, size_t size)', where array is the
pointer you plan on reallocating and size is the new length of the array in bytes.
One-dimensional array:
{
Two-dimensional array:
{
array2d[5][5] = 24;
/* Clean up. */
for (index = 0; index < xlen; index++) {
free(array2d[index]);
}
free(array2d);
return 0;
}
68. How can you increase the size of a dynamically allocated array?
realloc
It is often useful to be able to grow or shrink a block of memory. This can be done
using realloc which returns a pointer to a memory region of the specified size, which contains the
same data as the old region pointed to by ptr (truncated to the minimum of the old and new
sizes). If realloc is unable to resize the memory region in-place, it allocates new storage, copies
the required data, and frees the old pointer. If this allocation fails, realloc maintains the original
pointer unaltered, and returns the null pointer value. The newly allocated region of memory is
uninitialized (its contents are not predictable). The function prototype is
void *realloc(void *pointer, size_t size);
69. How can you increase the size of a statically allocated array?
int arr[10];
When an array is declared as above, memory is allocated for the elements of the array when the
program starts, and this memory remains allocated during the lifetime of the program. This is
known as static array allocation. Hence we cannot increase size of statically allocated array.
70. When reallocating memory if any other pointers point into the same piece of memory do
you have to readjust these other pointers or do they get readjusted automatically?
71. Which function should be used to free the memory allocated by calloc()?
free() is a function used to free the memory allocated dynamically by both malloc and calloc
functions.
free(ptr): ptr is a pointer to a memory block which has already been creeated by malloc or calloc.
72. How much maximum can you allocate in a single call to malloc()?
The largest possible memory block malloc can allocate depends on the host system particularly
the size of physical memory and the operating system implementation. Theoretically the largest
number should be the maximum value that can be held in a size_t type which is an
implementation-dependent unsigned integer representing the size of an area of memory. The
maximum value is (size_t) 1 or the constant SIZE_MAX in the C99 standard.
74. What is object file? How can you access object file?
Object file :
An object file is binary representation of source(text) file. On Linux, object and executable
files have ELF format. It's a collection of various sections segragating type of data in:
- text section
- data section
- stack
- heap
Also addresses in an object file are relative and hence object files can't be run unless it's
linked. for ex, if you make a call to printf(), object file will have an entry as
("call printf ___") where the blank is the address of printf function. Since printf is part of
libc.so, you have no knowledge of its address. Linking with libc pads the correct address and
enable you to call it successfully.
When we compile a C program using cc program.c, the compiler places the object code in
program.o and loads the executable in a.out(called as assembler output).When we write
./a.out, the loader loads it in the memory and executes it .
75. Which header file should you include if you are to develop a function which can
accept variable number of arguments?
The header file should you include if you are to develop a function which can accept
variable number of arguments is stdarg.h
77. How can a called function determine the number of arguments that have been
passed to it?
use the variable length argument - va_arg va_list ca_start and va_end macros
78. Can there be at least some solution to determine the number of arguments passed
to a variable argument list function?
In general, you cannot. Ideally, you should provide a version of that other function which
accepts a va_list pointer.
Suppose you want to write a faterror function which will print a fatal error message, then
exit. You might like to write it in terms of the error function of question 15.5:
void faterror(const char *fmt, ...)
{
error(fmt, what goes here? );
exit(EXIT_FAILURE);
}
but it's not obvious how to hand faterror's arguments off to error.
Proceed as follows. First split up the existing error function to create a new verror which
accepts not a variable argument list but a single va_list pointer. (Note that doing so is little
extra work, because verror contains much of the code that used to be inerror, and the
new error becomes a simple wrapper around verror.)
#include <stdio.h>
#include <stdarg.h>
81. Does there exist any other function which can be used to convert an integer or a
float to a string?
Some implementations provide a nonstandard function called itoa(), which converts an
integer to string.
#include
string:
Points to the buffer that is to hold resulting string.
The resulting string may be as long as seventeen bytes.
radix:
Is the base of the number; must be in the range 2 - 36.
sprintf ( s, %d %f\n, i, f );
82. How would you use qsort() function to sort an array of structures?
qsort() function to sort an array of structures :
#include <stdio.h>
#include <stdlib.h>
void printarray(void);
int compare(struct animal *, struct animal *);
void main(void)
{
printf("List before sorting:\n");
printarray();
83. How would you use qsort() function to sort the name stored in an array of
pointers to string?
It is same as you would sort an array of integers using qsort. Only difference is that you
would have to do string comparison.
84. How would you use bsearch() function to search a name stored in array of
pointers to string?
bsearch() function to sort to search a name stored in array of pointers to string :
Syntax:
where;
emt->Element.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TABSIZE 1000
85. How would you use the functions sin(), pow(), sqrt()?
First of all you have to insert the following line in your code:
#include <math.h>
sin is defined function which takes argument fo double type and returns double type:
sin(x)
If you write something like:
double x, y;
The function pow has two arguments (pow(x, y)) of double type and in returns the
value of double type. For instance,
pow(2, 3) in mathematical language can be written as 23.
86. How would you use the functions memcpy(), memset(), memmove()?
memcpy() function copies n bytes from the object pointed to by s2 into the object
pointed to by s1. If copying takes place between objects that overlap, the behavior is
undefined.
memmove() function shall copy n bytes from the object pointed to by s2 into the
object pointed to by s1. Copying takes place as if the n bytes from the object pointed
to by s2 are first copied into a temporary array of n bytes that does not overlap the
objects pointed to by s1 and s2, and then the n bytes from the temporary array are
copied into the object pointed to by s1.
memset() function copies c (converted to an unsigned char) into each of the first n
bytes of the object pointed to by s. SYNTAX:
void *memcpy(void *s1, const void *s2, size_t n);
void *memmove(void *s1, const void *s2, size_t n);
void *memset(void *s, int c, size_t n);
87. How would you use the functions fseek(), freed(), fwrite() and ftell()?
fread(s,i1,i2,f) Enter i2 dataitems,each of size i1 bytes,from file f to string s.
fseek(f,1,i) Move the pointer for file f a distance 1 byte from location i.
The data type returned for functions fread,fseek and fwrite is int and ftell is long int.
88. How would you obtain the current time and difference between two times?
we have Date class in java.util.
Ex:
java.util.Date d1 new java.util.Date();
System.out.println(d1.getTime());
statement 1;
statement 2;
statement 3;
java.util.Date d2 new java.util.Date();
System.out.println(d2.getTime());
89. How would you use the functions randomize() and random()?
Random() returns a random number between 0 and (num-1).random(num) is a
macro defined in stdlib.h
#include<conio.h>
main()
{int i;
i=random(50);
printf9'%d",i);
}
90. How would you implement a substr() function that extracts a sub string
from a given string?
substr(string position [ count])
It extract substring starting from start and going for count characters. If count is
not specified the string is clipped from the start till the end
91. What is the difference between the functions rand(), random(), srand()
and randomize()?
94. Can you use the function fprintf() to display the output on the screen?
yes we can
#include <stdio.h>
#include <conio.h>
void main()
{
fprintf(stdout sdfsdf );
getch();
}
fprintf() accepts the first argument which is a file pointer to a stream. the stdout
is also a file pointer to the standard output(screen)