QUOTE
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.
CODE
#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".
CODE
#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...
QUOTE
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:
CODE
#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:
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:
CODE
What's your name? James
What's your Surname? Bond
Your full name is James, Bond
Case 2:
CODE
What's your name? Bond
What's your Surname? James
Your full name is , James
Where is the error? why in the second case, there is a word missed??
And this one?
CODE
#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:
CODE
Expected 256 caracters to be read, but just 26 did so, what the **** is going on here?
Case 2. Executed on a linux console:
CODE
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:
CODE
#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...
Comment/Reply (w/o sign-up)