1. Trang chủ
  2. » Công Nghệ Thông Tin

C Programming Tutorial doc

145 2,4K 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 145
Dung lượng 2,82 MB

Nội dung

C Programming Tutorial i C PROGRAMMING TUTORIAL Simply Easy Learning by tutorialspoint.com tutorialspoint.com ii C O P Y R I G H T & D I S C L A I M E R N O T I C E  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 webmaster@tutorialspoint.com iii 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 13 Variable Initialization in C 14 Lvalues and Rvalues in C 15 C Constants and Literals 16 Integer literals 16 Floating-point literals 17 Character constants 17 iii String literals 18 Defining Constants 18 The #define Preprocessor 18 The const Keyword 19 C Storage Classes 21 The auto Storage Class 21 The register Storage Class 21 The static Storage Class 22 The extern Storage Class 23 C Operators 24 Arithmetic Operators 24 Relational Operators 25 Logical Operators 27 Bitwise Operators 28 Assignment Operators 30 Misc Operators ↦ sizeof & ternary 32 Operators Precedence in C 32 Decision Making in C 34 if statement 35 Syntax 35 Flow Diagram 35 Example 35 if else statement 36 Syntax 36 Flow Diagram 37 Example 37 The if else if else Statement 38 Syntax 38 Example 38 Nested if statements 39 Syntax 39 Example 39 switch statement 40 Syntax 40 Flow Diagram 41 Example 41 Nested switch statements 42 Syntax 42 Example 42 iii The ? : Operator 43 C Loops 44 while loop in C 45 Syntax 45 Flow Diagram 45 Example 46 for loop in C 46 Syntax 46 Flow Diagram 47 Example 47 do while loop in C 48 Syntax 48 Flow Diagram 49 Example 49 nested loops in C 50 Syntax 50 Example 51 break statement in C 52 Syntax 52 Flow Diagram 52 Example 53 continue statement in C 53 Syntax 53 Flow Diagram 54 Example 54 goto statement in C 55 Syntax 55 Flow Diagram 55 Example 56 The Infinite Loop 56 C Functions 58 Defining a Function 58 Example 59 Function Declarations 59 Calling a Function 60 Function Arguments 61 Function call by value 61 Function call by reference 62 C Scope Rules 64 iii Local Variables 64 Global Variables 65 Formal Parameters 66 Initializing Local and Global Variables 66 C Arrays 68 Declaring Arrays 68 Initializing Arrays 69 Accessing Array Elements 69 Multi-dimensional Arrays 70 Two-Dimensional Arrays 70 Initializing Two-Dimensional Arrays 71 Accessing Two-Dimensional Array Elements 71 Passing Arrays as Function Arguments 72 Way-1 72 Way-2 73 Way-3 73 Example 73 Return array from function 74 Pointer to an Array 76 C Pointers 78 What Are Pointers? 79 How to use Pointers? 79 NULL Pointers in C 80 Pointer arithmetic 80 Incrementing a Pointer 81 Decrementing a Pointer 82 Pointer Comparisons 82 Array of pointers 83 Pointer to Pointer 85 Passing pointers to functions 86 Return pointer from functions 87 C Strings 90 C Structures 93 Defining a Structure 93 Accessing Structure Members 94 Structures as Function Arguments 95 Pointers to Structures 96 C Unions 99 Defining a Union 99 iii Accessing Union Members 100 Bit Fields 102 Bit Field Declaration 103 Typedef 105 typedef vs #define 106 Input & Output 107 The Standard Files 107 The getchar() & putchar() functions 107 The gets() & puts() functions 108 The scanf() and printf() functions 109 File I/O 110 Opening Files 110 Closing a File 111 Writing a File 111 Reading a File 112 Binary I/O Functions 113 Preprocessors 114 Preprocessors Examples 114 Predefined Macros 115 Preprocessor Operators 116 Macro Continuation (\) 116 Stringize (#) 116 Token Pasting (##) 117 The defined() Operator 117 Parameterized Macros 118 Header Files 119 Include Syntax 119 Include Operation 120 Once-Only Headers 120 Computed Includes 121 Type Casting 122 Integer Promotion 123 Usual Arithmetic Conversion 123 Error Handling 125 The errno, perror() and strerror() 125 Divide by zero errors 126 Program Exit Status 127 Recursion 128 Number Factorial 128 iii Fibonacci Series 129 Variable Arguments 130 Memory Management 132 Allocating Memory Dynamically 132 Resizing and Releasing Memory 133 Command Line Arguments 135 TUTORIALS POINT Simply Easy Learning Page 1 C Language Overview This chapter describes the basic detail about C programming language, how it emerged, what are strengths of C and why we should use C. T he C programming language is a general purpose high level language that was originally developed by Dennis M. Ritchie to develop the Unix operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972. In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of C, now known as the K&R standard. The UNIX operating system, the C compiler, and essentially all UNIX applications programs have been written in C. The C has now become a widely used professional language for various reasons.  Easy to learn  Structured language  It produces efficient programs.  It can handle low-level activities.  It can be compiled on a variety of computer platforms. Facts about CC was invented to write an operating system called UNIX.  C is a successor of B language which was introduced around 1970  The language was formalized in 1988 by the American National Standard Institute. (ANSI).  The UNIX OS was totally written in C By 1973. CHAPTER 1 [...]... Illegal: incomplete exponent */ Illegal: no decimal or exponent */ Illegal: missing integer or fraction */ Character constants Character literals are enclosed in single quotes e.g., 'x' and can be stored in a simple variable of char type A character literal can be a plain character (e.g., 'x'), an escape sequence (e.g., '\t'), or a universal character (e.g., '\u0 2C0 ') There are certain characters in C when... 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 executable program I assume you have basic knowledge about a programming language compiler... Learning Page 4 3 CHAPTER C Program Structure Let’s look into Hello World example using C Programming Language B efore we study basic building blocks of the C programming language, let us look a bare minimum C program structure so that we can take it as a reference in upcoming chapters C Hello World Example A C program basically consists of the following parts:  Preprocessor Commands  Functions  Variables... when they are proceeded by a back slash they will have special meaning and they are used to represent like newline (\n) or tab (\t) Here you have a list of some of such escape sequence codes: Escape sequence Meaning \\ \ character \' ' character \" " character \? ? character \a Alert or bell \b Backspace \f Form feed \n Newline \r Carriage return \t Horizontal tab \v Vertical tab \ooo Octal number of... printf("Line c = a / b; printf("Line c = a % b; printf("Line c = a++; printf("Line c = a ; printf("Line 1 - Value of c is %d\n", c ); 2 - Value of c is %d\n", c ); 3 - Value of c is %d\n", c ); 4 - Value of c is %d\n", c ); 5 - Value of c is %d\n", c ); 6 - Value of c is %d\n", c ); 7 - Value of c is %d\n", c ); } When you compile and execute the above program it produces following result: Line 1 - Value of c. .. compiler Most frequently used and free available compiler is GNU C/ C++ compiler, otherwise you can have compilers either from HP or Solaris if you have respective Operating Systems Following section guides you on how to install GNU C/ C++ compiler on various OS I'm mentioning C/ C++ together because GNU gcc compiler works for both C and C+ + programming languages TUTORIALS POINT Simply Easy Learning Page 3... 6 4 CHAPTER C Basic Syntax This chapter will give detail about all the basic syntax about C programming language including tokens, keywords, identifiers etc Y ou have seen a basic structure of C program, so it will be easy to understand other basic building blocks of the C programming language Tokens in C A C program consists of various tokens and a token is either a keyword, an identifier, a constant,... it each time it comes into and goes out of scope Therefore, making local variables static allows them to maintain their values between function calls The static modifier may also be applied to global variables When this is done, it causes that variable's scope to be restricted to the file in which it is declared In C programming, when static is used on a class data member, it causes only one copy of... all objects of its class #include /* function declaration */ void func(void); static int count = 5; /* global variable */ main() { while(count ) { func(); } return 0; } /* function definition */ void func( void ) { static int i = 5; /* local static variable */ i++; printf("i is %d and count is %d\n", i, count); } You may not understand this example at this time because I have used function... various other type of variables which we will cover in subsequent chapters like Enumeration, Pointer, Array, Structure, Union etc For this chapter, let us study only basic variable types Variable Declaration in C All variables must be declared before we use them in C program, although certain declarations can be made implicitly by content A declaration specifies a type, and contains a list of one or more . C Programming Tutorial i C PROGRAMMING TUTORIAL Simply Easy Learning by tutorialspoint.com tutorialspoint.com ii C O P. copyright laws. This tutorial may contain inaccuracies or errors and tutorialspoint provides no guarantee regarding the accuracy of the site or its contents

Ngày đăng: 11/03/2014, 08:20

TỪ KHÓA LIÊN QUAN

w