SlideShare uma empresa Scribd logo
1 de 44
Baixar para ler offline
The C programming language
Why we love to hate C
and hate that we love C
Bent Thomsen
Institut for Datalogi, AAU
InfinIT Embedded Systems Engineering interest group meeting
21.11.2018
“C is quirky, flawed, and a tremendous success”
Dennis Ritchie, inventor of C
#include <stdio.h>
int main() {
/* my first program in C */
printf("Hello, World! n");
return 0;
}
C is popular
The IT world runs on C
• v6UNIX About 9,000 LoC – mainly C and assembly
• L4 micro kernel About 6,400 LoC – mainly C and assembly
• seL4 About 10,200 LoC – mainly C and assembly
• Linux 2.6 About 5.6M LoC – mainly C and assembly
• Current Linux About 16M LoC – mainly C and assembly
• Current Mac OS X About 13M LoC – some C, but mainly ObjectiveC
• Current iOS About 12M LoC - some C, but mainly ObjectiveC
• Android About 15M LoC – C, C++, Java and others
• NT 3.1 About 5M LoC – C, C++ and assembly
• XP About 48M LoC - C, C++ and assembly
• Windows 10 About 60M LoC - C, C++ and assembly
Source: Various more or less reliable source on the web
The Embedded world and C
Aspencore: 2017 Embedded Market Study
C Language History
• Dennis Ritchie developed the language C in years 1969-1973
• C was developed as a high level language for writing the Unix OS
• C was based on CPL, BCPL and B – also influenced by Algol
• In 1978 Dennis Ritchie and Brian Kernighan wrote a book on C:
• The C Programming Language, Prentice-Hall. ISBN 0-13-110163-3.
• includes a definition of the C language in BNF
• instrumental in removing syntax ambiguities between different versions of the language
• In 1982 American National Standards for Information Systems (ANSI)
established a committee with the goal of producing a C standard
• In 1989 ISO published its first C standard (C89)
• The C standard has been revised in 1999 (C99), 2011 (C11) and 2018 (C18)
The C89 standard – 519 pages
C99• C99 introduced several new features, many of which had already been implemented as extensions in several compilers:[4]
• inline functions
• intermingled declarations and code: variable declaration is no longer restricted to file scope or the start of a compound statement (block), facilitating
static single assignment form
• several new data types, including long long int, optional extended integer types, an explicit boolean data type, and a complex type to represent
complex numbers
• variable-length arrays (although subsequently relegated in C11 to a conditional feature that implementationsare not required to support)
• flexible array members
• support for one-line comments beginning with //, as in BCPL, C++ and Java
• new library functions, such as snprintf
• new headers, such as <stdbool.h>,<complex.h>, <tgmath.h>, and <inttypes.h>
• type-generic math (macro) functions, in <tgmath.h>, which select a math library function based upon float, double, or long double arguments, etc.
• improved support for IEEE floating point
• designated initializers
• compound literals (for instance, it is possible to construct structures in function calls: function((struct x) {1, 2}))[5]
• support for variadic macros (macros with a variable number of arguments)
• restrict qualification allows more aggressive code optimization, removing compile-time array access advantages previously held by FORTRAN over
ANSI C[6]
• universal character names, which allows user variables to contain other characters than the standard character set
• keyword static in array indices in parameter declarations[7]
C11
• The standard includes severalchanges to the C99 languageand libraryspecifications,such as:[9]
• Alignment specification (_Alignas specifier, _Alignof operator,aligned_allocfunction, <stdalign.h>header file)
• The _Noreturn function specifier and the <stdnoreturn.h>header file
• Type-generic expressionsusingthe _Generic keyword. For example, the followingmacro cbrt(x) translates to cbrtl(x), cbrt(x) or cbrtf(x) dependingon the type of x:
• #define cbrt(x) _Generic((x),long double:cbrtl, 
• default: cbrt, 
• float: cbrtf)(x)
• Multi-threadingsupport(_Thread_localstorage-classspecifier, <threads.h>header includingthread creation/managementfunctions,mutex, condition variableand thread-
specific storage functionality, as well as <stdatomic.h>[10]for atomic operations supportingthe C11 memorymodel).
• Improved Unicodesupportbased on the C Unicode Technical Report ISO/IECTR 19769:2004 (char16_t and char32_t types for storing UTF-16/UTF-32 encoded data, including
conversion functions in <uchar.h>and the correspondingu and U string literal prefixes,as well as the u8 prefix for UTF-8 encoded literals).[11]
• Removal of the gets function,deprecated in the previous C language standard revision,ISO/IEC9899:1999/Cor.3:2007(E), in favor of a new safe alternative, gets_s.
• Bounds-checkinginterfaces (Annex K).[12]
• Analyzabilityfeatures (Annex L).
• More macros for querying the characteristics of floating pointtypes, concerning subnormalfloatingpointnumbers and the number of decimal digits the type is able to store.
• Anonymousstructures and unions,usefulwhen unions and structures are nested, e.g. in struct T { int tag; union { float x; int n; }; };.
• Static assertions,which are evaluated during translation at a later phasethan #if and #error, when types are understood bythe translator.
• An exclusive create-and-openmode("…x" suffix) for fopen. This behaves like O_CREAT|O_EXCL in POSIX, which is commonlyused for lock files.
• The quick_exit function as a third way to terminate a program, intended to do at least minimaldeinitialization if termination with exit fails.[13]
• Macros for the construction of complex values (partly becausereal + imaginary*I might not yield the expected value if imaginaryis infinite or NaN).[14]
Towards the C2x standard
• A clarification of the restrict keyword.
• The restrict keyword is used to inform the compiler that a given object in memory can only be accessed by that pointer, as an
optimization. In C2x, there will be more detailed examples of how restrict ought to behave, the better to ensure compilers don’t end up
making unsafe or unnecessary optimizations.
• Making static_assert behave the same in C as it does in C++.
•The static_assert declaration, found in both C and C++, is used to ensure that a given constant expression is valid at compile time, but it is
implemented differently in the two languages. With this change, the C2x version will behave the same as the C++ version, making it easier to
share header code between the languages and to translate between C and C++.
•Better definitions for behavior of unions.
•Different implementations of C have different behavior when it comes to anonymous unions, a feature added in C11. C2x clarifies how this
works so it’s not dependent on the implementation.
The IT world when C was invented
• The programmers view of the computer:
• This model was pretty accurate in 1970
(and lasted until 1985)
• C was as expressively time-accurate as a
language could be: almost all C operators
took one or two cycles.
• Compiler needed to run on PDP-11 with
24KB of memory
Programming Languages Genealogy
Fortran (1954)
Algol (1958)
LISP (1957)
Scheme (1975)
CPL (1963), U Cambridge
Combined Programming Language
BCPL (1967), MIT
Basic Combined Programming Language
B (1969), Bell Labs
C (1970), Bell Labs
C++ (1983), Bell Labs
Java (1995), Sun
Objective C
Simula (1967)
C as a language
• C is an imperative block structured programming language
• C has variables and assignments (thus a C program manipulates a state)
• C has structured commands like while and for loops
• But also goto
• Blocks of commands are enclosed within { } which defines a scope
• Scopes can be nested
• Flat block structure for functions
• Simplifies compilation and run-time stack management
• Function definitions
• the main() function is where program execution begins
• Function parameters in C are passed by value to the function
• By reference is achieved through pointers
• Functions are usually given a prototype line
• either at the top of the source file, or in a separate file called a header file(.h extension)
• Facilitates separate compilation and mutual recursion in single pass compiler
Basic types and type modifiers
• character char
• integer int
• floating point float
• double floating point double
• valueless void
• signed
• unsigned
• short
• long
constructed types
• Enum
• Structs
• Unions
• Arrays
• Strings
• are simply character arrays, terminating null character, ascii '0'.
• Pointers
C was designed for a single pass compiler
Mutual recursion problem:
– Every identifier must be declared
before it is used.
– How to handle mutual recursion then?
17
void ping(int x)
{
pong(x-1); ...
}
void pong(int x)
{
ping(x); ...
}
void pong(int x);
void ping(x:integer)
{
pong(x-1); ...
}
Void pong(int x)
{
ping(x); ...
}
OK!
Criteria in a good language design and C
• Readability
– understand and comprehend a computation easily and
accurately
• Write-ability
– express a computation clearly, correctly, concisely, and
quickly
• Reliability
– assures a program will not behave in unexpected or
disastrous ways
• Orthogonality
– A relatively small set of primitive constructs can be
combined in a relatively small number of ways
– Every possible combination is legal
– Lack of orthogonality leads to exceptions to rules
• Readability/Writability
– Structured commands:
• if, while, for, etc.
• Even goto requires label not line no.
– Pointer can be hard to read
– { } vs. begin .. end
– = vs. :=
• Reliability
– Type checking, but weak
– No exception mechanism
– No bounds check
– No required variable initialization
• Orthogonality
– No return of arrays
– No nested functions
18
Tennent’s Language Design principles
• C has two main syntactic categories
– Expressions
• Functions
– Commands
• Procedures – void functions
• Thus C satisfy the Principle of Abs
• Some violation of Data Type compl
– E.g. no array return
19
Principle of correspondence
• Example in Pascal:
var i : integer;
begin
i := -j;
write(i)
end
and
procedure p(i : integer);
begin
write(i)
end;
begin p(-j) end
• Are equivalent
• Example in C
int i;
Void main() {
i = -j;
printf(i)
}
and
void p(int i) {
printf(i)
}
void main() {
p(-j)
}
• Are equivalent
20
Example of missing correspondence
In Pascal:
procedure inc(var i : integer);
begin
i := i + 1
end;
var x : integer;
begin
x := 1;
inc(x);
writeln(x);
end
No corresponding declaration
However C has correspondence
void inc(int *i) {
*i = *i + 1;
}
int x = 1;
inc(&x);
printf("%d", x);
int x = 1;
{
int *i = &x;
*i = *i + 1;
}
printf("%d", x);
21
Pointers in C and the C memory model
Address
0x3dc
0x3d8
Program Memory
0x3cc
0x3c8
0x3c4
0x3c0
Note: The compiler converts z[1] or *(z+1) to
Value at address (Address of z + sizeof(int));
In C you would write the byte address as:
(char *)z + sizeof(int);
or letting the compiler do the work for you
(int *)z + 1;
int main (int argc, argv) {
int x = 4;
int *y = &x;
int *z[4] = {NULL, NULL, NULL, NULL};
int a[4] = {1, 2, 3, 4};
...
0x3bc
0x3b8
0x3b4
0x3b0
0x3d4
0x3d0
z[3]
z[2]
z[1]
z[0]
a[3]
a[2]
a[1]
a[0]
4
0x3dc
0
0
0
0
4
3
2
1
NA
NA
x
y
Beware of pointers
int f (void) {
int s = 1;
int t = 1;
int *ps = &s;
int **pps = &ps;
int *pt = &t;
**pps = 2;
pt = ps;
*pt = 3;
t = s;
}
s == 1, t == 1
s == 2, t == 1
s == 3, t == 3
s == 3, t == 1
Parameter Passing in C (and pointers)
• Actual parameters are transferred by value
void swap (int a, int b) {
int tmp = b; b = a; a = tmp;
}
int main (void) {
int i = 3;
int j = 4;
swap (i, j);
…
}
The value of i (3) is passed, not its location!
swap does nothing
Parameter Passing in C
• Can pass addresses around
void swap (int *a, int *b) {
int tmp = *b; *b = *a; *a = tmp;
}
int main (void) {
int i = 3;
int j = 4;
swap (&i, &j);
…
}
The value of &i is passed, which is the address of i
Pointers to functions
Map in C
Beware!
int *value (void)
{
int i = 3;
return &i;
}
void callme (void)
{
int x = 35;
}
int main (void) {
int *ip;
ip = value ();
printf (“*ip == %dn", *ip);
callme ();
printf ("*ip == %dn", *ip);
}
*ip == 3
*ip == 35
But it could really be anything!
20 November 2003 CS 201J Fall 2003 29
Manipulating Addresses
char s[6];
s[0] = ‘h’;
s[1] = ‘e’;
s[2]= ‘l’;
s[3] = ‘l’;
s[4] = ‘o’;
s[5] = ‘0’;
printf (“s: %sn”, s);
s: hello
expr1[expr2] in C is just syntactic sugar for
*(expr1 + expr2)
20 November 2003 CS 201J Fall 2003 30
Obfuscating C
char s[6];
*s = ‘h’;
*(s + 1) = ‘e’;
2[s] = ‘l’;
3[s] = ‘l’;
*(s + 4) = ‘o’;
5[s] = ‘0’;
printf (“s: %sn”, s);
s: hello
In C, undefined behavior means anything can
happen
• With undefined behavior, “Anything at all can happen; the Standard
imposes no requirements. The program may fail to compile, or it may
execute incorrectly (either crashing or silently generating incorrect results),
or it may fortuitously do exactly what the programmer intended.” [C FAQ]
• “If any step in a program’s execution has undefined behavior, then the
entire execution is without meaning.
– This is important: it’s not that evaluating (1<<32) has an unpredictable result, but
rather that the entire execution of a program that evaluates this expression is
meaningless.
– Also, it’s not that the execution is meaningful up to the point where undefined
behavior happens: the bad effects can actually precede the undefined operation.”
[Regehr]
31
[Regehr] Regehr, John. A Guide to Undefined Behavior in C and C++, Parts 1-3. http://blog.regehr.org/archives/213
Examples of undefined behaviors
• Pointer
– Dereferencing a NULL pointer
– Using pointers to objects whose lifetime has ended
– Dereferencing a pointer that has not yet been definitely initialized
– Performing pointer arithmetic that yields a result outside the
boundaries (either above or below) of an array.
– Dereferencing the pointer at a location beyond the end of an array
• Buffer overflows
– Reading or writing to an object or array at an offset that is negative, or
beyond the size of that object (stack/heap overflow)
• Integer Overflows
– Signed integer overflow
– Evaluating an expression that is not mathematically defined
– Left-shifting values by a negative amount (right shifts by negative
amounts are implementation defined)
– Shifting values by an amount greater than or equal to the number of
bits in the number
32
http://stackoverflow.com/questions/367633/what-are-all-the-common-undefined-behaviours-that-a-c-programmer-should-know-a
What really happens with undefined code?
• C compilers are allowed to assume that undefined behaviors cannot
happen
– Market pressures for performance encourage this
– Ever-more-aggressive optimizations are increasingly adding dependencies on
this assumption
– Leads to faster code but also turns previously-working code into broken code
• C compilers are no longer “high level assemblers”
– They implement a complex model
33
A trivial division by 0 example
• Trivial example from Linux kernel lib/mpi/mpi-pow.c:
if (!msize)
msize = 1 / msize; /* provoke a signal */
• On gcc with x86, generated signal as expected
• On gcc with PowerPC, does not generate exception
• On clang, no code generated at all
– Division by 0 is undefined behavior
– Since this “can’t” happen, the compiler presumes that on this branch msize !=
0
– Since this branch only occurs when msize == 0, it must be impossible, and
compiler removes everything as dead code
34
[Wang2012] Wang, Xi, et al., Undefined Behavior: What Happened to My Code?,
APSys ‘12, 2012, ACM, https://pdos.csail.mit.edu/papers/ub:apsys12.pdf
Optimizer reordering creates
debugging snares
void bar (void);
int a;
void foo3(unsigned y, unsigned z)
{
bar();
a = y%z;
}
void bar(void)
{
setlinebuf(stdout);
printf ("hello!n");
}
int main(void)
{
foo3(1,0);
return 0;
}
35
On many compilers, when optimizing
this will crash without printing “hello”
first because foo3 will compute before
calling bar().
C compilers are allowed to reorder
anything without side-effects, and
undefined behaviors don’t need to be
considered as side effects. Source: [Regehr]
Clang-compiled program executes function
never called (undef issue)
#include <cstdlib>
typedef int (*Function)();
static Function Do;
static int EraseAll() {
return system("rm -rf /");
}
void NeverCalled() {
Do = EraseAll;
}
int main() {
return Do();
}
main:
movl $.L.str, %edi
jmp system
.L.str:
.asciz "rm -rf /"
36
Source: “Why undefined behavior may call a never-called function”, Krister Walfridsson, September 4,
2017, https://kristerw.blogspot.com/2017/09/why-undefined-behavior-may-call-never.html
This always runs “rm –rf /”, even though
NeverCalled() is never called.
The value of Do() is never set, so execution
behavior of the program is undefined.
Seeding random numbers in BSD libc
struct timeval tv;
unsigned long junk; /* XXX left uninitialized on purpose */
gettimeofday(&tv, NULL);
srandom((getpid() << 16) ^ tv.tv_sec ^ tv.tv_usec ^ junk);
Process IDSeed Time Junk value
38
A C programmer’s view of computers
This model was pretty accurate for the PDP-11 in 1973 and worked fine
until 1985. Processors (386, ARM, MIPS, SPARC) all ran at 1–10MHz clock
speed and could access external memory in 1 cycle; and most
instructions took 1 cycle.
Indeed the C language was as expressively time-accurate as a
language could be: almost all C operators took one or two cycles.
But this model is no longer accurate!
39
A modern view of memory timings
So what happened?
On-chip computation (clock-speed) sped up faster (1985–2005) than off-chip
communication (with memory) as feature sizes shrank.
The gap was filled by spending transistor budget on caches which
(statistically) filled the mismatch until 2005 or so.
Techniques like caches, deep pipelining with bypasses, and
superscalar instruction issue burned power to preserve our illusions.
2005 or so was crunch point as faster, hotter, single-CPU Pentiums
were scrapped. These techniques had delayed the inevitable.
40
The Current Mainstream Processor
Will scale to 2, 4 maybe 8 processors.
But ultimately shared memory becomes the bottleneck (1024 processors?!?).
… introduce NUMA (Non Uniform Memory Access) …
CPU and GPU on one chip
41
Concurrency on Multiprocessors
Output not consistent with any interleaved execution!
 can be the result of out-of-order stores
 can be the result of out-of-order loads
 improves performance
x = 1
y = 1
print y
print x
thread 1 thread 2
→ 1
→ 0
Initially x = y = 0
Conclusions
• “C is quirky, flawed, and a tremendous success”
• C is not a bad language (for its time)
• C programming requires (very) skilled programmers
• C programmers often abuse language features
• C undefined behaviour is problematic
• But the C standard is moving in the right direction
• C’s memory model is problematic in the multi-core world
• C is no longer a low level language
• I hate and love C !
C som-programmeringssprog-bt

Mais conteúdo relacionado

Mais procurados

C Programming Language Step by Step Part 1
C Programming Language Step by Step Part 1C Programming Language Step by Step Part 1
C Programming Language Step by Step Part 1Rumman Ansari
 
Chapter 1: Introduction
Chapter 1: IntroductionChapter 1: Introduction
Chapter 1: IntroductionEric Chou
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programmingManoj Tyagi
 
Introduction to programming with c,
Introduction to programming with c,Introduction to programming with c,
Introduction to programming with c,Hossain Md Shakhawat
 
Sachin kumar ppt on programming in c
Sachin kumar ppt on programming in cSachin kumar ppt on programming in c
Sachin kumar ppt on programming in cSachin Kumar
 
Fundamentals of programming and problem solving
Fundamentals of programming and problem solvingFundamentals of programming and problem solving
Fundamentals of programming and problem solvingJustine Dela Serna
 
Issues in the design of Code Generator
Issues in the design of Code GeneratorIssues in the design of Code Generator
Issues in the design of Code GeneratorDarshan sai Reddy
 
New c sharp4_features_part_iv
New c sharp4_features_part_ivNew c sharp4_features_part_iv
New c sharp4_features_part_ivNico Ludwig
 
C programming basics
C  programming basicsC  programming basics
C programming basicsargusacademy
 
Why C is Called Structured Programming Language
Why C is Called Structured Programming LanguageWhy C is Called Structured Programming Language
Why C is Called Structured Programming LanguageSinbad Konick
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C ProgrammingMOHAMAD NOH AHMAD
 
C programming language
C programming languageC programming language
C programming languageMaha lakshmi
 
introduction to C programming (C)
introduction to C programming (C)introduction to C programming (C)
introduction to C programming (C)Abhishek Walia
 
C language programming
C language programmingC language programming
C language programmingpullarao29
 
Declare Your Language: What is a Compiler?
Declare Your Language: What is a Compiler?Declare Your Language: What is a Compiler?
Declare Your Language: What is a Compiler?Eelco Visser
 
Issues in design_of_code_generator
Issues in design_of_code_generatorIssues in design_of_code_generator
Issues in design_of_code_generatorvinithapanneer
 

Mais procurados (20)

C Programming Language Step by Step Part 1
C Programming Language Step by Step Part 1C Programming Language Step by Step Part 1
C Programming Language Step by Step Part 1
 
Chapter 1: Introduction
Chapter 1: IntroductionChapter 1: Introduction
Chapter 1: Introduction
 
C PROGRAMMING
C PROGRAMMINGC PROGRAMMING
C PROGRAMMING
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Introduction to programming with c,
Introduction to programming with c,Introduction to programming with c,
Introduction to programming with c,
 
Sachin kumar ppt on programming in c
Sachin kumar ppt on programming in cSachin kumar ppt on programming in c
Sachin kumar ppt on programming in c
 
Code Generation
Code GenerationCode Generation
Code Generation
 
Fundamentals of programming and problem solving
Fundamentals of programming and problem solvingFundamentals of programming and problem solving
Fundamentals of programming and problem solving
 
Issues in the design of Code Generator
Issues in the design of Code GeneratorIssues in the design of Code Generator
Issues in the design of Code Generator
 
New c sharp4_features_part_iv
New c sharp4_features_part_ivNew c sharp4_features_part_iv
New c sharp4_features_part_iv
 
Compiler Design Unit 1
Compiler Design Unit 1Compiler Design Unit 1
Compiler Design Unit 1
 
C programming basics
C  programming basicsC  programming basics
C programming basics
 
Why C is Called Structured Programming Language
Why C is Called Structured Programming LanguageWhy C is Called Structured Programming Language
Why C is Called Structured Programming Language
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
C programming language
C programming languageC programming language
C programming language
 
introduction to C programming (C)
introduction to C programming (C)introduction to C programming (C)
introduction to C programming (C)
 
C language programming
C language programmingC language programming
C language programming
 
Declare Your Language: What is a Compiler?
Declare Your Language: What is a Compiler?Declare Your Language: What is a Compiler?
Declare Your Language: What is a Compiler?
 
Issues in design_of_code_generator
Issues in design_of_code_generatorIssues in design_of_code_generator
Issues in design_of_code_generator
 
Embedded c
Embedded cEmbedded c
Embedded c
 

Semelhante a C som-programmeringssprog-bt

Computer Programming In C.pptx
Computer Programming In C.pptxComputer Programming In C.pptx
Computer Programming In C.pptxchouguleamruta24
 
Cpu.ppt INTRODUCTION TO “C”
Cpu.ppt INTRODUCTION TO “C” Cpu.ppt INTRODUCTION TO “C”
Cpu.ppt INTRODUCTION TO “C” Sukhvinder Singh
 
CS4443 - Modern Programming Language - I Lecture (1)
CS4443 - Modern Programming Language - I Lecture (1)CS4443 - Modern Programming Language - I Lecture (1)
CS4443 - Modern Programming Language - I Lecture (1)Dilawar Khan
 
67404923-C-Programming-Tutorials-Doc.pdf
67404923-C-Programming-Tutorials-Doc.pdf67404923-C-Programming-Tutorials-Doc.pdf
67404923-C-Programming-Tutorials-Doc.pdfRajb54
 
Basics of C Lecture 2[16097].pptx
Basics of C Lecture 2[16097].pptxBasics of C Lecture 2[16097].pptx
Basics of C Lecture 2[16097].pptxCoolGamer16
 
C++ vs C#
C++ vs C#C++ vs C#
C++ vs C#sudipv
 
C language myths & secrets
C language myths & secretsC language myths & secrets
C language myths & secretsankush1510
 
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTRE
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTREC & C++ Training Centre in Ambala! BATRA COMPUTER CENTRE
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTREjatin batra
 
c-introduction.pptx
c-introduction.pptxc-introduction.pptx
c-introduction.pptxMangala R
 
.NET Core, ASP.NET Core Course, Session 3
.NET Core, ASP.NET Core Course, Session 3.NET Core, ASP.NET Core Course, Session 3
.NET Core, ASP.NET Core Course, Session 3aminmesbahi
 

Semelhante a C som-programmeringssprog-bt (20)

Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
 
Computer Programming In C.pptx
Computer Programming In C.pptxComputer Programming In C.pptx
Computer Programming In C.pptx
 
Cpu.ppt INTRODUCTION TO “C”
Cpu.ppt INTRODUCTION TO “C” Cpu.ppt INTRODUCTION TO “C”
Cpu.ppt INTRODUCTION TO “C”
 
CS4443 - Modern Programming Language - I Lecture (1)
CS4443 - Modern Programming Language - I Lecture (1)CS4443 - Modern Programming Language - I Lecture (1)
CS4443 - Modern Programming Language - I Lecture (1)
 
Introduction Of C++
Introduction Of C++Introduction Of C++
Introduction Of C++
 
C for Engineers
C for EngineersC for Engineers
C for Engineers
 
67404923-C-Programming-Tutorials-Doc.pdf
67404923-C-Programming-Tutorials-Doc.pdf67404923-C-Programming-Tutorials-Doc.pdf
67404923-C-Programming-Tutorials-Doc.pdf
 
Basics of C Lecture 2[16097].pptx
Basics of C Lecture 2[16097].pptxBasics of C Lecture 2[16097].pptx
Basics of C Lecture 2[16097].pptx
 
C language unit-1
C language unit-1C language unit-1
C language unit-1
 
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDYC LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
 
C languaGE UNIT-1
C languaGE UNIT-1C languaGE UNIT-1
C languaGE UNIT-1
 
C++ vs C#
C++ vs C#C++ vs C#
C++ vs C#
 
C language myths & secrets
C language myths & secretsC language myths & secrets
C language myths & secrets
 
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTRE
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTREC & C++ Training Centre in Ambala! BATRA COMPUTER CENTRE
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTRE
 
Embedded _c_
Embedded  _c_Embedded  _c_
Embedded _c_
 
Unit ii
Unit   iiUnit   ii
Unit ii
 
c-introduction.pptx
c-introduction.pptxc-introduction.pptx
c-introduction.pptx
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
c++ ppt.ppt
c++ ppt.pptc++ ppt.ppt
c++ ppt.ppt
 
.NET Core, ASP.NET Core Course, Session 3
.NET Core, ASP.NET Core Course, Session 3.NET Core, ASP.NET Core Course, Session 3
.NET Core, ASP.NET Core Course, Session 3
 

Mais de InfinIT - Innovationsnetværket for it

Mais de InfinIT - Innovationsnetværket for it (20)

Erfaringer med-c kurt-noermark
Erfaringer med-c kurt-noermarkErfaringer med-c kurt-noermark
Erfaringer med-c kurt-noermark
 
Object orientering, test driven development og c
Object orientering, test driven development og cObject orientering, test driven development og c
Object orientering, test driven development og c
 
Embedded softwaredevelopment hcs
Embedded softwaredevelopment hcsEmbedded softwaredevelopment hcs
Embedded softwaredevelopment hcs
 
C og c++-jens lund jensen
C og c++-jens lund jensenC og c++-jens lund jensen
C og c++-jens lund jensen
 
201811xx foredrag c_cpp
201811xx foredrag c_cpp201811xx foredrag c_cpp
201811xx foredrag c_cpp
 
Infinit seminar 060918
Infinit seminar 060918Infinit seminar 060918
Infinit seminar 060918
 
DCR solutions
DCR solutionsDCR solutions
DCR solutions
 
Not your grandfathers BPM
Not your grandfathers BPMNot your grandfathers BPM
Not your grandfathers BPM
 
Kmd workzone - an evolutionary approach to revolution
Kmd workzone - an evolutionary approach to revolutionKmd workzone - an evolutionary approach to revolution
Kmd workzone - an evolutionary approach to revolution
 
EcoKnow - oplæg
EcoKnow - oplægEcoKnow - oplæg
EcoKnow - oplæg
 
Martin Wickins Chatbots i fronten
Martin Wickins Chatbots i frontenMartin Wickins Chatbots i fronten
Martin Wickins Chatbots i fronten
 
Marie Fenger ai kundeservice
Marie Fenger ai kundeserviceMarie Fenger ai kundeservice
Marie Fenger ai kundeservice
 
Mads Kaysen SupWiz
Mads Kaysen SupWizMads Kaysen SupWiz
Mads Kaysen SupWiz
 
Leif Howalt NNIT Service Support Center
Leif Howalt NNIT Service Support CenterLeif Howalt NNIT Service Support Center
Leif Howalt NNIT Service Support Center
 
Jan Neerbek NLP og Chatbots
Jan Neerbek NLP og ChatbotsJan Neerbek NLP og Chatbots
Jan Neerbek NLP og Chatbots
 
Anders Soegaard NLP for Customer Support
Anders Soegaard NLP for Customer SupportAnders Soegaard NLP for Customer Support
Anders Soegaard NLP for Customer Support
 
Stephen Alstrup infinit august 2018
Stephen Alstrup infinit august 2018Stephen Alstrup infinit august 2018
Stephen Alstrup infinit august 2018
 
Innovation og værdiskabelse i it-projekter
Innovation og værdiskabelse i it-projekterInnovation og værdiskabelse i it-projekter
Innovation og værdiskabelse i it-projekter
 
Rokoko infin it presentation
Rokoko infin it presentation Rokoko infin it presentation
Rokoko infin it presentation
 
Kenny erleben infinit_workshop
Kenny erleben infinit_workshopKenny erleben infinit_workshop
Kenny erleben infinit_workshop
 

Último

Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 

Último (20)

Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 

C som-programmeringssprog-bt

  • 1. The C programming language Why we love to hate C and hate that we love C Bent Thomsen Institut for Datalogi, AAU InfinIT Embedded Systems Engineering interest group meeting 21.11.2018
  • 2. “C is quirky, flawed, and a tremendous success” Dennis Ritchie, inventor of C #include <stdio.h> int main() { /* my first program in C */ printf("Hello, World! n"); return 0; }
  • 4. The IT world runs on C • v6UNIX About 9,000 LoC – mainly C and assembly • L4 micro kernel About 6,400 LoC – mainly C and assembly • seL4 About 10,200 LoC – mainly C and assembly • Linux 2.6 About 5.6M LoC – mainly C and assembly • Current Linux About 16M LoC – mainly C and assembly • Current Mac OS X About 13M LoC – some C, but mainly ObjectiveC • Current iOS About 12M LoC - some C, but mainly ObjectiveC • Android About 15M LoC – C, C++, Java and others • NT 3.1 About 5M LoC – C, C++ and assembly • XP About 48M LoC - C, C++ and assembly • Windows 10 About 60M LoC - C, C++ and assembly Source: Various more or less reliable source on the web
  • 5. The Embedded world and C Aspencore: 2017 Embedded Market Study
  • 6. C Language History • Dennis Ritchie developed the language C in years 1969-1973 • C was developed as a high level language for writing the Unix OS • C was based on CPL, BCPL and B – also influenced by Algol • In 1978 Dennis Ritchie and Brian Kernighan wrote a book on C: • The C Programming Language, Prentice-Hall. ISBN 0-13-110163-3. • includes a definition of the C language in BNF • instrumental in removing syntax ambiguities between different versions of the language • In 1982 American National Standards for Information Systems (ANSI) established a committee with the goal of producing a C standard • In 1989 ISO published its first C standard (C89) • The C standard has been revised in 1999 (C99), 2011 (C11) and 2018 (C18)
  • 7. The C89 standard – 519 pages
  • 8. C99• C99 introduced several new features, many of which had already been implemented as extensions in several compilers:[4] • inline functions • intermingled declarations and code: variable declaration is no longer restricted to file scope or the start of a compound statement (block), facilitating static single assignment form • several new data types, including long long int, optional extended integer types, an explicit boolean data type, and a complex type to represent complex numbers • variable-length arrays (although subsequently relegated in C11 to a conditional feature that implementationsare not required to support) • flexible array members • support for one-line comments beginning with //, as in BCPL, C++ and Java • new library functions, such as snprintf • new headers, such as <stdbool.h>,<complex.h>, <tgmath.h>, and <inttypes.h> • type-generic math (macro) functions, in <tgmath.h>, which select a math library function based upon float, double, or long double arguments, etc. • improved support for IEEE floating point • designated initializers • compound literals (for instance, it is possible to construct structures in function calls: function((struct x) {1, 2}))[5] • support for variadic macros (macros with a variable number of arguments) • restrict qualification allows more aggressive code optimization, removing compile-time array access advantages previously held by FORTRAN over ANSI C[6] • universal character names, which allows user variables to contain other characters than the standard character set • keyword static in array indices in parameter declarations[7]
  • 9. C11 • The standard includes severalchanges to the C99 languageand libraryspecifications,such as:[9] • Alignment specification (_Alignas specifier, _Alignof operator,aligned_allocfunction, <stdalign.h>header file) • The _Noreturn function specifier and the <stdnoreturn.h>header file • Type-generic expressionsusingthe _Generic keyword. For example, the followingmacro cbrt(x) translates to cbrtl(x), cbrt(x) or cbrtf(x) dependingon the type of x: • #define cbrt(x) _Generic((x),long double:cbrtl, • default: cbrt, • float: cbrtf)(x) • Multi-threadingsupport(_Thread_localstorage-classspecifier, <threads.h>header includingthread creation/managementfunctions,mutex, condition variableand thread- specific storage functionality, as well as <stdatomic.h>[10]for atomic operations supportingthe C11 memorymodel). • Improved Unicodesupportbased on the C Unicode Technical Report ISO/IECTR 19769:2004 (char16_t and char32_t types for storing UTF-16/UTF-32 encoded data, including conversion functions in <uchar.h>and the correspondingu and U string literal prefixes,as well as the u8 prefix for UTF-8 encoded literals).[11] • Removal of the gets function,deprecated in the previous C language standard revision,ISO/IEC9899:1999/Cor.3:2007(E), in favor of a new safe alternative, gets_s. • Bounds-checkinginterfaces (Annex K).[12] • Analyzabilityfeatures (Annex L). • More macros for querying the characteristics of floating pointtypes, concerning subnormalfloatingpointnumbers and the number of decimal digits the type is able to store. • Anonymousstructures and unions,usefulwhen unions and structures are nested, e.g. in struct T { int tag; union { float x; int n; }; };. • Static assertions,which are evaluated during translation at a later phasethan #if and #error, when types are understood bythe translator. • An exclusive create-and-openmode("…x" suffix) for fopen. This behaves like O_CREAT|O_EXCL in POSIX, which is commonlyused for lock files. • The quick_exit function as a third way to terminate a program, intended to do at least minimaldeinitialization if termination with exit fails.[13] • Macros for the construction of complex values (partly becausereal + imaginary*I might not yield the expected value if imaginaryis infinite or NaN).[14]
  • 10. Towards the C2x standard • A clarification of the restrict keyword. • The restrict keyword is used to inform the compiler that a given object in memory can only be accessed by that pointer, as an optimization. In C2x, there will be more detailed examples of how restrict ought to behave, the better to ensure compilers don’t end up making unsafe or unnecessary optimizations. • Making static_assert behave the same in C as it does in C++. •The static_assert declaration, found in both C and C++, is used to ensure that a given constant expression is valid at compile time, but it is implemented differently in the two languages. With this change, the C2x version will behave the same as the C++ version, making it easier to share header code between the languages and to translate between C and C++. •Better definitions for behavior of unions. •Different implementations of C have different behavior when it comes to anonymous unions, a feature added in C11. C2x clarifies how this works so it’s not dependent on the implementation.
  • 11. The IT world when C was invented • The programmers view of the computer: • This model was pretty accurate in 1970 (and lasted until 1985) • C was as expressively time-accurate as a language could be: almost all C operators took one or two cycles. • Compiler needed to run on PDP-11 with 24KB of memory
  • 12. Programming Languages Genealogy Fortran (1954) Algol (1958) LISP (1957) Scheme (1975) CPL (1963), U Cambridge Combined Programming Language BCPL (1967), MIT Basic Combined Programming Language B (1969), Bell Labs C (1970), Bell Labs C++ (1983), Bell Labs Java (1995), Sun Objective C Simula (1967)
  • 13. C as a language • C is an imperative block structured programming language • C has variables and assignments (thus a C program manipulates a state) • C has structured commands like while and for loops • But also goto • Blocks of commands are enclosed within { } which defines a scope • Scopes can be nested • Flat block structure for functions • Simplifies compilation and run-time stack management • Function definitions • the main() function is where program execution begins • Function parameters in C are passed by value to the function • By reference is achieved through pointers • Functions are usually given a prototype line • either at the top of the source file, or in a separate file called a header file(.h extension) • Facilitates separate compilation and mutual recursion in single pass compiler
  • 14. Basic types and type modifiers • character char • integer int • floating point float • double floating point double • valueless void • signed • unsigned • short • long
  • 15. constructed types • Enum • Structs • Unions • Arrays • Strings • are simply character arrays, terminating null character, ascii '0'. • Pointers
  • 16.
  • 17. C was designed for a single pass compiler Mutual recursion problem: – Every identifier must be declared before it is used. – How to handle mutual recursion then? 17 void ping(int x) { pong(x-1); ... } void pong(int x) { ping(x); ... } void pong(int x); void ping(x:integer) { pong(x-1); ... } Void pong(int x) { ping(x); ... } OK!
  • 18. Criteria in a good language design and C • Readability – understand and comprehend a computation easily and accurately • Write-ability – express a computation clearly, correctly, concisely, and quickly • Reliability – assures a program will not behave in unexpected or disastrous ways • Orthogonality – A relatively small set of primitive constructs can be combined in a relatively small number of ways – Every possible combination is legal – Lack of orthogonality leads to exceptions to rules • Readability/Writability – Structured commands: • if, while, for, etc. • Even goto requires label not line no. – Pointer can be hard to read – { } vs. begin .. end – = vs. := • Reliability – Type checking, but weak – No exception mechanism – No bounds check – No required variable initialization • Orthogonality – No return of arrays – No nested functions 18
  • 19. Tennent’s Language Design principles • C has two main syntactic categories – Expressions • Functions – Commands • Procedures – void functions • Thus C satisfy the Principle of Abs • Some violation of Data Type compl – E.g. no array return 19
  • 20. Principle of correspondence • Example in Pascal: var i : integer; begin i := -j; write(i) end and procedure p(i : integer); begin write(i) end; begin p(-j) end • Are equivalent • Example in C int i; Void main() { i = -j; printf(i) } and void p(int i) { printf(i) } void main() { p(-j) } • Are equivalent 20
  • 21. Example of missing correspondence In Pascal: procedure inc(var i : integer); begin i := i + 1 end; var x : integer; begin x := 1; inc(x); writeln(x); end No corresponding declaration However C has correspondence void inc(int *i) { *i = *i + 1; } int x = 1; inc(&x); printf("%d", x); int x = 1; { int *i = &x; *i = *i + 1; } printf("%d", x); 21
  • 22. Pointers in C and the C memory model Address 0x3dc 0x3d8 Program Memory 0x3cc 0x3c8 0x3c4 0x3c0 Note: The compiler converts z[1] or *(z+1) to Value at address (Address of z + sizeof(int)); In C you would write the byte address as: (char *)z + sizeof(int); or letting the compiler do the work for you (int *)z + 1; int main (int argc, argv) { int x = 4; int *y = &x; int *z[4] = {NULL, NULL, NULL, NULL}; int a[4] = {1, 2, 3, 4}; ... 0x3bc 0x3b8 0x3b4 0x3b0 0x3d4 0x3d0 z[3] z[2] z[1] z[0] a[3] a[2] a[1] a[0] 4 0x3dc 0 0 0 0 4 3 2 1 NA NA x y
  • 23. Beware of pointers int f (void) { int s = 1; int t = 1; int *ps = &s; int **pps = &ps; int *pt = &t; **pps = 2; pt = ps; *pt = 3; t = s; } s == 1, t == 1 s == 2, t == 1 s == 3, t == 3 s == 3, t == 1
  • 24. Parameter Passing in C (and pointers) • Actual parameters are transferred by value void swap (int a, int b) { int tmp = b; b = a; a = tmp; } int main (void) { int i = 3; int j = 4; swap (i, j); … } The value of i (3) is passed, not its location! swap does nothing
  • 25. Parameter Passing in C • Can pass addresses around void swap (int *a, int *b) { int tmp = *b; *b = *a; *a = tmp; } int main (void) { int i = 3; int j = 4; swap (&i, &j); … } The value of &i is passed, which is the address of i
  • 28. Beware! int *value (void) { int i = 3; return &i; } void callme (void) { int x = 35; } int main (void) { int *ip; ip = value (); printf (“*ip == %dn", *ip); callme (); printf ("*ip == %dn", *ip); } *ip == 3 *ip == 35 But it could really be anything!
  • 29. 20 November 2003 CS 201J Fall 2003 29 Manipulating Addresses char s[6]; s[0] = ‘h’; s[1] = ‘e’; s[2]= ‘l’; s[3] = ‘l’; s[4] = ‘o’; s[5] = ‘0’; printf (“s: %sn”, s); s: hello expr1[expr2] in C is just syntactic sugar for *(expr1 + expr2)
  • 30. 20 November 2003 CS 201J Fall 2003 30 Obfuscating C char s[6]; *s = ‘h’; *(s + 1) = ‘e’; 2[s] = ‘l’; 3[s] = ‘l’; *(s + 4) = ‘o’; 5[s] = ‘0’; printf (“s: %sn”, s); s: hello
  • 31. In C, undefined behavior means anything can happen • With undefined behavior, “Anything at all can happen; the Standard imposes no requirements. The program may fail to compile, or it may execute incorrectly (either crashing or silently generating incorrect results), or it may fortuitously do exactly what the programmer intended.” [C FAQ] • “If any step in a program’s execution has undefined behavior, then the entire execution is without meaning. – This is important: it’s not that evaluating (1<<32) has an unpredictable result, but rather that the entire execution of a program that evaluates this expression is meaningless. – Also, it’s not that the execution is meaningful up to the point where undefined behavior happens: the bad effects can actually precede the undefined operation.” [Regehr] 31 [Regehr] Regehr, John. A Guide to Undefined Behavior in C and C++, Parts 1-3. http://blog.regehr.org/archives/213
  • 32. Examples of undefined behaviors • Pointer – Dereferencing a NULL pointer – Using pointers to objects whose lifetime has ended – Dereferencing a pointer that has not yet been definitely initialized – Performing pointer arithmetic that yields a result outside the boundaries (either above or below) of an array. – Dereferencing the pointer at a location beyond the end of an array • Buffer overflows – Reading or writing to an object or array at an offset that is negative, or beyond the size of that object (stack/heap overflow) • Integer Overflows – Signed integer overflow – Evaluating an expression that is not mathematically defined – Left-shifting values by a negative amount (right shifts by negative amounts are implementation defined) – Shifting values by an amount greater than or equal to the number of bits in the number 32 http://stackoverflow.com/questions/367633/what-are-all-the-common-undefined-behaviours-that-a-c-programmer-should-know-a
  • 33. What really happens with undefined code? • C compilers are allowed to assume that undefined behaviors cannot happen – Market pressures for performance encourage this – Ever-more-aggressive optimizations are increasingly adding dependencies on this assumption – Leads to faster code but also turns previously-working code into broken code • C compilers are no longer “high level assemblers” – They implement a complex model 33
  • 34. A trivial division by 0 example • Trivial example from Linux kernel lib/mpi/mpi-pow.c: if (!msize) msize = 1 / msize; /* provoke a signal */ • On gcc with x86, generated signal as expected • On gcc with PowerPC, does not generate exception • On clang, no code generated at all – Division by 0 is undefined behavior – Since this “can’t” happen, the compiler presumes that on this branch msize != 0 – Since this branch only occurs when msize == 0, it must be impossible, and compiler removes everything as dead code 34 [Wang2012] Wang, Xi, et al., Undefined Behavior: What Happened to My Code?, APSys ‘12, 2012, ACM, https://pdos.csail.mit.edu/papers/ub:apsys12.pdf
  • 35. Optimizer reordering creates debugging snares void bar (void); int a; void foo3(unsigned y, unsigned z) { bar(); a = y%z; } void bar(void) { setlinebuf(stdout); printf ("hello!n"); } int main(void) { foo3(1,0); return 0; } 35 On many compilers, when optimizing this will crash without printing “hello” first because foo3 will compute before calling bar(). C compilers are allowed to reorder anything without side-effects, and undefined behaviors don’t need to be considered as side effects. Source: [Regehr]
  • 36. Clang-compiled program executes function never called (undef issue) #include <cstdlib> typedef int (*Function)(); static Function Do; static int EraseAll() { return system("rm -rf /"); } void NeverCalled() { Do = EraseAll; } int main() { return Do(); } main: movl $.L.str, %edi jmp system .L.str: .asciz "rm -rf /" 36 Source: “Why undefined behavior may call a never-called function”, Krister Walfridsson, September 4, 2017, https://kristerw.blogspot.com/2017/09/why-undefined-behavior-may-call-never.html This always runs “rm –rf /”, even though NeverCalled() is never called. The value of Do() is never set, so execution behavior of the program is undefined.
  • 37. Seeding random numbers in BSD libc struct timeval tv; unsigned long junk; /* XXX left uninitialized on purpose */ gettimeofday(&tv, NULL); srandom((getpid() << 16) ^ tv.tv_sec ^ tv.tv_usec ^ junk); Process IDSeed Time Junk value
  • 38. 38 A C programmer’s view of computers This model was pretty accurate for the PDP-11 in 1973 and worked fine until 1985. Processors (386, ARM, MIPS, SPARC) all ran at 1–10MHz clock speed and could access external memory in 1 cycle; and most instructions took 1 cycle. Indeed the C language was as expressively time-accurate as a language could be: almost all C operators took one or two cycles. But this model is no longer accurate!
  • 39. 39 A modern view of memory timings So what happened? On-chip computation (clock-speed) sped up faster (1985–2005) than off-chip communication (with memory) as feature sizes shrank. The gap was filled by spending transistor budget on caches which (statistically) filled the mismatch until 2005 or so. Techniques like caches, deep pipelining with bypasses, and superscalar instruction issue burned power to preserve our illusions. 2005 or so was crunch point as faster, hotter, single-CPU Pentiums were scrapped. These techniques had delayed the inevitable.
  • 40. 40 The Current Mainstream Processor Will scale to 2, 4 maybe 8 processors. But ultimately shared memory becomes the bottleneck (1024 processors?!?). … introduce NUMA (Non Uniform Memory Access) …
  • 41. CPU and GPU on one chip 41
  • 42. Concurrency on Multiprocessors Output not consistent with any interleaved execution!  can be the result of out-of-order stores  can be the result of out-of-order loads  improves performance x = 1 y = 1 print y print x thread 1 thread 2 → 1 → 0 Initially x = y = 0
  • 43. Conclusions • “C is quirky, flawed, and a tremendous success” • C is not a bad language (for its time) • C programming requires (very) skilled programmers • C programmers often abuse language features • C undefined behaviour is problematic • But the C standard is moving in the right direction • C’s memory model is problematic in the multi-core world • C is no longer a low level language • I hate and love C !