<<

The

Video 18

Daniel J. Bodony Department of Aerospace Engineering University of Illinois at Urbana-Champaign In this video you will learn. . .

1 Before you go on. . . 2 About the “C” programming language 3 Basic program structure 4 How to compile a program into an executable 5 How to run an executable

D. J. Bodony (UIUC) AE199 IAC Video 18 2 / 11 Before you go on. . .

For the rest of the class, you will need to be comfortable using , the -like that is freely available and one of most popularly used operating systems, especially for doing research. Please use the following tutorials to learn how to use UNIX/Linux:

UNIX Tutorial For Beginners http://www.youtube.com/watch?v=cX9ASUE3YAQ UNIX-1.2: UNIX Introduction and Basic Commands Tutorial http://www.youtube.com/watch?v=lhvrktwgg9M Intro Shell: http://openclassroom.stanford.edu/MainFolder/VideoPage. ?course=PracticalUnix&video=intro-shell&speed=100

D. J. Bodony (UIUC) AE199 IAC Video 18 3 / 11 About the “C” Programming Language

History Developed from 1969–1973 at AT&T Bell Labs as a general programming language (i.e., not -specific) closely tied with the Unix operating system. Before “C” was accepted, most operating systems were written in and were very difficult to construct and modify.

Because of C’s simplicity and power, it’s use spread beyond operating systems into one of the most widely used programming languages in the world.

D. J. Bodony (UIUC) AE199 IAC Video 18 4 / 11 Basic Program Structure

A C program hello.c has the following basic structure: #include #include int main(void){ printf("Hello, World!\n"); return EXIT_SUCCESS; } Let’s look at each element separately.

D. J. Bodony (UIUC) AE199 IAC Video 18 5 / 11 Basic Program Structure

#include #include Tells the C compiler to insert the code contained in the header file stdio.h into the file hello.c.

The code contained in stdio.h defines the basic functions that are needed for hello.c; printf in this example.

The angle brackets < and > tell the compiler that the file stdio.h can be found in the “usual” place, which is known to the compiler and you don’t need to know.

The code in stdlib.h defines the variable SUCCESS.

D. J. Bodony (UIUC) AE199 IAC Video 18 6 / 11 Basic Program Structure

int main(void){ } Every C program needs a function called main(). The beginning and ending of the function are marked with the braces { and }.

The keyword void tells the compiler that main() will not take any input arguments.

The keyword int tells the compiler that main() will return an integer as its output argument.

D. J. Bodony (UIUC) AE199 IAC Video 18 7 / 11 Basic Program Structure

fprintf(stdout, "Hello, World!\n"); return EXIT_SUCCESS; The fprintf function works just like fprintf in Matlab. In fact fprintf(stdout,...) in C is exactly equivalent to fprintf(1,...) from Matlab.

Also just like in Matlab, a function should include a . Because we declared this function to return an int, the C compiler requires we contain a line return Some Int.

I recommend using EXIT SUCCESS since it is meaningful. If the program should need to fail, then you can use EXIT FAILURE.

D. J. Bodony (UIUC) AE199 IAC Video 18 8 / 11 How to compile a program into an executable

Now that we have a C program in the file hello.c, we need to compile it into an executable. After logging into linux.ews.illinois.edu, when I issue the command ls, this is what I see:

[bodony@linux-v1 Homework10]$ ls hello.c hello.c~

which shows the file hello.c and a temporary backup created by emacs (or vi) called hello.c~.

D. J. Bodony (UIUC) AE199 IAC Video 18 9 / 11 How to compile a program into an executable

To compile, type the command gcc hello.c: [bodony@linux-v1 Homework10]$ gcc hello.c [bodony@linux-v1 Homework10]$ ls a.out hello.c hello.c~

And you see a new file called a.out was created. This new file is the executable that contains the machine-readable instructions associated with hello.c.

D. J. Bodony (UIUC) AE199 IAC Video 18 10 / 11 How to run an executable

To run the executable, type ./a.out: [bodony@linux-v1 Homework10]$ ./a.out Hello, World!

The program works!

D. J. Bodony (UIUC) AE199 IAC Video 18 11 / 11