Main

In C the main function is called automatically whenever code is running.

#include <stdio.h>
#include <cs50.h>
int main(void)
{
	
}
(void)

Programs that take command like arguments:

#include <stdio.h>
#include <cs50.h>

int main(void)
{
	string s = get_string("What's your name? ");
	printf("%s\n Hello, " , s);	
}

But there are keywords such as argc and argv that can go inside the main function:

#include <stdio.h>
#include <cs50.h>
int main(int argc, string argv[]){
// This program takes the input from the command line
	printf("Hello, %s\n", argv[1] );
	

The value that the main function returns , is called anexit status.

#include <stdio.h>
#include <cs50.h>
int main(int argc, string argv[]){
	if (argc != 2)
	{
		printf("Missing CL argument\n");
		return 1;
	}
	printf("Hello, %s\n", argv[1]);
	return 0;
}

We can check secretly the return value of a program by digiting: echo $?
In C when a program returns the a value, the execution stops.

Powered by Forestry.md