SlideShare una empresa de Scribd logo
1 de 1
Descargar para leer sin conexión
The Joy of Programming  |                        Guest Column 


                                                                                               S.G. Ganesh


Some Puzzling Things About C Language!
Have you wondered why some of the features of C language are unintuitive? As we’ll see in this
column, there are historical reasons for many of C’s features.


1     Can you guess why there is no distinct format specifier
      for ‘double’ in the printf/scanf format string, although
it is one of the four basic data types? (Remember we use
                                                                 evaluation. However, I had cold feet about the precedence
                                                                 problems. For example, there were lots of programs with
                                                                 things like: if (a==b & c==d) ...
%lf for printing the double value in printf/scanf; %d is for         “In retrospect it would have been better to go ahead
integers).                                                       and change the precedence of & to higher than ==, but it
                                                                 seemed safer just to split & and && without moving & past

2   Why is some of the precedence of operators in C
    wrong? For example, equality operators (==, != etc)
                                                                 an existing operator.”

have higher precedence than logical operators (&&, ||).
                                                                 3    Since C was originally designed for writing UNIX
                                                                      (system programming), the nature of its application

3   In the original C library, <math.h> has all operations
    done in double precision, i.e., long float or double (and
not single precision, i.e., float). Why?
                                                                 reduced the necessity for floating point operations.
                                                                 Moreover, in the hardware of the original and initial
                                                                 implementations of C (PDP-11) floating point arithmetic
                                                                 was done in double precision (long float or double type)

4   Why is the output file of the C compiler called
    a.out?
                                                                 only. Writing library functions seemed to be easy if only
                                                                 one type was handled. For these reasons, the library
                                                                 functions involving mathematics (<math.h>) were done
Answers:                                                         for double types, and all the floating point calculations

1    In older versions of C, there was no ‘double’—it was just
     ‘long float’ type—and that is the reason why it has the
format specifier ‘%lf’ (‘%d’ was already in use to indicate
                                                                 were promoted and were done in double precision only.
                                                                 For the same reason, when we use a floating point literal,
                                                                 such as 10.0, it is treated as double precision and not single
signed decimal values). Later, double type was added to          precision.
indicate that the floating point type might be of ‘double
precision’ (IEEE format, 64-bit value). So a format specifier
for long float and double was kept the same.                     4    The a.out stands for ‘assembler.output’ file [2]. The
                                                                      original UNIX was written using an assembler for the
                                                                 PDP-7 machine. The output of the assembler was a fixed

2   The confusion in the precedence of the logical and
    equality operators is the source of numerous bugs
in C. For example, in (a && b == c && d), == has higher
                                                                 file name, which was a.out to indicate that it was the
                                                                 output file from the assembler. No assembly needs to be
                                                                 done in modern compilers; instead, linking and loading of
