Академический Документы
Профессиональный Документы
Культура Документы
Chapter 2: Programming in C
Bachelor Course
Summer Term 2017
Prof. Dr. Gregor Schiele n FG Eingebettete Systeme der Informatik n Uni Duisburg-Essen
C in a Nutshell
n C11
§ _Atomic § char § goto § static
§ _Bool § const § if § struct
§ _Complex § continue § inline § switch
n C99
§ _Generic § default § int § typedef
§ _Imaginary § do § long § union
§ _Noreturn § double § register § unsigned
n C89
§ _Static_assert § else § restrict § void
§ _Thread_local § enum § return § volatile
§ auto § extern § short § while
Embedded Systems Group
University of Duisburg-Essen 7
Our First Program: Hello World!
hello.c
#include <stdio.h>
int main(void) {
char h[] = "Hello world\n";
printf("%s", h);
return 0;
}
Note:
Since C99 this can be omitted.
Main function will implicitly return status of 0
A global int x;
variable with
name ‘x’ and
int main(void) {
type integer
float y; A local variable
... with name ‘y’
} and type float
Variables
§ ... must be declared before first use
§ ... are statically typed (static type binding)
§ ... use static name binding
§ ... can have different scopes (visibility) and lifetimes
Again if you know Java then these should look very familiar
But be careful, again there are some differences
§ Arithmetic: +, -, *, /, %
§ Bit manipulation: &, |, ^, ~, <<, >>
§ Comparison: ==, !=, <, <=, >, >=
§ Logic: !, &&, ||
§ Assignment: =, ++, --, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=
§ Conditional: ? :
§ Type size: sizeof(type) returns size of data type in bytes
int i;
‘old’ style
for (i=0; i<10; i++) {
/* do something */
}
§ But yet again there are subtle but important differences (àlater)
/* start of comment
// this works end of
/* this does not work */ comment
*/
§ API doc: there are tools to use comments of style /** API */
like in Java but it‘s not part of the core C language
contains implementation
declares printf of printf
C
stdio.h Standard
Library
Prepro- Com-
hello.c hello.o Linker hello.exe
cessor piler
header
libraries
files
...
executable
...
...
...
...
Linker
code
§ source code files compiled separately, then linked into one program
§ dynamic linking (by OS) at runtime is also possible
Embedded Systems Group
University of Duisburg-Essen 17
C Code Files
§ Libraries (.jar)
◦ Precompiled implementations, e.g. system libraries
Other classes /
Byte Code / jars
§ Symbolic constants
◦ Constants represented as symbols
◦ When program compiled, all occurrences replaced
§ Format
◦ #define identifier replacement-text
◦ #define PI 3.14159
§ Everything to right of identifier replaces text
◦ #define PI =3.14159
◦ Replaces PI with =3.14159
◦ Probably an error
§ Cannot redefine symbolic constants
Embedded Systems Group
University of Duisburg-Essen 25
Preprocessor: Symbolic Constants (2)
§ Advantages:
◦ Takes no memory
§ Disadvantages:
◦ Name not seen by debugger (only replacement text)
◦ Do not have specific data type
area = ( 3.14159 * ( 4 ) * ( 4 ) );
§ Multiple arguments:
#define RECTANGLE_AREA( x, y ) ( ( x ) * ( y ) )
rectArea = RECTANGLE_AREA( a + 4, b + 7 );
becomes
rectArea = ( ( a + 4 ) * ( b + 7 ) );
§ #undef
◦ Undefines symbolic constant or macro
◦ Can be redefined later
§ #error tokens
◦ Prints implementation-dependent message
◦ Tokens are groups of characters separated by spaces
▪ #error 1 - Out of range error has 6 tokens
◦ Compilation may stop (depends on compiler)
§ #pragma tokens
◦ Actions depend on compiler
◦ May use compiler-specific options
◦ Unrecognized #pragmas are ignored
§ # operator
◦ Replacement text token converted to string with quotes
#define HELLO(x) printf("Hello " #x "!\n");
◦ HELLO(world) becomes
▪ printf("Hello " "world" "!\n");
▪ Same as printf("Hello world!\n");
§ ## operator
◦ Concatenates two tokens
#define TOKENCONCAT( x, y ) x ## y
◦ TOKENCONCAT( O, K ) becomes
▪ OK
Integer types:
§ char
§ short int (or simply: short)
§ int
§ long int (or simply: long)
§ Since C99: long long int (or simply: long long)
Examples:
§ -3 : signed integer with base 10
§ 12Us : unsigned short integer
§ 0x12LL : signed long long integer with base 16 (hexadecimal)
§ Since C99 also binary literals
unsigned int x = 1;
int y = -1;
if(y < x)
printf("of course it is");
else
printf("strange stuff");
Examples:
§ -3.0 : signed double with base 10
§ 17e2f : signed float with base 10 (17.e2f is ok, too)
int main(void) {
char h[] = "Hello world\n";
printf("%s", h);
return 0;
}
Pitfall 1:
§ C99 defines this as mandatory for hosted implementations but
optional for standalone (embedded chip might not need it)
§ C11 redefines it as optional in both cases
§ Check if macro constant __STDC_NO_COMPLEX__ is defined
à no support for complex.h
§ Further macro constants exist (e.g. _Imaginary supported?)
Pitfall 2:
§ C++ provides completely different (template) class
complex<T> defined in header file “complex” (w/o extension)
§ Note: C++ has native type bool with values true and false
§ Keyword typedef
◦ Makes synonyms (aliases) for previously defined data types
▪ Does not create new type, only an alias
◦ Creates shorter type names
▪ Often used for pointers to certain type or for structs
(see next slides)
§ Example:
◦ typedef int age;
◦ Defines new type name age as synonym for type int