C Adv Notes
C Adv Notes
%2d means:
Suppose you are printing n= 18999 with printf("%2d",n) then it will reserve the output console with
2 character and if the number of digits of n are larger than the specifier then it will print with no
spaces, now suppose you print with %6d then what will happen the number of character it will
reserve i.e. 6 now number of digits in n is 5 so it will print after 1 space character on the console. In
%8d, leaving 3 spaces and then it prints number n.
printf("%d",1234);
printf("%3d",1234);
printf("%6d",1234);
printf("%-6d",1234);
printf("%06d",1234);
Output :
1234
1234
--1234
1234--
001234
%6.2f means:
that it will reserve the output console to 6 characters and rounding off to 2 decimal places.
n=1.9890778;
n=7687.87686;
then it prints 7687.88 with three space in the beginning because total character to the output
console is 7 while we have reserved 10 characters so to compensate there will be 3 space in the
beginning.
printf("%.2f",1.9890778);
printf("%.3f",1.9890778);
printf("%.4f",1.9890778);
printf("%6.2f",1.9890778);
printf("%-6.2f",1.9890778);
printf("%06.2f",1.9890778);
Output :
1.99
1.989
1.9891
--1.99
1.99--
To print \ symbol :
printf("\\");
To print % symbol:
printf("%%");
printf("\"");
The order of evaluation is from right to left. This is same as in approach (c) but the three statements
are compounded into one statement.
2)a=(a+b)–(b=a);
In the above axample, parenthesis operator enjoys the highest priority & the order of evaluation is
from left to right. Hence (a+b) is evaluated first and replaced with 15. Then (b=a) is evaluated and the
value of a is assigned to b, which is 5. Finally a is replaced with 15-5, i.e. 10. Now the two numbers
are swapped
3)a=a^b;
b=b^a;
a=a^b;
4)a=a+b;
b=a-b;
a=a-b;
5)c=a;
a=b;
b=c;
#include<stdio.h>
int main()
printf("%d",((d_type *)0)+1);
Whenever we typecast any constant, it converts the constants into a base address of specified data
type and as we know (address+1) will always return the next address of its type or we can say the
address of next block of memory of that type, so accordingly if 1 will be added with the address ‘0’,
then it will return the size of any data type in bytes. For example 1 for char, 2 for short int, 4 for long
int or float and so on.
Explanation:
By using the &x, we get the base address of the variable x and by adding 1 to it we get the base
address of next short int type. Hence the resulting address of (&x+1) will be 2 bytes more than the
base address of the variable x. But if we just display the difference between the base address of x and
the incremented address, then the difference will be ‘1’, means “1 block of short int type has been
added” but we need the result of size of variable in terms of bytes not in terms of blocks. This can be
achieved if we typecast the address into char *, because the address of char data type will always be
in block of one byte, hence if the difference between the base address of x and the incremented
address is displayed in terms of char type, then the difference will be displayed as 2, because the
difference is actually 2 blocks or 2 bytes in terms of char type representation
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
if(!(n&&(n&(n-1))))
printf("YES");
else
printf("NO");
qsort() Function:
The qsort() function in C is available in stdlib.h library. As the name indicates, qsort() function
internally uses quick sort algorithm and is used to sort array.
Syntax:
void qsort (void *arr, size_t arrSize, size_t elementSize, int (*comparator)(const void *,const void *));
The return value <0 when the first argument pointed value is smaller than second argument
pointed value.
The return value is >0 when the first argument pointed value is greater than the second
argument pointed value.
The return value is =0 when the first argument pointed value is equivalent to the second
argument pointed value.
Example:
#include <stdio.h>
#include <stdlib.h>
int main ()
int i;
return(0);
#include<stdio.h>
int main()
float x = 0.1;
if (x == 0.1)
printf("IF");
else if (x == 0.1f)
printf("ELSE IF");
else
printf("ELSE");
The output of above program is “ELSE IF” which means the expression “x == 0.1” returns false and
expression “x == 0.1f” returns true.
Let consider the following program to understand the reason behind the above output.
#include<stdio.h>
int main()
float x = 0.1;
The output of above program is "4 8 4" on a typical C compiler. It actually prints size of float, size of
double and size of float.
The values used in an expression are considered as double (double precision floating point format)
unless a ‘f’ is specified at the end. So the expression “x==0.1” has a double on right side and float
which are stored in a single precision floating point format on left side. In such situations, float is
promoted to double (see this). The double precision format uses more bits for precision than single
precision format. The binary equivalent of 0.110 can be written as (0.00011001100110011…)2 which
goes up to infinity(See this article to know more about conversion). Since the precision of float is less
than the double therefore after a certain point(23 in float and 52 in double) it would truncate the result.
Hence, after promotion of float into double(at the time of comparison) compiler will pad the remaining
bits with zeroes. Hence, we get the different result in which decimal equivalent of both would be
different. For instance,
Hence we can see the result of both equations are different. Therefore 'if' statement can never be
executed. Note that the promotion of float to double can only cause mismatch when a value (like 0.1)
uses more precision bits than the bits of single precision. For example, the following C program prints
“IF”.
#include<stdio.h>
int main()
float x = 0.5;
if (x == 0.5)
printf("IF");
else if (x == 0.5f)
printf("ELSE IF");
else
printf("ELSE");
Ouput: IF
Here binary equivalent of 0.510 is (0.100000…)2 (No precision will be lost in both float and double
type). Therefore if compiler pad the extra zeroes at the time of promotion then we would get the same
result in the decimal equivalent of both left and right side in comparison(x == 0.5).
memset() is used to fill a block of memory with a particular value. The syntax of memset() function is
as follows :