The document provides information on the history and design of the C programming language. It was developed between 1969-1973 along with Unix and was designed for systems programming tasks like operating systems and compilers. Key aspects of C's design included producing efficient code, being portable across different processors/systems, and using less memory. C introduced many features still used in modern languages today like functions, variables, arrays, structures, pointers, and libraries.
This PPT is all about INTRODUCTION TO C LANGUAGE in which all basic topics are covered and the information present in this is refered from a book called "Let Us C" by YASHAVANT KANETKAR i.e. This PPT is having all important points of first chapter of "Let Us C". If any doubt plz ask us in comment.
This PPT is all about INTRODUCTION TO C LANGUAGE in which all basic topics are covered and the information present in this is refered from a book called "Let Us C" by YASHAVANT KANETKAR i.e. This PPT is having all important points of first chapter of "Let Us C". If any doubt plz ask us in comment.
An introduction to the C programming language for the students of the course "HJ-82 Ontwerpen voor de optie multimedia en signaalverwerking: seminaries", taught by the authors at the Catholic University of Leuven.
C Programming Language Tutorial for beginners - JavaTpointJavaTpoint.Com
JavaTpoint share a presentation of C Programming language for beginners and professionals. now in this slideshare you will be learned basics of c programming language, what is c programming language, history of c programming, installing turbo c, features of c programming language, datatypes of c language, operaters in c, control statement of c language, c language functions, c array, pointer in c programming, and structure and union.
The C programming language is a structure oriented programming language, developed at Bell Laboratories in 1972 by Dennis Ritchie C programming language features were derived from an earlier language called “B” (Basic Co..
At the end of this lecture students should be able to;
Describe features of C programming language.
Justify the terminology related to computer programming.
Define the editing, compiling, linking, debugging stages of C programming
Recognize the basic structure of a C program
Apply comments for C programs to improve readability.
This course teaches engineering students how to program in C. I gave this course for several years in the framework of the "Advanced Technology Higher Education Network" / SOCRATES program.
An introduction to the C programming language for the students of the course "HJ-82 Ontwerpen voor de optie multimedia en signaalverwerking: seminaries", taught by the authors at the Catholic University of Leuven.
C Programming Language Tutorial for beginners - JavaTpointJavaTpoint.Com
JavaTpoint share a presentation of C Programming language for beginners and professionals. now in this slideshare you will be learned basics of c programming language, what is c programming language, history of c programming, installing turbo c, features of c programming language, datatypes of c language, operaters in c, control statement of c language, c language functions, c array, pointer in c programming, and structure and union.
The C programming language is a structure oriented programming language, developed at Bell Laboratories in 1972 by Dennis Ritchie C programming language features were derived from an earlier language called “B” (Basic Co..
At the end of this lecture students should be able to;
Describe features of C programming language.
Justify the terminology related to computer programming.
Define the editing, compiling, linking, debugging stages of C programming
Recognize the basic structure of a C program
Apply comments for C programs to improve readability.
This course teaches engineering students how to program in C. I gave this course for several years in the framework of the "Advanced Technology Higher Education Network" / SOCRATES program.
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Chris Adamson
Abstract: Chances are you code in a language that's either descended from C, inspired by C, or run in an interpreter that itself is written in C. Still... do you actually know how to code in C? Despite its long-standing position as a sort of "lingua franca", an agreed-upon common language, more and more developers are putting together successful, satisfying careers, without ever learning this seminal language. But what if you have to call into C code from your favorite scripting language, or use APIs like OpenGL that are written to be called from C? Many developers find C very challenging, particularly its manual memory-management and other low-level concerns. In this session, we'll show you why you shouldn't be afraid of C, how you can use the skills you already have from the languages you code in today, and how to master structs, enums, typedefs, malloc(), free(), and the rest of C's sharp edges. Examples will be from the point-of-view of the C-skewing iPhone SDK, but will be designed to be broadly applicable and platform-agnostic.
PHP Frameworks: I want to break free (IPC Berlin 2024)Ralf Eggert
In this presentation, we examine the challenges and limitations of relying too heavily on PHP frameworks in web development. We discuss the history of PHP and its frameworks to understand how this dependence has evolved. The focus will be on providing concrete tips and strategies to reduce reliance on these frameworks, based on real-world examples and practical considerations. The goal is to equip developers with the skills and knowledge to create more flexible and future-proof web applications. We'll explore the importance of maintaining autonomy in a rapidly changing tech landscape and how to make informed decisions in PHP development.
This talk is aimed at encouraging a more independent approach to using PHP frameworks, moving towards a more flexible and future-proof approach to PHP development.
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...Neo4j
Leonard Jayamohan, Partner & Generative AI Lead, Deloitte
This keynote will reveal how Deloitte leverages Neo4j’s graph power for groundbreaking digital twin solutions, achieving a staggering 100x performance boost. Discover the essential role knowledge graphs play in successful generative AI implementations. Plus, get an exclusive look at an innovative Neo4j + Generative AI solution Deloitte is developing in-house.
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...James Anderson
Effective Application Security in Software Delivery lifecycle using Deployment Firewall and DBOM
The modern software delivery process (or the CI/CD process) includes many tools, distributed teams, open-source code, and cloud platforms. Constant focus on speed to release software to market, along with the traditional slow and manual security checks has caused gaps in continuous security as an important piece in the software supply chain. Today organizations feel more susceptible to external and internal cyber threats due to the vast attack surface in their applications supply chain and the lack of end-to-end governance and risk management.
The software team must secure its software delivery process to avoid vulnerability and security breaches. This needs to be achieved with existing tool chains and without extensive rework of the delivery processes. This talk will present strategies and techniques for providing visibility into the true risk of the existing vulnerabilities, preventing the introduction of security issues in the software, resolving vulnerabilities in production environments quickly, and capturing the deployment bill of materials (DBOM).
Speakers:
Bob Boule
Robert Boule is a technology enthusiast with PASSION for technology and making things work along with a knack for helping others understand how things work. He comes with around 20 years of solution engineering experience in application security, software continuous delivery, and SaaS platforms. He is known for his dynamic presentations in CI/CD and application security integrated in software delivery lifecycle.
Gopinath Rebala
Gopinath Rebala is the CTO of OpsMx, where he has overall responsibility for the machine learning and data processing architectures for Secure Software Delivery. Gopi also has a strong connection with our customers, leading design and architecture for strategic implementations. Gopi is a frequent speaker and well-known leader in continuous delivery and integrating security into software delivery.
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf91mobiles
91mobiles recently conducted a Smart TV Buyer Insights Survey in which we asked over 3,000 respondents about the TV they own, aspects they look at on a new TV, and their TV buying preferences.
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionAggregage
Join Maher Hanafi, VP of Engineering at Betterworks, in this new session where he'll share a practical framework to transform Gen AI prototypes into impactful products! He'll delve into the complexities of data collection and management, model selection and optimization, and ensuring security, scalability, and responsible use.
Dr. Sean Tan, Head of Data Science, Changi Airport Group
Discover how Changi Airport Group (CAG) leverages graph technologies and generative AI to revolutionize their search capabilities. This session delves into the unique search needs of CAG’s diverse passengers and customers, showcasing how graph data structures enhance the accuracy and relevance of AI-generated search results, mitigating the risk of “hallucinations” and improving the overall customer journey.
Removing Uninteresting Bytes in Software FuzzingAftab Hussain
Imagine a world where software fuzzing, the process of mutating bytes in test seeds to uncover hidden and erroneous program behaviors, becomes faster and more effective. A lot depends on the initial seeds, which can significantly dictate the trajectory of a fuzzing campaign, particularly in terms of how long it takes to uncover interesting behaviour in your code. We introduce DIAR, a technique designed to speedup fuzzing campaigns by pinpointing and eliminating those uninteresting bytes in the seeds. Picture this: instead of wasting valuable resources on meaningless mutations in large, bloated seeds, DIAR removes the unnecessary bytes, streamlining the entire process.
In this work, we equipped AFL, a popular fuzzer, with DIAR and examined two critical Linux libraries -- Libxml's xmllint, a tool for parsing xml documents, and Binutil's readelf, an essential debugging and security analysis command-line tool used to display detailed information about ELF (Executable and Linkable Format). Our preliminary results show that AFL+DIAR does not only discover new paths more quickly but also achieves higher coverage overall. This work thus showcases how starting with lean and optimized seeds can lead to faster, more comprehensive fuzzing campaigns -- and DIAR helps you find such seeds.
- These are slides of the talk given at IEEE International Conference on Software Testing Verification and Validation Workshop, ICSTW 2022.
The Art of the Pitch: WordPress Relationships and SalesLaura Byrne
Clients don’t know what they don’t know. What web solutions are right for them? How does WordPress come into the picture? How do you make sure you understand scope and timeline? What do you do if sometime changes?
All these questions and more will be explored as we talk about matching clients’ needs with what your agency offers without pulling teeth or pulling your hair out. Practical tips, and strategies for successful relationship building that leads to closing the deal.
In his public lecture, Christian Timmerer provides insights into the fascinating history of video streaming, starting from its humble beginnings before YouTube to the groundbreaking technologies that now dominate platforms like Netflix and ORF ON. Timmerer also presents provocative contributions of his own that have significantly influenced the industry. He concludes by looking at future challenges and invites the audience to join in a discussion.
Epistemic Interaction - tuning interfaces to provide information for AI supportAlan Dix
Paper presented at SYNERGY workshop at AVI 2024, Genoa, Italy. 3rd June 2024
https://alandix.com/academic/papers/synergy2024-epistemic/
As machine learning integrates deeper into human-computer interactions, the concept of epistemic interaction emerges, aiming to refine these interactions to enhance system adaptability. This approach encourages minor, intentional adjustments in user behaviour to enrich the data available for system learning. This paper introduces epistemic interaction within the context of human-system communication, illustrating how deliberate interaction design can improve system understanding and adaptation. Through concrete examples, we demonstrate the potential of epistemic interaction to significantly advance human-computer interaction by leveraging intuitive human communication strategies to inform system design and functionality, offering a novel pathway for enriching user-system engagements.
Communications Mining Series - Zero to Hero - Session 1DianaGray10
This session provides introduction to UiPath Communication Mining, importance and platform overview. You will acquire a good understand of the phases in Communication Mining as we go over the platform with you. Topics covered:
• Communication Mining Overview
• Why is it important?
• How can it help today’s business and the benefits
• Phases in Communication Mining
• Demo on Platform overview
• Q/A
GridMate - End to end testing is a critical piece to ensure quality and avoid...ThomasParaiso2
End to end testing is a critical piece to ensure quality and avoid regressions. In this session, we share our journey building an E2E testing pipeline for GridMate components (LWC and Aura) using Cypress, JSForce, FakerJS…
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfPeter Spielvogel
Building better applications for business users with SAP Fiori.
• What is SAP Fiori and why it matters to you
• How a better user experience drives measurable business benefits
• How to get started with SAP Fiori today
• How SAP Fiori elements accelerates application development
• How SAP Build Code includes SAP Fiori tools and other generative artificial intelligence capabilities
• How SAP Fiori paves the way for using AI in SAP apps
20240605 QFM017 Machine Intelligence Reading List May 2024
C language introduction
1.
2. Currently, the most commonly-used language for
embedded systems
“High-level assembly”
Very portable: compilers exist for virtually every
processor
Easy-to-understand compilation
Produces efficient code
Fairly concise
3. Developed between 1969 and 1973 along with
Unix
Due mostly to Dennis Ritchie
Designed for systems programming
◦ Operating systems
◦ Utility programs
◦ Compilers
◦ Filters
Evolved from B, which evolved from BCPL
4. Designed by Martin Richards (Cambridge) in 1967
Typeless
◦ Everything an n-bit integer (a machine word)
◦ Pointers (addresses) and integers identical
Memory is an undifferentiated array of words
Natural model for word-addressed machines
Local variables depend on frame-pointer-relative
addressing: dynamically-sized automatic objects not
permitted
Strings awkward
◦ Routines expand and pack bytes to/from word arrays
5. Original machine (DEC PDP-
11) was very small
◦ 24K bytes of memory, 12K used
for operating system
Written when computers were
big, capital equipment
◦ Group would get one, develop new
language, OS
6. Many language features designed to reduce
memory
◦ Forward declarations required for everything
◦ Designed to work in one pass: must know everything
◦ No function nesting
PDP-11 was byte-addressed
◦ Now standard
◦ Meant BCPL’s word-based model was insufficient
8. #include <stdio.h>
void main()
{
printf(“Hello, world!n”);
}
Program mostly a
collection of functions
“main” function special:
the entry point
“void” qualifier indicates
function does not return
anything
I/O performed by a library
function: not included in
the language
9. int gcd(int m, int n)
{
int r;
while ( (r = m % n) != 0) {
m = n;
n = r;
}
return n;
}
“New Style” function
declaration lists
number and type of
arguments
Originally only listed
return type.
Generated code did
not care how many
arguments were
actually passed.
Arguments are call-
by-value
10. int gcd(int m, int n)
{
int r;
while ( (r = m % n) != 0) {
m = n;
n = r;
}
return n;
}
Automatic variable
Storage allocated on
stack when function
entered, released
when it returns.
All parameters,
automatic variables
accessed w.r.t.
frame pointer.
Extra storage
needed while
evaluating large
expressions also
placed on the stack
n
m
ret. addr.
r
Frame
pointer Stack
pointer
Excess
arguments
simply
ignored
11. int gcd(int m, int n)
{
int r;
while ( (r = m % n) != 0) {
m = n;
n = r;
}
return n;
}
Expression: C’s
basic type of
statement.
Arithmetic and
logical
Assignment (=)
returns a value, so
can be used in
expressions
% is remainder
!= is not equal
12. int gcd(int m, int n)
{
int r;
while ( (r = m % n) != 0) {
m = n;
n = r;
}
return n;
}
High-level control-flow
statement. Ultimately
becomes a conditional
branch.
Supports “structured
programming”
Each function
returns a single
value, usually an
integer. Returned
through a specific
register by
convention.
13. .globl _gcd r0-r7
.text PC is r7, SP is r6, FP is r5
_gcd:
jsr r5,rsave save sp in frame pointer r5
L2:mov 4(r5),r1 r1 = n
sxt r0 sign extend
div 6(r5),r0 m / n = r0,r1
mov r1,-10(r5) r = m % n
jeq L3
mov 6(r5),4(r5) m = n
mov -10(r5),6(r5) n = r
jbr L2
L3:mov 6(r5),r0 return n in r0
jbr L1
L1:jmp rretrn restore sp ptr, return
int gcd(int m, int n)
{
int r;
while ( (r = m % n) != 0) {
m = n;
n = r;
}
return n;
}
14. .globl _gcd
.text
_gcd:
jsr r5,rsave
L2:mov 4(r5),r1
sxt r0
div 6(r5),r0
mov r1,-10(r5)
jeq L3
mov 6(r5),4(r5)
mov -10(r5),6(r5)
jbr L2
L3:mov 6(r5),r0
jbr L1
L1:jmp rretrn
Very natural mapping from C into
PDP-11 instructions.
Complex addressing modes make
frame-pointer-relative accesses
easy.
Another idiosyncrasy: registers
were memory-mapped, so taking
address of a variable in a register
is straightforward.
15. Types and Variables
◦ Definitions of data in memory
Expressions
◦ Arithmetic, logical, and assignment operators in an infix
notation
Statements
◦ Sequences of conditional, iteration, and branching
instructions
Functions
◦ Groups of statements and variables invoked recursively
16. Basic types: char, int, float, and double
Meant to match the processor’s native types
◦ Natural translation into assembly
◦ Fundamentally nonportable
Declaration syntax: string of specifiers followed by
a declarator
Declarator’s notation matches that in an
expression
Access a symbol using its declarator and get the
basic type back
17. int i;
int *j, k;
unsigned char *ch;
float f[10];
char nextChar(int,
char*);
int a[3][5][10];
int *func1(float);
int (*func2)(void);
Integer
j: pointer to integer, int k
ch: pointer to unsigned char
Array of 10 floats
2-arg function
Array of three arrays of five …
function returning int *
pointer to function returning int
18. Type declarations recursive, complicated.
Name new types with typedef
Instead of
int (*func2)(void)
use
typedef int func2t(void);
func2t *func2;
19. A struct is an object with named fields:
struct {
char *name;
int x, y;
int h, w;
} box;
Accessed using “dot” notation:
box.x = 5;
box.y = 2;
20. Way to aggressively pack data in memory
struct {
unsigned int baud : 5;
unsigned int div2 : 1;
unsigned int use_external_clock : 1;
} flags;
Compiler will pack these fields into words
Very implementation dependent: no guarantees of
ordering, packing, etc.
Usually less efficient
◦ Reading a field requires masking and shifting
21. Can store objects of different types at different times
union {
int ival;
float fval;
char *sval;
};
Useful for arrays of dissimilar objects
Potentially very dangerous
Good example of C’s philosophy
◦ Provide powerful mechanisms that can be abused
22. Most processors require n-byte objects to be in
memory at address n*k
Side effect of wide memory busses
E.g., a 32-bit memory bus
Read from address 3 requires two accesses,
shifting
4 3 2
1
4 3 2 1
23. Compilers add “padding” to structs to ensure proper
alignment, especially for arrays
Pad to ensure alignment of largest object (with biggest
requirement)
struct {
char a;
int b;
char c;
}
Moral: rearrange to save memory
a
bbbb
c
a
bbbb
c
Pad
24. #include <stdlib.h>
int global_static;
static int file_static;
void foo(int auto_param)
{
static int func_static;
int auto_i, auto_a[10];
double *auto_d =
malloc(sizeof(double)*5);
}
Linker-visible.
Allocated at fixed
location
Visible within file.
Allocated at fixed
location.
Visible within func.
Allocated at fixed
location.
25. #include <stdlib.h>
int global_static;
static int file_static;
void foo(int auto_param)
{
static int func_static;
int auto_i, auto_a[10];
double *auto_d =
malloc(sizeof(double)*5);
}
Space allocated on
stack by function.
Space allocated on
stack by caller.
Space allocated on
heap by library routine.
26. Library routines for managing the heap
int *a;
a = (int *) malloc(sizeof(int) * k);
a[5] = 3;
free(a);
Allocate and free arbitrary-sized chunks of
memory in any order
27. More flexible than automatic variables (stacked)
More costly in time and space
◦ malloc() and free() use complicated non-constant-time
algorithms
◦ Each block generally consumes two additional words of
memory
Pointer to next empty block
Size of this block
Common source of errors
◦ Using uninitialized memory
◦ Using freed memory
◦ Not allocating enough
28. Memory usage errors so pervasive, entire
successful company (Pure Software) founded to
sell tool to track them down
Purify tool inserts code that verifies each memory
access
Reports accesses of uninitialized memory,
unallocated memory, etc.
Publicly-available Electric Fence tool does
something similar
29. What are malloc() and free() actually doing?
Pool of memory segments:
Free
malloc( )
30. Rules:
◦ Each segment contiguous in memory (no holes)
◦ Segments do not move once allocated
malloc()
◦ Find memory area large enough for segment
◦ Mark that memory is allocated
free()
◦ Mark the segment as unallocated
31. Three issues:
How to maintain information about free memory
The algorithm for locating a suitable block
The algorithm for freeing an allocated block
32. Three issues:
How to maintain information about free memory
◦ Linked list
The algorithm for locating a suitable block
◦ First-fit
The algorithm for freeing an allocated block
◦ Coalesce adjacent free blocks
33. Next
Size
Next
SizeSize
Free block Allocated block
malloc( )
First large-enough
free block selected
Free block divided
into two
Previous next
pointer updated
Newly-allocated
region begins with
a size value
35. Many, many variants
Other “fit” algorithms
Segregation of objects by sizes
◦ 8-byte objects in one region, 16 in another, etc.
More intelligent list structures
36. An alternative: Memory pools
Separate management policy for each pool
Stack-based pool: can only free whole pool at
once
◦ Very cheap operation
◦ Good for build-once data structures (e.g., compilers)
Pool for objects of a single size
◦ Useful in object-oriented programs
Not part of the C standard library
37. Array: sequence of identical objects
in memory
int a[10]; means space for ten
integers
Filippo Brunelleschi,
Ospdale degli Innocenti,
Firenze, Italy, 1421
By itself, a is the address of the first integer
*a and a[0] mean the same thing
The address of a is not stored in memory: the
compiler inserts code to compute it when it appears
Ritchie calls this interpretation the biggest
conceptual jump from BCPL to C
38. Array declarations read right-to-left
int a[10][3][2];
“an array of ten arrays of three arrays of two ints”
In memory
2 2 2
3
2 2 2
3
2 2 2
3
...
10
Seagram Building, Ludwig
Mies van der Rohe,1957
39. Passing a multidimensional array as an argument
requires all but the first dimension
int a[10][3][2];
void examine( a[][3][2] ) { … }
Address for an access such as a[i][j][k] is
a + k + 2*(j + 3*i)
40. Use arrays of pointers for variable-sized
multidimensional arrays
You need to allocate space for and initialize the
arrays of pointers
int ***a;
a[3][5][4] expands to *(*(*(a+3)+5)+4)
The value
int ** int * int
int ***a
41. Traditional mathematical expressions
y = a*x*x + b*x + c;
Very rich set of expressions
Able to deal with arithmetic and bit manipulation
43. and: & or: | xor: ^ not: ~ left shift: << right shift: >>
Useful for bit-field manipulations
#define MASK 0x040
if (a & MASK) { … } /* Check bits */
c |= MASK; /* Set bits */
c &= ~MASK; /* Clear bits */
d = (a & MASK) >> 4; /* Select field */
44. “Short circuit” tests save time
if ( a == 3 && b == 4 && c == 5 ) { … }
equivalent to
if (a == 3) { if (b ==4) { if (c == 5) { … } } }
Evaluation order (left before right) provides safety
if ( i <= SIZE && a[i] == 0 ) { … }
45. c = a < b ? a + 1 : b – 1;
Evaluate first expression. If true, evaluate second,
otherwise evaluate third.
Puts almost statement-like behavior in expressions.
BCPL allowed code in an expression:
a := 5 + valof{ int i, s = 0; for (i = 0 ; i < 10 ; i++) s += a[I];
return s; }
46. Evaluating an expression often has side-effects
a++ increment a afterwards
a = 5 changes the value of a
a = foo() function foo may have side-effects
47. From BCPL’s view of the world
Pointer arithmetic is natural: everything’s an
integer
int *p, *q;
*(p+5) equivalent to p[5]
If p and q point into same array, p – q is number
of elements between p and q.
Accessing fields of a pointed-to structure has a
shorthand:
p->field means (*p).field
48. Expression
Conditional
◦ if (expr) { … } else {…}
◦ switch (expr) { case c1: case c2: … }
Iteration
◦ while (expr) { … } zero or more iterations
◦ do … while (expr) at least one iteration
◦ for ( init ; valid ; next ) { … }
Jump
◦ goto label
◦ continue;go to start of loop
◦ break; exit loop or switch
◦ return expr; return from function
50. Sparse case labels tested sequentially
if (e == 1) goto L1;
else if (e == 10) goto L2;
else if (e == 100) goto L3;
Dense cases use a jump table
table = { L1, L2, Default, L4, L5 };
if (e >= 1 and e <= 5) goto table[e];
Clever compilers may combine these
51. A way to exit from deeply nested functions
A hack now a formal part of the standard library
#include <setjmp.h>
jmp_buf jmpbuf;
void top(void) {
switch (setjmp(jmpbuf)) {
case 0: child(); break;
case 1: /* longjmp called */ break;
} }
void deeplynested() { longjmp(jmpbuf,
Space for a return
address and registers
(including stack pointer,
frame pointer)
Stores context, returns 0
Returns to context, making it
appear setjmp() returned 1
52. Relatively late and awkward addition to the
language
Symbolic constants
#define PI 3.1415926535
Macros with arguments for emulating inlining
#define min(x,y) ((x) < (y) ? (x) : (y))
Conditional compilation
#ifdef __STDC__
File inclusion for sharing of declarations
#include “myheaders.h”
53. Header file dependencies usually form a directed
acyclic graph (DAG)
How do you avoid defining things twice?
Convention: surround each header (.h) file with a
conditional:
#ifndef __MYHEADER_H__
#define __MYHEADER_H__
/* Declarations */
#endif
54. Macros with arguments do not have function call
semantics
Function Call:
◦ Each argument evaluated once, in undefined order,
before function is called
Macro:
◦ Each argument evaluated once every time it appears in
expansion text
55. Example: the “min” function
int min(int a, int b)
{ if (a < b) return a; else return b; }
#define min(a,b) ((a) < (b) ? (a) : (b))
Identical for min(5,x)
Different when evaluating expression has side-
effect:
min(a++,b)
◦ min function increments a once
◦ min macro may increment a twice if a < b
56. Text substitution can expose unexpected groupings
#define mult(a,b) a*b
mult(5+3,2+4)
Expands to 5 + 3 * 2 + 4
Operator precedence evaluates this as
5 + (3*2) + 4 = 15 not (5+3) * (2+4) = 48 as intended
Moral: By convention, enclose each macro argument
in parenthesis:
#define mult(a,b) (a)*(b)
57. Library routines
◦ malloc() returns a nondeterministically-chosen address
◦ Address used as a hash key produces nondeterministic results
Argument evaluation order
◦ myfunc( func1(), func2(), func3() )
◦ func1, func2, and func3 may be called in any order
Word sizes
int a;
a = 1 << 16; /* Might be zero */
a = 1 << 32; /* Might be zero */
58. Uninitialized variables
◦ Automatic variables may take values from stack
◦ Global variables left to the whims of the OS
Reading the wrong value from a union
◦ union { int a; float b; } u; u.a = 10; printf(“%g”, u.b);
Pointer dereference
◦ *a undefined unless it points within an allocated array and has
been initialized
◦ Very easy to violate these rules
◦ Legal: int a[10]; a[-1] = 3; a[10] = 2; a[11] = 5;
◦ int *a, *b; a - b only defined if a and b point into the same array
59. How to deal with nondeterminism?
◦ Caveat programmer
Studiously avoid nondeterministic constructs
◦ Compilers, lint, etc. don’t really help
Philosophy of C: get out of the programmer’s way
“C treats you like a consenting adult”
◦ Created by a systems programmer (Ritchie)
“Pascal treats you like a misbehaving child”
◦ Created by an educator (Wirth)
“Ada treats you like a criminal”
◦ Created by the Department of Defense
60. C evolved from the typeless languages BCPL and B
Array-of-bytes model of memory permeates the
language
Original weak type system strengthened over time
C programs built from
◦ Variable and type declarations
◦ Functions
◦ Statements
◦ Expressions
61. Built from primitive types that match processor types
char, int, float, double, pointers
Struct and union aggregate heterogeneous objects
Arrays build sequences of identical objects
Alignment restrictions ensured by compiler
Multidimensional arrays
Three storage classes
◦ global, static (address fixed at compile time)
◦ automatic (on stack)
◦ heap (provided by malloc() and free() library calls)
65. Like a high-level assembly language
Array-of-cells model of memory
Very efficient code generation follows from close
semantic match
Language lets you do just about everything
Very easy to make mistakes