precedence than &&. So it is interpreted as, ( (a && (b ==       object files is done. This tradition continues and the output
c) && d), which is not intuitive.                                of cc is by default a.out!
    There is a historical background for this wrong                   With this month, JoP is successfully entering its
operator precedence. To quote from Dennis M. Ritchie’s           third year. Thanks for all your continuous feedback and
book, ‘Operator precedence’ [1]: “Early C had no separate        support! Keep filling my mailbox as usual and I’ll be
operators for & and && or | and ||. Instead it used the          more than happy to help you. Wishing you a happy new
notion (inherited from B and BCPL) of ‘truth-value               year!!
context’: where a Boolean value was expected, after ‘if ’ and
‘while’ and so forth; the & and | operators were interpreted       References:
as && and || are now; in ordinary expressions, the bit-wise        • Dennis M. Ritchie, “Operator precedence”, net.lang.c, 1982
interpretations were used. It worked out pretty well, but          • cm.bell-labs.com/who/dmr/chist.html
was hard to explain. (There was the notion of ‘top-level
operators’ in a truth-value context.)
                                                                  S.G. Ganesh
    “The precedence of & and | were as they are now.
                                                                 The author is a research engineer in Siemens (Corporate Technology).
Primarily at the urging of Alan Snyder, the && and ||            His latest book is “60 Tips on Object Oriented Programming”, published
operators were added. This successfully separated the            by Tata McGraw-Hill in December 2007. You can reach him at
concepts of bit-wise operations and short-circuit Boolean        sgganesh@gmail.com



12  |  January 2009 | LInuX For you | www.openITis.com

Más contenido relacionado

La actualidad más candente

Brief introduction to the c programming language
Brief introduction to the c programming languageBrief introduction to the c programming language
Brief introduction to the c programming language
Kumar Gaurav
 
Writing Efficient Code Feb 08
Writing Efficient Code Feb 08Writing Efficient Code Feb 08
Writing Efficient Code Feb 08
Ganesh Samarthyam
 
C interview-questions-techpreparation
C interview-questions-techpreparationC interview-questions-techpreparation
C interview-questions-techpreparation
Kushaal Singla
 

La actualidad más candente (20)

difference between c c++ c#
difference between c c++ c#difference between c c++ c#
difference between c c++ c#
 
Introduction Of C++
Introduction Of C++Introduction Of C++
Introduction Of C++
 
Report on c and c++
Report on c and c++Report on c and c++
Report on c and c++
 
Compiler unit 5
Compiler  unit 5Compiler  unit 5
Compiler unit 5
 
Brief introduction to the c programming language
Brief introduction to the c programming languageBrief introduction to the c programming language
Brief introduction to the c programming language
 
10 Jo P Oct 07
10 Jo P Oct 0710 Jo P Oct 07
10 Jo P Oct 07
 
Writing Efficient Code Feb 08
Writing Efficient Code Feb 08Writing Efficient Code Feb 08
Writing Efficient Code Feb 08
 
Code generation
Code generationCode generation
Code generation
 
Compiler optimizations based on call-graph flattening
Compiler optimizations based on call-graph flatteningCompiler optimizations based on call-graph flattening
Compiler optimizations based on call-graph flattening
 
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
 
F# Intro for Scala Developers
F# Intro for Scala DevelopersF# Intro for Scala Developers
F# Intro for Scala Developers
 
2. data, operators, io
2. data, operators, io2. data, operators, io
2. data, operators, io
 
C tutorials
C tutorialsC tutorials
C tutorials
 
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
 
C PROGRAMMING
C PROGRAMMINGC PROGRAMMING
C PROGRAMMING
 
Difference between c# generics and c++ templates
Difference between c# generics and c++ templatesDifference between c# generics and c++ templates
Difference between c# generics and c++ templates
 
Introduction to c programming language
Introduction to c programming languageIntroduction to c programming language
Introduction to c programming language
 
Lecture 3 RE NFA DFA
Lecture 3   RE NFA DFA Lecture 3   RE NFA DFA
Lecture 3 RE NFA DFA
 
C interview-questions-techpreparation
C interview-questions-techpreparationC interview-questions-techpreparation
C interview-questions-techpreparation
 
Programming c sharp 3rd ch6 7 21
Programming c sharp 3rd ch6 7 21Programming c sharp 3rd ch6 7 21
Programming c sharp 3rd ch6 7 21
 

Similar a 25 Jo P Jan 09

COMP 2103X1 Assignment 2Due Thursday, January 26 by 700 PM.docx
COMP 2103X1 Assignment 2Due Thursday, January 26 by 700 PM.docxCOMP 2103X1 Assignment 2Due Thursday, January 26 by 700 PM.docx
COMP 2103X1 Assignment 2Due Thursday, January 26 by 700 PM.docx
donnajames55
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
Alpana Gupta
 
C LANGUAGE UNIT-1 PREPARED BY MVB REDDY
C LANGUAGE UNIT-1 PREPARED BY MVB REDDYC LANGUAGE UNIT-1 PREPARED BY MVB REDDY
C LANGUAGE UNIT-1 PREPARED BY MVB REDDY
Rajeshkumar Reddy
 

Similar a 25 Jo P Jan 09 (20)

14 Jo P Feb 08
14 Jo P Feb 0814 Jo P Feb 08
14 Jo P Feb 08
 
Boo Manifesto
Boo ManifestoBoo Manifesto
Boo Manifesto
 
88 c programs 15184
88 c programs 1518488 c programs 15184
88 c programs 15184
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
 
C language tutorial
C language tutorialC language tutorial
C language tutorial
 
Complete C programming Language Course
Complete C programming Language CourseComplete C programming Language Course
Complete C programming Language Course
 
C notes.pdf
C notes.pdfC notes.pdf
C notes.pdf
 
COMP 2103X1 Assignment 2Due Thursday, January 26 by 700 PM.docx
COMP 2103X1 Assignment 2Due Thursday, January 26 by 700 PM.docxCOMP 2103X1 Assignment 2Due Thursday, January 26 by 700 PM.docx
COMP 2103X1 Assignment 2Due Thursday, January 26 by 700 PM.docx
 
Basic c
Basic cBasic c
Basic c
 
C programming day#1
C programming day#1C programming day#1
C programming day#1
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
2CPP02 - C++ Primer
2CPP02 - C++ Primer2CPP02 - C++ Primer
2CPP02 - C++ Primer
 
01 c
01 c01 c
01 c
 
The basics of c programming
The basics of c programmingThe basics of c programming
The basics of c programming
 
c-for-c-programmers.pdf
c-for-c-programmers.pdfc-for-c-programmers.pdf
c-for-c-programmers.pdf
 
01 Jo P Jan 07
01 Jo P Jan 0701 Jo P Jan 07
01 Jo P Jan 07
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
 
C LANGUAGE UNIT-1 PREPARED BY MVB REDDY
C LANGUAGE UNIT-1 PREPARED BY MVB REDDYC LANGUAGE UNIT-1 PREPARED BY MVB REDDY
C LANGUAGE UNIT-1 PREPARED BY MVB REDDY
 
Introduction of c language
Introduction of c languageIntroduction of c language
Introduction of c language
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
 

Más de Ganesh Samarthyam

Más de Ganesh Samarthyam (20)

Wonders of the Sea
Wonders of the SeaWonders of the Sea
Wonders of the Sea
 
Animals - for kids
Animals - for kids Animals - for kids
Animals - for kids
 
Applying Refactoring Tools in Practice
Applying Refactoring Tools in PracticeApplying Refactoring Tools in Practice
Applying Refactoring Tools in Practice
 
CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”
 
Great Coding Skills Aren't Enough
Great Coding Skills Aren't EnoughGreat Coding Skills Aren't Enough
Great Coding Skills Aren't Enough
 
College Project - Java Disassembler - Description
College Project - Java Disassembler - DescriptionCollege Project - Java Disassembler - Description
College Project - Java Disassembler - Description
 
Coding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeCoding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean Code
 
Design Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesDesign Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on Examples
 
Bangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief PresentationBangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief Presentation
 
Bangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - PosterBangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - Poster
 
Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)
 
OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ OO Design and Design Patterns in C++
OO Design and Design Patterns in C++
 
Bangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship DeckBangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship Deck
 
Let's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming LanguageLet's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming Language
 
Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction
 
Java Generics - Quiz Questions
Java Generics - Quiz QuestionsJava Generics - Quiz Questions
Java Generics - Quiz Questions
 
Java Generics - by Example
Java Generics - by ExampleJava Generics - by Example
Java Generics - by Example
 
Software Architecture - Quiz Questions
Software Architecture - Quiz QuestionsSoftware Architecture - Quiz Questions
Software Architecture - Quiz Questions
 
Docker by Example - Quiz
Docker by Example - QuizDocker by Example - Quiz
Docker by Example - Quiz
 
Core Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quizCore Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quiz
 

Último

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 

25 Jo P Jan 09

  • 1. The Joy of Programming  | Guest Column  S.G. Ganesh Some Puzzling Things About C Language! Have you wondered why some of the features of C language are unintuitive? As we’ll see in this column, there are historical reasons for many of C’s features. 1 Can you guess why there is no distinct format specifier for ‘double’ in the printf/scanf format string, although it is one of the four basic data types? (Remember we use evaluation. However, I had cold feet about the precedence problems. For example, there were lots of programs with things like: if (a==b & c==d) ... %lf for printing the double value in printf/scanf; %d is for “In retrospect it would have been better to go ahead integers). and change the precedence of & to higher than ==, but it seemed safer just to split & and && without moving & past 2 Why is some of the precedence of operators in C wrong? For example, equality operators (==, != etc) an existing operator.” have higher precedence than logical operators (&&, ||). 3 Since C was originally designed for writing UNIX (system programming), the nature of its application 3 In the original C library, <math.h> has all operations done in double precision, i.e., long float or double (and not single precision, i.e., float). Why? reduced the necessity for floating point operations. Moreover, in the hardware of the original and initial implementations of C (PDP-11) floating point arithmetic was done in double precision (long float or double type) 4 Why is the output file of the C compiler called a.out? only. Writing library functions seemed to be easy if only one type was handled. For these reasons, the library functions involving mathematics (<math.h>) were done Answers: for double types, and all the floating point calculations 1 In older versions of C, there was no ‘double’—it was just ‘long float’ type—and that is the reason why it has the format specifier ‘%lf’ (‘%d’ was already in use to indicate were promoted and were done in double precision only. For the same reason, when we use a floating point literal, such as 10.0, it is treated as double precision and not single signed decimal values). Later, double type was added to precision. indicate that the floating point type might be of ‘double precision’ (IEEE format, 64-bit value). So a format specifier for long float and double was kept the same. 4 The a.out stands for ‘assembler.output’ file [2]. The original UNIX was written using an assembler for the PDP-7 machine. The output of the assembler was a fixed 2 The confusion in the precedence of the logical and equality operators is the source of numerous bugs in C. For example, in (a && b == c && d), == has higher file name, which was a.out to indicate that it was the output file from the assembler. No assembly needs to be done in modern compilers; instead, linking and loading of precedence than &&. So it is interpreted as, ( (a && (b == object files is done. This tradition continues and the output c) && d), which is not intuitive. of cc is by default a.out! There is a historical background for this wrong With this month, JoP is successfully entering its operator precedence. To quote from Dennis M. Ritchie’s third year. Thanks for all your continuous feedback and book, ‘Operator precedence’ [1]: “Early C had no separate support! Keep filling my mailbox as usual and I’ll be operators for & and && or | and ||. Instead it used the more than happy to help you. Wishing you a happy new notion (inherited from B and BCPL) of ‘truth-value year!! context’: where a Boolean value was expected, after ‘if ’ and ‘while’ and so forth; the & and | operators were interpreted References: as && and || are now; in ordinary expressions, the bit-wise • Dennis M. Ritchie, “Operator precedence”, net.lang.c, 1982 interpretations were used. It worked out pretty well, but • cm.bell-labs.com/who/dmr/chist.html was hard to explain. (There was the notion of ‘top-level operators’ in a truth-value context.) S.G. Ganesh “The precedence of & and | were as they are now. The author is a research engineer in Siemens (Corporate Technology). Primarily at the urging of Alan Snyder, the && and || His latest book is “60 Tips on Object Oriented Programming”, published operators were added. This successfully separated the by Tata McGraw-Hill in December 2007. You can reach him at concepts of bit-wise operations and short-circuit Boolean sgganesh@gmail.com 12  |  January 2009 | LInuX For you | www.openITis.com