\documentclass{beamer} %include lhs2TeX.fmt \author{Piyush P Kurur\\ Office no: 224\\ Dept. of Comp. Sci. and Engg.\\ IIT Kanpur} \usepackage{dot2texi} \usepackage{tikz} \usetikzlibrary{positioning,shapes,chains} \usetikzlibrary{shapes.symbols} \usetikzlibrary{matrix} \title{Fundamentals of Computing: Lecture 3} \usepackage{pgfkeys} \pgfkeys % {% /tikz/noise/.style={ starburst, draw=red }, /tikz/qnoise/.style={ starburst, fill=yellow, draw=red }, /tikz/boxcolor/.style={ draw=black top color=white, bottom color=red!50!black!50 }, /tikz/box/.style={ rectangle, minimum size=1cm, /tikz/boxcolor }, /tikz/unbox/.style={ rectangle, minimum size=1cm }, /tikz/point/.style={rectangle,minimum size=1cm} } \usetikzlibrary{backgrounds} \usetikzlibrary{shapes.geometric} \usepackage{multicol} \date{July 31, 2009} \begin{document} \begin{frame} \maketitle \end{frame} \begin{frame} \frametitle{Getting started} You need to first creat a file using the editor. Use one of vi/emacs. The Lab TA will tell you how to use the editor. \begin{block}{Algorithm to creating a file and execute} \begin{enumerate} \item Open the file with the editor \begin{code} $ emacs foo.c \end{code} \item Edit the program save and exit. \label{step-edit} \item Compile the program > > $ gcc foo.c > \item If there are errors go to step~\ref{step-edit} \item Run the program > > $ ./a.out > \end{enumerate} \end{block} \end{frame} \begin{frame} \frametitle{The first C program} \begin{code} #include int main() { printf("The answer is 42") } \end{code} \end{frame} \begin{frame} \frametitle{Variables} \begin{code} #include int main() { int answer = 41; printf("The answer is %d",answer); answer = answer + 1; printf("The answer is %d",answer); } \end{code} \begin{block}{Rule for variable names} A variable name can start with any letter and contain any letter digit. The symbol |'_'| is treated as a letter. eg |x123|, |_x123| non examples |23x| \end{block} \end{frame} \begin{frame} \frametitle{Basic types} \begin{enumerate} \item Integers > int i = 100; \item Character > char c = 'a'; \item Floting point numbers > float f = 1.234; \end{enumerate} \end{frame} \begin{frame} \frametitle{Expression} \begin{itemize} \item Usual aritmetic expressions. eg |x + y * 3| \item Relational expressions > x <= 15 \item Boolean (truth) expressions > (x <= 10) || (y < 100) && (10 > 100) Here the operator @||@ denotes a logical OR function and |&&| denote logical AND function. \end{itemize} \begin{block}{Important point} C does not have Boolean (or truth) type. Integers, characters, etc are treated like boolean. Zero is treated as False and non-zero as True. \end{block} \end{frame} \begin{frame} \frametitle{Operator Precedence} What does |2 + 3*6 > 5 && 5 < 2| mean ? \pause \begin{block}{Precedence rules among different kinds of operators} \begin{itemize} \item Arithmetic has highest \item then comes relational. \item then comes boolean. \end{itemize} \end{block} \begin{block}{Among arithmetic operators} \begin{itemize} \item @*@ and @/@ have more precedence than |+| and |-|. \item Unary operator has more precedence than binary. \end{itemize} \end{block} \end{frame} \begin{frame} \frametitle{Factorial program} \begin{code} # include int main(){ int n = 100; int i = 1; int fact = 1; while(i <= n) { fact = fact * i; i = i + 1; } printf("The factorial of %d is %d", n , fact); } \end{code} \end{frame} \end{document}