( p1 "Name" p2 p2 (Char ) Malloc (20) Memset (p2, 0, 20) While ( p2++ p1++) ("%sn",p2) )
( p1 "Name" p2 p2 (Char ) Malloc (20) Memset (p2, 0, 20) While ( p2++ p1++) ("%sn",p2) )
What will print out? main() { char *p1=name; char *p2; p2=(char*)malloc(20); memset (p2, 0, 20); while(*p2++ = *p1++); printf(%sn,p2); } Answer:empty string.
2.
#define swap(a,b) a=a+b;b=a-b;a=a-b; void main() { int x=5, y=10; swap (x,y);
printf(%d %dn,x,y); swap2(x,y); printf(%d %dn,x,y); } int swap2(int a, int b) { int temp; temp=a; b=a; a=temp; return 0; }
Answer: 10, 5 10, 5 5. What will be printed as the result of the operation below:
main() {
char *p1; char *p2; p1=(char *)malloc(25); p2=(char *)malloc(25); strcpy(p1,Cisco); strcpy(p2,systems); strcat(p1,p2); printf(%s,p1); }
Answer: Ciscosystems 8. The following variable is available in file1.c, who can access it?:
9. static int average;
Answer: all the functions in the file1.c can access the variable. 10. WHat will be the result of the following code?
void main() { int x=10; x++; changevalue(x); x++; modifyvalue(); printf("First output:%dn",x); x++; changevalue(x); printf("Second output:%dn",x); modifyvalue(); printf("Third output:%dn",x); }
Answer: 12 , 13 , 13 12. What will be printed as the result of the operation below:
Q14
Whenever an array name appears in an expression such as ?array as an operand of the sizeof operator ?array as an operand of & operator ?array as a string literal initializer for a character array Then the compiler does not implicitly generate the address of the address of the first element of an array Q-15 What is the difference between null array and an empty array? Null array :- Arry is decleared but it's size is not define as : int array[] Empty array :- Array is decleared but it has no elements as: int a[12]={} Q-16 Can the sizeof operator be used to tell the size of an array passed to a
function?
YES.You can tell how large the array is. However, you cannot determine how many elements have been copied into it so far. EX: Take for instance the following code. int testList[5]; int sizeofList = sizeof(testList); printf("sizeof list = %d", sizeofList); The output will be: "sizeof list = 20" Meaning that 4(int) * 5 = 20. Q 17 Can we add the name as "Mixed Arrays" instead of the name given as
"Structures" in c?why the name structure is given?
Array is collection of variables of same data type. structure is collection of variables of same or dissimilar data types but logically related. structure is like a user defined data type arrays are derived data type. The name "Mixed Array" does'nt make sense.
Q-18
Does mentioning the array name gives the base address in all the integers?
Yes, the name of the array always stores the starting address of the array. Hence if we print the name of the array, it will print the base address. But also remember, that address while printed using printf() function should use %u and not %d, as addresses are not integers, but unsigned integers. Hence %u Q19 Can the size of an array be declared at runtime? No. In an array declaration, the size must be known at compile time. You can?t specify a size that?s known only at runtime. For example, if i is a variable, you can?t write code like this: char array[i]; /* not valid C */
Some languages provide this latitude. C doesn?t. If it did, the stack would be more complicated, function calls would be more expensive, and programs would run a lot slower. If you know that you have an array but you won?t know until runtime how big it will be, declare a pointer to it and use malloc() or calloc() to allocate the array from the heap. Q 20 When does the compiler not implicitly generate the address of the first element
of an array?
Whenever an array name appears in an expression such as ?array as an operand of the sizeof operator ?array as an operand of & operator ?array as a string literal initializer for a character array Then the compiler does not implicitly generate the address of the address of the first element of an array.