main functionEvery C program coded to run in a hosted execution environment contains the definition of a function named main, which is the designated start of the program.
// Other things (functions, structures, ...), if any ...
int main(void) {
// The program starts here.
statement_1;
statement_2;
// ...
statement_n;
}
* What is a function?
A function in mathematics:
Example:
Write it in C:
int f(int x) {
return x * x;
}
Example:
Write it in C:
double f(double x, double y) { // Two arguments
return x + y;
}
double: double-precision floating-point number
Syntax: ReturnType FunctionName(Parameters) { FunctionBody }
FunctionBody can also contain more complex statements:
int max(int a, int b) {
if (a < b)
return b;
else
return a;
}
if statement
Syntax: ReturnType FunctionName(Parameters) { FunctionBody }
A function can have no arguments. To define such a function, write void in Parameters:
int always42(void) {
return 42;
}
We will introduce more on functions in later lectures.
main functionEvery C program coded to run in a hosted execution environment contains the definition of a function named main, which is the designated start of the program.
According to the standard, the main function must has one of the following signatures:
int main(void) { ... }int main(int argc, char *argv[]) { ... }/* another implementation-defined signature */For now, we only use the first one: int main(void) { ... }.
"signature": consisting of the return-type, parameters, and some other possible information
main functionint main(void) {
printf("hello world\n");
return 0;
}
The return value of main: Indicates whether the program exits successfully.
A program exits successfully if and only if its main function returns 0.
main functionA program exits successfully if and only if its main function returns 0.
You may also see this somewhere else:
main functionA program exits successfully if and only if its main function returns 0.
You may also see this somewhere else:
main functionIt is ok to omit return 0; in main (but not in other functions):
int main(void) {
printf("hello world\n");
}
According to the standard:
If the return type is compatible with
intand control reaches the terminating}, the value returned to the environment is the same as if executingreturn 0;.
printfDeclared in the standard library header file stdio.h.
#include <stdio.h> in the beginning.Writes something to the standard output.
printf("hello world\n");
hello world, with a newline \n at the end.printf("hello\nworld\n");int main(void) {
printf("hello world\n");
return 0;
}
main?int main(void) {
printf("hello world\n");
return 0;
}
hello world with an ending newline.main? 0.Reads two integers from input (separated by whitespaces), and prints the sum of them.
#include <stdio.h>
int main(void) {
int a, b; // declares two variables of type "int", named "a" and "b".
scanf("%d%d", &a, &b);
printf("%d\n", a + b);
return 0;
}
Reads two integers from input (separated by whitespaces), and prints the sum of them.
scanfAlso declared in stdio.h.
Reads something from the standard input.
Example: Reads two integers from the standard input, separated by whitespaces.
scanf("%d%d", &a, &b);
%d: Indicates that an integer is expected, and will be stored into an int variable.&: The address-of operator For now, just remember to add & when passing things to scanf.
scanfscanf("%d%d", &a, &b);
How should these two integers be separated? Try it out:
scanfscanf("%d%d", &a, &b);
%d will skip any leading whitespaces.
' ', newline '\n, tab '\t', etc.More on the rules related to scanf will be covered in recitations.
printf printing an integerGiven a b
printf("%d\n", a + b);
72, with a newline in the end.
printf("%d + %d equals %d\n", a, b, a + b);
What is the output?
printf printing an integerGiven a b
printf("%d\n", a + b);
72, with a newline in the end.
printf("%d + %d equals %d\n", a, b, a + b);
30 + 42 equals 72, with a newline in the end.
Understand the following two programs:
#include <stdio.h>
int main(void) {
printf("hello world\n");
return 0;
}
#include <stdio.h>
int main(void) {
int a, b;
scanf("%d%d", &a, &b);
printf("%d + %d equals %d\n",
a, b, a + b);
return 0;
}
Why is #include <stdio.h> needed?
What does int main(void) mean?
What is the meaning of return 0;? Can it be omitted?
How do we represent a newline?
What does %d mean?
How are whitespaces handled when reading integers with scanf?
#include <stdio.h> needed?
scanf and printf are declared in the standard library header file stdio.h.int main(void) mean?
main function is where the program starts. int is the return type and void indicates that this function accepts no arguments.return 0;? Can it be omitted?
0 if it exits successfully. The main function of C will execute return 0 automatically at the end if we don't write it exiplicitly.'\n'.%d mean?
int.scanf?
int with %d in scanf, leading whitespaces are ignored.Run the following code. Try to understand what it means.
#include <stdio.h>
int max(int a, int b) {
if (a < b)
return b;
else
return a;
}
int main(void) {
int a, b;
scanf("%d%d", &a, &b);
printf("%d\n", max(a, b));
return 0;
}