tag:blogger.com,1999:blog-34579200238276607872024-02-07T13:00:35.973+05:30Harish NoteProgramming, Philosophy, Blog, Physics and so on.Harishhttp://www.blogger.com/profile/09680319865654952936noreply@blogger.comBlogger146125tag:blogger.com,1999:blog-3457920023827660787.post-31467082831473114282021-06-23T21:55:00.000+05:302021-06-23T21:55:06.200+05:30K & R : Exercise 2.6 - setbits<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2021-06-23 Wed 21:48 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>K & R : Exercise 2.6 - setbits</title>
<meta name="generator" content="Org mode" />
<meta name="author" content="Harish SK" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
/* Languages per Org manual */
pre.src-asymptote:before { content: 'Asymptote'; }
pre.src-awk:before { content: 'Awk'; }
pre.src-C:before { content: 'C'; }
/* pre.src-C++ doesn't work in CSS */
pre.src-clojure:before { content: 'Clojure'; }
pre.src-css:before { content: 'CSS'; }
pre.src-D:before { content: 'D'; }
pre.src-ditaa:before { content: 'ditaa'; }
pre.src-dot:before { content: 'Graphviz'; }
pre.src-calc:before { content: 'Emacs Calc'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-fortran:before { content: 'Fortran'; }
pre.src-gnuplot:before { content: 'gnuplot'; }
pre.src-haskell:before { content: 'Haskell'; }
pre.src-hledger:before { content: 'hledger'; }
pre.src-java:before { content: 'Java'; }
pre.src-js:before { content: 'Javascript'; }
pre.src-latex:before { content: 'LaTeX'; }
pre.src-ledger:before { content: 'Ledger'; }
pre.src-lisp:before { content: 'Lisp'; }
pre.src-lilypond:before { content: 'Lilypond'; }
pre.src-lua:before { content: 'Lua'; }
pre.src-matlab:before { content: 'MATLAB'; }
pre.src-mscgen:before { content: 'Mscgen'; }
pre.src-ocaml:before { content: 'Objective Caml'; }
pre.src-octave:before { content: 'Octave'; }
pre.src-org:before { content: 'Org mode'; }
pre.src-oz:before { content: 'OZ'; }
pre.src-plantuml:before { content: 'Plantuml'; }
pre.src-processing:before { content: 'Processing.js'; }
pre.src-python:before { content: 'Python'; }
pre.src-R:before { content: 'R'; }
pre.src-ruby:before { content: 'Ruby'; }
pre.src-sass:before { content: 'Sass'; }
pre.src-scheme:before { content: 'Scheme'; }
pre.src-screen:before { content: 'Gnu Screen'; }
pre.src-sed:before { content: 'Sed'; }
pre.src-sh:before { content: 'shell'; }
pre.src-sql:before { content: 'SQL'; }
pre.src-sqlite:before { content: 'SQLite'; }
/* additional languages in org.el's org-babel-load-languages alist */
pre.src-forth:before { content: 'Forth'; }
pre.src-io:before { content: 'IO'; }
pre.src-J:before { content: 'J'; }
pre.src-makefile:before { content: 'Makefile'; }
pre.src-maxima:before { content: 'Maxima'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-picolisp:before { content: 'Pico Lisp'; }
pre.src-scala:before { content: 'Scala'; }
pre.src-shell:before { content: 'Shell Script'; }
pre.src-ebnf2ps:before { content: 'ebfn2ps'; }
/* additional language identifiers per "defun org-babel-execute"
in ob-*.el */
pre.src-cpp:before { content: 'C++'; }
pre.src-abc:before { content: 'ABC'; }
pre.src-coq:before { content: 'Coq'; }
pre.src-groovy:before { content: 'Groovy'; }
/* additional language identifiers from org-babel-shell-names in
ob-shell.el: ob-shell is the only babel language using a lambda to put
the execution function name together. */
pre.src-bash:before { content: 'bash'; }
pre.src-csh:before { content: 'csh'; }
pre.src-ash:before { content: 'ash'; }
pre.src-dash:before { content: 'dash'; }
pre.src-ksh:before { content: 'ksh'; }
pre.src-mksh:before { content: 'mksh'; }
pre.src-posh:before { content: 'posh'; }
/* Additional Emacs modes also supported by the LaTeX listings package */
pre.src-ada:before { content: 'Ada'; }
pre.src-asm:before { content: 'Assembler'; }
pre.src-caml:before { content: 'Caml'; }
pre.src-delphi:before { content: 'Delphi'; }
pre.src-html:before { content: 'HTML'; }
pre.src-idl:before { content: 'IDL'; }
pre.src-mercury:before { content: 'Mercury'; }
pre.src-metapost:before { content: 'MetaPost'; }
pre.src-modula-2:before { content: 'Modula-2'; }
pre.src-pascal:before { content: 'Pascal'; }
pre.src-ps:before { content: 'PostScript'; }
pre.src-prolog:before { content: 'Prolog'; }
pre.src-simula:before { content: 'Simula'; }
pre.src-tcl:before { content: 'tcl'; }
pre.src-tex:before { content: 'TeX'; }
pre.src-plain-tex:before { content: 'Plain TeX'; }
pre.src-verilog:before { content: 'Verilog'; }
pre.src-vhdl:before { content: 'VHDL'; }
pre.src-xml:before { content: 'XML'; }
pre.src-nxml:before { content: 'XML'; }
/* add a generic configuration mode; LaTeX export needs an additional
(add-to-list 'org-latex-listings-langs '(conf " ")) in .emacs */
pre.src-conf:before { content: 'Configuration File'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.equation-container {
display: table;
text-align: center;
width: 100%;
}
.equation {
vertical-align: middle;
}
.equation-label {
display: table-cell;
text-align: right;
vertical-align: middle;
}
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
.org-svg { width: 90%; }
/*]]>*/-->
</style>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2020 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<div id="outline-container-org65b5619" class="outline-2">
<h2 id="org65b5619">Problem</h2>
<div class="outline-text-2" id="text-org65b5619">
<p>
Write a function <code>setbits(x, p, n, y)</code> that returns x with n bits that begin at position p set to the right most n bits of y, leaving the other bits unchanged.
</p>
<div class="HTML">
<p>
<a name='more'></a><br />
</p>
</div>
</div>
</div>
<div id="outline-container-orgebd1959" class="outline-2">
<h2 id="orgebd1959">Solution</h2>
<div class="outline-text-2" id="text-orgebd1959">
<p>
Here, i feel most complicated thing is understanding the question rather than solving it by programming. Let me explain the question visually by an example.
</p>
<p>
<div class="separator" style="clear: both;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiPt2ILhvD5WCgZaWNRTDy5JDwUAycNzaQTBgZ_IwtHJiyYrRSHiEfcjEfP6P9WTN0Py7Ci9BKz-dk4QXVF3YrT_c7NpNVTUA-4Px5sj0STx1qZQEnizH1Nso1WqwT8H_ZTHS0XHys2GKg/s331/E-2-6.svg.png" style="display: block; padding: 1em 0; text-align: left; "><img alt="" border="0" height="320" data-original-height="331" data-original-width="177" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiPt2ILhvD5WCgZaWNRTDy5JDwUAycNzaQTBgZ_IwtHJiyYrRSHiEfcjEfP6P9WTN0Py7Ci9BKz-dk4QXVF3YrT_c7NpNVTUA-4Px5sj0STx1qZQEnizH1Nso1WqwT8H_ZTHS0XHys2GKg/s320/E-2-6.svg.png"/></a></div>
</p>
<p>
n bits at position p in x has to replaced with n right most bits of y to get the result.
</p>
<ol class="org-ol">
<li>First we need to get the Yellow region extracted from X</li>
<li>Second we need to get blue region from Y</li>
<li>We need to shift the blue region so that it fits between two yellow region</li>
<li>We need to 'bitwise OR' 1 and 3.</li>
</ol>
</div>
<div id="outline-container-org846a2c2" class="outline-3">
<h3 id="org846a2c2">Yellow region</h3>
<div class="outline-text-3" id="text-org846a2c2">
<p>
We need to create zero mask(means selective bits are zero and rest all are one) of n bits at position p. Expression to that is
</p>
<div class="org-src-container">
<pre class="src src-C">((~0 << (p + 1)) | (~(~0 << (p + 1 - n))))
</pre>
</div>
</div>
</div>
<div id="outline-container-org1564b2d" class="outline-3">
<h3 id="org1564b2d">Blue region</h3>
<div class="outline-text-3" id="text-org1564b2d">
<p>
Blue region can be easily extracted from the below expression.
</p>
<div class="org-src-container">
<pre class="src src-C">(y & ~(~0 << n))
</pre>
</div>
</div>
</div>
<div id="outline-container-org4ebd046" class="outline-3">
<h3 id="org4ebd046">Shifting Blue region</h3>
<div class="outline-text-3" id="text-org4ebd046">
<p>
This can be very easily gone by
</p>
<div class="org-src-container">
<pre class="src src-C">((y & ~(~0 << n)) << (p + 1 - n)
</pre>
</div>
</div>
</div>
<div id="outline-container-org0ce14cb" class="outline-3">
<h3 id="org0ce14cb">Putting all together</h3>
<div class="outline-text-3" id="text-org0ce14cb">
<p>
Now putting all together final program looks like this.
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #228b22;">unsigned</span> <span style="color: #0000ff;">setbits</span>(<span style="color: #228b22;">unsigned</span> <span style="color: #a0522d;">x</span>, <span style="color: #228b22;">unsigned</span> <span style="color: #a0522d;">p</span>, <span style="color: #228b22;">unsigned</span> <span style="color: #a0522d;">n</span>, <span style="color: #228b22;">unsigned</span> <span style="color: #a0522d;">y</span>)
{
<span style="color: #a020f0;">return</span> (x & ((~0 << (p + 1)) |
(~(~0 << (p + 1 - n))))) |
((y & ~(~0 << n)) << (p + 1 - n));
}
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-orgc8baa23" class="outline-2">
<h2 id="orgc8baa23">Links</h2>
<div class="outline-text-2" id="text-orgc8baa23">
<ul class="org-ul">
<li>Next Article -</li>
<li>Previous Article - <a href="https://www.harishnote.com/2021/06/k-r-exercise-25-alternate-strpbrk.html">K & R : Exercise 2.5 - alternate strpbrk</a></li>
<li>All Article - <a href="http://www.harishnote.com/p/my-k-r-answers.html">K & R Answer</a></li>
</ul>
</div>
</div>
</div>
</body>
</html>
Harishhttp://www.blogger.com/profile/09680319865654952936noreply@blogger.com2tag:blogger.com,1999:blog-3457920023827660787.post-72304050659481458942021-06-22T07:39:00.001+05:302021-06-23T21:57:00.749+05:30K & R : Exercise 2.5 - alternate strpbrk <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2021-06-23 Wed 21:56 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>K & R : Exercise 2.5 - alternate strpbrk</title>
<meta name="generator" content="Org mode" />
<meta name="author" content="Harish SK" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
/* Languages per Org manual */
pre.src-asymptote:before { content: 'Asymptote'; }
pre.src-awk:before { content: 'Awk'; }
pre.src-C:before { content: 'C'; }
/* pre.src-C++ doesn't work in CSS */
pre.src-clojure:before { content: 'Clojure'; }
pre.src-css:before { content: 'CSS'; }
pre.src-D:before { content: 'D'; }
pre.src-ditaa:before { content: 'ditaa'; }
pre.src-dot:before { content: 'Graphviz'; }
pre.src-calc:before { content: 'Emacs Calc'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-fortran:before { content: 'Fortran'; }
pre.src-gnuplot:before { content: 'gnuplot'; }
pre.src-haskell:before { content: 'Haskell'; }
pre.src-hledger:before { content: 'hledger'; }
pre.src-java:before { content: 'Java'; }
pre.src-js:before { content: 'Javascript'; }
pre.src-latex:before { content: 'LaTeX'; }
pre.src-ledger:before { content: 'Ledger'; }
pre.src-lisp:before { content: 'Lisp'; }
pre.src-lilypond:before { content: 'Lilypond'; }
pre.src-lua:before { content: 'Lua'; }
pre.src-matlab:before { content: 'MATLAB'; }
pre.src-mscgen:before { content: 'Mscgen'; }
pre.src-ocaml:before { content: 'Objective Caml'; }
pre.src-octave:before { content: 'Octave'; }
pre.src-org:before { content: 'Org mode'; }
pre.src-oz:before { content: 'OZ'; }
pre.src-plantuml:before { content: 'Plantuml'; }
pre.src-processing:before { content: 'Processing.js'; }
pre.src-python:before { content: 'Python'; }
pre.src-R:before { content: 'R'; }
pre.src-ruby:before { content: 'Ruby'; }
pre.src-sass:before { content: 'Sass'; }
pre.src-scheme:before { content: 'Scheme'; }
pre.src-screen:before { content: 'Gnu Screen'; }
pre.src-sed:before { content: 'Sed'; }
pre.src-sh:before { content: 'shell'; }
pre.src-sql:before { content: 'SQL'; }
pre.src-sqlite:before { content: 'SQLite'; }
/* additional languages in org.el's org-babel-load-languages alist */
pre.src-forth:before { content: 'Forth'; }
pre.src-io:before { content: 'IO'; }
pre.src-J:before { content: 'J'; }
pre.src-makefile:before { content: 'Makefile'; }
pre.src-maxima:before { content: 'Maxima'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-picolisp:before { content: 'Pico Lisp'; }
pre.src-scala:before { content: 'Scala'; }
pre.src-shell:before { content: 'Shell Script'; }
pre.src-ebnf2ps:before { content: 'ebfn2ps'; }
/* additional language identifiers per "defun org-babel-execute"
in ob-*.el */
pre.src-cpp:before { content: 'C++'; }
pre.src-abc:before { content: 'ABC'; }
pre.src-coq:before { content: 'Coq'; }
pre.src-groovy:before { content: 'Groovy'; }
/* additional language identifiers from org-babel-shell-names in
ob-shell.el: ob-shell is the only babel language using a lambda to put
the execution function name together. */
pre.src-bash:before { content: 'bash'; }
pre.src-csh:before { content: 'csh'; }
pre.src-ash:before { content: 'ash'; }
pre.src-dash:before { content: 'dash'; }
pre.src-ksh:before { content: 'ksh'; }
pre.src-mksh:before { content: 'mksh'; }
pre.src-posh:before { content: 'posh'; }
/* Additional Emacs modes also supported by the LaTeX listings package */
pre.src-ada:before { content: 'Ada'; }
pre.src-asm:before { content: 'Assembler'; }
pre.src-caml:before { content: 'Caml'; }
pre.src-delphi:before { content: 'Delphi'; }
pre.src-html:before { content: 'HTML'; }
pre.src-idl:before { content: 'IDL'; }
pre.src-mercury:before { content: 'Mercury'; }
pre.src-metapost:before { content: 'MetaPost'; }
pre.src-modula-2:before { content: 'Modula-2'; }
pre.src-pascal:before { content: 'Pascal'; }
pre.src-ps:before { content: 'PostScript'; }
pre.src-prolog:before { content: 'Prolog'; }
pre.src-simula:before { content: 'Simula'; }
pre.src-tcl:before { content: 'tcl'; }
pre.src-tex:before { content: 'TeX'; }
pre.src-plain-tex:before { content: 'Plain TeX'; }
pre.src-verilog:before { content: 'Verilog'; }
pre.src-vhdl:before { content: 'VHDL'; }
pre.src-xml:before { content: 'XML'; }
pre.src-nxml:before { content: 'XML'; }
/* add a generic configuration mode; LaTeX export needs an additional
(add-to-list 'org-latex-listings-langs '(conf " ")) in .emacs */
pre.src-conf:before { content: 'Configuration File'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.equation-container {
display: table;
text-align: center;
width: 100%;
}
.equation {
vertical-align: middle;
}
.equation-label {
display: table-cell;
text-align: right;
vertical-align: middle;
}
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
.org-svg { width: 90%; }
/*]]>*/-->
</style>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2020 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<div id="outline-container-orgae53739" class="outline-2">
<h2 id="orgae53739">Problem</h2>
<div class="outline-text-2" id="text-orgae53739">
<p>
Write the function any(s1, s2) which returns the first location in the string s1, where any charecter from s2 occurs or
-1 if s1 contains no charecters from s2. (The standard library function strpbrk does the same job but returns a pointer to the location).
</p>
<div class="HTML">
<p>
<a name='more'></a><br />
</p>
</div>
</div>
</div>
<div id="outline-container-org0f105cd" class="outline-2">
<h2 id="org0f105cd">Solution</h2>
<div class="outline-text-2" id="text-org0f105cd">
<p>
First we will traverse s1 charecter by charecter (outer for loop with index i).
Second loop (with index j) will traverse s2.
Each charecter in s1 is tested against charecter in s2, if found then break and return index.
If not charecter is not found then -1 is returned.
Solution would looks something like below.
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><string.h></span>
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">any</span>(<span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span> *<span style="color: #a0522d;">s1</span>, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span> *<span style="color: #a0522d;">s2</span>);
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">char</span> *<span style="color: #a0522d;">s1</span> = <span style="color: #8b2252;">"hello"</span>;
<span style="color: #228b22;">char</span> *<span style="color: #a0522d;">s2</span> = <span style="color: #8b2252;">"world"</span>;
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">idx</span>;
idx = any(s1, s2);
printf(<span style="color: #8b2252;">"First character matching %c is at %d\n"</span>, s1[idx], idx);
<span style="color: #a020f0;">return</span> 0;
}
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">any</span>(<span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span> *<span style="color: #a0522d;">s1</span>, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span> *<span style="color: #a0522d;">s2</span>)
{
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">i</span>, <span style="color: #a0522d;">j</span>;
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">k</span> = 0;
<span style="color: #a020f0;">for</span>(i = 0; i < strlen(s1); i++) {
<span style="color: #a020f0;">for</span>(j = 0; j < strlen(s2); j++) {
<span style="color: #a020f0;">if</span>(s1[i] == s2[j])
<span style="color: #a020f0;">break</span>;
}
<span style="color: #a020f0;">if</span>(j != strlen(s2))
<span style="color: #a020f0;">break</span>;
}
<span style="color: #a020f0;">if</span>(i == strlen(s1))
<span style="color: #a020f0;">return</span> -1;
<span style="color: #a020f0;">else</span>
<span style="color: #a020f0;">return</span> i;
}
</pre>
</div>
<pre class="example">
First character matching l is at 2
</pre>
</div>
</div>
<div id="outline-container-org1116065" class="outline-2">
<h2 id="org1116065">Links</h2>
<div class="outline-text-2" id="text-org1116065">
<ul class="org-ul">
<li>Next Article - <a href="https://www.harishnote.com/2021/06/k-r-exercise-26-setbits.html">K & R : Exercise 2.6 - setbits</a></li>
<li>Previous Article - <a href="https://www.harishnote.com/2021/06/k-r-exercise-24-squeeze-string.html">K & R : Exercise 2.4 - squeeze string</a></li>
<li>All Article - <a href="http://www.harishnote.com/p/my-k-r-answers.html">K & R Answer</a></li>
</ul>
</div>
</div>
</div>
</body>
</html>Harishhttp://www.blogger.com/profile/09680319865654952936noreply@blogger.com0tag:blogger.com,1999:blog-3457920023827660787.post-66196529249599143912021-06-21T20:00:00.003+05:302021-06-22T07:44:11.230+05:30K & R : Exercise 2.4 - squeeze string<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2021-06-22 Tue 07:43 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>K & R : Exercise 2.4 - squeeze string</title>
<meta name="generator" content="Org mode" />
<meta name="author" content="Harish SK" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
/* Languages per Org manual */
pre.src-asymptote:before { content: 'Asymptote'; }
pre.src-awk:before { content: 'Awk'; }
pre.src-C:before { content: 'C'; }
/* pre.src-C++ doesn't work in CSS */
pre.src-clojure:before { content: 'Clojure'; }
pre.src-css:before { content: 'CSS'; }
pre.src-D:before { content: 'D'; }
pre.src-ditaa:before { content: 'ditaa'; }
pre.src-dot:before { content: 'Graphviz'; }
pre.src-calc:before { content: 'Emacs Calc'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-fortran:before { content: 'Fortran'; }
pre.src-gnuplot:before { content: 'gnuplot'; }
pre.src-haskell:before { content: 'Haskell'; }
pre.src-hledger:before { content: 'hledger'; }
pre.src-java:before { content: 'Java'; }
pre.src-js:before { content: 'Javascript'; }
pre.src-latex:before { content: 'LaTeX'; }
pre.src-ledger:before { content: 'Ledger'; }
pre.src-lisp:before { content: 'Lisp'; }
pre.src-lilypond:before { content: 'Lilypond'; }
pre.src-lua:before { content: 'Lua'; }
pre.src-matlab:before { content: 'MATLAB'; }
pre.src-mscgen:before { content: 'Mscgen'; }
pre.src-ocaml:before { content: 'Objective Caml'; }
pre.src-octave:before { content: 'Octave'; }
pre.src-org:before { content: 'Org mode'; }
pre.src-oz:before { content: 'OZ'; }
pre.src-plantuml:before { content: 'Plantuml'; }
pre.src-processing:before { content: 'Processing.js'; }
pre.src-python:before { content: 'Python'; }
pre.src-R:before { content: 'R'; }
pre.src-ruby:before { content: 'Ruby'; }
pre.src-sass:before { content: 'Sass'; }
pre.src-scheme:before { content: 'Scheme'; }
pre.src-screen:before { content: 'Gnu Screen'; }
pre.src-sed:before { content: 'Sed'; }
pre.src-sh:before { content: 'shell'; }
pre.src-sql:before { content: 'SQL'; }
pre.src-sqlite:before { content: 'SQLite'; }
/* additional languages in org.el's org-babel-load-languages alist */
pre.src-forth:before { content: 'Forth'; }
pre.src-io:before { content: 'IO'; }
pre.src-J:before { content: 'J'; }
pre.src-makefile:before { content: 'Makefile'; }
pre.src-maxima:before { content: 'Maxima'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-picolisp:before { content: 'Pico Lisp'; }
pre.src-scala:before { content: 'Scala'; }
pre.src-shell:before { content: 'Shell Script'; }
pre.src-ebnf2ps:before { content: 'ebfn2ps'; }
/* additional language identifiers per "defun org-babel-execute"
in ob-*.el */
pre.src-cpp:before { content: 'C++'; }
pre.src-abc:before { content: 'ABC'; }
pre.src-coq:before { content: 'Coq'; }
pre.src-groovy:before { content: 'Groovy'; }
/* additional language identifiers from org-babel-shell-names in
ob-shell.el: ob-shell is the only babel language using a lambda to put
the execution function name together. */
pre.src-bash:before { content: 'bash'; }
pre.src-csh:before { content: 'csh'; }
pre.src-ash:before { content: 'ash'; }
pre.src-dash:before { content: 'dash'; }
pre.src-ksh:before { content: 'ksh'; }
pre.src-mksh:before { content: 'mksh'; }
pre.src-posh:before { content: 'posh'; }
/* Additional Emacs modes also supported by the LaTeX listings package */
pre.src-ada:before { content: 'Ada'; }
pre.src-asm:before { content: 'Assembler'; }
pre.src-caml:before { content: 'Caml'; }
pre.src-delphi:before { content: 'Delphi'; }
pre.src-html:before { content: 'HTML'; }
pre.src-idl:before { content: 'IDL'; }
pre.src-mercury:before { content: 'Mercury'; }
pre.src-metapost:before { content: 'MetaPost'; }
pre.src-modula-2:before { content: 'Modula-2'; }
pre.src-pascal:before { content: 'Pascal'; }
pre.src-ps:before { content: 'PostScript'; }
pre.src-prolog:before { content: 'Prolog'; }
pre.src-simula:before { content: 'Simula'; }
pre.src-tcl:before { content: 'tcl'; }
pre.src-tex:before { content: 'TeX'; }
pre.src-plain-tex:before { content: 'Plain TeX'; }
pre.src-verilog:before { content: 'Verilog'; }
pre.src-vhdl:before { content: 'VHDL'; }
pre.src-xml:before { content: 'XML'; }
pre.src-nxml:before { content: 'XML'; }
/* add a generic configuration mode; LaTeX export needs an additional
(add-to-list 'org-latex-listings-langs '(conf " ")) in .emacs */
pre.src-conf:before { content: 'Configuration File'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.equation-container {
display: table;
text-align: center;
width: 100%;
}
.equation {
vertical-align: middle;
}
.equation-label {
display: table-cell;
text-align: right;
vertical-align: middle;
}
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
.org-svg { width: 90%; }
/*]]>*/-->
</style>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2020 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<div id="outline-container-org8aea0a7" class="outline-2">
<h2 id="org8aea0a7">Problem</h2>
<div class="outline-text-2" id="text-org8aea0a7">
<p>
Write an alternate version of squeeze(s1, s2) that deletes each charecter in s1 that matches any charecter in the string s2.
</p>
<div class="HTML">
<p>
<a name='more'></a><br />
</p>
</div>
</div>
</div>
<div id="outline-container-orgd6b2440" class="outline-2">
<h2 id="orgd6b2440">Solution</h2>
<div class="outline-text-2" id="text-orgd6b2440">
<p>
Hence s1 would be modified removing all the charecters from s1 which are there in s2.
First we will traverse s1 charecter by charecter (outer for loop with index i).
Second loop (with index j) will traverse s2.
Each charecter in s1 is tested against charecter in s2, if found then break is hit so that it is skipped.
Solution would looks something like below.
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><string.h></span>
<span style="color: #228b22;">void</span> <span style="color: #0000ff;">squeeze</span>(<span style="color: #228b22;">char</span> *<span style="color: #a0522d;">s1</span>, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span> *<span style="color: #a0522d;">s2</span>);
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">s1</span>[10] = <span style="color: #8b2252;">"hello"</span>;
<span style="color: #228b22;">char</span> *<span style="color: #a0522d;">s2</span> = <span style="color: #8b2252;">"world"</span>;
printf(<span style="color: #8b2252;">"Before Squeeze s1 is %s\n"</span>, s1);
squeeze(s1, s2);
printf(<span style="color: #8b2252;">"After Squeeze s1 is %s\n"</span>, s1);
<span style="color: #a020f0;">return</span> 0;
}
<span style="color: #228b22;">void</span> <span style="color: #0000ff;">squeeze</span>(<span style="color: #228b22;">char</span> *<span style="color: #a0522d;">s1</span>,<span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span> *<span style="color: #a0522d;">s2</span>)
{
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">i</span>, <span style="color: #a0522d;">j</span>;
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">k</span> = 0;
<span style="color: #a020f0;">for</span>(i = 0; i < strlen(s1); i++) {
<span style="color: #a020f0;">for</span>(j = 0; j < strlen(s2); j++) {
<span style="color: #a020f0;">if</span>(s1[i] == s2[j])
<span style="color: #a020f0;">break</span>;
}
<span style="color: #a020f0;">if</span>(j == strlen(s2)) {
s1[k++] = s1[i];
}
}
s1[k] = <span style="color: #8b2252;">'\0'</span>;
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
<pre class="example">
Before Squeeze s1 is hello
After Squeeze s1 is he
</pre>
</div>
</div>
<div id="outline-container-org31d11e0" class="outline-2">
<h2 id="org31d11e0">Links</h2>
<div class="outline-text-2" id="text-org31d11e0">
<ul class="org-ul">
<li>Next Article - <a href="https://www.harishnote.com/2021/06/k-r-exercise-25-alternate-strpbrk.html">K & R : Exercise 2.5 - alternate strpbrk</a></li>
<li>Previous Article - <a href="https://www.harishnote.com/2016/11/k-r-exercise-23-htoi-hex-to-integer.html">K & R : Exercise 2.3 - htoi - hex to integer</a></li>
<li>All Article - <a href="https://www.harishnote.com/p/my-k-r-answers.html">K & R Answer</a></li>
</ul>
</div>
</div>
</div>
</body>
</html>
Harishhttp://www.blogger.com/profile/09680319865654952936noreply@blogger.com0tag:blogger.com,1999:blog-3457920023827660787.post-11158808191005864292017-08-16T20:23:00.001+05:302017-08-16T20:23:57.701+05:30SICP# E1.2: Translate formula to prefix notation<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2017-08-16 Wed 20:22 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>SICP# E1.2: Translate formula to prefix notation</title>
<meta name="generator" content="Org-mode" />
<meta name="author" content="Harish SK" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
pre.src-sh:before { content: 'sh'; }
pre.src-bash:before { content: 'sh'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-R:before { content: 'R'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-java:before { content: 'Java'; }
pre.src-sql:before { content: 'SQL'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
/*]]>*/-->
</style>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2013 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
displayAlign: "center",
displayIndent: "0em",
"HTML-CSS": { scale: 100,
linebreaks: { automatic: "false" },
webFont: "TeX"
},
SVG: {scale: 100,
linebreaks: { automatic: "false" },
font: "TeX"},
NativeMML: {scale: 100},
TeX: { equationNumbers: {autoNumber: "AMS"},
MultLineWidth: "85%",
TagSide: "right",
TagIndent: ".8em"
}
});
</script>
<script type="text/javascript"
src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"></script>
</head>
<body>
<div id="content">
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1">Translate the following expression into prefix form</h2>
<div class="outline-text-2" id="text-orgheadline1">
<p>
\(\dfrac{5 + 4 + (2 - (3 - (6 + \dfrac{4}{3})))}{3(6 - 2) (2-7)}\)
</p>
<a name='more'></a><br />
<div class="org-src-container">
<pre class="src src-scheme">(/ (+ 5 4 ( - 2 (- 3 (+ 6 (/ 4 3)))))
(* 3 (- 6 2) (- 2 7)))
<span style="color: #b22222;">;</span><span style="color: #b22222;">Value: -23/90</span>
</pre>
</div>
</div>
</div>
</div>
</body>
</html>Harishhttp://www.blogger.com/profile/09680319865654952936noreply@blogger.com0tag:blogger.com,1999:blog-3457920023827660787.post-41901945908057380992017-08-15T12:18:00.000+05:302017-08-16T20:27:24.431+05:30SICP# E1.1: Evaluate scheme expression<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2017-08-16 Wed 20:27 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>SICP# E1.1: Evalute scheme expression</title>
<meta name="generator" content="Org-mode" />
<meta name="author" content="Harish SK" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
pre.src-sh:before { content: 'sh'; }
pre.src-bash:before { content: 'sh'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-R:before { content: 'R'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-java:before { content: 'Java'; }
pre.src-sql:before { content: 'SQL'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
/*]]>*/-->
</style>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2013 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<p>
Below is a sequence of expressions. What is the result printed by the interpretter in response to each expression ?
Assume that sequence is to be evaluated in the order in which it is presented.
</p>
<a name='more'></a><br />
<div class="org-src-container">
<pre class="src src-scheme">10
<span style="color: #b22222;">;</span><span style="color: #b22222;">Value: 10</span>
(+ 5 3 4)
<span style="color: #b22222;">;</span><span style="color: #b22222;">Value: 12</span>
(- 9 1)
<span style="color: #b22222;">;</span><span style="color: #b22222;">Value: 8</span>
(/ 6 2)
<span style="color: #b22222;">;</span><span style="color: #b22222;">Value: 3</span>
(+ (* 2 4) (- 4 6))
<span style="color: #b22222;">;</span><span style="color: #b22222;">Value: 6</span>
(<span style="color: #a020f0;">define</span> <span style="color: #0000ff;">a</span> 3)
<span style="color: #b22222;">;</span><span style="color: #b22222;">Value: a</span>
(<span style="color: #a020f0;">define</span> <span style="color: #0000ff;">b</span> (+ a 1))
<span style="color: #b22222;">;</span><span style="color: #b22222;">Value: b</span>
(+ a b (* a b))
<span style="color: #b22222;">;</span><span style="color: #b22222;">Value: 19</span>
(= a b)
<span style="color: #b22222;">;</span><span style="color: #b22222;">Value: #f</span>
(<span style="color: #a020f0;">if</span> (<span style="color: #a020f0;">and</span> (> b a) (< b (* a b)))
b
a)
<span style="color: #b22222;">;</span><span style="color: #b22222;">Value: 4</span>
(* (<span style="color: #a020f0;">cond</span> ((> a b) a)
((< a b) b)
(<span style="color: #a020f0;">else</span> -1))
(+ a 1))
<span style="color: #b22222;">;</span><span style="color: #b22222;">Value: 4</span>
</pre>
</div>
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1">Links</h2>
<div class="outline-text-2" id="text-orgheadline1">
<ul class="org-ul">
<li>Next Article - <a href="http://www.harishnote.com/2017/08/sicp-e11-evaluate-scheme-expression.html">E1.1 - Evaluate scheme expression</a></li>
<li>Previous Article -</li>
<li>All Article - <a href="http://www.harishnote.com/p/sicp.html">SICP</a></li>
</ul>
</div>
</div>
</div>
</body>
</html>Harishhttp://www.blogger.com/profile/09680319865654952936noreply@blogger.com0tag:blogger.com,1999:blog-3457920023827660787.post-3178431506134854552016-11-27T21:39:00.004+05:302016-12-20T12:35:38.666+05:30Blog: My life changing YouTube video (FIGHTMEDIOCRITY TOP 9 BOOKS)<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRIPTR-_96LO8cjsfABmCsf2JfQcK01m68Q9Dzvi0k83xDPHBGpethOH2DsDk2hhyphenhyphenjUlCyNeiF7DhiW0bebfYyY-BJV4DK9eOg3s1rBBYKGCExhDYNca0Tx8uJDicr-1w5ApEC8NZjmkk/s1600/IMG_20161127_212850.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="240" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRIPTR-_96LO8cjsfABmCsf2JfQcK01m68Q9Dzvi0k83xDPHBGpethOH2DsDk2hhyphenhyphenjUlCyNeiF7DhiW0bebfYyY-BJV4DK9eOg3s1rBBYKGCExhDYNca0Tx8uJDicr-1w5ApEC8NZjmkk/s320/IMG_20161127_212850.jpg" width="320" /></a></div>
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<!-- 2016-11-27 Sun 21:18 -->
<title>Blog #3: My life changing YouTube video (FIGHTMEDIOCRITY TOP 9 BOOKS)</title>
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
pre.src-sh:before { content: 'sh'; }
pre.src-bash:before { content: 'sh'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-R:before { content: 'R'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-java:before { content: 'Java'; }
pre.src-sql:before { content: 'SQL'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
/*]]>*/-->
</style>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2013 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
Around 7 months ago I stumbled across a YouTube video by a channel <a href="https://www.youtube.com/channel/UCXLesGEfmyhxqOjoAqhRwhA">FightMediocrity</a>.
I had subscribed to that channel long back, but some how had missed this particular video.
You can see the whole video below
<br />
<iframe allowfullscreen="" frameborder="0" height="360" src="https://www.youtube.com/embed/huJrpO4Fev4" width="640"></iframe>
<br />
It is 17:52 minute long video which lists Fightmediocrity's top 9 books.
I highly recommend you to watch the video and then continue reading.
<br />
<a name='more'></a><br />
I would not try to repeat of what is in the video because explanation in video is exceptional.
But i would first list those 9 books and authors and would give my take on it.
He mentions list of nine books which he puts in 3 categories (2 un-categorized).
<br />
A. Money
<br />
<ol class="org-ol">
<li><b>Rich Dad Poor Dad</b> by Robert T. Kiyosaki</li>
<li><b>The 4-hour workweek</b> by Timothy Ferriss</li>
</ol>
B. Social intelligence
<br />
<ol class="org-ol">
<li><b>How to Win Friends and Influence People</b> by Dale Carnegie</li>
<li><b>The 48 Laws Of Power</b> by Robert Greene</li>
</ol>
C. Becoming a Master
<br />
<ol class="org-ol">
<li><b>The Way Of The Superior Man</b> by David Deida</li>
<li><b>Man's Search for Meaning</b> by Viktor E Frankl</li>
<li><b>Mastery</b> by George Leonard</li>
</ol>
<ol class="org-ol">
<li><b>Mindfulness for Beginners</b> by Jon Kabat-Zinn</li>
<li><b>The Obstacle is the Way</b> by Ryan Holiday</li>
</ol>
After watching the video 7 months ago, I was highly inspired to read them.
Rich Dad Poor Dad I had already read during my collage days and found the theme very useful.
May be as I have a job now it really did not matter now.
<br />
I was blown away by the author (Tim Ferriss) more than the book "The 4-hour workweek".
He has a YouTube account (<a href="https://www.youtube.com/user/masterlock77">YouTube:Tim Ferriss</a>) where he does podcast with really inspiring people.
Podcast are around (~1-2 hours long), but he indulges audience and digs deep.
<br />
Fourth book (The 48 Laws Of Power) was most blunt in this list.
It talks about power and people go ways around power which shape the social interaction.
For me it meant as a gateway to understand other person's behaviour in social interaction.
Humans always try to be logical and want the answer to several "Whys".
This book has answer to several "Why" on social interaction topic.
<br />
Third book (How to Win Friends and Influence People) i had always seen with my close friend (he was the nicest guy) during collage days.
I did not read it during my collage days, i don't know why; but after reading it now i regret not reading it then.
This book had some of the best piece of advice when interacting with others.
<br />
Fifth book (The Way Of The Superior Man) really eye opener in personal relationship front.
I would highly recommend it for men, who want to be in control of their life.
<br />
Sixth book Man's Search for Meaning is Viktor Frankl's experience in concentration camps.
I always believed that hearing someones misery and feeling good that we are not in that situation is very bad way to cheer up.
But this book is beyond the above thought. Philosophy that author derives from being in the extreme condition are so pure and basic.
<br />
Seventh and Eight book i have not read yet.
May be i am not in right situation in my life for these books to make an impact.
But with inspiration that i had from the other books i feel these would turn out good.
<br />
Ninth book by Ryan Holiday - The Obstacle is the Way was the first book that i read after watching the video.
May be it is my nature where I read the new book's back first.
Reading this book alone is the best thing that has happened to me till date.
It has changed the way I see life, way I react to situation.
If you ask in some way it has changed me in small way.
I read another book by same author "Ego is the Enemy", which turned out even more helpful than first one.
Both the books are on Stoic philosophy.
I think many of stoic philosophical traits can be found in Bhagavat-gita. (May be I will write some comparison in future).
<br />
I am not just finished when I have read all the books.
I would re-read them till I understand every bit of it.
I find that these books have the gist of all self-help book ever written.
<br />
Human life is constant struggle for improvement, just not the individual but the entire race.
Each person becomes better and finds inspiration from
<br />
<ol class="org-ol">
<li>Interaction with people</li>
<li>Travelling different places and seeing different culture.</li>
<li>Life experience (mostly bitter ones teach the most).</li>
<li>Reading books.</li>
</ol>
For me reading above books has been far more inspiration compared to other 3 things.
My heart filled thanks to Fightmediocrity putting this video out.
<br />
Please leave your suggestion and thoughts in the comment section below.
<br />
</div>
</body>
</html>Harishhttp://www.blogger.com/profile/09680319865654952936noreply@blogger.com2tag:blogger.com,1999:blog-3457920023827660787.post-71293926641941675462016-11-26T23:19:00.000+05:302016-12-20T12:35:29.472+05:30Blog: Vada<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhWZ8EWodIA2tPVqs05VxqZMUOBS_W_7cJrSs9hKPJSC_0UQ6PUrlkk9_X92wLgo_0xVny3xeXj0WNUyDVbBw-HCPLQftMGtxcZZVk3_I6jR29NXiHKyiEqUqDWRMue7HBwJ-nCZ6ERDB8/s1600/IMG_20161126_194851.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="240" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhWZ8EWodIA2tPVqs05VxqZMUOBS_W_7cJrSs9hKPJSC_0UQ6PUrlkk9_X92wLgo_0xVny3xeXj0WNUyDVbBw-HCPLQftMGtxcZZVk3_I6jR29NXiHKyiEqUqDWRMue7HBwJ-nCZ6ERDB8/s320/IMG_20161126_194851.jpg" width="320" /></a></div>
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<!-- 2016-11-26 Sat 23:13 -->
<title>Blog #2: Vada</title>
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
pre.src-sh:before { content: 'sh'; }
pre.src-bash:before { content: 'sh'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-R:before { content: 'R'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-java:before { content: 'Java'; }
pre.src-sql:before { content: 'SQL'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
/*]]>*/-->
</style>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2013 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
Saturday and Sunday being the weekends, my wife makes some wonderful breakfast.
Weekdays i generally have my breakfast in office cafeteria.
Yesterday being a Friday, I made general enquiry to my wife in evening what is special tomorrow for breakfast.
She declared it is Vada (It is south indian dish).
My wife's preparation of Vada and Sambar (side dish) is the best.
If you are interested in making Vada, you can see this youtube video <a href="https://www.youtube.com/watch?v=DwiDbLfHnjI">Medu Vada - By VahChef @ VahRehVah.com</a>.
<br />
<a name='more'></a><br />
Night around 1:00am my 8 month daughter started making some weird sound.
Both of us got up trying to console her.
She suddenly started crawling towards me and climbed up my lap.
She was having a loose motion, It was her first time.
Initially we thought it might not be as bad as we thought and slept off.
<br />
Morning again at around 6:00 my daughter got up and started making the sound again.
She was again having loose motion.
Doing twice in 12 hours gap we thought it might her belly getting cleared up.
My wife started weening her and both of them slept off.
I could not sleep again, so i came out of bedroom, and suddenly see last days mess in hall.
All things were scattered here and there, thought let me clean it all before my Wife and daughter wake up.
It took around ~1 hour to clean up the mess
I generally go to office very early and never see my house untidy.
I never see who tidies it, I assume. I should really appreciate my wife.
Then for some reason i went into Utility and saw huge pile of cloths for laundry and cloths in washing needed to be dried.
I started my work again in Utility now, after finishing everything i see my Wife and daughter woke up again.
I just made suggestion to my Wife it is 9:30, Breakfast ? Vada Sambar ?
She replied to me telling that may be we should Visit doctor as daughter had loose motion again.
We both now were in some panic mode.
Being Saturday it would be a tremendous rush in Doctor's Clinic.
Having no appointment we need to Walk-in and would be given last preference.
But we had to visit Doctor as tomorrow being Sunday holiday, we did not want to take the risk.
My wife quickly feed the baby.
From the time we decided to visit doctor to we actually stepped into Clinic, my daughter had totally had 6 times loose motion.
Situation, was little concerning to both of us. We told the situation to nurse and she casually told -
"Lets take baby weight, height…".
After the formalities we were told we might need to wait for 2 hours time.
Daughter still being cheerful and playful we assumed she should be OK.
We had some quick breakfast in near by Hotel and went to relatives house which is very near to clinic.
We enquired from phone so that we could be in Clinic just before our turn comes. I took almost 3 hours before our turn came.
Doctor consultation was over and she suggested some pro-biotics and ORS.
Doctor suggested it not be a infection, and this should suffice.
We returned to relatives house for quick and lunch and returned home.
It was around 4:00pm, we were all tired and just slept off.
<br />
I woke to sudden door bell, some courier had come.
I had totally forgotten about Vada-Sambar by now. Mostly with digression of topic you might have too.
But Wife did not, she prepared delicious Vada-Sambar for dinner.
I was in awe towards my wife.
Small things like this makes life beautiful.
Finally my Weekend special breakfast became special dinner.
<br />
My idea of writing the whole day events here was to get some experience in flow of writing simple and boring thoughts and hopefully keep reader engaged.
<br />
Do you write your suggestions in the comment section below
<br />
</div>
</body>
</html>Harishhttp://www.blogger.com/profile/09680319865654952936noreply@blogger.com1tag:blogger.com,1999:blog-3457920023827660787.post-29011222999800268552016-11-26T22:49:00.000+05:302016-11-26T22:56:48.075+05:30C Programming #91: fileio - fprintf, fscanf<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2016-11-26 Sat 22:56 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>C Programming #91: fileio - fprintf, fscanf</title>
<meta name="generator" content="Org-mode" />
<meta name="author" content="Harish SK" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
pre.src-sh:before { content: 'sh'; }
pre.src-bash:before { content: 'sh'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-R:before { content: 'R'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-java:before { content: 'Java'; }
pre.src-sql:before { content: 'SQL'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
/*]]>*/-->
</style>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2013 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<p>
Similarly how there was formatted print function for <code>stdio</code>, there are function that could be used with file.
We could apply all the formatting tricks that was used for <code>printf</code> and <code>scanf</code>.
</p>
<a name='more'></a><br />
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1">fprintf</h2>
<div class="outline-text-2" id="text-orgheadline1">
<div class="org-src-container">
<pre class="src src-C"><span style="color: #228b22;">int</span> <span style="color: #0000ff;">fprintf</span>(<span style="color: #228b22;">FILE</span> *<span style="color: #a0522d;">stream</span>, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span> *<span style="color: #a0522d;">format</span>, ...);
</pre>
</div>
<p>
First argument is pointer to FILE. Rest of it is like <code>printf</code>.
Small example to demo, by writing 'a', 10 and "hello" into a file.
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><errno.h></span>
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">FILE</span> *<span style="color: #a0522d;">fp</span>;
fp = fopen(<span style="color: #8b2252;">"/tmp/test_file"</span>, <span style="color: #8b2252;">"w+"</span>);
<span style="color: #a020f0;">if</span>(fp != <span style="color: #008b8b;">NULL</span>) {
printf(<span style="color: #8b2252;">"file opened successfully fp is %p\n"</span>, fp);
}<span style="color: #a020f0;">else</span> {
printf(<span style="color: #8b2252;">"fopen failed with errorno %d\n"</span>, errno);
}
fprintf(fp,<span style="color: #8b2252;">"%c%d%s"</span>, <span style="color: #8b2252;">'a'</span>, 10, <span style="color: #8b2252;">"hello"</span>);
<span style="color: #a020f0;">if</span>(fclose(fp) != EOF) {
printf(<span style="color: #8b2252;">"file closed successfully"</span>);
}<span style="color: #a020f0;">else</span> {
printf(<span style="color: #8b2252;">"fclose failed with errorno %d\n"</span>, errno);
}
fp = <span style="color: #008b8b;">NULL</span>;
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
<pre class="example">
file opened successfully fp is 0x2337010
file closed successfully
# cat /tmp/test_file
a10hello
</pre>
</div>
</div>
<div id="outline-container-orgheadline2" class="outline-2">
<h2 id="orgheadline2">fscanf</h2>
<div class="outline-text-2" id="text-orgheadline2">
<div class="org-src-container">
<pre class="src src-C"><span style="color: #228b22;">int</span> <span style="color: #0000ff;">fscanf</span>(<span style="color: #228b22;">FILE</span> *<span style="color: #a0522d;">stream</span>, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span> *<span style="color: #a0522d;">format</span>, ...);
</pre>
</div>
<p>
Now lets read those there variable using fscanf.
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><errno.h></span>
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">FILE</span> *<span style="color: #a0522d;">fp</span>;
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">c</span>;
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">i</span>;
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">s</span>[100];
fp = fopen(<span style="color: #8b2252;">"/tmp/test_file"</span>, <span style="color: #8b2252;">"r"</span>);
<span style="color: #a020f0;">if</span>(fp != <span style="color: #008b8b;">NULL</span>) {
printf(<span style="color: #8b2252;">"file opened successfully fp is %p\n"</span>, fp);
}<span style="color: #a020f0;">else</span> {
printf(<span style="color: #8b2252;">"fopen failed with errorno %d\n"</span>, errno);
}
fscanf(fp, <span style="color: #8b2252;">"%c%d%s"</span>, &c, &i, s);
printf(<span style="color: #8b2252;">"c is %c, i is %d and s is %s\n"</span>, c, i, s);
<span style="color: #a020f0;">if</span>(fclose(fp) != EOF) {
printf(<span style="color: #8b2252;">"file closed successfully"</span>);
}<span style="color: #a020f0;">else</span> {
printf(<span style="color: #8b2252;">"fclose failed with errorno %d\n"</span>, errno);
}
fp = <span style="color: #008b8b;">NULL</span>;
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
</div>
</div>
<div id="outline-container-orgheadline3" class="outline-2">
<h2 id="orgheadline3">Links</h2>
<div class="outline-text-2" id="text-orgheadline3">
<ul class="org-ul">
<li>Next Article -</li>
<li>Previous Article - <a href="http://www.harishnote.com/2016/11/c-programming-90-fileio-fopen-fclose.html">C Programming #90: fileio - fopen, fclose, fputc, fgetc, fputs, fgets</a></li>
<li>All Article - <a href="http://www.harishnote.com/p/c-programming.html">C Programming</a></li>
</ul>
</div>
</div>
</div>
</body>
</html>Harishhttp://www.blogger.com/profile/09680319865654952936noreply@blogger.com0tag:blogger.com,1999:blog-3457920023827660787.post-41593399475982564172016-11-25T23:14:00.000+05:302016-12-20T12:35:48.593+05:30Blog: Getting started, Why K&R is the best book on C programming?<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUEG8XAW_fR25Bs8hGTHKTqC0BPHrYDkLnLXUiLfKutSX7elGXaAjvMoQM7eVuwp8EsFpGNzuhk7Lb7AMM7vaCYc_71p9UGQhN6W4k473cMjZw3XrrcaPU0qKc1A-C8_08I2TKwlqeTcc/s1600/IMG_20161125_230551.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="240" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUEG8XAW_fR25Bs8hGTHKTqC0BPHrYDkLnLXUiLfKutSX7elGXaAjvMoQM7eVuwp8EsFpGNzuhk7Lb7AMM7vaCYc_71p9UGQhN6W4k473cMjZw3XrrcaPU0qKc1A-C8_08I2TKwlqeTcc/s320/IMG_20161125_230551.jpg" width="320" /></a></div>
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<!-- 2016-11-25 Fri 23:03 -->
<title>Blog #1: Getting started, Why K&R is the best book on C programming?</title>
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
pre.src-sh:before { content: 'sh'; }
pre.src-bash:before { content: 'sh'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-R:before { content: 'R'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-java:before { content: 'Java'; }
pre.src-sql:before { content: 'SQL'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
/*]]>*/-->
</style>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2013 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
On November 25th 2016 @ 8:20 I have decided that - "I would write a blog daily".
My idea is to write on something interesting and fun.
It will be on wide range of things including but not restricted to books, technical topic, personal, philosophical, god, daily routine or YouTube videos…
My idea is Writing daily would lead to creative life and I can hone my writing skills to too.
Making it public is to take criticism and to improve.
<br />
<a name='more'></a><br />
Today i will write on my favorite book C Programming by Brian Kernighan and Dennis Ritchie.
I am working as Software Engineer working on Embedded Software for ~10 years now.
With the word Embedded comes my affection to "C" Language.
<br />
I was introduced to C during by 8th standard (Year 1998)(First year of high school).
During those days there no curriculum of programming in school.
But i had good number of Acquaintances working as Software Engineers, and their suggestion for vacation is to "learn computers and programming".
Then i was made to join course on "Basics of computer and programming in C".
I used to daily go 4 km peddling my cycle to institute for 6 weeks.
At that age my interest was gaming; playing console based games such as Dave, Contra etc.
I used Blue screen Turbo C compiler and editor.
<br />
When I joined my Engineering that is when Programming became serious.
Even though I did my engineering in Electronics and Communication i was interested in programming a lot.
I was introduced to K&R book by one of my senior (Year 2003) and i bought a copy of it immediately.
I have to admit that i never read the book from start to end. But i have read most part of it many time (~ 10 times).
I read the introduction of the book i felt the information density is very high and needed most concentration.
Really liked the idea of giving the birds eye view in introduction of language before giving details.
Most of my friend did not past the 1st chapter.
Along with reading this book I wrote some simple C program not just example given in the book.
I still remember writing program like "Who wants to be Crorepati" (Who wants to be millionaire), snake (game), line based text editor so on.
There was no internet connection in my PC those days, had to go to College lab for that.
Not much resources like <a href="http://ocw.mit.edu/">http://ocw.mit.edu/</a> other programming sites like <a href="http://www.geeksforgeeks.org/">http://www.geeksforgeeks.org/</a> or <a href="https://www.topcoder.com/">https://www.topcoder.com/</a>
<br />
I used to read and re-read the book and makes many notes in the side.
Each read used to highlight to me some part which i had ignored previously.
Information density is so much that you need to read it more than once.
I participated in several programming quiz held in Collage and used to do well.
Most of questions become easy once concepts are clear.
I remember one of senior telling me that "Questions are several, concepts are limited".
Hence understand the concepts you will do well with questions.
K & R helped me getting this concepts very clearly.
I had weird thought process of "What would compiler do ?"; it helped me a lot.
By my 3 year of Engineering; i started conducting C classes and C quizzes.
This increased my understanding a lot as setting question and conducting classes are lot difficult.
I still cherish reading K&R time and again, i still own a copy.
<br />
<b>I highly recommend K&R</b>.
<br />
For any-one interested can also read my series of Tutorial articles on C programming here - <a href="http://www.harishnote.com/p/c-programming.html">http://www.harishnote.com/p/c-programming.html</a>
<br />
Do share your thoughts in the comment section bellow.
<br />
</div>
</body>
</html>Harishhttp://www.blogger.com/profile/09680319865654952936noreply@blogger.com0tag:blogger.com,1999:blog-3457920023827660787.post-5394952775166172302016-11-25T20:07:00.002+05:302021-06-21T20:14:39.511+05:30K & R : Exercise 2.3 - htoi - hex to integer<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2021-06-21 Mon 20:13 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>K & R : Exercise 2.3 - htoi - hex to integer</title>
<meta name="generator" content="Org mode" />
<meta name="author" content="Harish SK" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
/* Languages per Org manual */
pre.src-asymptote:before { content: 'Asymptote'; }
pre.src-awk:before { content: 'Awk'; }
pre.src-C:before { content: 'C'; }
/* pre.src-C++ doesn't work in CSS */
pre.src-clojure:before { content: 'Clojure'; }
pre.src-css:before { content: 'CSS'; }
pre.src-D:before { content: 'D'; }
pre.src-ditaa:before { content: 'ditaa'; }
pre.src-dot:before { content: 'Graphviz'; }
pre.src-calc:before { content: 'Emacs Calc'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-fortran:before { content: 'Fortran'; }
pre.src-gnuplot:before { content: 'gnuplot'; }
pre.src-haskell:before { content: 'Haskell'; }
pre.src-hledger:before { content: 'hledger'; }
pre.src-java:before { content: 'Java'; }
pre.src-js:before { content: 'Javascript'; }
pre.src-latex:before { content: 'LaTeX'; }
pre.src-ledger:before { content: 'Ledger'; }
pre.src-lisp:before { content: 'Lisp'; }
pre.src-lilypond:before { content: 'Lilypond'; }
pre.src-lua:before { content: 'Lua'; }
pre.src-matlab:before { content: 'MATLAB'; }
pre.src-mscgen:before { content: 'Mscgen'; }
pre.src-ocaml:before { content: 'Objective Caml'; }
pre.src-octave:before { content: 'Octave'; }
pre.src-org:before { content: 'Org mode'; }
pre.src-oz:before { content: 'OZ'; }
pre.src-plantuml:before { content: 'Plantuml'; }
pre.src-processing:before { content: 'Processing.js'; }
pre.src-python:before { content: 'Python'; }
pre.src-R:before { content: 'R'; }
pre.src-ruby:before { content: 'Ruby'; }
pre.src-sass:before { content: 'Sass'; }
pre.src-scheme:before { content: 'Scheme'; }
pre.src-screen:before { content: 'Gnu Screen'; }
pre.src-sed:before { content: 'Sed'; }
pre.src-sh:before { content: 'shell'; }
pre.src-sql:before { content: 'SQL'; }
pre.src-sqlite:before { content: 'SQLite'; }
/* additional languages in org.el's org-babel-load-languages alist */
pre.src-forth:before { content: 'Forth'; }
pre.src-io:before { content: 'IO'; }
pre.src-J:before { content: 'J'; }
pre.src-makefile:before { content: 'Makefile'; }
pre.src-maxima:before { content: 'Maxima'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-picolisp:before { content: 'Pico Lisp'; }
pre.src-scala:before { content: 'Scala'; }
pre.src-shell:before { content: 'Shell Script'; }
pre.src-ebnf2ps:before { content: 'ebfn2ps'; }
/* additional language identifiers per "defun org-babel-execute"
in ob-*.el */
pre.src-cpp:before { content: 'C++'; }
pre.src-abc:before { content: 'ABC'; }
pre.src-coq:before { content: 'Coq'; }
pre.src-groovy:before { content: 'Groovy'; }
/* additional language identifiers from org-babel-shell-names in
ob-shell.el: ob-shell is the only babel language using a lambda to put
the execution function name together. */
pre.src-bash:before { content: 'bash'; }
pre.src-csh:before { content: 'csh'; }
pre.src-ash:before { content: 'ash'; }
pre.src-dash:before { content: 'dash'; }
pre.src-ksh:before { content: 'ksh'; }
pre.src-mksh:before { content: 'mksh'; }
pre.src-posh:before { content: 'posh'; }
/* Additional Emacs modes also supported by the LaTeX listings package */
pre.src-ada:before { content: 'Ada'; }
pre.src-asm:before { content: 'Assembler'; }
pre.src-caml:before { content: 'Caml'; }
pre.src-delphi:before { content: 'Delphi'; }
pre.src-html:before { content: 'HTML'; }
pre.src-idl:before { content: 'IDL'; }
pre.src-mercury:before { content: 'Mercury'; }
pre.src-metapost:before { content: 'MetaPost'; }
pre.src-modula-2:before { content: 'Modula-2'; }
pre.src-pascal:before { content: 'Pascal'; }
pre.src-ps:before { content: 'PostScript'; }
pre.src-prolog:before { content: 'Prolog'; }
pre.src-simula:before { content: 'Simula'; }
pre.src-tcl:before { content: 'tcl'; }
pre.src-tex:before { content: 'TeX'; }
pre.src-plain-tex:before { content: 'Plain TeX'; }
pre.src-verilog:before { content: 'Verilog'; }
pre.src-vhdl:before { content: 'VHDL'; }
pre.src-xml:before { content: 'XML'; }
pre.src-nxml:before { content: 'XML'; }
/* add a generic configuration mode; LaTeX export needs an additional
(add-to-list 'org-latex-listings-langs '(conf " ")) in .emacs */
pre.src-conf:before { content: 'Configuration File'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.equation-container {
display: table;
text-align: center;
width: 100%;
}
.equation {
vertical-align: middle;
}
.equation-label {
display: table-cell;
text-align: right;
vertical-align: middle;
}
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
.org-svg { width: 90%; }
/*]]>*/-->
</style>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2020 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<div id="outline-container-org5678d66" class="outline-2">
<h2 id="org5678d66">Problem</h2>
<div class="outline-text-2" id="text-org5678d66">
<p>
Write the function htoi(s) which converts a string of hexadecimal digits (including an optional 0x, 0X) into its equivalent integer value.
Allowable digits are 0..9, a through f, A through F.
</p>
<div class="HTML">
<p>
<a name='more'></a><br />
</p>
</div>
</div>
</div>
<div id="outline-container-orgae04cbf" class="outline-2">
<h2 id="orgae04cbf">Solution</h2>
<div class="outline-text-2" id="text-orgae04cbf">
<p>
Lets write a function called htoi which does things that is explained in the problem.
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><string.h></span>
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">htoi</span>(<span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span> *<span style="color: #a0522d;">s</span>);
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">char</span> *<span style="color: #a0522d;">s</span> = <span style="color: #8b2252;">"0x1Af"</span>;
printf(<span style="color: #8b2252;">"integer value of hex %s is %d\n"</span>, s, htoi(s));
<span style="color: #a020f0;">return</span> 0;
}
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">htoi</span>(<span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span> *<span style="color: #a0522d;">s</span>)
{
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">i</span> = 0;
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">res</span> = 0;
<span style="color: #a020f0;">if</span>(s[0] == <span style="color: #8b2252;">'0'</span> && (s[1] == <span style="color: #8b2252;">'x'</span> || s[1] == <span style="color: #8b2252;">'X'</span>)) {
i = 2;
}
<span style="color: #a020f0;">for</span>(; i < strlen(s); i++) {
res = res * 16;
<span style="color: #a020f0;">if</span>(s[i] >= <span style="color: #8b2252;">'0'</span> && s[i] <= <span style="color: #8b2252;">'9'</span>) {
res = res + s[i] - <span style="color: #8b2252;">'0'</span>;
}<span style="color: #a020f0;">else</span> <span style="color: #a020f0;">if</span>(s[i] >= <span style="color: #8b2252;">'a'</span> && s[i] <= <span style="color: #8b2252;">'f'</span>) {
res = res + s[i] - <span style="color: #8b2252;">'a'</span> + 10;
}<span style="color: #a020f0;">else</span> <span style="color: #a020f0;">if</span>(s[i] >= <span style="color: #8b2252;">'A'</span> && s[i] <= <span style="color: #8b2252;">'F'</span>) {
res = res + s[i] - <span style="color: #8b2252;">'A'</span> + 10;
}
}
<span style="color: #a020f0;">return</span> res;
}
</pre>
</div>
<pre class="example">
integer value of hex 0x1Af is 431
</pre>
</div>
</div>
<div id="outline-container-org5292e68" class="outline-2">
<h2 id="org5292e68">Links</h2>
<div class="outline-text-2" id="text-org5292e68">
<ul class="org-ul">
<li>Next Article - <a href="https://www.harishnote.com/2021/06/k-r-exercise-24-squeeze-string.html">K & R : Exercise 2.4 - sqeeze string</a></li>
<li>Previous Article - <a href="http://www.harishnote.com/2016/11/k-r-exercise-22-loop-equivalent.html">K & R : Exercise 2.2 - loop equivalent</a></li>
<li>All Article - <a href="http://www.harishnote.com/p/my-k-r-answers.html">K & R Answer</a></li>
</ul>
</div>
</div>
</div>
</body>
</html>Harishhttp://www.blogger.com/profile/09680319865654952936noreply@blogger.com0tag:blogger.com,1999:blog-3457920023827660787.post-78331119108498705572016-11-15T18:29:00.000+05:302016-11-26T22:51:16.482+05:30C Programming #90: fileio - fopen, fclose, fgetc, fputc, fgets, fputs<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2016-11-26 Sat 22:51 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>C Programming #90: fileio - fopen, fclose, fgetc, fputc, fgets, fputs</title>
<meta name="generator" content="Org-mode" />
<meta name="author" content="Harish SK" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
pre.src-sh:before { content: 'sh'; }
pre.src-bash:before { content: 'sh'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-R:before { content: 'R'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-java:before { content: 'Java'; }
pre.src-sql:before { content: 'SQL'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
/*]]>*/-->
</style>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2013 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<p>
This article go through several API related file operation.
Linux/Unix core philosophy is that everything is a file.
Hence file operations becomes core of any application/system programming.
Hence for a person who claims to be good at 'C' need to know the file operation really well.
In this series of article it introduces all the API with example it it never tries to exhaustive, for which you can always refer man pages.
</p>
<a name='more'></a><br />
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1">fopen</h2>
<div class="outline-text-2" id="text-orgheadline1">
<div class="org-src-container">
<pre class="src src-C"><span style="color: #228b22;">FILE</span> *<span style="color: #0000ff;">fopen</span>(<span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span> *<span style="color: #a0522d;">path</span>, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span> *<span style="color: #a0522d;">mode</span>);
</pre>
</div>
<p>
Before any operation is done on any file. It needs to be opened and <code>fopen</code> exactly does that.
It takes two argument. You have give the path of the file (can be relative/full).
Apart from it you need to also give in the mode, like read, write or append etc.. in which you want to open the file in.
fopen returns a FILE pointer that has all the details of file in it.
All the subsequent API's need this FILE pointer to access the file.
Different modes are as follows.
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-left" />
<col class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Mode</th>
<th scope="col" class="org-left">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">r</td>
<td class="org-left">Reading</td>
</tr>
<tr>
<td class="org-left">w</td>
<td class="org-left">Writing</td>
</tr>
<tr>
<td class="org-left">a</td>
<td class="org-left">Append</td>
</tr>
<tr>
<td class="org-left">r+</td>
<td class="org-left">Reading and Writing</td>
</tr>
<tr>
<td class="org-left">w+</td>
<td class="org-left">Reading and Writing, file is created</td>
</tr>
<tr>
<td class="org-left"> </td>
<td class="org-left">if it doesn't exist.</td>
</tr>
<tr>
<td class="org-left">a+</td>
<td class="org-left">Append, file is created if it does-not</td>
</tr>
<tr>
<td class="org-left"> </td>
<td class="org-left">exist</td>
</tr>
</tbody>
</table>
<p>
Note the for some reason fopen fails, then it will return NULL pointer.
And appropriate <code>errno</code> would be set for user for error handling. You need to include <code>errno.h</code>.
Hence always the fopen call should be followed by check with NULL and displaying the <code>errno</code> in case of error.
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><errno.h></span>
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">FILE</span> *<span style="color: #a0522d;">fp</span>;
fp = fopen(<span style="color: #8b2252;">"/tmp/test_file"</span>, <span style="color: #8b2252;">"w+"</span>);
<span style="color: #a020f0;">if</span>(fp != <span style="color: #008b8b;">NULL</span>) {
printf(<span style="color: #8b2252;">"file opened succesfully fp is %p\n"</span>, fp);
}<span style="color: #a020f0;">else</span> {
printf(<span style="color: #8b2252;">"fopen failed with errorno %d\n"</span>, errno);
}
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
<pre class="example">
file opened succesfully fp is 0x1f85010
</pre>
</div>
</div>
<div id="outline-container-orgheadline2" class="outline-2">
<h2 id="orgheadline2">fclose</h2>
<div class="outline-text-2" id="text-orgheadline2">
<div class="org-src-container">
<pre class="src src-C"><span style="color: #228b22;">int</span> <span style="color: #0000ff;">fclose</span>(<span style="color: #228b22;">FILE</span> *<span style="color: #a0522d;">stream</span>);
</pre>
</div>
<p>
All opened file should be closed once the usage of the file is done.
fclose closed the file properly by flushing out any unwritten data back to file.
fclose returns 0 on successful closure and returns EOF on error and setting appropriate <code>errno</code>.
It is highly recommended the re-init the file pointer to NULL once the close is done.
So that file pointer wont be accessed after it closure.
Now adding fclose to above program.
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><errno.h></span>
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">FILE</span> *<span style="color: #a0522d;">fp</span>;
fp = fopen(<span style="color: #8b2252;">"/tmp/test_file"</span>, <span style="color: #8b2252;">"w+"</span>);
<span style="color: #a020f0;">if</span>(fp != <span style="color: #008b8b;">NULL</span>) {
printf(<span style="color: #8b2252;">"file opened successfully fp is %p\n"</span>, fp);
}<span style="color: #a020f0;">else</span> {
printf(<span style="color: #8b2252;">"fopen failed with errorno %d\n"</span>, errno);
}
<span style="color: #a020f0;">if</span>(fclose(fp) != EOF) {
printf(<span style="color: #8b2252;">"file closed successfully"</span>);
}<span style="color: #a020f0;">else</span> {
printf(<span style="color: #8b2252;">"fclose failed with errorno %d\n"</span>, errno);
}
fp = <span style="color: #008b8b;">NULL</span>;
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
<pre class="example">
file opened successfully fp is 0x6de010
file closed successfully
</pre>
</div>
</div>
<div id="outline-container-orgheadline3" class="outline-2">
<h2 id="orgheadline3">fputc</h2>
<div class="outline-text-2" id="text-orgheadline3">
<div class="org-src-container">
<pre class="src src-C"><span style="color: #228b22;">int</span> <span style="color: #0000ff;">fputc</span>(<span style="color: #228b22;">int</span> <span style="color: #a0522d;">c</span>, <span style="color: #228b22;">FILE</span> *<span style="color: #a0522d;">stream</span>);
</pre>
</div>
<p>
<code>c</code> character is put to file pointed by stream.
Small example
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><errno.h></span>
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">FILE</span> *<span style="color: #a0522d;">fp</span>;
fp = fopen(<span style="color: #8b2252;">"/tmp/test_file"</span>, <span style="color: #8b2252;">"w+"</span>);
<span style="color: #a020f0;">if</span>(fp != <span style="color: #008b8b;">NULL</span>) {
printf(<span style="color: #8b2252;">"file opened successfully fp is %p\n"</span>, fp);
}<span style="color: #a020f0;">else</span> {
printf(<span style="color: #8b2252;">"fopen failed with errorno %d\n"</span>, errno);
}
fputc(<span style="color: #8b2252;">'h'</span>, fp);
fputc(<span style="color: #8b2252;">'e'</span>, fp);
fputc(<span style="color: #8b2252;">'l'</span>, fp);
fputc(<span style="color: #8b2252;">'l'</span>, fp);
fputc(<span style="color: #8b2252;">'o'</span>, fp);
<span style="color: #a020f0;">if</span>(fclose(fp) != EOF) {
printf(<span style="color: #8b2252;">"file closed succesfully"</span>);
}<span style="color: #a020f0;">else</span> {
printf(<span style="color: #8b2252;">"fclose failed with errorno %d\n"</span>, errno);
}
fp = <span style="color: #008b8b;">NULL</span>;
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
<pre class="example">
file opened successfully fp is 0xdc3010
file closed successfully
# cat /tmp/test_file
hello
</pre>
</div>
</div>
<div id="outline-container-orgheadline4" class="outline-2">
<h2 id="orgheadline4">fgetc</h2>
<div class="outline-text-2" id="text-orgheadline4">
<div class="org-src-container">
<pre class="src src-C"><span style="color: #228b22;">int</span> <span style="color: #0000ff;">getc</span>(<span style="color: #228b22;">FILE</span> *<span style="color: #a0522d;">stream</span>);
</pre>
</div>
<p>
It gets one character from the file stream.
If it reaches the end of the file it returns EOF.
Lets read the same file tmp_file till EOF.
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><errno.h></span>
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">FILE</span> *<span style="color: #a0522d;">fp</span>;
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">c</span>;
fp = fopen(<span style="color: #8b2252;">"/tmp/test_file"</span>, <span style="color: #8b2252;">"r"</span>);
<span style="color: #a020f0;">if</span>(fp != <span style="color: #008b8b;">NULL</span>) {
printf(<span style="color: #8b2252;">"file opened successfully fp is %p\n"</span>, fp);
}<span style="color: #a020f0;">else</span> {
printf(<span style="color: #8b2252;">"fopen failed with errorno %d\n"</span>, errno);
}
printf(<span style="color: #8b2252;">"Content of file is \n"</span>);
<span style="color: #a020f0;">while</span>((c = fgetc(fp)) != EOF) {
putchar(c);
}
printf(<span style="color: #8b2252;">"\n"</span>);
<span style="color: #a020f0;">if</span>(fclose(fp) != EOF) {
printf(<span style="color: #8b2252;">"file closed successfully"</span>);
}<span style="color: #a020f0;">else</span> {
printf(<span style="color: #8b2252;">"fclose failed with errorno %d\n"</span>, errno);
}
fp = <span style="color: #008b8b;">NULL</span>;
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
<pre class="example">
file opened successfully fp is 0x960010
Content of file is
hello
file closed successfully
</pre>
</div>
</div>
<div id="outline-container-orgheadline5" class="outline-2">
<h2 id="orgheadline5">fputs</h2>
<div class="outline-text-2" id="text-orgheadline5">
<div class="org-src-container">
<pre class="src src-C"><span style="color: #228b22;">int</span> <span style="color: #0000ff;">fputs</span>(<span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span> *<span style="color: #a0522d;">s</span>, <span style="color: #228b22;">FILE</span> *<span style="color: #a0522d;">stream</span>);
</pre>
</div>
<p>
Same thing as fputc is extended here to strings.
Here the small example that extends the same idea.
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><errno.h></span>
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">FILE</span> *<span style="color: #a0522d;">fp</span>;
fp = fopen(<span style="color: #8b2252;">"/tmp/test_file"</span>, <span style="color: #8b2252;">"w+"</span>);
<span style="color: #a020f0;">if</span>(fp != <span style="color: #008b8b;">NULL</span>) {
printf(<span style="color: #8b2252;">"file opened successfully fp is %p\n"</span>, fp);
}<span style="color: #a020f0;">else</span> {
printf(<span style="color: #8b2252;">"fopen failed with errorno %d\n"</span>, errno);
}
fputs(<span style="color: #8b2252;">"Hello World"</span>, fp);
<span style="color: #a020f0;">if</span>(fclose(fp) != EOF) {
printf(<span style="color: #8b2252;">"file closed successfully"</span>);
}<span style="color: #a020f0;">else</span> {
printf(<span style="color: #8b2252;">"fclose failed with errorno %d\n"</span>, errno);
}
fp = <span style="color: #008b8b;">NULL</span>;
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
<pre class="example">
file opened successfully fp is 0xc60010
file closed successfully
# cat /tmp/test_file
Hello World
</pre>
</div>
</div>
<div id="outline-container-orgheadline6" class="outline-2">
<h2 id="orgheadline6">fgets</h2>
<div class="outline-text-2" id="text-orgheadline6">
<div class="org-src-container">
<pre class="src src-C"><span style="color: #228b22;">char</span> *<span style="color: #0000ff;">fgets</span>(<span style="color: #228b22;">char</span> *<span style="color: #a0522d;">s</span>, <span style="color: #228b22;">int</span> <span style="color: #a0522d;">size</span>, <span style="color: #228b22;">FILE</span> *<span style="color: #a0522d;">stream</span>);
</pre>
</div>
<p>
Lets retrive the string from the same file here.
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><errno.h></span>
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">FILE</span> *<span style="color: #a0522d;">fp</span>;
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">str</span>[100];
fp = fopen(<span style="color: #8b2252;">"/tmp/test_file"</span>, <span style="color: #8b2252;">"r"</span>);
<span style="color: #a020f0;">if</span>(fp != <span style="color: #008b8b;">NULL</span>) {
printf(<span style="color: #8b2252;">"file opened succesfully fp is %p\n"</span>, fp);
}<span style="color: #a020f0;">else</span> {
printf(<span style="color: #8b2252;">"fopen failed with errorno %d\n"</span>, errno);
}
printf(<span style="color: #8b2252;">"Content of file is \n"</span>);
fgets(str, 100, fp);
puts(str);
printf(<span style="color: #8b2252;">"\n"</span>);
<span style="color: #a020f0;">if</span>(fclose(fp) != EOF) {
printf(<span style="color: #8b2252;">"file closed succesfully"</span>);
}<span style="color: #a020f0;">else</span> {
printf(<span style="color: #8b2252;">"fclose failed with errorno %d\n"</span>, errno);
}
fp = <span style="color: #008b8b;">NULL</span>;
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
<pre class="example">
file opened succesfully fp is 0xeb8010
Content of file is
Hello World
file closed succesfully
</pre>
</div>
</div>
<div id="outline-container-orgheadline7" class="outline-2">
<h2 id="orgheadline7">Links</h2>
<div class="outline-text-2" id="text-orgheadline7">
<ul class="org-ul">
<li>Next Article - <a href="http://www.harishnote.com/2016/11/c-programming-91-fileio-fprintf-fscanf.html">C Programming #91: fileio - fprintf, fscanf</a></li>
<li>Previous Article - <a href="http://www.harishnote.com/2016/09/c-programming-89-printf-n-specifier.html">C Programming #89: printf - %n specifier</a></li>
<li>All Article - <a href="http://www.harishnote.com/p/c-programming.html">C Programming</a></li>
</ul>
</div>
</div>
</div>
</body>
</html>Harishhttp://www.blogger.com/profile/09680319865654952936noreply@blogger.com0tag:blogger.com,1999:blog-3457920023827660787.post-77980646651847690142016-11-14T22:47:00.000+05:302016-11-25T20:09:27.329+05:30K & R : Exercise 2.2 - loop equivalent<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2016-11-25 Fri 20:09 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>K & R : Exercise 2.2 - loop equivalent</title>
<meta name="generator" content="Org-mode" />
<meta name="author" content="Harish SK" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
pre.src-sh:before { content: 'sh'; }
pre.src-bash:before { content: 'sh'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-R:before { content: 'R'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-java:before { content: 'Java'; }
pre.src-sql:before { content: 'SQL'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
/*]]>*/-->
</style>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2013 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1">Problem</h2>
<div class="outline-text-2" id="text-orgheadline1">
<p>
Write a loop equivalent to the for loop below without using && or ||.
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #1c86ee;">for</span>(i=0; i < lim -1 && (c=getchar()) != <span style="color: #8b7355;">'\n'</span> && c != EOF; ++i)
s[i] = c;
</pre>
</div>
<a name='more'></a><br />
</div>
</div>
<div id="outline-container-orgheadline2" class="outline-2">
<h2 id="orgheadline2">Solution</h2>
<div class="outline-text-2" id="text-orgheadline2">
<div class="org-src-container">
<pre class="src src-C"><span style="color: #1c86ee;">while</span>((c = getchar()) != EOF) {
<span style="color: #1c86ee;">if</span>(c == <span style="color: #8b7355;">'\n'</span>)
<span style="color: #1c86ee;">break</span>;
<span style="color: #1c86ee;">if</span>(i >= lim - 1)
<span style="color: #1c86ee;">break</span>;
s[i] = c;
i++;
}
</pre>
</div>
</div>
</div>
<div id="outline-container-orgheadline3" class="outline-2">
<h2 id="orgheadline3">Links</h2>
<div class="outline-text-2" id="text-orgheadline3">
<ul class="org-ul">
<li>Next Article - <a href="http://www.harishnote.com/2016/11/k-r-exercise-23-htoi-hex-to-integer.html">K & R : Exercise 2.3 - htoi - hex to integer</a></li>
<li>Previous Article - <a href="http://www.harishnote.com/2016/09/k-r-exercise-21-range-of-char-short-int.html">K & R : Exercise 2.1 - Range of char, short, int and long</a></li>
<li>All Article - <a href="http://www.harishnote.com/p/my-k-r-answers.html">K & R Answer</a></li>
</ul>
</div>
</div>
</div>
</body>
</html>Harishhttp://www.blogger.com/profile/09680319865654952936noreply@blogger.com0tag:blogger.com,1999:blog-3457920023827660787.post-76414248900622568892016-09-08T16:59:00.000+05:302016-11-15T18:30:40.895+05:30C Programming #89: printf - %n specifier<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2016-11-15 Tue 18:30 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>C Programming #89: printf - %n specifier</title>
<meta name="generator" content="Org-mode" />
<meta name="author" content="Harish SK" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
pre.src-sh:before { content: 'sh'; }
pre.src-bash:before { content: 'sh'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-R:before { content: 'R'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-java:before { content: 'Java'; }
pre.src-sql:before { content: 'SQL'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
/*]]>*/-->
</style>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2013 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<p>
This article explains format specifier %n which can be used in printf.
Already other format specifiers are covered <a href="http://www.harishnote.com/2016/09/c-programming-87-stdio-printf-scanf.html">C Programming #87: stdio - printf, scanf</a> and <a href="http://www.harishnote.com/2016/09/c-programming-88-stdio-insane.html">C Programming #88: stdio - insane formating in printf</a>.
</p>
<a name='more'></a><br />
<p>
All format specifier used in printf will result in printing something on stdout, except %n.
%n gets the number of characters printed till then to stdout and assigns to variable.
Since it modifies the variable we need to pass the address of the variable.
</p>
<p>
Lets take a small example of printing the dialogues from the movie inception in the following format.
</p>
<pre class="example">
Dominick Cobb: [holding Saito's gun in his lap]
You came prepared.
Saito: Not even my head of security knows about
this apartment. How did you find out?
Dominick Cobb: Very difficult for a man in your
position to keep a love nest
like this secret, particularly
when there's a married woman
involved.
Saito: She would never
Dominick Cobb: Yet, here we are.
</pre>
<p>
Say all the dialogues are placed in array of struct.
We need to fold the lines after 48 characters.
Note we need to give enough space after the name of character is printed.
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #cdad00;">#include</span> <span style="color: #8b7355;"><stdio.h></span>
<span style="color: #cdad00;">#define</span> <span style="color: #2e8b57;">TOTAL_WIDTH</span> 48
<span style="color: #1c86ee;">typedef</span> <span style="color: #1c86ee;">struct</span> <span style="color: #00688b;">dialogue_tag</span>
{
<span style="color: #00688b;">char</span> *<span style="color: #2e8b57;">ch</span>; <span style="color: #7f7f7f;">/* </span><span style="color: #7f7f7f;">Character Name </span><span style="color: #7f7f7f;">*/</span>
<span style="color: #00688b;">char</span> *<span style="color: #2e8b57;">se</span>; <span style="color: #7f7f7f;">/* </span><span style="color: #7f7f7f;">Dialogue Sentence </span><span style="color: #7f7f7f;">*/</span>
}<span style="color: #00688b;">dialogue_t</span>;
<span style="color: #00688b;">int</span> <span style="color: #cd9b1d;">main</span>()
{
<span style="color: #00688b;">dialogue_t</span> <span style="color: #2e8b57;">inception</span>[5] =
{ {<span style="color: #8b7355;">"Dominick Cobb"</span>,
<span style="color: #8b7355;">"[holding Saito's gun in his lap] You came prepared."</span>},
{<span style="color: #8b7355;">"Saito"</span>,
<span style="color: #8b7355;">"Not even my head of security knows about this apartment."</span>
<span style="color: #8b7355;">"How did you find out?"</span>},
{<span style="color: #8b7355;">"Dominick Cobb"</span>,
<span style="color: #8b7355;">"Very difficult for a man in your position to keep a love nest"</span>
<span style="color: #8b7355;">"like this secret, particularly"</span>
<span style="color: #8b7355;">"when there's a married woman "</span>
<span style="color: #8b7355;">"involved."</span>},
{<span style="color: #8b7355;">"Saito"</span>, <span style="color: #8b7355;">"She would never"</span>,},
{<span style="color: #8b7355;">"Dominick Cobb"</span>, <span style="color: #8b7355;">"Yet, here we are."</span>}};
<span style="color: #00688b;">int</span> <span style="color: #2e8b57;">i</span>, <span style="color: #2e8b57;">j</span>;
<span style="color: #00688b;">int</span> <span style="color: #2e8b57;">width</span>, <span style="color: #2e8b57;">diag_width</span>;
<span style="color: #1c86ee;">for</span>(i = 0; i < 5; i++) {
printf(<span style="color: #8b7355;">"%s : %n"</span>, inception[i].ch, &width);
diag_width = TOTAL_WIDTH - width;
<span style="color: #1c86ee;">for</span>(j = 0; j < (strlen(inception[i].se) / diag_width); j++) {
printf(<span style="color: #8b7355;">"%.*s"</span>, diag_width, inception[i].se + j*diag_width);
printf(<span style="color: #8b7355;">"\n%*s"</span>, width, <span style="color: #8b7355;">""</span>);
}
printf(<span style="color: #8b7355;">"%.*s\n"</span>, diag_width, inception[i].se + j*diag_width);
printf(<span style="color: #8b7355;">"\n"</span>);
}
<span style="color: #1c86ee;">return</span> 0;
}
</pre>
</div>
<pre class="example">
Dominick Cobb : [holding Saito's gun in his lap]
You came prepared.
Saito : Not even my head of security knows about
this apartment.How did you find out?
Dominick Cobb : Very difficult for a man in your
position to keep a love nest lik
e this secret, particularly when
there's a married woman involved
.
Saito : She would never
Dominick Cobb : Yet, here we are.
</pre>
<p>
Output could be improved a lot, it is pretty ok compared to size of program.
Note to print only 10 characters in a string we could use the following construct.
</p>
<div class="org-src-container">
<pre class="src src-C">printf(<span style="color: #8b7355;">"%.10s"</span>, temp);
</pre>
</div>
<p>
Now if the number of characters to be printed in variable, then following construct can be used.
</p>
<div class="org-src-container">
<pre class="src src-C">printf(<span style="color: #8b7355;">"%.*s"</span>, width, temp);
</pre>
</div>
<p>
Above construct is used in above program.
</p>
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1">Links</h2>
<div class="outline-text-2" id="text-orgheadline1">
<ul class="org-ul">
<li>Next Article - <a href="http://www.harishnote.com/2016/11/c-programming-90-fileio-fopen-fclose.html">C Programming #90: fileio - fopen, fclose, fputc, fgetc, fputs, fgets</a></li>
<li>Previous Article - <a href="http://www.harishnote.com/2016/09/c-programming-88-stdio-insane.html">C Programming #88: stdio - insane formating in printf</a></li>
<li>All Article - <a href="http://www.harishnote.com/p/c-programming.html">C Programming</a></li>
</ul>
</div>
</div>
</div>
</body>
</html>Harishhttp://www.blogger.com/profile/09680319865654952936noreply@blogger.com0tag:blogger.com,1999:blog-3457920023827660787.post-19368656631734540592016-09-07T22:02:00.000+05:302016-11-14T22:50:48.364+05:30K & R : Exercise 2.1 - Range of char, short, int and long<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2016-11-14 Mon 22:50 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>K & R : Exercise 2.1 - Range of char, short, int and long</title>
<meta name="generator" content="Org-mode" />
<meta name="author" content="Harish SK" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
pre.src-sh:before { content: 'sh'; }
pre.src-bash:before { content: 'sh'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-R:before { content: 'R'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-java:before { content: 'Java'; }
pre.src-sql:before { content: 'SQL'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
/*]]>*/-->
</style>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2013 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1">Problem</h2>
<div class="outline-text-2" id="text-orgheadline1">
<p>
Write a Program to determine the ranges of char, short, int and long variables.
Both signed and unsigned by printing appropriate values from standard header and by direct computation.
Harder if you compute them determine the ranges of various floating point types.
</p>
<a name='more'></a><br />
</div>
</div>
<div id="outline-container-orgheadline2" class="outline-2">
<h2 id="orgheadline2">Solution</h2>
<div class="outline-text-2" id="text-orgheadline2">
<p>
We need to calculate it manually by using bit manipulation operations.
Actually values can be found out in header file <code>limits.h</code>.
In binary representation
</p>
<ul class="org-ul">
<li>signed min -> MSB bit is 1 and rest all bits are 0.</li>
<li>signed max -> MSB bit is 0 and rest all nits are 1.</li>
<li>unsigned min -> It is always 0.</li>
<li>unsigned max -> All bits are 1.</li>
</ul>
<p>
Implementing the above logic for different data-types we get the max and min value.
Each of this max and min value can be verified against the symbolic const provided in limits.h.
Now putting all the together into a program we get -.
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #ffd700;">#include</span> <span style="color: #deb887;"><stdio.h></span>
<span style="color: #ffd700;">#include</span> <span style="color: #deb887;"><limits.h></span>
<span style="color: #98f5ff;">int</span> <span style="color: #daa520;">main</span>()
{
<span style="color: #98f5ff;">int</span> <span style="color: #4eee94;">char_min</span>, <span style="color: #4eee94;">char_max</span>;
<span style="color: #98f5ff;">int</span> <span style="color: #4eee94;">uchar_min</span>, <span style="color: #4eee94;">uchar_max</span>;
<span style="color: #98f5ff;">long</span> <span style="color: #4eee94;">int_min</span>, <span style="color: #4eee94;">int_max</span>;
<span style="color: #98f5ff;">long</span> <span style="color: #4eee94;">uint_min</span>, <span style="color: #4eee94;">uint_max</span>;
char_min = (<span style="color: #98f5ff;">char</span>)(1 << (<span style="color: #00bfff;">sizeof</span>(<span style="color: #98f5ff;">char</span>) * 8 - 1));
char_max = ((<span style="color: #98f5ff;">char</span>)(~0 & ~char_min));
printf(<span style="color: #deb887;">"char MIN is %d, char MAX is %d\n"</span>, char_min, char_max);
printf(<span style="color: #deb887;">"CHAR_MIN is %d, CHAR_MAX is %d\n"</span>, CHAR_MIN, CHAR_MAX);
uchar_min = (<span style="color: #98f5ff;">unsigned</span> <span style="color: #98f5ff;">char</span>)0;
uchar_max = (<span style="color: #98f5ff;">unsigned</span> <span style="color: #98f5ff;">char</span>)~0;
printf(<span style="color: #deb887;">"unsigned char MIN is %d, unsigned char MAX is %d\n"</span>, uchar_min, uchar_max);
printf(<span style="color: #deb887;">"UCHAR_MIN is %d, UCHAR_MAX is %d\n"</span>, 0, UCHAR_MAX);
int_min = (<span style="color: #98f5ff;">int</span>)(1 << (<span style="color: #00bfff;">sizeof</span>(<span style="color: #98f5ff;">int</span>) * 8 - 1));
int_max = ((<span style="color: #98f5ff;">int</span>)(~0 & ~int_min));
printf(<span style="color: #deb887;">"int MIN is %lld, int MAX is %lld\n"</span>, int_min, int_max);
printf(<span style="color: #deb887;">"INT_MIN is %d, INT_MAX is %d\n"</span>, INT_MIN, INT_MAX);
uint_min = (<span style="color: #98f5ff;">unsigned</span> <span style="color: #98f5ff;">int</span>)0;
uint_max = (<span style="color: #98f5ff;">unsigned</span> <span style="color: #98f5ff;">int</span>)~0;
printf(<span style="color: #deb887;">"unsigned int MIN is %lld, unsigned int MAX is %lld\n"</span>, uint_min, uint_max);
printf(<span style="color: #deb887;">"UINT_MIN is %u, UINT_MAX is %u\n"</span>, 0, UINT_MAX);
<span style="color: #00bfff;">return</span> 0;
}
</pre>
</div>
<pre class="example">
char MIN is -128, char MAX is 127
CHAR_MIN is -128, CHAR_MAX is 127
unsigned char MIN is 0, unsigned char MAX is 255
UCHAR_MIN is 0, UCHAR_MAX is 255
int MIN is -2147483648, int MAX is 2147483647
INT_MIN is -2147483648, INT_MAX is 2147483647
unsigned int MIN is 0, unsigned int MAX is 4294967295
UINT_MIN is 0, UINT_MAX is 4294967295
</pre>
</div>
</div>
<div id="outline-container-orgheadline3" class="outline-2">
<h2 id="orgheadline3">Links</h2>
<div class="outline-text-2" id="text-orgheadline3">
<ul class="org-ul">
<li>Next Article - <a href="http://www.harishnote.com/2016/11/k-r-exercise-22-loop-equivalent.html">K & R : Exercise 2.2 - loop equivalent</a></li>
<li>Previous Article - <a href="http://www.harishnote.com/2016/09/k-r-exercise-124-rudimentary-c-syntax.html">K & R : Exercise 1.24 - Rudimentary C syntax checker</a></li>
<li>All Article - <a href="http://www.harishnote.com/p/my-k-r-answers.html">K & R Answer</a></li>
</ul>
</div>
</div>
</div>
</body>
</html>Harishhttp://www.blogger.com/profile/09680319865654952936noreply@blogger.com0tag:blogger.com,1999:blog-3457920023827660787.post-49966483501559411132016-09-06T20:52:00.000+05:302016-09-08T17:00:50.822+05:30C Programming #88: stdio - insane formatting in printf<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2016-09-08 Thu 17:00 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>C Programming #88: stdio - insane formatting in printf</title>
<meta name="generator" content="Org-mode" />
<meta name="author" content="Harish SK" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
pre.src-sh:before { content: 'sh'; }
pre.src-bash:before { content: 'sh'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-R:before { content: 'R'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-java:before { content: 'Java'; }
pre.src-sql:before { content: 'SQL'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
/*]]>*/-->
</style>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2013 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<p>
We have covered simple format specifier for printf and scanf here [<a href="http://www.harishnote.com/2016/09/c-programming-87-stdio-printf-scanf.html">C Programming #87: stdio - printf, scanf</a>].
This article tries to give incrementally with example most complex and insane formatting that could be achieved.
I don't expect you to remember all of it. My idea to here is to show how beautiful and complex the construct are.
</p>
<a name='more'></a><br />
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1">constant width</h2>
<div class="outline-text-2" id="text-orgheadline1">
<p>
Say you want to show print something like below. Table of number of something per year.
</p>
<pre class="example">
2010 107
2011 9
2012 22
2013 120
2014 34
</pre>
<p>
Now say i write a C program where the year and number is stored in array of struct.
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #a020f0;">typedef</span> <span style="color: #a020f0;">struct</span> <span style="color: #228b22;">stat_tag</span> {
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">year</span>;
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">num</span>;
}<span style="color: #228b22;">stat_t</span>;
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">stat_t</span> <span style="color: #a0522d;">list</span>[5] = {{2010, 107},
{2011, 9},
{2012, 22},
{2013, 120},
{2014, 34}};
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">i</span>;
<span style="color: #a020f0;">for</span>(i = 0; i < 5; i++){
printf(<span style="color: #8b2252;">"%d %d\n"</span>, list[i].year, list[i].num);
}
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
<pre class="example">
2010 107
2011 9
2012 22
2013 120
2014 34
</pre>
<p>
If you see the output, it is not properly aligned.
Issue is that we need all the number aligned to its right.
This can be ac hive by specifying the number between <code>%</code> and <code>d</code>.
This number represents the width. Since it is constant number it is called constant width.
</p>
<p>
Now re-writing the above program with the appropriate width.
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #a020f0;">typedef</span> <span style="color: #a020f0;">struct</span> <span style="color: #228b22;">stat_tag</span> {
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">year</span>;
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">num</span>;
}<span style="color: #228b22;">stat_t</span>;
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">stat_t</span> <span style="color: #a0522d;">list</span>[5] = {{2010, 107},
{2011, 9},
{2012, 22},
{2013, 120},
{2014, 34}};
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">i</span>;
<span style="color: #a020f0;">for</span>(i = 0; i < 5; i++){
printf(<span style="color: #8b2252;">"%d %3d\n"</span>, list[i].year, list[i].num);
}
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
<pre class="example">
2010 107
2011 9
2012 22
2013 120
2014 34
</pre>
<p>
Now the o/p exactly looks as we would like it to be.
</p>
</div>
</div>
<div id="outline-container-orgheadline2" class="outline-2">
<h2 id="orgheadline2">variable width</h2>
<div class="outline-text-2" id="text-orgheadline2">
<p>
In the above C program we assumed that the width as constant by looking at the numbers.
Say in some program we don't know the numbers before hand.
Then variable width specifier comes into help.
Between <code>%</code> and <code>d</code> we need to put <code>*</code> suggesting that width is variable.
Now the width is passed as one of the argument to printf.
Hence for <code>%*d</code> we need to pass two arguments first the width and second the number itself to be printed.
</p>
<p>
Now re-writing the same above program to make it more generic.
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #a020f0;">typedef</span> <span style="color: #a020f0;">struct</span> <span style="color: #228b22;">stat_tag</span> {
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">year</span>;
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">num</span>;
}<span style="color: #228b22;">stat_t</span>;
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">stat_t</span> <span style="color: #a0522d;">list</span>[5] = {{2010, 1072345},
{2011, 9},
{2012, 22},
{2013, 120},
{2014, 34}};
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">i</span>, <span style="color: #a0522d;">width</span> = 0, <span style="color: #a0522d;">tw</span> = 0;
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">temp</span>[100];
<span style="color: #b22222;">/* </span><span style="color: #b22222;">This loop is for width calculation </span><span style="color: #b22222;">*/</span>
<span style="color: #b22222;">/* </span><span style="color: #b22222;">Note how snprintf is used to find number </span>
<span style="color: #b22222;"> of digits in a number </span><span style="color: #b22222;">*/</span>
<span style="color: #a020f0;">for</span>(i = 0; i < 5; i++){
tw = snprintf(temp, 100, <span style="color: #8b2252;">"%d"</span>, list[i].num);
<span style="color: #a020f0;">if</span>(tw > width) {
width = tw;
}
}
<span style="color: #a020f0;">for</span>(i = 0; i < 5; i++){
printf(<span style="color: #8b2252;">"%d %*d\n"</span>, list[i].year, width, list[i].num);
}
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
<pre class="example">
2010 1072345
2011 9
2012 22
2013 120
2014 34
</pre>
<p>
You can try changing the num and see how the o/p gets adapted.
</p>
</div>
</div>
<div id="outline-container-orgheadline3" class="outline-2">
<h2 id="orgheadline3">left or right justify</h2>
<div class="outline-text-2" id="text-orgheadline3">
<p>
Say you want to print the some items in tabular format, say something like below
</p>
<p>
James Smith
Michael Smith
Maria Garcia
Maria Rodriguez
</p>
<p>
To make something like above formatting we need to start second name at same place.
Hence First Name and spaces following it form a const width.
We know the format specifier for string is <code>%s</code>.
Above const width can be specified by adding a number in between <code>%</code> and <code>s</code>.
Example <code>%20s</code>, here it will print with the constant width 20 characters.
And we want it left justified we need to use <code>-</code>.
What would happen if the string is bigger than 20 characters, it would overflow the width boundary.
</p>
<p>
Let me give the example by storing the first name and second name in <code>struct</code>.
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #a020f0;">typedef</span> <span style="color: #a020f0;">struct</span> <span style="color: #228b22;">name_tag</span>{
<span style="color: #228b22;">char</span> *<span style="color: #a0522d;">first</span>;
<span style="color: #228b22;">char</span> *<span style="color: #a0522d;">second</span>;
}<span style="color: #228b22;">name_t</span>;
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">name_t</span> <span style="color: #a0522d;">list</span>[4] = {{<span style="color: #8b2252;">"James"</span>, <span style="color: #8b2252;">"Smith"</span>},
{<span style="color: #8b2252;">"Michael"</span>,<span style="color: #8b2252;">"Smith"</span>},
{<span style="color: #8b2252;">"Maria"</span>, <span style="color: #8b2252;">"Garcia"</span>},
{<span style="color: #8b2252;">"Maria"</span>, <span style="color: #8b2252;">"Rodriguez"</span>}};
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">i</span>;
<span style="color: #a020f0;">for</span>(i = 0; i < 4; i++) {
printf(<span style="color: #8b2252;">"%-15s %-15s\n"</span>, list[i].first, list[i].second);
}
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
<p>
James Smith
Michael Smith
Maria Garcia
Maria Rodriguez
</p>
<p>
Same thing could be applied to numbers as well, but it is customary to align number right justified and string left justified.
</p>
</div>
</div>
<div id="outline-container-orgheadline4" class="outline-2">
<h2 id="orgheadline4">zero fill</h2>
<div class="outline-text-2" id="text-orgheadline4">
<p>
Lets take another example where you want numbers as follows.
</p>
<pre class="example">
2010 107
2011 009
2012 022
2013 120
2014 034
</pre>
<p>
See that each number is padded with zero.
This can be easily achieved by using <code>0</code> in between <code>%</code> and <code>d</code> for zero padding.
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #a020f0;">typedef</span> <span style="color: #a020f0;">struct</span> <span style="color: #228b22;">stat_tag</span> {
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">year</span>;
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">num</span>;
}<span style="color: #228b22;">stat_t</span>;
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">stat_t</span> <span style="color: #a0522d;">list</span>[5] = {{2010, 107},
{2011, 9},
{2012, 22},
{2013, 120},
{2014, 34}};
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">i</span>;
<span style="color: #a020f0;">for</span>(i = 0; i < 5; i++){
printf(<span style="color: #8b2252;">"%d %03d\n"</span>, list[i].year, list[i].num);
}
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
<pre class="example">
2010 107
2011 009
2012 022
2013 120
2014 034
</pre>
</div>
</div>
<div id="outline-container-orgheadline5" class="outline-2">
<h2 id="orgheadline5">compulsory +</h2>
<div class="outline-text-2" id="text-orgheadline5">
<p>
Now say we have following table to be formatted.
</p>
<pre class="example">
2010 +107
2011 -9
2012 +22
2013 -120
2014 +34
</pre>
<p>
Above formatting can be achieved by mentioning + in between <code>%</code> and <code>d</code>.
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #a020f0;">typedef</span> <span style="color: #a020f0;">struct</span> <span style="color: #228b22;">stat_tag</span> {
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">year</span>;
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">num</span>;
}<span style="color: #228b22;">stat_t</span>;
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">stat_t</span> <span style="color: #a0522d;">list</span>[5] = {{2010, +107},
{2011, -9},
{2012, +22},
{2013, -120},
{2014, +34}};
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">i</span>;
<span style="color: #a020f0;">for</span>(i = 0; i < 5; i++){
printf(<span style="color: #8b2252;">"%d %+4d\n"</span>, list[i].year, list[i].num);
}
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
<pre class="example">
2010 +107
2011 -9
2012 +22
2013 -120
2014 +34
</pre>
<p>
Note that here width also includes the +/- sign also.
</p>
</div>
</div>
<div id="outline-container-orgheadline6" class="outline-2">
<h2 id="orgheadline6">precision</h2>
<div class="outline-text-2" id="text-orgheadline6">
<p>
This is applicable to floating point number. Say i want to print following table.
</p>
<pre class="example">
2010 10.7
2011 9.0
2012 22.1
2013 120.3
2014 34.0
</pre>
<p>
If you see the above number number following <code>.</code> tells the precision of it.
Here width of precision is 1. Above formatting can be achieved as follows.
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #a020f0;">typedef</span> <span style="color: #a020f0;">struct</span> <span style="color: #228b22;">stat_tag</span> {
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">year</span>;
<span style="color: #228b22;">float</span> <span style="color: #a0522d;">num</span>;
}<span style="color: #228b22;">stat_t</span>;
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">stat_t</span> <span style="color: #a0522d;">list</span>[5] = {{2010, 10.7},
{2011, 9.0},
{2012, 22.1},
{2013, 120.3},
{2014, 34.0}};
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">i</span>;
<span style="color: #a020f0;">for</span>(i = 0; i < 5; i++){
printf(<span style="color: #8b2252;">"%d %6.1f\n"</span>, list[i].year, list[i].num);
}
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
<pre class="example">
2010 10.7
2011 9.0
2012 22.1
2013 120.3
2014 34.0
</pre>
<p>
Note here the width is total width of number including all numbers and <code>.</code>
</p>
</div>
</div>
<div id="outline-container-orgheadline7" class="outline-2">
<h2 id="orgheadline7">entire spec</h2>
<div class="outline-text-2" id="text-orgheadline7">
<p>
Overall spec can be found here - <a href="http://man7.org/linux/man-pages/man3/printf.3.html">http://man7.org/linux/man-pages/man3/printf.3.html</a>
I don't want to repeat it as such this article is quite big enough.
</p>
</div>
</div>
<div id="outline-container-orgheadline8" class="outline-2">
<h2 id="orgheadline8">Links</h2>
<div class="outline-text-2" id="text-orgheadline8">
<ul class="org-ul">
<li>Next Article - <a href="http://www.harishnote.com/2016/09/c-programming-89-printf-n-specifier.html">C Programming #89: printf - %n specifier</a></li>
<li>Previous Article - <a href="http://www.harishnote.com/2016/09/c-programming-87-stdio-printf-scanf.html">C Programming #87: stdio - printf, scanf</a></li>
<li>All Article - <a href="http://www.harishnote.com/p/c-programming.html">C Programming</a></li>
</ul>
</div>
</div>
</div>
</body>
</html>Harishhttp://www.blogger.com/profile/09680319865654952936noreply@blogger.com0tag:blogger.com,1999:blog-3457920023827660787.post-11507670752376369142016-09-05T06:01:00.000+05:302016-09-07T22:02:54.120+05:30K & R : Exercise 1.24 - Rudimentary C syntax checker<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2016-09-07 Wed 22:02 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>K & R : Exercise 1.24 - Rudimentary C syntax checker</title>
<meta name="generator" content="Org-mode" />
<meta name="author" content="Harish SK" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
pre.src-sh:before { content: 'sh'; }
pre.src-bash:before { content: 'sh'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-R:before { content: 'R'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-java:before { content: 'Java'; }
pre.src-sql:before { content: 'SQL'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
/*]]>*/-->
</style>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2013 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1">Problem</h2>
<div class="outline-text-2" id="text-orgheadline1">
<p>
Write a program to check a C program for rudimentary syntax errors like unbalanced parenthesis
brackets and braces. Don't forget about quotes both single and double, escape sequence and comments.
(This program is hard, if you do it in full generality).
</p>
<a name='more'></a><br />
</div>
</div>
<div id="outline-container-orgheadline2" class="outline-2">
<h2 id="orgheadline2">Solution</h2>
<div class="outline-text-2" id="text-orgheadline2">
<p>
First and for most important thing is that it has to simple.
We are not writing a C compiler here.
Lets stick to simpleness rather than completeness.
</p>
<p>
First thing is balancing a brackets,
We could increment counter for every <code>(</code> and decrement for every <code>)</code>.
In the end if the count is <code>0</code> then the brackets are balanced.
This is not exactly correct (correct implementation would need a stack) but very simple.
Same could be extended to <code>{</code>, <code>[</code>.
Every comment <code>/*</code> should end with <code>*/</code>.
Lets put all these things into C Program.
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">ch</span> = 0, <span style="color: #a0522d;">pch</span> = 0;
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">round_count</span> = 0;
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">square_count</span> = 0;
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">flower_count</span> = 0;
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">comment_count</span> = 0;
<span style="color: #a020f0;">while</span>((ch = getchar()) != EOF) {
<span style="color: #a020f0;">if</span>(ch == <span style="color: #8b2252;">'('</span>) {
round_count++;
}<span style="color: #a020f0;">else</span> <span style="color: #a020f0;">if</span>(ch == <span style="color: #8b2252;">'['</span>) {
square_count++;
}<span style="color: #a020f0;">else</span> <span style="color: #a020f0;">if</span>(ch == <span style="color: #8b2252;">'{'</span>) {
flower_count++;
}<span style="color: #a020f0;">else</span> <span style="color: #a020f0;">if</span>(ch == <span style="color: #8b2252;">'*'</span> && pch == <span style="color: #8b2252;">'/'</span>) {
comment_count++;
}<span style="color: #a020f0;">else</span> <span style="color: #a020f0;">if</span>(ch == <span style="color: #8b2252;">')'</span>) {
round_count--;
}<span style="color: #a020f0;">else</span> <span style="color: #a020f0;">if</span>(ch == <span style="color: #8b2252;">']'</span>) {
square_count--;
}<span style="color: #a020f0;">else</span> <span style="color: #a020f0;">if</span>(ch == <span style="color: #8b2252;">'}'</span>) {
flower_count--;
}<span style="color: #a020f0;">else</span> <span style="color: #a020f0;">if</span>(ch == <span style="color: #8b2252;">'/'</span> && pch == <span style="color: #8b2252;">'*'</span>) {
comment_count--;
}
pch = ch;
}
<span style="color: #a020f0;">if</span>(round_count != 0) {
printf(<span style="color: #8b2252;">"Syntax Error: Unbalanced Round brackets\n"</span>);
}
<span style="color: #a020f0;">if</span>(square_count != 0) {
printf(<span style="color: #8b2252;">"Syntax Error: Unbalanced Square brackets\n"</span>);
}
<span style="color: #a020f0;">if</span>(flower_count != 0) {
printf(<span style="color: #8b2252;">"Syntax Error: Unbalanced Flower brackets\n"</span>);
}
<span style="color: #a020f0;">if</span>(comment_count != 0) {
printf(<span style="color: #8b2252;">"Syntax Error: Unbalanced Comment\n"</span>);
}
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
</div>
</div>
<div id="outline-container-orgheadline3" class="outline-2">
<h2 id="orgheadline3">Links</h2>
<div class="outline-text-2" id="text-orgheadline3">
<ul class="org-ul">
<li>Next Article - <a href="http://www.harishnote.com/2016/09/k-r-exercise-21-range-of-char-short-int.html">K & R : Exercise 2.1 - Range of char, short, int and long</a></li>
<li>Previous Article - <a href="http://www.harishnote.com/2016/09/k-r-exercise-123-remove-c-comments.html">K & R : Exercise 1.23 - remove C comments</a></li>
<li>All Article - <a href="http://www.harishnote.com/p/my-k-r-answers.html">K & R Answer</a></li>
</ul>
</div>
</div>
</div>
</body>
</html>Harishhttp://www.blogger.com/profile/09680319865654952936noreply@blogger.com0tag:blogger.com,1999:blog-3457920023827660787.post-18509697451411288552016-09-04T06:17:00.000+05:302016-09-06T20:55:05.679+05:30C Programming #87: stdio - printf, scanf<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2016-09-06 Tue 20:54 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>C Programming #87: stdio - printf, scanf</title>
<meta name="generator" content="Org-mode" />
<meta name="author" content="Harish SK" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
pre.src-sh:before { content: 'sh'; }
pre.src-bash:before { content: 'sh'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-R:before { content: 'R'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-java:before { content: 'Java'; }
pre.src-sql:before { content: 'SQL'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
/*]]>*/-->
</style>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2013 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<p>
Following article explores formatted stdio functions <code>printf</code> and <code>scanf</code>.
printf and scanf are varidac function (function that take variable arguments).
Variable argument function are covered in later articles.
</p>
<a name='more'></a><br />
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1">printf</h2>
<div class="outline-text-2" id="text-orgheadline1">
<div class="org-src-container">
<pre class="src src-C"><span style="color: #228b22;">int</span> <span style="color: #0000ff;">printf</span>(<span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span> *<span style="color: #a0522d;">format</span>, ...);
</pre>
</div>
<p>
Here are the simplest use of printf without using any format specifiers.
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">char</span> *<span style="color: #a0522d;">p</span> = <span style="color: #8b2252;">"This is pointer to const string\n"</span>;
printf(<span style="color: #8b2252;">"This is const string\n"</span>);
printf(p);
printf(<span style="color: #8b2252;">"Hello "</span> <span style="color: #8b2252;">"World\n"</span>);
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
<pre class="example">
This is const string
This is pointer to const string
Hello World
</pre>
<p>
If you want to print the content of the variable we need to use format specifier in <code>format</code> string and variable is passed as argument to printf.
Since you can print variable number of variables <code>printf</code> is a varidac function.
We will cover how to write a varidac function later in this series of article.
</p>
<p>
Now different format specifier that is available are as follows.
Each format specifier is prepended by <code>%</code>.
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-left" />
<col class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Format specifier</th>
<th scope="col" class="org-left">Details</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">d, i</td>
<td class="org-left">signed integer</td>
</tr>
<tr>
<td class="org-left">u</td>
<td class="org-left">unsigned integer</td>
</tr>
<tr>
<td class="org-left">o</td>
<td class="org-left">unsigned octal</td>
</tr>
<tr>
<td class="org-left">x, X</td>
<td class="org-left">unsigned hex</td>
</tr>
<tr>
<td class="org-left">f, F</td>
<td class="org-left">Double</td>
</tr>
<tr>
<td class="org-left">e, E</td>
<td class="org-left">Double exponential</td>
</tr>
<tr>
<td class="org-left">g, G</td>
<td class="org-left">either f or e</td>
</tr>
<tr>
<td class="org-left">a, A</td>
<td class="org-left">Hex Double(Added in C99)</td>
</tr>
<tr>
<td class="org-left">c</td>
<td class="org-left">unsigned char</td>
</tr>
<tr>
<td class="org-left">s</td>
<td class="org-left">string(const char *)</td>
</tr>
<tr>
<td class="org-left">p</td>
<td class="org-left">pointer</td>
</tr>
<tr>
<td class="org-left">%</td>
<td class="org-left">character '%'</td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="outline-container-orgheadline2" class="outline-2">
<h2 id="orgheadline2">scanf</h2>
<div class="outline-text-2" id="text-orgheadline2">
<div class="org-src-container">
<pre class="src src-C"><span style="color: #228b22;">int</span> <span style="color: #0000ff;">scanf</span>(<span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span> *<span style="color: #a0522d;">format</span>, ...);
</pre>
</div>
<p>
It is used to take input from the user.
But one thing we need to make sure is that we need to pass the address of variable here.
This is common point of mistake most of the novice programmer makes.
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">a</span>;
scanf(<span style="color: #8b2252;">"%d"</span>, &a);
printf(<span style="color: #8b2252;">"value of a is %d"</span>, a);
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
<pre class="example">
0
value of a is 0
</pre>
<p>
Same format specifier that were discussed earlier can be used here to.
We could do lot of formatting using format specifier which will be covered in next article.
</p>
</div>
</div>
<div id="outline-container-orgheadline3" class="outline-2">
<h2 id="orgheadline3">Links</h2>
<div class="outline-text-2" id="text-orgheadline3">
<ul class="org-ul">
<li>Next Article - <a href="http://www.harishnote.com/2016/09/c-programming-88-stdio-insane.html">C Programming #88: stdio - insane formating in printf</a></li>
<li>Previous Article - <a href="http://www.harishnote.com/2016/09/c-programming-86-stdio-getchar-putchar.html">C Programming #86: stdio - getchar, putchar, gets, puts</a></li>
<li>All Article - <a href="http://www.harishnote.com/p/c-programming.html">C Programming</a></li>
</ul>
</div>
</div>
</div>
</body>
</html>Harishhttp://www.blogger.com/profile/09680319865654952936noreply@blogger.com0tag:blogger.com,1999:blog-3457920023827660787.post-29376137807972969202016-09-03T09:21:00.000+05:302016-09-05T06:09:26.632+05:30K & R : Exercise 1.23 - remove C comments<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2016-09-05 Mon 06:09 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>K & R : Exercise 1.23-remove C comments</title>
<meta name="generator" content="Org-mode" />
<meta name="author" content="Harish SK" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
pre.src-sh:before { content: 'sh'; }
pre.src-bash:before { content: 'sh'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-R:before { content: 'R'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-java:before { content: 'Java'; }
pre.src-sql:before { content: 'SQL'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
/*]]>*/-->
</style>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2013 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1">Problem</h2>
<div class="outline-text-2" id="text-orgheadline1">
<p>
Write a program to remove all comments from a c program,
don't forget to handle quoted strings and character constants properly.
C comments do not nest.
</p>
<a name='more'></a><br />
</div>
</div>
<div id="outline-container-orgheadline2" class="outline-2">
<h2 id="orgheadline2">Solution</h2>
<div class="outline-text-2" id="text-orgheadline2">
<p>
We need to remove all the C comments. There are two types of comments
</p>
<ol class="org-ol">
<li><code>/*</code> <code>*/</code></li>
<li><code>//</code></li>
</ol>
<p>
We should not remove it when we are inside a string. Strings are always found inside "".
First we need to find whether we are inside a sting.
If we are inside a string we should not apply the C comment removal method.
We need to remove all the characters inside <code>/* */</code> it and replace it with white-space.
And all the charters inside <code>//</code> till end of the line and replace it with space and newline.
</p>
<p>
Putting all the above idea into C program it would look as follows
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #a020f0;">enum</span> <span style="color: #228b22;">com_type_t</span> { <span style="color: #a0522d;">NOT_COMMENT</span> = 0,
<span style="color: #a0522d;">COMMENT_SINGLE_LINE</span> = 1,
<span style="color: #a0522d;">COMMENT_MULTI_LINE</span> = 2};
<span style="color: #483d8b;">#define</span> <span style="color: #0000ff;">is_not_com</span>() (com == NOT_COMMENT)
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">is_str</span> = 0;
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">ch</span> = -1, <span style="color: #a0522d;">pch</span> = -1;
<span style="color: #a020f0;">enum</span> <span style="color: #228b22;">com_type_t</span> <span style="color: #a0522d;">com</span> = NOT_COMMENT;
<span style="color: #a020f0;">while</span>((ch = getchar()) != EOF) {
<span style="color: #a020f0;">if</span>(ch == <span style="color: #8b2252;">'"'</span> && is_not_com()){
is_str = ~is_str;
}
<span style="color: #a020f0;">if</span>(!is_str &&
is_not_com() &&
ch == <span style="color: #8b2252;">'/'</span> &&
pch == <span style="color: #8b2252;">'/'</span>) {
com = COMMENT_SINGLE_LINE;
}<span style="color: #a020f0;">else</span> <span style="color: #a020f0;">if</span>(!is_str &&
is_not_com() &&
ch == <span style="color: #8b2252;">'*'</span> &&
pch == <span style="color: #8b2252;">'/'</span>) {
com = COMMENT_MULTI_LINE;
}
<span style="color: #a020f0;">if</span>(is_not_com() && pch != -1) {
putchar(pch);
}
<span style="color: #a020f0;">if</span>(!is_str &&
com == COMMENT_SINGLE_LINE &&
ch == <span style="color: #8b2252;">'\n'</span>) {
putchar(<span style="color: #8b2252;">' '</span>);
com = NOT_COMMENT;
}<span style="color: #a020f0;">else</span> <span style="color: #a020f0;">if</span>(!is_str &&
com == COMMENT_MULTI_LINE &&
ch == <span style="color: #8b2252;">'/'</span> &&
pch == <span style="color: #8b2252;">'*'</span>) {
ch = <span style="color: #8b2252;">' '</span>;
com = NOT_COMMENT;
}
pch = ch;
}
}
</pre>
</div>
</div>
</div>
<div id="outline-container-orgheadline3" class="outline-2">
<h2 id="orgheadline3">Links</h2>
<div class="outline-text-2" id="text-orgheadline3">
<ul class="org-ul">
<li>Next Article - <a href="http://www.harishnote.com/2016/09/k-r-exercise-124-rudimentary-c-syntax.html">K & R : Exercise 1.24 - Rudimentary C syntax checker</a></li>
<li>Previous Article - <a href="http://www.harishnote.com/2016/09/k-r-exercise-122-fold-long-lines.html">K & R : Exercise 1.22 - Fold long lines</a></li>
<li>All Article - <a href="http://www.harishnote.com/p/my-k-r-answers.html">K & R Answer</a></li>
</ul>
</div>
</div>
</div>
</body>
</html>Harishhttp://www.blogger.com/profile/09680319865654952936noreply@blogger.com0tag:blogger.com,1999:blog-3457920023827660787.post-60504361598303139332016-09-02T20:40:00.000+05:302016-09-04T06:19:28.096+05:30C Programming #86: stdio - getchar, putchar, fgets, puts<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2016-09-04 Sun 06:19 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>C Programming #86: stdio - getchar, putchar, gets, puts</title>
<meta name="generator" content="Org-mode" />
<meta name="author" content="Harish SK" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
pre.src-sh:before { content: 'sh'; }
pre.src-bash:before { content: 'sh'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-R:before { content: 'R'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-java:before { content: 'Java'; }
pre.src-sql:before { content: 'SQL'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
/*]]>*/-->
</style>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2013 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<p>
Following article will go through standard input output (stdio) function such as getchar, putchar.
There are several others which are covered in subsequent articles.
</p>
<a name='more'></a><br />
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1">getchar</h2>
<div class="outline-text-2" id="text-orgheadline1">
<div class="org-src-container">
<pre class="src src-C"><span style="color: #b22222;">/* </span><span style="color: #b22222;">Reads the next character from standard input and </span>
<span style="color: #b22222;"> returns it as an unsigned char cast to an int, </span>
<span style="color: #b22222;"> or EOF on end of file or error. </span><span style="color: #b22222;">*/</span>
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">getchar</span>(<span style="color: #228b22;">void</span>);
</pre>
</div>
<p>
There are several examples of it already covered in previous articles.
Couple of other variation of the above function are
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #b22222;">/* </span><span style="color: #b22222;">It is same as getc but instead of stdio it reads </span>
<span style="color: #b22222;"> from stream</span><span style="color: #b22222;">*/</span>
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">fgetc</span>(<span style="color: #228b22;">FILE</span> *<span style="color: #a0522d;">stream</span>);
<span style="color: #b22222;">/* </span><span style="color: #b22222;">getc() is equivalent to fgetc() and it is implemented </span>
<span style="color: #b22222;"> as a macro </span><span style="color: #b22222;">*/</span>
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">getc</span>(<span style="color: #228b22;">FILE</span> *<span style="color: #a0522d;">stream</span>);
<span style="color: #b22222;">/* </span><span style="color: #b22222;">pushes c back to stream, cast to unsigned char, </span>
<span style="color: #b22222;"> where it is available for subsequent read operations. </span><span style="color: #b22222;">*/</span>
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">ungetc</span>(<span style="color: #228b22;">int</span> <span style="color: #a0522d;">c</span>, <span style="color: #228b22;">FILE</span> *<span style="color: #a0522d;">stream</span>);
</pre>
</div>
<p>
Some notes are
</p>
<ul class="org-ul">
<li>fgetc(stdin); call is same as getchar()</li>
<li>getc is macro which can be re-defined to something more intelligent if the programmer wishes to</li>
<li>fgetc and getchar are function with addresses and can be passed to other function as argument.</li>
</ul>
<p>
FILE stream would be covered in future articles, where same function could be used to read from the file.
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">ch</span>;
printf(<span style="color: #8b2252;">"putting 'a' into stdio using ungetc\n"</span>);
ungetc(<span style="color: #8b2252;">'a'</span>, stdin);
ch = getchar();
printf(<span style="color: #8b2252;">"o/p of getchar is %c\n"</span>, ch);
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
<pre class="example">
putting 'a' into stdio using ungetc
o/p of getchar is a
</pre>
</div>
</div>
<div id="outline-container-orgheadline2" class="outline-2">
<h2 id="orgheadline2">putchar</h2>
<div class="outline-text-2" id="text-orgheadline2">
<div class="org-src-container">
<pre class="src src-C"><span style="color: #b22222;">/* </span><span style="color: #b22222;">putchar writes the character c, cast to an unsigned char</span>
<span style="color: #b22222;"> to stdout </span><span style="color: #b22222;">*/</span>
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">putchar</span>(<span style="color: #228b22;">int</span> <span style="color: #a0522d;">c</span>);
</pre>
</div>
<p>
Some other variations are
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #b22222;">/* </span><span style="color: #b22222;">Same as putchar but puts it to stream instead </span><span style="color: #b22222;">*/</span>
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">fputc</span>(<span style="color: #228b22;">int</span> <span style="color: #a0522d;">c</span>, <span style="color: #228b22;">FILE</span> *<span style="color: #a0522d;">stream</span>);
<span style="color: #b22222;">/* </span><span style="color: #b22222;">Same as fputc but is implemented as macro </span><span style="color: #b22222;">*/</span>
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">putc</span>(<span style="color: #228b22;">int</span> <span style="color: #a0522d;">c</span>, <span style="color: #228b22;">FILE</span> *<span style="color: #a0522d;">stream</span>);
</pre>
</div>
<p>
Notes
</p>
<ul class="org-ul">
<li>fputc('c', stdout) is same as putchar('c')</li>
</ul>
</div>
</div>
<div id="outline-container-orgheadline3" class="outline-2">
<h2 id="orgheadline3">puts</h2>
<div class="outline-text-2" id="text-orgheadline3">
<div class="org-src-container">
<pre class="src src-C"><span style="color: #b22222;">/* </span><span style="color: #b22222;">writes the string s and a trailing newline to stdout.</span><span style="color: #b22222;">*/</span>
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">puts</span>(<span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span> *<span style="color: #a0522d;">s</span>);
<span style="color: #b22222;">/* </span><span style="color: #b22222;">fputs() writes the string s to stream, </span>
<span style="color: #b22222;"> without its terminating null byte ('\0').</span><span style="color: #b22222;">*/</span>
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">fputs</span>(<span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span> *<span style="color: #a0522d;">s</span>, <span style="color: #228b22;">FILE</span> *<span style="color: #a0522d;">stream</span>);
</pre>
</div>
</div>
</div>
<div id="outline-container-orgheadline4" class="outline-2">
<h2 id="orgheadline4">gets</h2>
<div class="outline-text-2" id="text-orgheadline4">
<div class="org-src-container">
<pre class="src src-C"><span style="color: #228b22;">char</span> *<span style="color: #0000ff;">gets</span>(<span style="color: #228b22;">char</span> *<span style="color: #a0522d;">s</span>);
</pre>
</div>
<p>
If you see the linux man pages of <code>gets</code> it is strongly written as - <b>Never use this function.</b>
Reason why it should be used is it can easily cause buffer over-flow.
Following is the only place where <code>gets</code> is used, to demo why it is dangerous.
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">buf</span>[5];
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">i</span>;
ungetc(<span style="color: #8b2252;">'\n'</span>, stdin);
ungetc(<span style="color: #8b2252;">'\0'</span>, stdin);
<span style="color: #a020f0;">for</span>(i = 0; i < 100; i++) {
ungetc(<span style="color: #8b2252;">'a'</span>, stdin);
}
gets(buf);
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
<p>
stack smashing detected <b>*</b>: ./a.out terminated
Aborted (core dumped)
</p>
<p>
In above program i am purposefully putting 100 'a' into stdin stream using ungetc.
<code>buf</code> is just 5 bytes long and when gets is called called it try to put 100 'a' on to buf which can hold just 5 'a'
But gets doesnot care and continues writing which will cause stack corruption.
That is exactly the run-time error that we got.
<b>HENCE NEVER USE <code>gets</code></b>.
Instead of it use the safer version called ~fgets
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #b22222;">/* </span><span style="color: #b22222;">reads in at most one less than size characters </span>
<span style="color: #b22222;"> from stream and stores them into the buffer pointed </span>
<span style="color: #b22222;"> to by s. Reading stops after an EOF or a newline. </span>
<span style="color: #b22222;"> If a newline is read, it is stored into the buffer. </span>
<span style="color: #b22222;"> A terminating null byte ('\0') is stored after the</span>
<span style="color: #b22222;"> last character in the buffer. </span><span style="color: #b22222;">*/</span>
<span style="color: #228b22;">char</span> *<span style="color: #0000ff;">fgets</span>(<span style="color: #228b22;">char</span> *<span style="color: #a0522d;">s</span>, <span style="color: #228b22;">int</span> <span style="color: #a0522d;">size</span>, <span style="color: #228b22;">FILE</span> *<span style="color: #a0522d;">stream</span>);
</pre>
</div>
<p>
We could write the above program safely as below
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">buf</span>[5];
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">i</span>;
ungetc(<span style="color: #8b2252;">'\n'</span>, stdin);
ungetc(<span style="color: #8b2252;">'\0'</span>, stdin);
<span style="color: #a020f0;">for</span>(i = 0; i < 100; i++) {
ungetc(<span style="color: #8b2252;">'a'</span>, stdin);
}
fgets(buf, 5, stdin);
puts(buf);
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
</div>
</div>
<div id="outline-container-orgheadline5" class="outline-2">
<h2 id="orgheadline5">Links</h2>
<div class="outline-text-2" id="text-orgheadline5">
<ul class="org-ul">
<li>Next Article - <a href="http://www.harishnote.com/2016/09/c-programming-87-stdio-printf-scanf.html">C Programming #87: stdio - printf, scanf</a></li>
<li>Previous Article - <a href="http://www.harishnote.com/2016/08/c-programming-85-memory-functions.html">C Programming #85: Memory functions</a></li>
<li>All Article - <a href="http://www.harishnote.com/p/c-programming.html">C Programming</a></li>
</ul>
</div>
</div>
</div>
</body>
</html>Harishhttp://www.blogger.com/profile/09680319865654952936noreply@blogger.com0tag:blogger.com,1999:blog-3457920023827660787.post-68730962404654239232016-09-01T20:21:00.000+05:302016-09-03T09:22:41.276+05:30K & R : Exercise 1.22 - Fold long lines<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2016-09-03 Sat 09:22 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>K & R : Exercise 1.22 - Fold long lines</title>
<meta name="generator" content="Org-mode" />
<meta name="author" content="Harish SK" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
pre.src-sh:before { content: 'sh'; }
pre.src-bash:before { content: 'sh'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-R:before { content: 'R'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-java:before { content: 'Java'; }
pre.src-sql:before { content: 'SQL'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
/*]]>*/-->
</style>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2013 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1">Problem</h2>
<div class="outline-text-2" id="text-orgheadline1">
<p>
Write a program to fold long input lines into two or more shorter lines after last non blank character that occurs before the nth column of input.
Make sure your program does something intelligent with very long lines.
If there are no blanks or tabs before the specified column.
</p>
<a name='more'></a><br />
</div>
</div>
<div id="outline-container-orgheadline2" class="outline-2">
<h2 id="orgheadline2">Solution</h2>
<div class="outline-text-2" id="text-orgheadline2">
<p>
Lets assume the nth column to be 80 characters.
Hence we need to buffer those 80 characters before any decision is taken.
For any inputs which has more 80 characters of spaces lets just insert one more blank line.
Putting all the ideas we get the following code
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #483d8b;">#define</span> <span style="color: #a0522d;">COLUMN_LEN_MAX</span> 80
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">buf</span>[COLUMN_LEN_MAX] = {<span style="color: #8b2252;">'\0'</span>, };
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">ch</span>, <span style="color: #a0522d;">count</span> = 0, <span style="color: #a0522d;">tcount</span> = 0, <span style="color: #a0522d;">i</span>, <span style="color: #a0522d;">j</span>;
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">temp</span>[COLUMN_LEN_MAX] = {<span style="color: #8b2252;">'\0'</span>, };
<span style="color: #a020f0;">while</span>((ch = getchar()) != EOF){
buf[count] = ch;
count++;
<span style="color: #a020f0;">if</span>((ch == <span style="color: #8b2252;">'\n'</span>) && (count < COLUMN_LEN_MAX - 1)) {
<span style="color: #b22222;">/* </span><span style="color: #b22222;">Given line is small and just dump </span>
<span style="color: #b22222;"> it no folding required </span><span style="color: #b22222;">*/</span>
<span style="color: #a020f0;">for</span>(i = 0; i < count; i++) {
putchar(buf[i]);
}
count = 0;
}<span style="color: #a020f0;">else</span> <span style="color: #a020f0;">if</span>(count >= COLUMN_LEN_MAX - 1) {
<span style="color: #b22222;">/* </span><span style="color: #b22222;">Now the given line is 80 characters </span><span style="color: #b22222;">*/</span>
<span style="color: #a020f0;">for</span>(i = 0; i < count; i++) {
<span style="color: #b22222;">/* </span><span style="color: #b22222;">store all the space till we hit non white characters </span><span style="color: #b22222;">*/</span>
<span style="color: #a020f0;">if</span>(buf[i] == <span style="color: #8b2252;">' '</span> || buf[i] == <span style="color: #8b2252;">'\t'</span>) {
temp[tcount] = buf[i];
tcount++;
}<span style="color: #a020f0;">else</span> {
<span style="color: #b22222;">/* </span><span style="color: #b22222;">Putchar all white space before putchar non-white space </span><span style="color: #b22222;">*/</span>
<span style="color: #a020f0;">if</span>(tcount > 1) {
<span style="color: #a020f0;">for</span>(j = 0; j < tcount; j++) {
putchar(temp[j]);
}
putchar(buf[i]);
tcount = 0;
}<span style="color: #a020f0;">else</span> {
putchar(buf[i]);
}
}
}
putchar(<span style="color: #8b2252;">'\n'</span>);
count = 0;
}
}
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
</div>
</div>
<div id="outline-container-orgheadline3" class="outline-2">
<h2 id="orgheadline3">Links</h2>
<div class="outline-text-2" id="text-orgheadline3">
<ul class="org-ul">
<li>Next Article - <a href="http://www.harishnote.com/2016/09/k-r-exercise-123-remove-c-comments.html">K & R : Exercise 1.23 - remove C comments</a></li>
<li>Previous Article - <a href="http://www.harishnote.com/2016/08/title-k-r-exercise-121-replace-space.html">K & R : Exercise 1.21 - Replace space with min tab - entab</a></li>
<li>All Article - <a href="http://www.harishnote.com/p/my-k-r-answers.html">K & R Answer</a></li>
</ul>
</div>
</div>
</div>
</body>
</html>Harishhttp://www.blogger.com/profile/09680319865654952936noreply@blogger.com0tag:blogger.com,1999:blog-3457920023827660787.post-81712975310096411402016-08-31T17:54:00.000+05:302016-09-02T20:45:44.701+05:30 C Programming #85: Memory functions<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2016-09-02 Fri 20:41 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>C Programming #85: Memory functions</title>
<meta name="generator" content="Org-mode" />
<meta name="author" content="Harish SK" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
pre.src-sh:before { content: 'sh'; }
pre.src-bash:before { content: 'sh'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-R:before { content: 'R'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-java:before { content: 'Java'; }
pre.src-sql:before { content: 'SQL'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
/*]]>*/-->
</style>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2013 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<p>
Following article will try to put together memory functions just like how string related functions were covered.
Memory functions which are covered are <code>memcmp</code>, <code>memcpy</code>, <code>memmove</code>, <code>memset</code>, <code>memchr</code>.
Note all the above function are part of <code>strings.h</code> library.
Each of above function is covered with an example.
</p>
<a name='more'></a><br />
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1">memcmp</h2>
<div class="outline-text-2" id="text-orgheadline1">
<p>
Below is prototype of <code>memcmp</code> :
</p>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #b22222;">/* </span><span style="color: #b22222;">memcmp is similar to strcmp, except that bytes </span>
<span style="color: #b22222;"> equal to 0 are not treated as comparison terminators.</span><span style="color: #b22222;">*/</span>
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">memcmp</span>(<span style="color: #a020f0;">const</span> <span style="color: #228b22;">void</span> *<span style="color: #a0522d;">ptr1</span>, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">void</span> *<span style="color: #a0522d;">ptr2</span>, <span style="color: #228b22;">size_t</span> <span style="color: #a0522d;">len</span>);
</pre>
</div>
<p>
Example for usage is as follows
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">a</span>[10] = { 2, 3, 4, 5 };
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">b</span>[10] = { 2, 3, 4, 6 };
<span style="color: #a020f0;">if</span>(memcmp(a, b, 3) == 0) {
printf(<span style="color: #8b2252;">"First 3 items are same\n"</span>);
}<span style="color: #a020f0;">else</span> {
printf(<span style="color: #8b2252;">"First 3 items are not same\n"</span>);
}
<span style="color: #a020f0;">if</span>(memcmp(a, b, 4) == 0) {
printf(<span style="color: #8b2252;">"First 4 items are same\n"</span>);
}<span style="color: #a020f0;">else</span> {
printf(<span style="color: #8b2252;">"First 4 items are not same\n"</span>);
}
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
<pre class="example">
First 3 items are same
First 4 items are not same
</pre>
</div>
</div>
<div id="outline-container-orgheadline2" class="outline-2">
<h2 id="orgheadline2">memcpy</h2>
<div class="outline-text-2" id="text-orgheadline2">
<p>
Below is prototype of <code>memcpy</code> :
</p>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #b22222;">/* </span><span style="color: #b22222;">memcpy copies len characters from src to dst </span>
<span style="color: #b22222;"> and returns the original value of dst</span>
<span style="color: #b22222;"> The result of memcpy is undefined if src and </span>
<span style="color: #b22222;"> dst point to overlapping areas of memory </span><span style="color: #b22222;">*/</span>
<span style="color: #228b22;">void</span> *<span style="color: #0000ff;">memcpy</span>(<span style="color: #228b22;">void</span> *<span style="color: #a0522d;">dst</span>, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">void</span> *<span style="color: #a0522d;">src</span>, <span style="color: #228b22;">size_t</span> <span style="color: #a0522d;">len</span>);
</pre>
</div>
<p>
Example for usage is as follows
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><strings.h></span>
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">a</span>[4] = { 1, 2, 3 };
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">b</span>[4] = { 0, 0, 0 };
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">i</span>;
printf(<span style="color: #8b2252;">"b Before memcpy: "</span>);
<span style="color: #a020f0;">for</span>(i = 0; i < 3; i++) {
printf(<span style="color: #8b2252;">"%d, "</span>, b[i]);
}
printf(<span style="color: #8b2252;">"\n"</span>);
memcpy(b, a, 3);
printf(<span style="color: #8b2252;">"b After memcpy: "</span>);
<span style="color: #a020f0;">for</span>(i = 0; i < 3; i++) {
printf(<span style="color: #8b2252;">"%d, "</span>, b[i]);
}
printf(<span style="color: #8b2252;">"\n"</span>);
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
<pre class="example">
b Before memcpy: 0, 0, 0,
b After memcpy: 1, 2, 3,
</pre>
</div>
</div>
<div id="outline-container-orgheadline3" class="outline-2">
<h2 id="orgheadline3">memmove</h2>
<div class="outline-text-2" id="text-orgheadline3">
<p>
Below is prototype of <code>memmove</code> :
</p>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #b22222;">/* </span><span style="color: #b22222;">memmove is just like memcpy except that </span>
<span style="color: #b22222;"> memmove is guaranteed to work even </span>
<span style="color: #b22222;"> if the memory areas overlap </span><span style="color: #b22222;">*/</span>
<span style="color: #228b22;">void</span> *<span style="color: #0000ff;">memmove</span>(<span style="color: #228b22;">void</span> *<span style="color: #a0522d;">dst</span>, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">void</span> *<span style="color: #a0522d;">src</span>, <span style="color: #228b22;">size_t</span> <span style="color: #a0522d;">len</span>);
</pre>
</div>
<p>
memmove takes care of overlap which can be demonstated by following example
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><strings.h></span>
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">a</span>[5] = { 1, 2, 3 };
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">b</span>[5] = { 0, 0, 1, 2, 3 };
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">i</span>;
printf(<span style="color: #8b2252;">"a Before memmove: "</span>);
<span style="color: #a020f0;">for</span>(i = 0; i < 5; i++) {
printf(<span style="color: #8b2252;">"%d, "</span>, a[i]);
}
printf(<span style="color: #8b2252;">"\n"</span>);
memmove(a + 2, a, 3);
printf(<span style="color: #8b2252;">"a After memmove: "</span>);
<span style="color: #a020f0;">for</span>(i = 0; i < 5; i++) {
printf(<span style="color: #8b2252;">"%d, "</span>, a[i]);
}
printf(<span style="color: #8b2252;">"\n"</span>);
printf(<span style="color: #8b2252;">"b Before memmove: "</span>);
<span style="color: #a020f0;">for</span>(i = 0; i < 5; i++) {
printf(<span style="color: #8b2252;">"%d, "</span>, b[i]);
}
printf(<span style="color: #8b2252;">"\n"</span>);
memmove(b, b + 2, 3);
printf(<span style="color: #8b2252;">"b After memmove: "</span>);
<span style="color: #a020f0;">for</span>(i = 0; i < 5; i++) {
printf(<span style="color: #8b2252;">"%d, "</span>, b[i]);
}
printf(<span style="color: #8b2252;">"\n"</span>);
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
<pre class="example">
a Before memmove: 1, 2, 3, 0, 0,
a After memmove: 1, 2, 1, 2, 3,
b Before memmove: 0, 0, 1, 2, 3,
b After memmove: 1, 2, 3, 2, 3,
</pre>
</div>
</div>
<div id="outline-container-orgheadline4" class="outline-2">
<h2 id="orgheadline4">memset</h2>
<div class="outline-text-2" id="text-orgheadline4">
<p>
Below is prototype of <code>memset</code> :
</p>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #b22222;">/* </span><span style="color: #b22222;">memset sets the first len bytes of the memory area </span>
<span style="color: #b22222;"> pointed to by ptr to the value specified by byteval </span><span style="color: #b22222;">*/</span>
<span style="color: #228b22;">void</span> *<span style="color: #0000ff;">memset</span>(<span style="color: #228b22;">void</span> *<span style="color: #a0522d;">ptr</span>, <span style="color: #228b22;">int</span> <span style="color: #a0522d;">byteval</span>, <span style="color: #228b22;">size_t</span> <span style="color: #a0522d;">len</span>);
</pre>
</div>
<p>
Example for memset is
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><strings.h></span>
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">a</span>[4] = { 1, 2, 3, 4};
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">i</span>;
printf(<span style="color: #8b2252;">"a Before memset: "</span>);
<span style="color: #a020f0;">for</span>(i = 0; i < 4; i++) {
printf(<span style="color: #8b2252;">"%d, "</span>, a[i]);
}
printf(<span style="color: #8b2252;">"\n"</span>);
memset(a, <span style="color: #8b2252;">'\0'</span>, 4);
printf(<span style="color: #8b2252;">"a After memset: "</span>);
<span style="color: #a020f0;">for</span>(i = 0; i < 4; i++) {
printf(<span style="color: #8b2252;">"%d, "</span>, a[i]);
}
printf(<span style="color: #8b2252;">"\n"</span>);
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
<pre class="example">
a Before memset: 1, 2, 3, 4,
a After memset: 0, 0, 0, 0,
</pre>
</div>
</div>
<div id="outline-container-orgheadline5" class="outline-2">
<h2 id="orgheadline5">memchr</h2>
<div class="outline-text-2" id="text-orgheadline5">
<p>
Below is prototype of <code>memchr</code> :
</p>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #b22222;">/* </span><span style="color: #b22222;">memchr finds the first occurence of ch in ptr </span>
<span style="color: #b22222;"> and returns a pointer to it (or a null pointer </span>
<span style="color: #b22222;"> if ch was not found in the first len bytes </span><span style="color: #b22222;">*/</span>
<span style="color: #228b22;">void</span> *<span style="color: #0000ff;">memchr</span>(<span style="color: #a020f0;">const</span> <span style="color: #228b22;">void</span> *<span style="color: #a0522d;">ptr</span>, <span style="color: #228b22;">int</span> <span style="color: #a0522d;">ch</span>, <span style="color: #228b22;">size_t</span> <span style="color: #a0522d;">len</span>);
</pre>
</div>
<p>
Example that demonstates memchr is as follows
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><strings.h></span>
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">a</span>[4] = {11, 34, 81, 23};
<span style="color: #228b22;">char</span> *<span style="color: #a0522d;">p</span>, <span style="color: #a0522d;">i</span>;
printf(<span style="color: #8b2252;">"A is: "</span>);
<span style="color: #a020f0;">for</span>(i = 0; i < 4; i++) {
printf(<span style="color: #8b2252;">"%d, "</span>, a[i]);
}
printf(<span style="color: #8b2252;">"\n"</span>);
p = memchr(a, 81, 4);
printf(<span style="color: #8b2252;">"Pointer of element 81 is %p\n"</span>, p);
printf(<span style="color: #8b2252;">"Position of element 81 is %d\n"</span>, p - a);
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
<pre class="example">
A is: 11, 34, 81, 23,
Pointer of element 81 is 0x7ffc82526ab2
Position of element 81 is 2
</pre>
</div>
</div>
<div id="outline-container-orgheadline6" class="outline-2">
<h2 id="orgheadline6">Links</h2>
<div class="outline-text-2" id="text-orgheadline6">
<ul class="org-ul">
<li>Next Article - <a href="http://www.harishnote.com/2016/09/c-programming-86-stdio-getchar-putchar.html">C Programming #86: stdio - getchar, putchar, fgets, puts</a></li>
<li>Previous Article - <a href="http://www.harishnote.com/2016/08/c-programming-84-flexible-array-member.html">C Programming #84: flexible array member</a></li>
<li>All Article - <a href="http://www.harishnote.com/p/c-programming.html">C Programming</a></li>
</ul>
</div>
</div>
</div>
</body>
</html>Harishhttp://www.blogger.com/profile/09680319865654952936noreply@blogger.com0tag:blogger.com,1999:blog-3457920023827660787.post-35247419999361899952016-08-30T22:11:00.000+05:302016-09-01T20:22:31.226+05:30K & R : Exercise 1.21 - Replace space with minimum tab - entab<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2016-09-01 Thu 20:22 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>K & R : Exercise 1.21 - Replace space with minimum tab - entab</title>
<meta name="generator" content="Org-mode" />
<meta name="author" content="Harish SK" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
pre.src-sh:before { content: 'sh'; }
pre.src-bash:before { content: 'sh'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-R:before { content: 'R'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-java:before { content: 'Java'; }
pre.src-sql:before { content: 'SQL'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
/*]]>*/-->
</style>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2013 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1">Problem</h2>
<div class="outline-text-2" id="text-orgheadline1">
<p>
Write a program <code>entab</code> that replaces strings of blank by minimum number of tabs and blanks to achieve the same spacing.
Use the same tab stops as for <code>detab</code>. When either a tab or single blank would suffice to reach a tab stop which should be given preference.
</p>
<a name='more'></a><br />
</div>
</div>
<div id="outline-container-orgheadline2" class="outline-2">
<h2 id="orgheadline2">Solution</h2>
<div class="outline-text-2" id="text-orgheadline2">
<p>
We are converting only spaces into tabs and spaces.
Lets assume that we convert 4 space into 1 tab.
So if there are 6 spaces it will get converted into 1 tab and 2 spaces.
Hence we need to count how many spaces are there.
We can take decision of conversion only when we hit a non white characters.
Converting above ideas into C Program.
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #483d8b;">#define</span> <span style="color: #a0522d;">SPACE_TO_TAB</span> 4
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">space_count</span> = 0, <span style="color: #a0522d;">i</span>;
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">ch</span>;
<span style="color: #a020f0;">while</span>((ch = getchar()) !=EOF) {
<span style="color: #a020f0;">if</span>(ch == <span style="color: #8b2252;">' '</span>) {
space_count++;
}<span style="color: #a020f0;">else</span> {
<span style="color: #a020f0;">if</span>(space_count > 0) {
<span style="color: #a020f0;">for</span>(i = 0; i< space_count/SPACE_TO_TAB; i++) {
putchar(<span style="color: #8b2252;">'\t'</span>);
}
<span style="color: #a020f0;">for</span>(i = 0; i< space_count % SPACE_TO_TAB; i++) {
putchar(<span style="color: #8b2252;">' '</span>);
}
space_count = 0;
}
putchar(ch);
}
}
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
</div>
</div>
<div id="outline-container-orgheadline3" class="outline-2">
<h2 id="orgheadline3">Links</h2>
<div class="outline-text-2" id="text-orgheadline3">
<ul class="org-ul">
<li>Next Article - <a href="http://www.harishnote.com/2016/09/k-r-exercise-122-fold-long-lines.html">K & R : Exercise 1.22 - Fold long lines</a></li>
<li>Previous Article - <a href="http://www.harishnote.com/2016/08/k-r-exercise-120-detab.html">K & R : Exercise 1.20 - detab</a></li>
<li>All Article - <a href="http://www.harishnote.com/p/my-k-r-answers.html">K & R Answer</a></li>
</ul>
</div>
</div>
</div>
</body>
</html>Harishhttp://www.blogger.com/profile/09680319865654952936noreply@blogger.com0tag:blogger.com,1999:blog-3457920023827660787.post-9596243779855043792016-08-29T22:27:00.000+05:302016-08-31T17:56:35.891+05:30C Programming #84: flexible array member<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2016-08-31 Wed 17:56 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>C Programming #84: flexible array member</title>
<meta name="generator" content="Org-mode" />
<meta name="author" content="Harish SK" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
pre.src-sh:before { content: 'sh'; }
pre.src-bash:before { content: 'sh'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-R:before { content: 'R'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-java:before { content: 'Java'; }
pre.src-sql:before { content: 'SQL'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
/*]]>*/-->
</style>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2013 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<p>
Following article explores the Flexible array member or also called array of length zero.
It is very useful when you don't know the size during compile time.
</p>
<a name='more'></a><br />
<p>
Consider following code
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">a</span>[0];
printf(<span style="color: #8b2252;">"sizeof a is %d\n"</span>, <span style="color: #a020f0;">sizeof</span>(a));
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
<pre class="example">
sizeof a is 0
</pre>
<p>
Here a is array of size 0, there are no elements in a. a takes no memory at all.
You might think there is no use for such a C construct.
It is used in very useful one of useful structure called variable length object.
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #a020f0;">struct</span> <span style="color: #228b22;">buf_tag</span> {
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">len</span>;
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">data</span>[0];
};
</pre>
</div>
<p>
Here data can be of any length, memory allocated using malloc (dynamic behaviour).
Only last member of struct can be flexible array member.
</p>
<p>
Usage of is explored in below program.
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #a020f0;">typedef</span> <span style="color: #a020f0;">struct</span> <span style="color: #228b22;">buf_tag</span> {
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">len</span>;
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">data</span>[0];
}<span style="color: #228b22;">buf_t</span>;
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">buf_t</span> *<span style="color: #a0522d;">a</span>;
a = malloc(<span style="color: #a020f0;">sizeof</span>(buf_t) + 3);
a->len = 3;
a->data[0] = 10;
a->data[1] = 11;
a->data[2] = 12;
printf(<span style="color: #8b2252;">"a len is %d\n"</span>, a->len);
<span style="color: #a020f0;">for</span>(<span style="color: #228b22;">int</span> <span style="color: #a0522d;">i</span> = 0; i < a->len; i++) {
printf(<span style="color: #8b2252;">"data[%d] is %d\n"</span>, i, a->data[i]);
}
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
<pre class="example">
a len is 3
data[0] is 10
data[1] is 11
data[2] is 12
</pre>
<p>
Above syntax that was used was GNU C standard. C99 dictates that it should be as follows
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #a020f0;">typedef</span> <span style="color: #a020f0;">struct</span> <span style="color: #228b22;">buf_tag</span> {
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">len</span>;
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">data</span>[];
}<span style="color: #228b22;">buf_t</span>;
</pre>
</div>
<p>
Hence for writing more portable code it is recommended the above way.
Make sure allocation matches the len correctly otherwise we could run into issue easily.
</p>
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1">Links</h2>
<div class="outline-text-2" id="text-orgheadline1">
<ul class="org-ul">
<li>Next Article - <a href="http://www.harishnote.com/2016/08/c-programming-85-memory-functions.html">C Programming #85: Memory functions</a></li>
<li>Previous Article - <a href="http://www.harishnote.com/2016/08/c-programming-83-designated-intializers.html">C Programming #83: designated intializers</a></li>
<li>All Article - <a href="http://www.harishnote.com/p/c-programming.html">C Programming</a></li>
</ul>
</div>
</div>
</div>
</body>
</html>Harishhttp://www.blogger.com/profile/09680319865654952936noreply@blogger.com0tag:blogger.com,1999:blog-3457920023827660787.post-66336238852734417942016-08-28T13:50:00.000+05:302016-08-30T22:14:04.309+05:30K & R : Exercise 1.20 - detab=<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2016-08-30 Tue 22:13 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>K & R : Exercise 1.20 - detab</title>
<meta name="generator" content="Org-mode" />
<meta name="author" content="Harish SK" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
pre.src-sh:before { content: 'sh'; }
pre.src-bash:before { content: 'sh'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-R:before { content: 'R'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-java:before { content: 'Java'; }
pre.src-sql:before { content: 'SQL'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
/*]]>*/-->
</style>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2013 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1">Problem Statement</h2>
<div class="outline-text-2" id="text-orgheadline1">
<p>
Exercise 1-20: Write a program detab that replaces tabs in the input with the proper number of blanks to space to the next tab stop.
</p>
<a name='more'></a><br />
</div>
</div>
<div id="outline-container-orgheadline2" class="outline-2">
<h2 id="orgheadline2">Solution</h2>
<div class="outline-text-2" id="text-orgheadline2">
<p>
We need to take each character and check if it a tab and replace multiple spaces.
Solution to above problem would look something like this.
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #483d8b;">#define</span> <span style="color: #a0522d;">SPACE_FOR_TAB</span> 4
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">ch</span>, <span style="color: #a0522d;">i</span>;
<span style="color: #a020f0;">while</span>((ch = getchar()) != EOF) {
<span style="color: #a020f0;">if</span>(ch == <span style="color: #8b2252;">'\t'</span>) {
<span style="color: #a020f0;">for</span>(i = 0; i < SPACE_FOR_TAB; i++) {
putchar(<span style="color: #8b2252;">' '</span>);
}
}<span style="color: #a020f0;">else</span>{
putchar(ch);
}
}
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
</div>
</div>
<div id="outline-container-orgheadline3" class="outline-2">
<h2 id="orgheadline3">Links</h2>
<div class="outline-text-2" id="text-orgheadline3">
<ul class="org-ul">
<li>Next Article - <a href="http://www.harishnote.com/2016/08/title-k-r-exercise-121-replace-space.html">K & R : Exercise 1.21 - Replace space with min tab - entab</a></li>
<li>Previous Article - <a href="http://www.harishnote.com/2016/08/k-r-exercise-119-reverse-input-line.html">K & R : Exercise 1.19 - Reverse input line</a></li>
<li>All Article - <a href="http://www.harishnote.com/p/my-k-r-answers.html">K & R Answer</a></li>
</ul>
</div>
</div>
</div>
</body>
</html>Harishhttp://www.blogger.com/profile/09680319865654952936noreply@blogger.com0tag:blogger.com,1999:blog-3457920023827660787.post-47581915332764830652016-08-27T08:06:00.000+05:302016-08-29T22:28:20.268+05:30C Programming #83: designated intializers<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2016-08-29 Mon 22:28 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>C Programming #83: designated intializers</title>
<meta name="generator" content="Org-mode" />
<meta name="author" content="Harish SK" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
pre.src-sh:before { content: 'sh'; }
pre.src-bash:before { content: 'sh'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-R:before { content: 'R'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-java:before { content: 'Java'; }
pre.src-sql:before { content: 'SQL'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
/*]]>*/-->
</style>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2013 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<p>
You have already know that array/structure variables can be fully or partially initialized.
And when partially initialized only the beginning elements(array members, structure members) could be initialized.
This limitation in C90 standard is removed by designated intializers that was introduced in C99 standard.
This is whole new syntax that is used, which has some interesting application.
This article examples designated intializers applied to array, structures, and array of structures.
</p>
<a name='more'></a><br />
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1">designated intializers for array</h2>
<div class="outline-text-2" id="text-orgheadline1">
<p>
Say we would we have integer array <code>a</code> of size 10.
We would like to initialize the 3'rd element to 101 and 6th element to 105.
It can be done as follows.
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">a</span>[10] = {[2] = 101, [5] = 105};
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">i</span> = 0;
<span style="color: #a020f0;">for</span>(i = 0; i < 10; i++) {
printf(<span style="color: #8b2252;">"a[%d]=%d\n"</span>, i, a[i]);
}
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
<pre class="example">
a[0]=0
a[1]=0
a[2]=101
a[3]=0
a[4]=0
a[5]=105
a[6]=0
a[7]=0
a[8]=0
a[9]=0
</pre>
</div>
</div>
<div id="outline-container-orgheadline2" class="outline-2">
<h2 id="orgheadline2">designated range intializer for array</h2>
<div class="outline-text-2" id="text-orgheadline2">
<p>
Say we have huge array of size 1000 and we would like to initialize 200-300 elements with value 5.
Even the above method would not be concise, in such places range operator can be used.
Below example demonstrates element 2-5 initialized with value 11.
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">a</span>[10] = {[1 ... 4] = 11};
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">i</span> = 0;
<span style="color: #a020f0;">for</span>(i = 0; i < 10; i++) {
printf(<span style="color: #8b2252;">"a[%d]=%d\n"</span>, i, a[i]);
}
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
<pre class="example">
a[0]=0
a[1]=11
a[2]=11
a[3]=11
a[4]=11
a[5]=0
a[6]=0
a[7]=0
a[8]=0
a[9]=0
</pre>
</div>
</div>
<div id="outline-container-orgheadline3" class="outline-2">
<h2 id="orgheadline3">designated intializer for structure</h2>
<div class="outline-text-2" id="text-orgheadline3">
<p>
Say we have following structure
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #a020f0;">typedef</span> <span style="color: #a020f0;">struct</span> <span style="color: #228b22;">s_tag</span> {
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">a</span>,
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">b</span>,
<span style="color: #228b22;">float</span> <span style="color: #a0522d;">f</span>,
}<span style="color: #228b22;">s_t</span>;
</pre>
</div>
<p>
Designated intializers for struct allows to initialize the elements as follows.
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #a020f0;">typedef</span> <span style="color: #a020f0;">struct</span> <span style="color: #228b22;">s_tag</span> {
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">a</span>;
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">b</span>;
<span style="color: #228b22;">float</span> <span style="color: #a0522d;">c</span>;
}<span style="color: #228b22;">s_t</span>;
<span style="color: #228b22;">s_t</span> <span style="color: #a0522d;">s</span> = {.c=1.1, .b=100, .a=<span style="color: #8b2252;">'x'</span>};
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
printf(<span style="color: #8b2252;">"s.a is %c, s.b is %d, s.c is %f\n"</span>,
s.a, s.b, s.c);
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
<pre class="example">
s.a is x, s.b is 100, s.c is 1.100000
</pre>
<p>
Now <code>struct</code> and <code>union</code> follow similar syntax only difference being memory layouts.
Same designated intializers could be extended to unions too.
</p>
</div>
</div>
<div id="outline-container-orgheadline4" class="outline-2">
<h2 id="orgheadline4">designated intialisers for array of struct</h2>
<div class="outline-text-2" id="text-orgheadline4">
<p>
Combining the 1st and 3rd method that is explained in this article.
Designated intialisers for array of struct would look like this.
</p>
<div class="org-src-container">
<pre class="src src-C"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #a020f0;">typedef</span> <span style="color: #a020f0;">struct</span> <span style="color: #228b22;">s_tag</span> {
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">a</span>;
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">b</span>;
<span style="color: #228b22;">float</span> <span style="color: #a0522d;">c</span>;
}<span style="color: #228b22;">s_t</span>;
<span style="color: #228b22;">s_t</span> <span style="color: #a0522d;">s</span>[2] = {[0].c=1.1, [1].b=100, [0].a=<span style="color: #8b2252;">'x'</span>, [1].a=<span style="color: #8b2252;">'y'</span>};
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>()
{
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">i</span> = 0;
<span style="color: #a020f0;">for</span>(i = 0; i < 2; i++) {
printf(<span style="color: #8b2252;">"index %d s.a is %c, s.b is %d, s.c is %f\n"</span>,
i, s[i].a, s[i].b, s[i].c);
}
<span style="color: #a020f0;">return</span> 0;
}
</pre>
</div>
<pre class="example">
index 0 s.a is x, s.b is 0, s.c is 1.100000
index 1 s.a is y, s.b is 100, s.c is 0.000000
</pre>
<p>
Make sure that currently used c compiler is having C99 standard implemented.
</p>
</div>
</div>
<div id="outline-container-orgheadline5" class="outline-2">
<h2 id="orgheadline5">Links</h2>
<div class="outline-text-2" id="text-orgheadline5">
<ul class="org-ul">
<li>Next Article - <a href="http://www.harishnote.com/2016/08/c-programming-84-flexible-array-member.html">C Programming #84: flexible array member</a></li>
<li>Previous Article - <a href="http://www.harishnote.com/2016/08/c-programming-82-stringification.html">C Programming #82: stringification operator</a></li>
<li>All Article - <a href="http://www.harishnote.com/p/c-programming.html">C Programming</a></li>
</ul>
</div>
</div>
</div>
</body>
</html>Harishhttp://www.blogger.com/profile/09680319865654952936noreply@blogger.com0