Programming in C: by Vishal Vanaki
Programming in C: by Vishal Vanaki
By Vishal Vanaki
A brief history of c
Programming languages are used to specify, design, and build software systems.. Dennis Ritchie(1969-1973). Standardized by ANSI in 1982. To solve the performance problems of B a new language was created: C declaration of data types definition of data structures
C as a programming language
C has been standardized (ANSI C) and spawned new languages (C++, Stroustrup, 1986) that improve C
loose typing (lots of freedom, error prone) structured (extensive use of functions) C is higher level than assembler
Basics of C programming
All keywords have fixed meanings Basic building blocks for program statements. 32 keywords(in C88) & 32+5 keywords(in C99) Identifiers refer to the names of variables, functions and arrays.
Basic Structure
preprocessor statements global declarations; main( ) { declarations; statements; } User defined function
Constants
(a)
Integer Constants
-sequence of digits. 123
Variables
Rules:
The variables must always begin with a letter ANSI standard recognizes a length of 31 characters Uppercase and lowercase are significant. The variable name should not be a keyword. White space is not allowed.
Declaration of Variables
data_type variable_name; Fundamental types: (primary) Data type Size int 2 float 4 char 1
Format Specifiers
d c f x o
Operators in C
Operators
Relational Operators(==,!=,<,>,<=,>=) Logical Operators( &&, ||, !) Assignment Operators(+,-,*,/,%) Bitwise Operators(&, |,^,~) Increment and Decrement Special Operators Conditional Operator
Increment/Decrement Operators
Increment Operator Prefix: First increment then use it Example: a=10; b=++a; Postfix: first use it then increment Example: a=10; b=a++;
Special operators
sizeof() operator int a,b; b=sizeof(a);
Pecedence
All operators work from left to right Except following: Prefix increment/decrement. Unary plus/minus. Logical not/bitwise complement. typecast (change type). *(Dereference.) &(address) sizeof
Control Statements
Control Statements
If Statement
if ( condition ) { statements; } Example: if(a==1) { a=10; }
If else statement
If (condition) { statement1; } Else { staement2; }
Elseif ladder
If (condition1) { statement1; } elseif(condition2) { staement2; } else { statement3; }
Switch statement
Switch(expression) { case value1: statement 1; break;
case value2:
statement 2; break; default:
statement 3; break;
}
Loops in C
While Loop
while(condition) { statements; }
Do-while Loop
do { statements;
}while(condition);
For Loop
for( initialization ; condition ; Increment/decrement) { statements; } Example: for( ; ; ) { }
Arrays
Arrays
A group of related data items that share a common name. One-dimensional array: data_type array_name[size]; int a[4]; a[0]
a[1] a[2] a[3]
Initialization of 1D array
int a[3]={1,2,3}; Reading of 1D array:
for(i=0;i<3;i++) { scanf(%d,&a[i]); }
Two-dimensional Array
data_type array_name [rows][columns] int a[2][2] a[0][0], a[0][1], a[1][0], a[1][1]
Initialization of 2D array
int a[2][2]={1,2,3,4} Reading of 2D array:
For(i=0;i<2;i++) { for(j=0;j<2;j++) { scanf(%d,&a[i][j]); } }