C Programming Tutorial - WordPress

Transcription

C Programming Tutorial

C PROGRAMMING TUTORIALSimply Easy Learning by tutorialspoint.comtutorialspoint.comi

COPYRIGHT & DISCLAIMER NOTICE All the content and graphics on this tutorial are the property of tutorialspoint.com. Any content fromtutorialspoint.com or this tutorial may not be redistributed or reproduced in any way, shape, or form without thewritten 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 ofthe site or its contents including this tutorial. If you discover that the tutorialspoint.com site or this tutorialcontent contains some errors, please contact us at webmaster@tutorialspoint.comii

Table of ContentsC Language Overview . 1Facts about C . 1Why to use C ? . 2C Programs . 2C Environment Setup . 3Text Editor . 3The C Compiler . 3Installation on Unix/Linux . 4Installation on Mac OS . 4Installation on Windows . 4C Program Structure . 5C Hello World Example . 5Compile & Execute C Program . 6C Basic Syntax . 7Tokens in C . 7Semicolons ; . 7Comments . 8Identifiers . 8Keywords . 8Whitespace in C . 9C Data Types . 10Integer Types . 10Floating-Point Types . 11The void Type . 12C Variables . 13Variable Declaration in C .Error! Bookmark not defined.Variable Initialization in C .Error! Bookmark not defined.Lvalues and Rvalues in C . 15C Constants and Literals . 17Integer literals. 17Floating-point literals. 18Character constants. 18iii

String literals. 19Defining Constants . 19The #define Preprocessor . 19The const Keyword . 20C Storage Classes . 22The auto Storage Class . 22The register Storage Class . 22The static Storage Class. 23The extern Storage Class . 24C Operators . 25Arithmetic Operators . 25Relational Operators. 26Logical Operators . 28Bitwise Operators. 29Assignment Operators . 31Misc Operators sizeof & ternary . 33Operators Precedence in C . 33Decision Making in C. 35if statement . 36Syntax . 36Flow Diagram . 36Example . 36if.else statement . 37Syntax . 37Flow Diagram . 38Example . 38The if.else if.else Statement . 39Syntax . 39Example . 39Nested if statements . 40Syntax . 40Example . 40switch statement . 41Syntax . 41Flow Diagram . 42Example . 42Nested switch statements . 43Syntax . 43Example . 43iii

The ? : Operator . 44C Loops. 45while loop in C . 46Syntax . 46Flow Diagram . 46Example . 47for loop in C . 47Syntax . 47Flow Diagram . 48Example . 48do.while loop in C . 49Syntax . 49Flow Diagram . 50Example . 50nested loops in C . 51Syntax . 51Example . 52break statement in C . 53Syntax . 53Flow Diagram . 53Example . 54continue statement in C . 54Syntax . 54Flow Diagram . 55Example . 55goto statement in C . 56Syntax . 56Flow Diagram . 56Example . 57The Infinite Loop . 57C Functions . 59Defining a Function . 59Example . 60Function Declarations . 60Calling a Function . 60Function Arguments . 61Function call by value . 62Function call by reference . 63C Scope Rules . 65iii

Local Variables . 65Global Variables . 66Formal Parameters . 67Initializing Local and Global Variables . 67C Arrays . 69Declaring Arrays . 69Initializing Arrays . 70Accessing Array Elements . 70Multi-dimensional Arrays . 71Two-Dimensional Arrays . 71Initializing Two-Dimensional Arrays. 72Accessing Two-Dimensional Array Elements . 72Passing Arrays as Function Arguments. 73Way-1 . 73Way-2 . 74Way-3.

The C Compiler The source code written in source file is the human readable source for your program. It needs to be "compiled", to turn into machine language so that your CPU can actually execute the program as per instructions given. This C programming language compiler will be used to compile your source code into final