I have an query regarding C program…Its how we can execute a piece of code before execution of code written in main function? e.g if in main() we have added two numbers and outside main() we have subtracted two numbers ,so I want that the output of subtractibg two numbers should come first than adding two numbers…
What’s wrong with the call fopen(â€Âc:\newdir\file.datâ€Â, “râ€Â)?
In C strings, \ should be used as \\ escape sequence.
Therefore the right way of doing this is fopen(â€Âc:\\newdir\\file.datâ€Â, “râ€Â)
Ans:-If you want to return multiple values from a function better to pass value by reference.So that whatever you made changes to variables it’ll be reflected.You don’t have to return anything.
ANSI C defines a macro __FILE__ which containes the filename which is been compiled.
The simplest solution will be
FILE *fp = fopen(__FILE__,”r”);For more on macros refer to K&R C programming language….
71 Comments on Tricky C questions
Such prog. are called “Quines”. Following is a Basic Quine..
//Quine By St0le!
#include
char *p=”#include %c%c char *p=%c%s%c; %c%c void main(){%c%c printf(p,13,10,34,p,34,13,10,13,10,13,10,13,10); %c%c }”;
void main(){
printf(p,13,10,34,p,34,13,10,13,10,13,10,13,10);
}
I have an query regarding C program…Its how we can execute a piece of code before execution of code written in main function? e.g if in main() we have added two numbers and outside main() we have subtracted two numbers ,so I want that the output of subtractibg two numbers should come first than adding two numbers…
use #pragma
Write a prgm that prints its own output.
#include
#include
int main(){
FILE *fp;
char c;
fp=fopen(”file2.cpp”,”r”);
while((c=getc(fp))!=EOF)
{printf(”%c”,c);}
fclose(fp);
getch();
return 0;
}
Will this program is going to execute ?
#include
123;
int main()
{
34;
return 0;
}
if yes why ?
if no why ?
please reply fast
without using third variable how to swap two variable?
ans:
a=a^b;
b=a^b;
a=a^b;
Advantage : with this method we can avoid overflow.
better yet, use this
a^=b^=a^=b;
To print a semicolon without using semicolon else where in the program
#include
void main()
{
if(printf(”;”))
{}
}
What’s wrong with the call
fopen(â€Âc:\newdir\file.datâ€Â, “râ€Â)?
In C strings, \ should be used as \\ escape sequence.
Therefore the right way of doing this isÂÂ
fopen(â€Âc:\\newdir\\file.datâ€Â, “râ€Â)
we can convert decimal  to hexadecimal as follows: printf("%ox "23); or printf("%OX",23); there also method for finding dec to hex,oct.
To add two numbers without using "+"
int add(int a, int b)
{
   int sum, carry;
   sum = a ^ b;
   carry = a & b;
   while (carry != 0)
   {
       carry <<= 1;
       a = sum;
       b = carry;
       sum = a ^ b;
       carry = a & b;
   }
   return sum;
}
How can I return multiple values from a function?
Ans:-If you want to return multiple values from a function better to pass value by reference.So that whatever you made changes to variables it’ll be reflected.You don’t have to return anything.
void main()
{
id *vptr = (void *) malloc(sizeof(void));
vptr++;
}
what is the out put?
we can print semocolon by the following program
#include
main()
{
if(printf(”%c”,59))
{
}
}
jst use the ascii value of it
ANSI C defines a macro __FILE__ which containes the filename which is been compiled.
The simplest solution will be
FILE *fp = fopen(__FILE__,”r”);For more on macros refer to K&R C programming language….
How to print your program code as output??
#include
main()
{
FILE *p;
char c;
p=fopen(”try.c”,”r”);
while((c=getc(p))!=EOF)
printf(”%c”,c);
}
try.c is my source file
I just want to say that your answer given with the question No. 50 is wrong………..
The Right Answer is………
-3 2 2 0
Please check the answers before publishing it………….
how to swap two nos without using a third variable?
#include
#include
int a=5;
void fun(int);
void main()
{
clrscr();
int a=5,b=6;
printf(”a=%d b=%d”, a, b );
a=b;
printf(”a=%d”,a);
fun(b);
getch();
}
void fun(int b)
{
b=a;
printf(”b=%d”,b);
}
For question 67, answer is wrongly specified. Actual answer is
int a = 5, b = 10;
a = a + b; // so a = 15
b = a - b; // So b = 5
a = a - b; // now a = 10, values swapped
Q50 & Q66 is Wrong Answer.
Correct answer is -1,1,1,0
request to publisher,before published pls ensure that ur answer is correct..
dont send wrong answer.
Ans: 50,66 and 69 are incorrect…
Its Compiler dependent and Machine dependent to implement it..
Output vary from one to another..
Check with turboc/gcc/dev-c and so on..
Correct ans: Undefined Behaviour…
If it is incorrect,leave me message
#include
char * fileName = “C:\\work\\c\\test.c”;
char myString[100];
int main(){
FILE * pFile = fopen(fileName, “r”);
while(!feof(pFile)) {
fgets(&myString[0], 100, pFile);
printf(myString);
}
return 0;
}