learning C language
programming in c
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
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
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
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
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
Fibonacci Series ....................................................................................... 129
Variable Arguments .............................................................. 130
Memory Management .......................................................... 132
Allocating Memory Dynamically ................................................................. 132
Resizing and Releasing Memory ................................................................. 133
Command Line Arguments ................................................... 135
Click here for Download PDF / FREE
Programming in c
0 commentaires: