Tuesday, April 20, 2010

int main(int argc, char *argv[]);

For those writing programs which will run in a hosted environment, arguments to main provide a useful opportunity to give parameters to programs. Typically, this facility is used to direct the way the program goes about its task. It's particularly common to provide file names to a program through its arguments.

The declaration of main looks like this:

int main(int argc, char *argv[]);
This indicates that main is a function returning an integer. In hosted environments such as DOS or UNIX, this value or exit status is passed back to the command line interpreter. Under UNIX, for example, the exit status is used to indicate that a program completed successfully (a zero value) or some error occurred (a non-zero value). The Standard has adopted this convention; exit(0) is used to return ‘success’ to its host environment, any other value is used to indicate failure. If the host environment itself uses a different numbering convention, exit will do the necessary translation. Since the translation is implementation-defined, it is now considered better practice to use the values defined in : EXIT_SUCCESS and EXIT_FAILURE.

There are at least two arguments to main: argc and argv. The first of these is a count of the arguments supplied to the program and the second is an array of pointers to the strings which are those arguments—its type is (almost) ‘array of pointer to char’. These arguments are passed to the program by the host system's command line interpreter or job control language.

The declaration of the argv argument is often a novice programmer's first encounter with pointers to arrays of pointers and can prove intimidating. However, it is really quite simple to understand. Since argv is used to refer to an array of strings, its declaration will look like this:

char *argv[]
Remember too that when it is passed to a function, the name of an array is converted to the address of its first element. This means that we can also declare argv as char **argv; the two declarations are equivalent in this context.

Indeed, you will often see the declaration of main expressed in these terms. This declaration is exactly equivalent to that shown above:

int main(int argc, char **argv);
When a program starts, the arguments to main will have been initialized to meet the following conditions:

argc is greater than zero.
argv[argc] is a null pointer.
argv[0] through to argv[argc-1] are pointers to strings whose meaning will be determined by the program.
argv[0] will be a string containing the program's name or a null string if that is not available. Remaining elements of argv represent the arguments supplied to the program. In cases where there is only support for single-case characters, the contents of these strings will be supplied to the program in lower-case.

Saturday, January 30, 2010

challenge solution


It really works on VS 2008.I am still searching how to improve the code ,cause I don't think it's professional and I can't explain some of the processes in my own word.

Tuesday, January 26, 2010

Basic data types and switch

First of all, I want to continue the question of last week. A nice friend help me get out of the problem. In his word, there are two distinct families of basic data types: integer and floating point.Char is one of the members of integer types. The difference between the char and int is the size. Usually, each char occupies one byte, while the size of int is system-dependent. For 16-bit CPU,int occupies 2 bytes, while int occupies 4 bytes for a 32-bit CPU. So, if I type the code like

char c;
c=print("ABC");
what is going to c is the same as I use the code

int a;
a=print("ABC");
a number 3 will be stored into the variable. But why the output is just ABC?. I have to correct a mistake in the blog 1 at first. The output for the

int a;
a=print("ABC");
printf("%d",a);
should be the ABC3. So, If I use

printf("%d",c);
,the output should be ABC3 as well. But I used the %c instead of %d, so the results presented differently.
This problem is very simple,but I know the importance of the data types through it.So, I am still working on them until I get familiar with them.If some one is interesting in the data types, you can go to the textbook page 21 to check it out.
Now, I think it is the time to back to now. Actually, I don't have much things to write down,because I had only one class this week. But I still want to talk about the switch.
switch(){
case 1:

break 3;
case 2:

break;
case 3:

break;
default:

}
I think break is very convenient. It can help us end the statment at once and jump to the other case in no time. So, we can use switch in various ways. Actually, I have a stupid question about the break. I wonder if break can do like

case 1:
break 3;
case 2:
break 1;
case 3:
break ;

That means if we can do the reverse break. I think may be I should try.

Monday, January 18, 2010

Question I got

Through the week 1 studying, I learn that

int a;
a=printf("ABC");

will cause a equal 3. Then I am curise that if I use this new thing with char ,what the result will be. So I do a test

char a;
a=printf("ABC");

There is no suprise for the result. It prints ABC. But another question came to my mind. Why it prints ABC instead of just A? I remember that a char can only be stored the first income character and the rest will be eliminated,if it's not a array.

I am still attempting to figure out the reason.

Thursday, September 17, 2009

Welcome

also welcome welcome welcome welcome