Transcription of The Basics of C Programming - University of …
1 The Basics of C ProgrammingMarshall BrainLast updated: October 30, 2013 Contents1 C programming1 What is C?.. 2 The simplest C program, I.. 2 Spacing and indentation.. 3 Compilation and run.. 3 The simplest C program, II.. 4 Variables.. 52 Input and output7printf.. 7scanf.. 10 Programming exercise.. 123 Branching and looping13if statement.. 13 Boolean expressions.. 14 Boolean: = vs ==.. 15while loop.. 16do-while loop.. 17for loop.. 17iCONTENTSCONTENTSL ooping: an example.. 19 Programming exercise.. 224 Arrays23 Programming exercise.
2 245 Variable Types29 Typecasting.. 29 Typedef.. 306 Operators33 Operator precedence, I.. 34 Incrementing.. 34 Programming exercise.. 347 Functions35 Programming exercise.. 38 Function prototypes.. 388 Structures419 Libraries43 Making a library.. 47 Compiling and running.. 4910 Makefiles5111 Pointers53 Pointers: why?.. 54 Pointer Basics .. 56iiCONTENTSCONTENTSP ointers: RAM Adresses.. 57 Pointing to the Same Addres.. 62 Pointers: Common Bugs.. 62 Bug #1 - Uninitialized pointers.. 62 Bug #2 - Invalid Pointer References.. 63 Bug #3 - Zero Pointer Reference.
3 63 Pointers: Function Parameters.. 6412 Dynamic Data Structures67 The Heap.. 67 Malloc and Free.. 6813 Advanced Pointers77 Pointer Types.. 77 Pointers to Structures.. 78 Pointers to Arrays.. 79 Arrays of Pointers.. 80 Structures Containing Pointers.. 81 Pointers to Pointers.. 81 Pointers to Structures Containing Pointers.. 82 Linking.. 83A Linked Stack Example.. 84 Programming exercise.. 87 Using Pointers with Arrays.. 8714 Strings91 Special Note on String Constants.. 96 Special Note on Using Strings with malloc.. 97 Programming exercise.
4 98iiiCONTENTSCONTENTS15 Operator Precedence, II9916 Command Line Arguments10117 Text files103 Text files: opening.. 105 Text files: reading.. 106 Main function return values.. 10718 Binary Files10919 Further reading115 Index117ivChapter 1 Basics of C programmingThe C Programming language is a popular and widely used Programming lan-guage for creatingcomputer programs. Programmers embrace C because itgives maximum control and efficiency to the you are a programmer, or if you are interested in becoming aprogrammer, thereare a couple of benefits you gain from learning C: You will be able to read and write code for a large number of platforms everything from microcontrollers to the most advanced scientific systemscan be written in C.
5 Because of the performance and portability of C, almost all popular cross-platform Programming languages and scripting languages, such as C++,Java, Python, Objective-C, Perl, Ruby, PHP, Lua, and Bash, are imple-mented in C and borrowed syntaxes and functions heavily fromC. Theyshare the similar operators, expressions, repetition statements, control struc-tures, arrays, input and output, and functions. Furthermore, almost all lan-guages can interface with C and C++ to take advantage of a large volume ofexisting C/C++ this article, we will walk through the entire language andshow you how tobecome a C programmer, starting at the IS C?
6 CHAPTER 1. C PROGRAMMINGWhat is C?C is a computerprogramming language. That means that you can use C to createlists of instructions for a computer to follow. C is one of thousands of program-ming languages currently in use. C has been around for several decades and haswon widespread acceptance because it gives programmers maximum control andefficiency. C is an easy language to learn. It is a bit more cryptic in its style thansome other languages, but you get beyond that fairly is what is called acompiled language. This means that once you write your Cprogram, you must run it through aC compilerto turn your program into anexe-cutablethat the computer can run (execute).
7 The C program is the human-readableform, while the executable that comes out of the compiler is the machine-readableand executable form. What this means is that to write and run aC program, youmust have access to a C will start at the beginning with an extremely simple C program and build upfrom there. I will assume that you are using the Linux commandline and gcc asyour environment for these examples; if you are not, all of the code will still workfine you will simply need to understand and use whatever compiler you simplest C program, ILet s start with the simplest possible C program and use it both to understand thebasics of C and the C compilation process.
8 Type the followingprogram into astandard text editor. Then save the program to a file If you , you will probably get some sort of error when you compile it,so makesure you remember Also, make sure that your editor does not automaticallyappend some extra characters (such as .txt) to the name of thefile. Here s the firstprogram:# i n c l u d e < s t d i o . h>i n t main ( void ){2 CHAPTER 1. C PROGRAMMINGSPACING AND INDENTATIONp r i n t f (" This i s o u t p u t from my f i r s t program ! \ n " ) ;r e t u r n 0;}Spacing and indentationWhen you enter this program, position#includeso that the pound sign is in col-umn 1 (the far left side).
9 Otherwise, the spacing and indentation can be any wayyou like it. On some Linux systems, you will find a program calledindent, whichwill format code for you. The spacing and indentation shown above is a goodexample to and runWhen executed, this program instructs the computer to printout the line This isoutput from my first program! then the program quits. You can t get muchsimpler than that!To compile this code on a Linux machine, typegcc samp . c o sampThis line invokes the C compiler called gcc, asks it to compile and asks itto place the executable file it creates under the namesamp.
10 To run the program,type. / sampYou should see the output This is output from my first program! when you runthe you mistype the program, it either will not compile or it will not run. If theprogram does not compile or does not run correctly, edit it again and see whereyou went wrong in your typing. Fix the error and try SIMPLEST C PROGRAM, IICHAPTER 1. C PROGRAMMINGThe simplest C program, IILet s walk through this program and start to see what the different lines are doing:# i n c l u d e < s t d i o . h>i n t main ( void ){p r i n t f (" This i s o u t p u t from my f i r s t program !)}