Introduction to C An overview of the programming language C, syntax, data types and input/output
Teil I. a first C program TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 1
PROGRAMMING LANGUAGE C is a general-purpose and imperative programming language. Developed in the beginning of the 1970s by Dennis Ritchie. It is old. It is simple. It is fast. It is not Fortran. It is not as userfriendly as newer programming languages. It is well suited to provide an appropriate understanding of scientific programming. TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 1
HELLO WORLD! The simplest program in any language is the output of a Hello World! string onto the screen. / h e l l o w o r l d program / #include<s t d i o. h> i n t main ( ) { p r i n t f ( " Hello World \n " ) ; return 0; } Task 7 Transfer the example code to the main.c file you created in the last section Linux Command Line! TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 2
COMPILATION To create an executable file out of our text file we need a compiler, that translates our programming language code to machine code and links it into an executable file. For C we use the gcc compiler, which is short for GNU C Compiler. To compile our Hello World! program we use the Linux command line in the same directory where the main.c file is located: gcc main. c to create an executable named a.out, which we execute via:. / a. out to get the wanted output of the program. TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 3
COMPILATION To be able to name the executable file, except from renaming it after the compilation with mv, we compile: gcc o h e l l o w o r l d main. c where helloworld is the name of the executable that is created and can be choosen at will. Task 8 Compile the example code from the main.c file and check the output of the executable file a.out! TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 4
Teil II. C syntax TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 5
KEYWORDS Keywords are words with special meaning and can not be chosen as variable names. In most text editors, with the correct setting of the used programming language, keywords are highlighted as in the example code in these slides. Some keywords in C: int, float, double, char, void,... - specifying data types, for, while, do, break, continue - regarding loops, if, else, switch, case - managing branches. Overall there are 32 keywords in C. TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 5
FUNCTIONS Functions in C are methods that are called by a unique name. They can be viewed as a block of code, which is executed in the same, programmed manner every time. The output for our first C program is created with p r i n t f ( " Hello World \n " ) ; which is a function with a unique identifier printf, like a variable has also. Distinctive though are the brackets () after the name. TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 6
FUNCTIONS They can have specific input data and generate different output data in every call, but the method itself does not change. i n t main ( ) {... return 0; } is a function too, but different from the printf() function in our example, because main() is created, while printf() is called, after being created somewhere else! Later in this course we will return to the topic of functions. TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 7
LIBRARIES Although the function printf() is visible nowhere in the main.c created, we can still use it. That is because it is part of a global library saved somewhere on the computer and we can access every function in that library by including it: #include<s t d i o. h> We will be able to create our own local libraries and include them with: #include " mylibrary. h " Notice the different signs encasing the.h files signalizing, that the first one is a global library, while the second one is local. Later in this course we will briefly return to this topic. TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 8
VARIABLES The other big part in programming, opposite of functions, take variables. Like functions they need to have a unique identifier, who can not be a keyword of the language. Variables are stored at runtime of our program on the random access memory (RAM) of the computer and assigned values throughout their lifetime in the program. Depending on its data type a variable needs a different amount of space on the RAM and its binary representation will be interpreted differently from the compiler. TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 9
VARIABLES An examples for a variable is: i n t i ; an integer with the name i. It has not been assigned a values yet, so it is not yet defined, but the line above is the declaration of a variable, meaning the reservation of the itendifier and space on the RAM. To define a variable, we have to assign a value to it: i = 5; Now the integer variable i is defined and the value 5 has been stored on the RAM. When introducing pointers, we will return to the concept of saving variables. TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 10
DATA TYPES Some selected data types available in C: data type memory range format boolean true or false char 1 byte 2 7 to 2 7 1 %c int 2 byte 2 31 to 2 31 1 %d float 4 byte 7 decimal digits %f long long int 8 byte 2 63 to 2 63 1 %lld double 8 byte 16 decimal digits %lf The format column will be needed for later when we add variables to the output of the program. TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 11
Teil III. first steps towards an own C program TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 12
OUTPUT The standard function to communicate output to the command line is printf(). There is at least another function fprintf(), but it is regarding files and we will attend it much later. As mentioned before, a function can use arguments. In the case of: p r i n t f ( arg1, [ arg2, arg3, arg4,... ] ) ; these arguments are seperated into two types: The first on arg1 is a string, where the other ones are variables whose values are put in the string arg1. Hint The printf() function is in the stdio.h library. TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 12
OUTPUT A simple example for only arg1 was given in the Hello World! program p r i n t f ( " Hello World! \ n " ) ; where the \n causes a line break in the output text. When we want to print values of variables we have to insert place holders into the string. This is where the format column of the data type table is needed: p r i n t f ( " Value of the i n t e g e r i = %d\n ", i ) ; p r i n t f ( " Value of the double d i = % l f \n ", d i ) ; The \n is not necessary but for readability. TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 13
INPUT The standard function to get input from the user during runtime is scanf(). The function fscanf() is for files and subject of later lessons. The function has two type of arguments: scanf ( [ fspec1 fspec2... ], [ vadd1 vadd2... ] ) ; fspec is short for format specifier and has to be put into a string and seperated by spaces. The vadd are the addresses of the variables where we want to store the entered values. In the chapter about pointer we will attend more closely the address of a variable. TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 14
INPUT To demonstrate the usage of scanf() some examples: scanf ( "%d ",& i ) ; for reading an integer value into an integer variable i. scanf ( "% l f % l f % l f ", &a1, &a2, &a3 ) ; for collecting three values of the data type double at once. The input needed from the user will have the form: 1.2 2.4 3.6 three numbers, seperated by spaces. Hint The scanf() function is in the stdio.h library. TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 15
FORMAT SPECIFIERS Task 9 Write a program that declares and defines variables of type int, char, float and double and prints their values in the command line! Hint The format specifiers for the data types above where d, c, f and lf. You can furthermore set the amount of pre-decimal and decimal digit with: p r i n t f ( " %3.4 l f ", d i ) ; This will cut the double variable di off, after the fourth decimal digit. TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 16
CALCULATOR Task 10 Write a program that resembles a basic calculator. It should introduce itself by explaining its purpose, then ask the user for two numbers as input for the following calculations: addition, substraction, multiplication and division. After calculations the four results need to be returned to the user! Hint Remember that \n cause a line break for better readability and scanf() can process two variables at the same time. TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 17