Title | Introduction to C - programming |
---|---|
Course | Maskinorienterad programmering |
Institution | Chalmers tekniska högskola |
Pages | 4 |
File Size | 268.7 KB |
File Type | |
Total Downloads | 79 |
Total Views | 162 |
Introduction to C - programming...
TKITEK 2019 Introduction to C - programming
C Background A computer only understands machine code which consists of sequences of ones and zeros. We can communicate through ones and zeros but that is difficult. A programming language, like C, gets converted to machine code. We write code, called source code which then needs to be compiled and the output in an excetucable program in which the computer can understand. Machine-oriented programming needs a language with pointers to absolute memory addresses. C has fewer lines of code, smaller risk of errors and is faster than asm (Assembler)
C vs. Java ● ● ●
C does not have classes but it has struct for composite data types Booleans are not a type. There is no true/false. 0 is false and a value which is !=0 is true ○ Note that 1 && 1 is not necessarily 1! “implementation-specific for the compiler” C has no array boundary checks, no exception handling (try/catch), no polymorphism, no overloading
Syntax Specifics Content from D erek Banas - C Video Tutorial #include
Lets us import certain functionality
#include
Lets us use input and output
#define MYNAME “”
Used to define constants, MYNAME =
int age = 21
This variable will be accesible to every single function of the entire program as it is outside the main
Main () { char firstLetter = ‘D’; }
A main in always nedded, it's where the computer starts excecuting the code. If you put in functions that are not referenced from main, they will not excecute. firstLetter of variable type char will be local to the main funciton
long int float double
Long used for bigger numbers Float used for decimals Double used for even larger decimals
printf(“Hello”)
Printf allows us to print out stuff to the screen
printf(“\n”)
Will “print”/skip a line
1
TKITEK 2019
printf(“I am %d years old”, age) %d or %i %ld %.5f %c %s
Used when you want to use/print out certain things. Wherever whe have the %d or %i sign, age, in this case will be attached to it and it is used for integers. The symbol is called a conversion character and there are different types depending on the data type we want to use. %ld used for long, % .5f used for floats and doubles, in this case, a number with 5 decimal places, %c for characters, %s for strings.
char myName[5] = “”; char myName[ ] = “”;
To create a string, you need to create a character array. You need to specify the total number of spots, counting from 0 as well as +1 for the string terminator, \0 which tells C that this is the end of our string. “” consists of 5 characters, our string is of length 6 (0 to 5) in order to make room for the string terminator. You do not need to specify the length of the array, is it okay to leave it blank and it will create an array of the required length. You can´t assign new values to a character array.
strcpy(myName, “Bob Smith”); Used to copy strings and assign new values. myName, w hich is assigned as “” will now be given the value “Bob Smith”
Used to receive input, use scanf. Can only accept one value at a time. You must use the & ampersand before the variable unless you're using %s when using scanf
2
TKITEK 2019
Note that the characer ‘a’ has its own value in digits, namely 97. Adding x (which is assigned to 32 in this case) and y will give 32+97 = 129. ‘a’ har värdet 97 i decimal och 61 i hex. Värderna fås från accitabellen
Typkonverteringar Also note that you cast char y with (int)y to convert it to its corresponding value in number. ● Implicit typkonvertering : x = x+y //konvertering sker utan att vi explicit skrivit vad det ska castas som ● Explicit typkovertering : (int)y // här specifierar vi att y ska castas om till en int
Funktioner int function( int x, char y ) // returvärdet av funktionen är av typen int. X och Y är argumenten och argumenten är “pass-by value”, deras värde är densamma som den var innan funktionsanropet som nedan;
3
TKITEK 2019
Synlighet
4...