ENGR101: Lecture 7 C language tools and intro to C programming March 2018 ENGR101: Lecture 7 March 2018 1 / 20
Not how to play piano but how to open it... Here is an example of simple (very) C program. # include <stdio.h> int main () { printf (" Hello world \n"); } Listing 1: Prints Hello world For now we will not look at what all these mysterious things mean. Much more about it soon. We will just try to make this program to run. ENGR101: Lecture 7 March 2018 2 / 20
C programming on ECS system There are several ways to write and run C language program on ECS machines. Using Integrated Development Environment (IDE). It is graphical. Using Command Line Interface (CLI) ENGR101: Lecture 7 March 2018 3 / 20
Graphical - Geany IDE(Integrated Development Environment) How to start with Geany: Go to System menu. In Development option find Geany and click it. Geany should open. Start typing your program. Save your program text in file with extension.cpp. It is important that you specify the extension - Geany can work with several languages and decides language tools on base of this extension. ENGR101: Lecture 7 March 2018 4 / 20
Program text to machine codes: Compile, build and run Figure: Compile, build and run in Geany Using Build menu option you can convert text of your program into machine codes (executable file) and run it. Somewhat similar to to RAM in LMC It is two stages process: Compile - convert some program text into object code. Object code can not run(execute). Build (sometimes called link) - takes object codes (usually from several programs) and converts object code into true executable. Execute - it runs the program. ENGR101: Lecture 7 March 2018 5 / 20
Geany messages during compiling Notice that as we click Compile and Build in Geany menu, messages are displayed. In Geany menu click: Build > Compile. Message:g++ -Wall -c m1.cpp. g++ compiler takes m1.cpp file as an input and compiles it (-c). Compiler gives all warnings: -Wall In Geany menu click Build > Build. Message: g++ -Wall -o m1 m1.cpp. Compiler takes object files(-o) for m1.cpp program and produces executable m1(name can be any of your choice) ENGR101: Lecture 7 March 2018 6 / 20
Another way to compile, build and run If you type in all these commands in Command Line windows you achieve same result as clicking Compile and Build buttons in Geany. To run the program executable type executable name preceded by./(way to explain to the system that it is custom-made program). ENGR101: Lecture 7 March 2018 7 / 20
What is happening when we compile? Roughly equivalent to clicking To RAM LMC button. Source code #include <stdio.h> int main() { printf("hello world\n"); int a; } Object file Mix of text and binary In any text editor - mostly giberish. Executable Processor codes Compile Build At compile stage text file is converted into mix of text and machine codes. Program text is checked for simple errors. Object file is produced - contains objects of the program. At build stage object file is converted into machine code. Processor can execute machine code. ENGR101: Lecture 7 March 2018 8 / 20
Let us step back and try to compile and run empty file. Compile - OK Build - (.text+0x20): undefined reference to main It needs main() - which is entry point of the program (like memory slot 0 in LMC). It is a function - block of code which starts with opening curly bracket and finishes with closing one (more on it later). OK, let us five it main()... main () { } Listing 2: Caption Still complaining: me1.cpp:2:6: warning: ISO C++ forbids declaration of main with no type [-Wreturn-type] ENGR101: Lecture 7 March 2018 9 / 20
Return type It is called a function because it gives some number back (remember calculus functions?). This number should be of some type. Convention for main is that it returns int. Usually if program finishes OK it should return 0. -1,-2 etc. indicate errors (file not found, no network, etc) ENGR101: Lecture 7 March 2018 10 / 20
Minimum program text which compiles and runs int main () { } Listing 3: Caption Not that it does much... ENGR101: Lecture 7 March 2018 11 / 20
Quiz time... Can I have two main() functions? Let s vote... 1 Yes 2 No Let s check... ENGR101: Lecture 7 March 2018 12 / 20
How to print something on the screen? print() seems to be reasonble guess.. Geany suggests printf instead of print. Listing 4: Caption int main () { printf (" Hello everybody "); } Not much luck... print1.cpp:4:26: error: printf was not declared in this scope It is because almost all C functionality is defined in additional programs, called libraries. Even such simple thing as printing requires library. For printing we need library stdio - standard input-output (more on libraries later). ENGR101: Lecture 7 March 2018 13 / 20
printf() f in printf stands for formatted. 1 printf - print formatted text. You should specify how you want to display number. Options are: %d - prints decimal integer %f - prints float number %c - prints character %s - prints string These are called format specifiers. 2 It prints string before comma replacing all format specifiers with values which come after comma. printf("somethin %d, more %f",12,7.6); ENGR101: Lecture 7 March 2018 14 / 20
Quiz? We need to display decimal number and string. What is right format? 1 Option 1: Listing 5: Caption printf ("v1 =%f v2 =%c", 12, " ddff "); 2 Option 2: Listing 6: Caption printf ("v1 =%d v2 =%s", 12, " ddff "); 3 Option 3: Listing 7: Caption printf ("v1 =%f v2 =%s", 12, " ddff "); ENGR101: Lecture 7 March 2018 15 / 20
Variables Variables store values which can change (unlike variables in calculus). Program is not an equation, it is sequence of actions. One line after another (remember LMC?) program flow a = 1;...... a = 5; a becomes 1 a becomes 5 OK, let us try that... ENGR101: Lecture 7 March 2018 16 / 20
Variable types error: a was not declared in this scope a is not even recognized as a variable. C is strongly typed language. All variables should be of certain type. int a = 0;, for example, works just fine. int is a type. What are types? ENGR101: Lecture 7 March 2018 17 / 20
Types All variables are stored in memory (as binary numbers). Type of the variable determines how many bits (Bytes) this variable takes in memory. Memory to store variable a Memory a b One Byte ENGR101: Lecture 7 March 2018 18 / 20
Variable types Type specifies how many bits particular number should fit in. There are many types in C. Common types: char - 8 bits (1 Byte). Maximum value is 1111 1111 (binary). int - number of Bytes depends upon hardware and operating system. Usually 4. float - usually 4 Bytes. Some bytes store fraction (part after.) and exponent takes rest of 32 bits. ENGR101: Lecture 7 March 2018 19 / 20
Questions? ENGR101: Lecture 7 March 2018 20 / 20