C Programming Tutorial
Total Page:16
File Type:pdf, Size:1020Kb
C Programming Tutorial C PROGRAMMING TUTORIAL Simply Easy Learning by tutorialspoint.com tutorialspoint.com i COPYRIGHT & DISCLAIMER NOTICE All the content and graphics on this tutorial are the property of tutorialspoint.com. Any content from tutorialspoint.com or this tutorial may not be redistributed or reproduced in any way, shape, or form without the written permission of tutorialspoint.com. Failure to do so is a violation of copyright laws. This tutorial may contain inaccuracies or errors and tutorialspoint provides no guarantee regarding the accuracy of the site or its contents including this tutorial. If you discover that the tutorialspoint.com site or this tutorial content contains some errors, please contact us at [email protected] ii Table of Contents C Language Overview .............................................................. 1 Facts about C ............................................................................................... 1 Why to use C ? ............................................................................................. 2 C Programs .................................................................................................. 2 C Environment Setup ............................................................... 3 Text Editor ................................................................................................... 3 The C Compiler ............................................................................................ 3 Installation on Unix/Linux ............................................................................. 4 Installation on Mac OS .................................................................................. 4 Installation on Windows ............................................................................... 4 C Program Structure ................................................................ 5 C Hello World Example ................................................................................. 5 Compile & Execute C Program ....................................................................... 6 C Basic Syntax ......................................................................... 7 Tokens in C .................................................................................................. 7 Semicolons ; ................................................................................................ 7 Comments ................................................................................................... 8 Identifiers .................................................................................................... 8 Keywords .................................................................................................... 8 Whitespace in C ........................................................................................... 9 C Data Types ......................................................................... 10 Integer Types ............................................................................................. 10 Floating-Point Types ................................................................................... 11 The void Type ............................................................................................ 12 C Variables ............................................................................ 13 Variable Declaration in C ................................ Error! Bookmark not defined. Variable Initialization in C ............................... Error! Bookmark not defined. Lvalues and Rvalues in C ............................................................................. 15 C Constants and Literals ........................................................ 17 Integer literals ............................................................................................ 17 Floating-point literals .................................................................................. 18 Character constants .................................................................................... 18 iii String literals .............................................................................................. 19 Defining Constants ..................................................................................... 19 The #define Preprocessor ...................................................................... 19 The const Keyword ................................................................................. 20 C Storage Classes ................................................................. 22 The auto Storage Class ................................................................................ 22 The register Storage Class ........................................................................... 22 The static Storage Class ............................................................................... 23 The extern Storage Class ............................................................................. 24 C Operators ........................................................................... 25 Arithmetic Operators .................................................................................. 25 Relational Operators................................................................................... 26 Logical Operators ....................................................................................... 28 Bitwise Operators ....................................................................................... 29 Assignment Operators ................................................................................ 31 Misc Operators ↦ sizeof & ternary .............................................................. 33 Operators Precedence in C .......................................................................... 33 Decision Making in C.............................................................. 35 if statement ............................................................................................... 36 Syntax ..................................................................................................... 36 Flow Diagram .......................................................................................... 36 Example .................................................................................................. 36 if...else statement ...................................................................................... 37 Syntax ..................................................................................................... 37 Flow Diagram .......................................................................................... 38 Example .................................................................................................. 38 The if...else if...else Statement ..................................................................... 39 Syntax ..................................................................................................... 39 Example .................................................................................................. 39 Nested if statements .................................................................................. 40 Syntax ..................................................................................................... 40 Example .................................................................................................. 40 switch statement ....................................................................................... 41 Syntax ..................................................................................................... 41 Flow Diagram .......................................................................................... 42 Example .................................................................................................. 42 Nested switch statements ........................................................................... 43 Syntax ..................................................................................................... 43 Example .................................................................................................. 43 iii The ? : Operator ......................................................................................... 44 C Loops .................................................................................. 45 while loop in C ........................................................................................... 46 Syntax ..................................................................................................... 46 Flow Diagram .......................................................................................... 46 Example .................................................................................................. 47 for loop in C ............................................................................................... 47 Syntax ..................................................................................................... 47 Flow Diagram .......................................................................................... 48 Example .................................................................................................. 48 do...while loop in C ..................................................................................... 49 Syntax ..................................................................................................... 49 Flow Diagram .........................................................................................