SlideShare ist ein Scribd-Unternehmen logo
1 von 9
C programming:
Tips, Tricks, Traps
# 0x00
Embedded Systems in Ukraine
Web: http://embedded.in.ua Email: avp@embedded.in.ua Twitter: embedded_in_ua
NULL pointer
• A null pointer is a pointer that points to no valid location by having a
value of 0. A null pointer is known not to point to any object; an
uninitialized pointer might point anywhere.
• The "NULL" and "0" are interchangeable. But any usage of "NULL" (as
opposed to "0") should be considered a gentle reminder that a pointer is
involved.
• The preprocessor macro NULL is #defined with value 0. Or (void *) 0,
which will not work in non-pointer contexts.
“char *a” and “char a[]”
• Arrays automatically allocate space, but can't be relocated or resized.
• Pointers must be explicitly assigned to point to allocated space, but can
be reassigned (i.e. pointed at different objects) at will, and have many
other uses besides serving as the base of blocks of memory.
“extern” in a function declaration
• The extern keyword means "declare without defining". In other words, it
is a way to explicitly declare a variable, or to force a declaration without a
definition.
• It can be used as a stylistic hint to indicate that the function's definition is
probably in another source file, but there is no formal difference between
“extern int f();” and “int f();”
Right type to use for boolean values in C? (1)
• The initial standards for the C language (1972) provided no Boolean type;
and, to this day, Boolean values are commonly represented by integers in
C programs.
• The preprocessor macros TRUE and FALSE are used for code readability,
not because the underlying values might ever change.
• The approach ("Boolean values are just integers") was retained in all later
versions of C. Some of its dialects, like C99, provide standard definitions
of a Boolean type as a synonym of int and macros for "false" and "true"
as 0 and 1, respectively.
Right type to use for boolean values in C? (2)
• The comparison operators ('>', '==', etc.), logical operators ('&&', '||', '!',
etc.) and condition-testing statements ('if', 'while') are defined to return a
signed integer (int) result, either zero (for false) or 1 (for true).
• A good rule of thumb is to use TRUE and FALSE (or the like) only for
assignment to a Boolean variable or function parameter, or as the return
value from a Boolean function, but never in a comparison.
"#define" and "const"?
• The "const" in C does not mean something is constant. It just means a
variable is read-only. But all other attributes of variable persist: it has
allocated storage, and this storage may be addressed. So any code do not
just use it as literal, but refers to it by accessing to specified memory
location.
• In places where the compiler requires a true constant, using a const
variable is just not possible.
• The macro, which after preprocessing expands to the constant
expression; and is known at compile time, so it can be used for array
dimensions, case labels, etc.
Defining versus declaring
• When you declare a variable, you are telling the compiler that the
variable was defined elsewhere. You are just telling the compiler that a
variable by that name and type exists, but the compiler should not
allocate memory for it since it is done somewhere else.
• When you define a variable, you are telling the compiler to allocate
memory for that variable, and possibly also to initialize its contents to
some value.
Reference
• Why do most C developers use define instead of const? - Stackoverflow
• External variable - Wikipedia, the free encyclopedia
• Boolean data type - Wikipedia, the free encyclopedia
• Boolean Expressions and Variables - FAQ in comp.lang.c
• Null Pointers - FAQ in comp.lang.c

Weitere ähnliche Inhalte

Kürzlich hochgeladen

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 

Kürzlich hochgeladen (20)

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 

Empfohlen

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Empfohlen (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

C programming: Tips, Tricks, Traps # 0x00

  • 1. C programming: Tips, Tricks, Traps # 0x00 Embedded Systems in Ukraine Web: http://embedded.in.ua Email: avp@embedded.in.ua Twitter: embedded_in_ua
  • 2. NULL pointer • A null pointer is a pointer that points to no valid location by having a value of 0. A null pointer is known not to point to any object; an uninitialized pointer might point anywhere. • The "NULL" and "0" are interchangeable. But any usage of "NULL" (as opposed to "0") should be considered a gentle reminder that a pointer is involved. • The preprocessor macro NULL is #defined with value 0. Or (void *) 0, which will not work in non-pointer contexts.
  • 3. “char *a” and “char a[]” • Arrays automatically allocate space, but can't be relocated or resized. • Pointers must be explicitly assigned to point to allocated space, but can be reassigned (i.e. pointed at different objects) at will, and have many other uses besides serving as the base of blocks of memory.
  • 4. “extern” in a function declaration • The extern keyword means "declare without defining". In other words, it is a way to explicitly declare a variable, or to force a declaration without a definition. • It can be used as a stylistic hint to indicate that the function's definition is probably in another source file, but there is no formal difference between “extern int f();” and “int f();”
  • 5. Right type to use for boolean values in C? (1) • The initial standards for the C language (1972) provided no Boolean type; and, to this day, Boolean values are commonly represented by integers in C programs. • The preprocessor macros TRUE and FALSE are used for code readability, not because the underlying values might ever change. • The approach ("Boolean values are just integers") was retained in all later versions of C. Some of its dialects, like C99, provide standard definitions of a Boolean type as a synonym of int and macros for "false" and "true" as 0 and 1, respectively.
  • 6. Right type to use for boolean values in C? (2) • The comparison operators ('>', '==', etc.), logical operators ('&&', '||', '!', etc.) and condition-testing statements ('if', 'while') are defined to return a signed integer (int) result, either zero (for false) or 1 (for true). • A good rule of thumb is to use TRUE and FALSE (or the like) only for assignment to a Boolean variable or function parameter, or as the return value from a Boolean function, but never in a comparison.
  • 7. "#define" and "const"? • The "const" in C does not mean something is constant. It just means a variable is read-only. But all other attributes of variable persist: it has allocated storage, and this storage may be addressed. So any code do not just use it as literal, but refers to it by accessing to specified memory location. • In places where the compiler requires a true constant, using a const variable is just not possible. • The macro, which after preprocessing expands to the constant expression; and is known at compile time, so it can be used for array dimensions, case labels, etc.
  • 8. Defining versus declaring • When you declare a variable, you are telling the compiler that the variable was defined elsewhere. You are just telling the compiler that a variable by that name and type exists, but the compiler should not allocate memory for it since it is done somewhere else. • When you define a variable, you are telling the compiler to allocate memory for that variable, and possibly also to initialize its contents to some value.
  • 9. Reference • Why do most C developers use define instead of const? - Stackoverflow • External variable - Wikipedia, the free encyclopedia • Boolean data type - Wikipedia, the free encyclopedia • Boolean Expressions and Variables - FAQ in comp.lang.c • Null Pointers - FAQ in comp.lang.c