iGuest 3 Report post Posted September 1, 2006 (edited) try to asnwers to these questions 1. void main() { printf(â%dâ,(float)3/2); } 2.void main() { char *s=âHello Worldâ; printf(â%câ,s); } 3.#include <stdio.h>int main(){char *str = "Hello, world";int i = sizeof(str);for(; i >= 0; i--)printf("%c" , str[i]);}4.#define MAX(A , B) ( A < B ? B : A ) #include <stdio.h>int main(){int a = MAX( 4+2 , 3*2); printf(" %d " , a);}some more1.#include <stdio.h>int main(){char *str = "Hello, world";printf("%5s" , str);}2.#include <stdio.h>void temp();void temp(void);int main(){temp();}void temp(){printf("C is exciting!");}3.main(){int x=0,y=1;if(x=y) y= 7;elsey=2;value of y?}4.main(){int a[]={1,2,9,8,6,3,5,7,8,9};int *p=a+1;int *q=a+6;printf("\n%d",q-p);} Edited September 2, 2006 by Jeigh (see edit history) Share this post Link to post Share on other sites
tommydanger 0 Report post Posted September 1, 2006 try to asnwers to these questions 1. void main() { printf(“%d”,(float)3/2); } 1 2. void main() { char *s=”Hello World”; printf(“%c”,s); } 3. %c keyword outputs a char H I think you could also write printf(“%c”,s[0]); as it is a pointer ? #include <stdio.h> int main() { char *str = "Hello, world"; int i = sizeof(str); for( ; i >= 0 ; i--) printf("%c" , str); } dlrow ,olleH 4. #define MAX(A , ( A < B ? B : A ) #include <stdio.h> int main() { int a = MAX( 4+2 , 3*2) ; printf(" %d " , a); } 6 (4+2) some more 1. #include <stdio.h> int main() { char *str = "Hello, world" ; printf("%5s" , str); } Hello, world ? 2. #include <stdio.h> void temp(); void temp(void); int main() { temp(); } void temp() { printf("C is exciting!"); } 'C is exiting! 3. main(){ int x=0,y=1; if(x=y) y= 7; else y=2; value of y? } shouldn't it be if(x==y)? Everytime I forgot the second '=' in a if statement it got unexpected behaviours 4. main(){ int a[]={1,2,9,8,6,3,5,7,8,9}; int *p=a+1; int *q=a+6; printf("\n%d",q-p); } 3 I'm sceptical about some of my answers, but it's always good to play compiler yourself, to sharpen your skills Share this post Link to post Share on other sites
DrK3055A 0 Report post Posted October 18, 2006 1.void main() { printf(â%dâ,(float)3/2); } Assuming that the code compiles to single precision floats, the 1.5 represents 4 bytes data in intel notation (least significant byte at first):00000000 00000000 11000000 00111111If i think of 16 integers, it will print just "0" (the 2 first bytes).To fix this, use "%f" for the format string.2.void main() { char *s=âHello Worldâ; printf(â%câ,s); } *s is a pointer, so the character printed will depend on the value of the address in that pointer (its least significant byte)To fix this, use "%s" for the format string.3.#include <stdio.h>int main(){char *str = "Hello, world";int i = sizeof(str);for(; i >= 0; i--)printf("%c" , str[i]);} sizeof(str) will return 4 (the size of a 32bit pointer in bytes), so the result will be:olleHTo fix this, use strlen(str) instead of sizeof(str)4.#define MAX(A , B) ( A < B ? B : A ) #include <stdio.h>int main(){int a = MAX( 4+2 , 3*2); printf(" %d " , a);} This is suposed to fail because a variable cannot be initialized by a macro (asuming ANSI C) . But it depends up on the compiler if this is valid or not (C++ should be fine with this). 1.#include <stdio.h>int main(){char *str = "Hello, world";printf("%5s" , str);} Maybe the programmer wanted the code to print "Hello", that is, printing up to the fifth character of str. The result is that str, as is longer that 5 chars, it will be printed as it, the whole string.I'd correct the code this way:#include <stdio.h>int main(){char *str = "Hello, world",str2[6]="";strncat(str2,str,5);printf("%s" , str2);} #include <stdio.h>void temp();void temp(void);int main(){temp();}void temp(){printf("C is exciting!");} This is suposed to fail because of multiple definition for the prototype of temp. Besides that, one of the prototypes doesn't tell the compiler the type of input parameters (in this case, this is "void").To fix this, remove the line "void temp();"NOTE: Some compilers will forgive this, asuming that both prototypes are equivalent. The fact is that when in the declaration of the function, you specify "void", then the calls to that function must be without any parameters in a mandatory fashion. If you don't specify void, function calls can hold parameters, but the function doesn't take care of them.3.main(){int x=0,y=1;if(x=y) y= 7;elsey=2;value of y?} This is quite simple. x was initially 0 (false), but the expression inside the brackets is not a comparison, but an asignment. After the asignment, x becomes 1 (true) so the "if statement" is executed, and y is assigned to value 7.to correct this, just replace the "=" asignment operator with the "==" comparison operator.main(){int a[]={1,2,9,8,6,3,5,7,8,9};int *p=a+1;int *q=a+6;printf("\n%d",q-p);} p and q are pointers, but a substraction of 2 pointers will lead us to the difference of their offsets. It will print a+6-a-1=5.If you wanted to print the result of a[6]-a[1], you would use printf("\n%d",(*q)-(*p)); and it would print 3. Share this post Link to post Share on other sites
nikhil1405241522 0 Report post Posted October 19, 2006 (edited) The computer OS calls the program and it always expects a return value ,hence return type of main should always be int as it makes the software more stable . Returning 1 to the OS means that every thing has gone correct returning 0 means something has gone wrong ... but if there is no return (void) it may cause problem for the system ... Try out these codes .Tell their outputs .... #include<stdio.h> int main() { printf(" %d",printf("1 2")); return(1); } #include<stdio.h> int main() { int i=10; i=i<2; printf("%d",i); } 2. Write a program to print semicolon ";" without the use of semicolon ";" . It should not be there in the C program anywhere. Edited October 19, 2006 by nikhil (see edit history) Share this post Link to post Share on other sites
DrK3055A 0 Report post Posted October 20, 2006 The computer OS calls the program and it always expects a return value ,hence return type of main should always be int as it makes the software more stable . Returning 1 to the OS means that every thing has gone correct returning 0 means something has gone wrong ... but if there is no return (void) it may cause problem for the system ... This is not the case for all systems. Some compilers will warn about an "int main" function without a returning value. Some will add just a transparent "return -1;" just before the program ends, and some will use the last value stored (i.e. the last value returned by some function) in the accumulator register (EAX when talking about i386+) as the program return value. OS doesn't expect necessary to find a "nice" execution of a program by getting 1 (or in a general condition, non-zero return). For instance, on MS-DOS, a "0" returning value will clear the ERRORLEVEL environment variable, meaning for a fair ending of the program. Other values will set up an ERRORLEVEL code so it will indicate an unusual termination state (not just an error, but any especial condition; i can think of 0 for Retry, 1, for Ignore, and 2 for Cancel). On Windows, by returning 1 to the system means that the program has quit the execution in a good behaviour. This is the opposite to MS-DOS convenction. #include<stdio.h>int main(){printf(" %d",printf("1 2"));return(1);} printf usually is an alias for fprintf(stdout,...). fprintf will return the numbers of chars writen to the stream when returned successfully. Otherwise it will return a negative value. printf is expected to behave in the same fashion, as long as stdout may be redirected to a file or a socket through pipe redirection. Thus, when the inner call to printf retuns fine, the output of the program will be: "1 2 3". #include<stdio.h>int main(){int i=10;i=i<2;printf("%d",i);} The asignment operator owns the least precedence order. That is, the comparison (i<2) is performed before the asignment. And, because i<2 condition is false, the asignment will lead to value 0 (C convection for false conditions). That value is what is printed to stdout then... 2. Write a program to print semicolon ";" without the use of semicolon ";" . It should not be there in the C program anywhere. This is funny and pretty easy. The trick is to make such a program only consisting of empty structure blocks, and performing the print out from a function located outside the structure blocks but not in the main program(for instance, inside a comparison). The following code will do the task: #include <stdio.h>int main(){if(putchar(0x3B)) { }} Notice that there isn't any quote char aswell, as long as putchar doesn't require any string for output (contrary to printf). Ok, let's consider this code: #include <stdio.h>int main(){char *b="Surname",*a="Name";printf("\nWhat's your name? ");scanf("%s",b);printf("\nWhat's your Surname? ");scanf("%s",a);printf("\nYour full name is %s, %s",b,a);} Case 1: What's your name? JamesWhat's your Surname? BondYour full name is James, Bond Case 2: What's your name? BondWhat's your Surname? JamesYour full name is , James Where is the error? why in the second case, there is a word missed?? And this one? #include <stdio.h>int main(){char a[256];int size=0;FILE *f;for(int i=0;i<4000;i++) a[i]=i;if((f=fopen("test.file","w+"))!=NULL) { size=fwrite(a,sizeof(char),256,f); fclose(f); }if((f=fopen("test.file","r+"))!=NULL) { size=fread(a,sizeof(char),256,f); fclose(f); }if(size==256) printf("\nOK, the file has been created and read successful."); else printf("\nExpected 256 caracters to be read, but just %d did so, what the **** is going on here?",size);} Case 1. Executed on a win32 console: Expected 256 caracters to be read, but just 26 did so, what the **** is going on here? Case 2. Executed on a linux console: Expected 256 caracters to be read, but just 25 did so, what the **** is going on here? So, what is the answer to the programmer's question for each case? (when i was a novell programmer i wondered about this issue for 4 hours before i realized what i did wrong). This is a program intended to work as a CGI for a webserver: #include <stdio.h>#include <stdlib.h>char a[4096];strcpy(a,getenv("QUERY_STRING"));printf("<html><head><title>Test page</title></head>\n");printf("<body>You asked the webserver for this query: %s\n",a);pirntf("</body></html>");} Why the free access to this CGI is a real danger for the entire server security? and, how much is dangerous for a normal user? I guess here're some guru programmers willing to answer to these questions... Share this post Link to post Share on other sites