Beruflich Dokumente
Kultur Dokumente
1 .mod(
fmod(x,y) - Calculates x modulo y, the remainder of x/y.
This function is the same as the modulus operator. But fmod() performs
Option A: assignment statements are always return in paranthesis in the case of conditional operator.
It should be a>b? (c=30):(c=40);
4.
Which of the following is the correct order if calling functions in the below code?
a = f1(23, 14) * f2(12/4) + f3();
2.
What will be the output of the program in 16 bit platform (Turbo C under DOS)?
#include<stdio.h>
int main()
{
extern int i;
i = 20;
printf("%d\n", sizeof(i));
return 0;
}
Linker Error : Undefined symbol 'i'
The statement extern int i specifies to the compiler that the memory for 'i' is allocated in some
other program and that address will be given to the current program at the time of linking. But linker
finds that no other variable of name 'i' is available in any other program with memory space
allocated for it. Hence a linker error has
void modify(struct emp*);//ERROR SICE EMP NOT DEFINED TILL NOWAND WE R MAKING USE OF
struct emp
{
char name[20];
int age;
float sal;
};
struct emp e = {"Tiger",45};
CASE 2:
struct emp e;
e.name = "Suresh";
e.age = 25;
We cannot assign a string to a struct variable like e.name = "Suresh"; in C.
We have to use strcpy(char *dest, const char *source) function to assign a string.
Ex: strcpy(e.name, "Suresh");
1. strnset()
char *strnset(char *s, int ch, size_t n); Sets the first n characters of s toch
printf("%f, ", f); Here '%f' specifies the "Decimal Floating Point" format. So, it prints the 43.20
as 43.200001.
printf("%g, ", f); Here '%g' "Use the shorter of %e or %f". So, it prints the 43.20 as 43.2.
note:
in post increment:always it has high precedence but if other operator r there it will
show its action a the last
ex. Return (n--)//n=5,,,,,,now it return the value 5 to the function then decrement 5
to 4
4.
#include<stdio.h>
int reverse(int);
int main()
{
int no=5;
reverse(no);
return 0;
}
int reverse(int no)
{
if(no == 0)
return 0;
else
printf("%d,", no);
reverse (no--);
}
A.
Print 5, 4, 3, 2, 1
B.
C.
Print 5, 4, 3, 2, 1, 0
D.
Print 1, 2, 3, 4, 5
Infinite loop
Answer: Option D
Explanation:
Step 1: int no=5; The variable no is declared as integer type and initialized to 5.
Step 2: reverse(no); becomes reverse(5); It calls the function reverse() with '5' as
parameter.
The function reverse accept an integer number 5 and it returns '0'(zero) if(5 == 0) if the given
number is '0'(zero) or else printf("%d,", no); it prints that number 5 and calls the
function reverse(5);.
The function runs infinetely because the there is a post-decrement operator is used. It will not
decrease the value of 'n' before calling the reverse() function. So, it
callsreverse(5) infinitely.
Note: If we use pre-decrement operator like reverse(--n), then the output will be 5, 4, 3,
2, 1. Because before calling the function, it decrements the value of 'n'.
*very imp:note :while solving functions ques alwyasmake sure that before using the
function their prototype is defined
*6.68 is double.
6.68L is long double constant.
6.68f is float constant.
6.68LF is not allowed in c.
.if(0.7 > a) here a is a float variable and 0.7 is a double constant. The double constant 0.7 is
greater than the float variable
*floata=0.7;
printf("%.10f%.10f\n",0.7,a);
0.7000000000 0.6999999881//double is always greater than float
float a=0.7;
printf("%.10f %.7f\n",0.7, a);output 0.70000000000.7000000//allthevaueshave
someprecisionvalurabovewhichtheywillshowthevaluesmallerthanexpected
whetheritisdoubleorfloat.butprecisonvaluroffloatis7approxabovewhichits
valurwilldecreasefromactualvalueexpected
What will be the output of the program if the array begins at 65472 and each integer occupies 2
bytes?
4,4
What will be the output of the program?
#include<stdio.h>
int main()
{
int arr[3] = {2, 3, 4};
char *p;
13.
p = arr;
p = (char*)((int*)(p));
printf("%d, ", *p);
p = (int*)(p+1);
printf("%d", *p);
return 0;
}
[A].
2, 3
[B].
2, 0
printf("%d,%d\n",
sizeof(NULL), sizeof(""));
return 0;
}
[C].
2, Garbage value
[D].
0, 0
p = (char*)((int*)(p));
// till now the pointer p is type casted to store the variable
of type character.
printf("%d, ", *p); // %d means integer value so value at
first address i.e. 2 will be printed.
p = (int*)(p+1); // here p is still of type character as type
casted in step 1 so p(i.e address) and plus 1 will increase
only by one byte so
printf("Computer","Programming"); Computer
B.
Note: #if checks for the value of the symbol, while #ifdef checks the existence of the
symbol (regardless of its value). Each #if directive in a source file must be matched by a
closing #endif directive. Any number of #elif directives can appear between
the #if and #endif directives, but at most one #else directive is allowed.
The #else directive, if present, must be the last directive before #endif. The conditional
macro #if must have an #endif. In this program there is no #endifstatement written.
The #ifdef identifier statement is equivalent to #if 1 when identifier has been defined, and
it is equivalent to #if 0 when identifier has not been defined or has been undefined with
the #undef directive. These directives check only for the presence or absence of identifiers
defined with #define, not for identifiers declared in the C or C++ source code.
Note: Macro names should only consist of alphanumeric characters and underscores, i.e. 'az', 'A-Z', '0-9', and '_', and the first character should not be a digit. Some preprocessors also
permit the dollar sign character '$', but you shouldn't use it; unfortunately I can't quote the C
standard since I don't have a copy of it.
Fill with 1s in the left side for right shift for negative numbers.
1.
2.
itoa(num1,str1,10);
itoa (no,buff,2);// itoa() takes the integer input value input and converts it to a number in base
radix. The resulting number a sequence of base-radix digits.
/*10radixvalue*/
2. ltoa() converts a long to a string.
3. ultoa() converts an unsigned long to a string.
4. sprintf() sends formatted output to a string, so it can be used to convert any type of values to
string type
fflush(FilePointer);
fflush(NULL); flushes all streams.
printf("%f,%f", floor(i), ceil(i));//IT WORKS WELL WITH FLOAT IDENTIFIER
Both ceil() and floor() return the integer found as a double. double to int it returns '0'.
So, the output is '2.000000, 0'.
float i = 2.3;
printf("%d\n", floor(i));
printf("%f", ceil(i));
return 0;
}
OUTPUT:
0
3.000000
Directly ceil and floor with int format specifier will result in zero.
memcmp(dest, src, 2)// Explanation:
memcmp compares the first 2 bytes of the blocks dest and src as unsigned chars. When comparing
the array dest and srcas unsigned chars, the first 2 bytes are same in both
variables.so memcmp returns '0'.
void *memccpy(void *dest, const void *src, int c, size_t n); : Copies a block of n
bytes from src to dest
p = (char*) memccpy(str2, str1, 'i', strlen(str1));// With memccpy(), the copying stops as
soon as either of the following occurs:
=> the character 'i' is first copied into str2
=> n bytes have been copied into str2
printf("%.#s%2s", str, str);// It prints out a minimum of 2 characters of the character string,
str. I don't see an immediate way of limiting the number of characters you want printed if that's what
your looking fo
You can terminate the string yourself by sticking the '\0' in there yourself:
char s[10];
char foo = "My hovercraft is full of eels."; // more than 10 chars
strncpy(s, foo, 9); // only copy 9 chars into positions 0-8
s[9] = '\0';
// position 9 gets the terminator
scanf() or atoi() function can be used to convert a string like "436" in to integer
. number = atoi("string");
int main()
{
int (*p)[4];
p = (int (*) [4])malloc(3*sizeof(*p));
#include<stdio.h>
#include<stdlib.h>
union employee
{
char name[15];
int age;
float salary;
};
const union employee e1;
int main()
{
strcpy(e1.name, "K");
printf("%s%d%f", e1.name, e1.age, e1.salary);
return 0;
}
OUTPUT:
K750.000000
Note: in case of constant array modification can ony be done if the is a pointer who tries to do.
A const variable can be indirectly modified by a pointer.
const int x;// Error: const variable have been initialised when declared.
x=128;
in short if substitute name is in small latters then just read it as normal statement
wheres for actual definition of typedef to hold it should contain leeter in CAPITAL for
alais name
What will be the output of the program ?
#include<stdio.h>
int main()
{
struct value
{
int bit1:1;
int bit3:4;
int bit4:4;//IF BIT FIELD IS LARGE EX.40 IT WIL DISPLAY ERROR
}bit={1, 2, 13};
printf("%d,%d,%d\n", bit.bit1, bit.bit3, bit.bit4);
return 0;
}
A.
1, 2, 13
B.
1, 4, 4
C.
-1, 2, -3
D.
Answer: Option C
Explanation:
Note the below statement inside the struct:
1.
2.
ENUM PROPERTIES: enum assigns to it's variable component sequential integer values.
Enum values are constant.r++ not alloowed
Note:
char arr[]="\0";
if(printf("%s",arr))
printf("not empty");
else
printf("empty")
char arr[]="\0";
Here, the char array arr has two elements, both of them are \0. When you use:
printf("%s",arr)
%s in format specifier tells printf to look for a string. And printf found it, but it stops printing
18.
What will be the output of the program assuming that the array begins at location 1002?
#include<stdio.h>
int main()
{
int a[2][3][4] = { {1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 1, 2},
{2, 1, 4, 7, 6, 7, 8, 9, 0, 0, 0, 0} };
printf("%u,%u,%u,%d\n", a, *a, **a, ***a);
return 0;
}
[A].
[B].
[C].
[D].
Error
#include<stdio.h>
int main()
{
int a=10, *j;
void *k;
j=k=&a;
j++;
k++;
printf("%u%u\n", j, k);
return 0;
}
A.
Yes
B.
No
Answer: Option B
Explanation:
Error in statement k++. We cannot perform arithmetic on void pointers.
Note:
There is compiler error in line "printf("%d", *ptr);". void * type pointers cannot be de-referenced. We
must type cast them before de-referencing. The following program works fine and prints 12.
Note: In C, when an intger value is compared with an unsigned it, the int is promoted to unsigned.
Negative numbers are stored in 2's complement form and unsigned value of the 2's complement form is
much higher than the sizeof int.
Consider the following program, where are i, j and k are stored in memory?
int i;
int main()
{
int j;
int *k = (int *) malloc (sizeof(int));
}
NOTE:
i is global variable and it is uninitialized so it is stored on BSS part of Data Segment
(http://en.wikipedia.org/wiki/.bss) j is local in main() so it is stored in stack frame
(http://en.wikipedia.org/wiki/Call_stack) k is dynamically allocated so it is stored on Heap Segment. See
int a[][3] = {1, 2, 3, 4, 5, 6};
int (*ptr)[3] = a;
printf("%d %d ", (*ptr)[1], (*ptr)[2]);
output : 2 3
static int y = x;
In, C static variables can only be initialized using constant literals.
Static variable will exist till the program exit ,but its scope will be within function wher e it is defined
Points:
On extern
1. Declaration can be done any number of times but definition only once.
2. extern keyword is used to extend the visibility of variables/functions().
3. Since functions are visible through out the program by default. The use of extern is not needed
in function declaration/definition. Its use is redundant.
4. When extern is used with a variable, its only declared not defined.
5. As an exception, when an extern variable is declared with initialization, it is taken as definition
of the variable as well.
2.
3.
http://www.geeksforgeeks.org/understanding-extern-keyword-in-c/
http://www.geeksforgeeks.org/understanding-register-keyword/
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
#include <stdio.h>
char *fun()
{
static char arr[1024];
return arr;
}
int main()
{
char *str = "geeksforgeeks";
strcpy(fun(), str);
str = fun();
15.
strcpy(str, "geeksquiz");
16.
printf("%s", fun());
17.
return 0;
}
Question 19 Explanation:
Note that arr[] is static in fun() so no problems of returning address, arr[] will stay there even after the fun()
returns and all calls to fun() share the same arr[].
strcpy(fun(), str); // Copies "geeksforgeeks" to arr[]
str = fun();
Question 5 Explanation:
Note that str_ref is a local variable to fun(). When we do str_ref++, it only changes the local variable
str_ref. We can change str pointer using dereference operator *. For example, the following program prints
"eeksQuiz"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *str = (void *)malloc(100*sizeof(char));
strcpy(str, "GeeksQuiz");
fun(&str);
puts(str);
free(str);
return 0;
}
Question 9 Explanation:
In C, array parameters are treated as pointers (See http://www.geeksforgeeks.org/why-c-treats-arrayparameters-as-pointers/ for details). So the variable arr represents an array in main(), but a pointer in
fun().
What is the output of the following C code? Assume that the address of x is 2000 (in decimal) and an
integer requires four bytes of memory.
#include <stdio.h>
int main()
{
unsigned int x[4][3] = {{1, 2, 3}, {4, 5, 6},
{7, 8, 9}, {10, 11, 12}};
printf("%u, %u, %u", x+3, *(x+3), *(x+2)+3);
}
x= 2000
int * g (void)
int x = 10;
return (&x);
}
pointer variable x is a local variable to g(), and g() returns pointer to this variable. x may vanish after
g() has returned as x exists on stack. So, &x may become invalid
while (m != n)
{
if(m>n)
m = m - n;
else
n = n - m;
}
printf("%d", n);
}
Run on IDE
The program computes (GATE CS 2004)
a) x + y using repeated subtraction
b) x mod y using repeated subtraction
c) the greatest common divisor of x and y
d) the least common multiple of x and y
Answer(c)
This is an implementation of Euclids
x=5;
P(&x);
print(x);
getchar();
}
Run on IDE
The output of this program is (GATE CS 2003)
a) 1276
b) 22 12 11
c) 14 6 6
d) 766
Answer (a)
Note that main() and Q() are accessing the global variable x. Inside P(), pointer variable y also holds
address of global variable x, but x in P() is its Ps own local variable.
int f1(int n)
{
if(n == 0 || n == 1)
return n;
else
return (2*f1(n-1) + 3*f1(n-2));
}
f1(8) a
(A) 1661 and
(B) 59 and
(C) 1640 an
(D)
Both functions perform same operation, so output is same, means either (B) or (C) is correct.
f1(2) = 2*f1(1) + 3*f1(0) = 2
f1(3) = 2*f1(2) + 3*f1(1) = 2*2 + 3*1 = 7
f1(4) = 2*f1(3) + 3*f1(2) = 2*7 + 3*2 = 20
f1(5) = 2*f1(4) + 3*f1(3) = 2*20 + 3*7 = 40 + 21 = 61
We can skip after this as the only remaining choice is (C)
f1(6) = 2*f1(5) + 3*f1(4) = 2*61 + 3*20 = 122 + 60 = 182
f1(7) = 2*f1(6) + 3*f1(5) = 2*182 + 3*61 = 364 + 183 = 547
f1(8) = 2*f1(7) + 3*f1(6) = 2*547 + 3*182 = 1094 + 546 = 1640
char -- This is the character to be written. This is passed as its int promotion.
int getchar(void)
Return Value
This function returns the character written as an unsigned char cast to an int or EOF on error.
2. What is the value printed by the following C program?
#include<stdio.h>
int f(int *a, int n)
{
if(n <= 0) return 0;
else if(*a % 2 == 0) return *a + f(a+1, n-1);
else return *a - f(a+1, n-1);
}
int main()
{
int a[] = {12, 7, 13, 4, 11, 6};
printf("%d", f(a, 6));
getchar();
return 0;
}
Run on IDE
(A) -9
(B) 5
(C) 15
(D) 19
Answer (C)
f() is a recursive function which adds f(a+1, n-1) to *a if *a is even. If *a is odd then f() subtracts f(a+1, n-1)
from *a. See below recursion tree for execution of f(a, 6).
.
}
int main()
{
printf("%s", getString());
getchar();
}
Run on IDE
Output: Some garbage value
The above program doesnt work because array variables are stored in Stack Section. So, when getString
returns values at str are deleted and str becomes dangling pointer