int main() { return 0; }
All C programs have a main
function.
int main ()
is the function header.
main
has a return type of int
, an integer.
()
defines function parameters.
{}
defines the beginning and end of the function.
{}
.
return 0;
returns an integer value of zero, meaning that all went well.
main()
returns an int
main()
does not return void
The ANSI standard says that main()
returns int
.
It doesn't matter if void
works for another compiler.
We teach ANSI C in this class, not Microsoft C or GNU C or any particular compiler.
int main() { /* There may be one or more lines in this type of comment. */ return 0; // Comment to the end of line }
Programs may have comments.
//
starts a comment, the rest of the line is ignored by the compiler
// something // else
/* ... */
is a block comment that can span lines
/*
is ignored within a comment. The first matching */
ends the comment.
#include <stdio.h> // Preprocessor Directive int main() { // Function // Print Hello, world // Comment printf("Hello, world!\n"); // Function Call return 0; // Function return value }
Declarations appear at the top of the program
#include <stdio.h>
#include
<stdio.h>
.h
= header file
#include <stdio.h> // Preprocessor Directive int main() { // Function // Print Hello, world // Comment printf("Hello, world!\n"); // Function Call return 0; // Function return value }
Functions defined by the header file are used in the program.
printf ("Hello, world!\n");
printf
= name of function
( )
are parameters
printf
prints (displays) the parameter values to the screen
\n
= new line character
pico
, vim
, or whatever editor you like.
c11
is just like using gcc -std=c11
.
#
.
c11
options you won’t need to
remember, but some are used often.
-o
file — make an executable program called file
instead of the default program a.out
. % c11 -o hello hello_world.c
-v
— give verbose output, show the commands being executed
--help
— give some basic usage information about the compiler
-Wall
— give tons of useful warnings
% ls hello_world.c % cat hello_world.c #include <stdio.h> int main() { printf("Hello, world!\n"); return 0; } % c11 hello_world.c % ls -l total 16 -rwx------ 1 cs156 class 4705 Feb 20 12:15 a.out -rw------- 1 cs156 class 95 Feb 20 12:10 hello_world.c % ./a.out Hello, world!
% ls hello_world.c % cat hello_world.c #include <stdio.h> int main() { printf("Hello, world!\n"); return 0; } % c11 -o hello hello_world.c % ls -l total 16 -rwx------ 1 cs156 class 4705 Feb 20 12:15 hello -rw------- 1 cs156 class 95 Feb 20 12:10 hello_world.c % ./hello Hello, world!
Modified: 2017-02-20T11:36 User: Guest Check: HTML CSSEdit History Source |
Apply to CSU |
Contact CSU |
Disclaimer |
Equal Opportunity Colorado State University, Fort Collins, CO 80523 USA © 2015 Colorado State University |
![]() |