Egs on unsigned integer
#include
main(){
unsigned int i;
for(i=1;i>-2;i–)
printf(“c aptitude”);
}
Explanation:
i is an unsigned integer. It is compared with a signed value. Since the both
types doesn’t match, signed is promoted to unsigned value. The unsigned
equivalent of -2 is a huge value so condition becomes false and control
comes out of the loop.
**#include
int main()
{
unsigned giveit=-1;
[Read More]
Extern keyword
main()
{
extern int i;
i=20;
printf("%d”,sizeof(i));
}
Answer:
Linker error: undefined symbol ‘_i’.
Explanation:
extern declaration specifies that the variable i is defined somewhere else.
The compiler passes the external variable to be resolved by the linker. So
compiler doesn’t find an error. During linking the linker searches for the
definition of i. Since it is not found the linker flags an error.
2)
main()
{
[Read More]
Using ## and # with define.
Stringize(#)
#define message\_for(a, b) \\
printf(#a " and " #b)
int main()
{
message\_for(Carole, Debra);
}
**Token pasting (##)**
#define f(g,g2) g##g2
main()
{
int var12=100;
printf("%d",f(var,12));
}
**Answer:**
100Return values of some functions in c
scanf
**scanf("%d” , & i); //10 is given as input
**
Scanf returns number of items successfully read and not 1/0. Here 10 is given as input which should have been scanned successfully. So number of items read is 1.
printf
Upon a successful return, the printf() function returns the number of characters printed (not including the trailing ‘\0’ used to end output to strings). If the output was truncated due to this limit then the return value is the number of characters (not including the trailing ‘\0’) which would have been written to the final string if enough space had been available.
[Read More]
The preprocessor directives can be redefined anywhere in the program
#include
#define a 10
main()
{
#define a 50
printf("%d”,a);
}
Answer:
50
Explanation:
The preprocessor directives can be redefined anywhere in the program. So
the most recently assigned value will be taken.
What is the difference between goto and longjmp() and setjmp()?
A goto statement implements a local jump of program execution, and the longjmp() and setjmp() functions implement a nonlocal, or far, jump of program execution. Generally, a jump in execution of any kind should be avoided because it is not considered good programming practice to use such statements as goto and longjmp in your program.
A goto statement simply bypasses code in your program and jumps to a predefined position. To use the goto statement, you give it a labeled position to jump to.
[Read More]
Given only putchar (no sprintf, itoa, etc.) write a routine putlong that prints out an unsigned long in decimal
void putlong(unsigned long x)
{
// we know that 32 bits can have 10 digits. 2^32 = 4294967296
for (unsigned long y = 1000000000; y > 0; y /= 10) {
putchar( (x / y) + '0');
x = x % y;
}
}
Some macros
#define SQR(x) ((x)*(x))
#define MAX(a,b) ( (a) > (b) ? (a) : (b) )
#define ISLP(y) ( (y % 400 == 0) || (y %100 != 0 && y%4 == 0) )
#define ISLOWER(a) (a>=97 && a<=127)
#define TOLOWER(a) (a - 32)
Using define without assigning value
#include
#define NO
#define YES
int main()
{
int i = 5,j;
if (i>5) j=YES;
else j=NO;
printf("%d”,j);
}
Output: Expression syntax in function main
Explanation: Because when assigning j with YES or NO we don’t know what is the value of YES or NO.
Stack ADT
Stacks are an elementary Data Structure where all interaction with the data is done through the looking at the first element. Stacks are last in first out (LIFO) data structures. It is named on the basis of how the elements are removed from it. Opposite to stack is queue, in which the last element is removed, the element which was most old item, and hence it is FIFO or first in first out data structure.
[Read More]