Skip to content

Instantly share code, notes, and snippets.

@onacit
Last active June 18, 2018 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onacit/168f6b39b7fed8f7af7493e9787ec861 to your computer and use it in GitHub Desktop.
Save onacit/168f6b39b7fed8f7af7493e9787ec861 to your computer and use it in GitHub Desktop.
Main function of C
#include <assert.h>
#include <stdio.h>
/*
* 5.1.2.2.1 Program startup
* N1548
*/
int main(int argc, char *argv[]) {
// The value of argc shall be nonnegative.
assert(argc >= 0);
// argv[argc] shall be a null pointer.
assert(argv[argc] == NULL);
/*
* If the value of argc is greater than zero, the array members argv[0] through
* argv[argc-1] inclusive shall contain pointers to strings, which are given
* implementation-defined values by the host environment prior to program startup. The
* intent is to supply to the program information determined prior to program startup
* from elsewhere in the hosted environment. If the host environment is not capable of
* supplying strings with letters in both uppercase and lowercase, the implementation
* shall ensure that the strings are received in lowercase.
*/
for (int i = 0; i < argc; i++) {
printf("argv[%d]: %s\n", i, argv[i]);
}
/*
* If the value of argc is greater than zero, the string pointed to by argv[0]
* represents the program name; argv[0][0] shall be the null character if the
* program name is not available from the host environment. If the value of argc is
* greater than one, the strings pointed to by argv[1] through argv[argc-1]
* represent the program parameters.
*/
if (argc > 0) {
if (argv[0][0] != '\0') {
printf("program name: %s\n", argv[0]);
}
}
if (argc > 0) {
for (int i = 1; i < argc; i++) {
printf("program parameter: %s\n", argv[i]);
}
}
/*
* The parameters argc and argv and the strings pointed to by the argv array shall
* be modifiable by the program, and retain their last-stored values between program
* startup and program termination.
*/
if (argc > 0) {
if (argv[0][0] != '\0') {
argv[0][0] = 'a';
}
}
if (argc > 1) {
argc = 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment