The programming language C was developed in the 1970s by Dennis Ritchie at AT & T Bell Labs
C is a Middle level language
C is a Structured Language
C is Highly Portable
Designed for Compilation
2. C Fundamentals
2.1. Program Structure
/*Program to add 2 numbers*/ // Comments
#include <stdio.h> // Pre-Processor Directives
#include “add.h”
Int sum(int a,int b) // Function Definition/Prototype
Int main() // Main Function
{
Int a,b; // Variable Declaration
Int Res = 0 ; //Variable Declaration + Initialization
Printf(“Enter 2 Numbers:”);
Scanf(“%d%d”,&a,&b);
Res =sum( a,b); // Function Call
Printf(“\nThe Sum = %d”,Res);
Return 0; //Returns to Main Function
}
Int Res(int x,int y) // Function Definition
{
Return(x+y); //Return to Function res
}
2.2. Character Set
Alphabets
A-Z , a-z
26 Upper + 26 lower Case
Digits
0 - 9
10 Digits
Special Symbols
!@#$%^&*..
29 Special Characters
White Spaces
Space, Horizontal/vertical Tab, newline
5 white spaces
Escape Sequence
\n - Newline
\b - Baskspace
\r - Carriage Return
\t - Horizontal Tab
\v - Vertical Tab
Trigraph Sequence
‘??=’ for #
2.3. Identifiers
Identifiers are names of Variables, functions, macros, types etc .
Rules :
a) First Letter of an identifier should be a letter
b) Can use only _ in special character.
c) They are Case_Sensitive.
d) only first 31 character of an identifier is significant
e) Should not use Keywords as they are reserved
2.4. Keywords
Keywords are words that have special meaning to the compiler. 32 in number as defned in c89 Eg:
Auto enum restrict unsigned
break extern return void
case float short volatile
char for signed while
const goto sizeof _Bool(*)
continue if static _Complex(*)
default inline(*) struct _Imaginary(*)
do int switch union
double long typedef else register
2.5. Constants
Constants can be Primary or Secondary
Primary Constants can be (Integer Constants, Real Constants, Character Constants )
a) Integer Constant Can be represented as follows
(Base-10) Decimal Constants(Begins without a 0) eg: 1934
(Base-8) OctalConstants(Begins with 0) eg: 012
(Base-16) HexadecimalConstants(Begins with 0X/0x) eg: 0x12A/0x12a
b) Real Constants
eg: 2.35, -3.42, 0.128, 3.12e7, -0.25E24
c) Character Constant (Single digit/letter/symbol in single quotes)
eg: ‘A’, ‘e’ , ‘5’, ‘$’
Secondary Constants are (Array, Pointer, Enumerator, Union….)
2.6. Variables
Variables are names given to memory location. Rules are similar to identifier.
2.7. Comments
Comments are used to give readability/ understanding to the program eg:
// This is a single line comment <- used for single line comment
/* This is a block
or multiline comment */ <- Multiline comment
2.8. Expressions and Operators
An expression is the combination of operators and operands. Operators are of the following type. It can be classified as number of operands used
Unary Operator: One Operand only Required eg: -5, &a
Binary Operator: Two Operands required eg: a+b, c%2
Ternary Operator: Three Operands required ?: eg (a>2)?c= 1:c=2;
They can also be classified as follows
2.8.1. Arithmetic Operators
Used for arithmetic operations
+ (Addition) 2+5 = 7
- (Subtraction) 8-3 =5
* (Multiplication) 4*3 = 12
/ (Division) 7/2 = 3 (int) , 7/2 = 3.5 (Float) [Quotient]
% (Modulus) 7/2 = 1 , 6/3=0 [Remainder]
Precedence of operators are as follows BODMAS(Brackets of Division Multiplication Additon Subtraction)
2.8.2. Assignment Operators
Used to assign some value to lvalue (can be simple and compound)
c = x Simple Assignment Assign the value x to c
c+=x Compound Assignment c = c+x (Instead of + we can use -,* etc)
2.8.3. Relational Operators
Used for comparison which yields 1 if true and 0 if false eg:
< Less Than
> Greater Than
<= Less Than OR Equal to
>= Greater Than OR Equal to
!= Not Equal to
== Equivalent
2.8.4. Logical Operators
The Logical operators can be used to combine the results of several comparison expressions into one logical expression.
OPERATOR
|
SYMBOL
|
EXPRESSION
|
RESULT
|
AND
|
&&
|
x &&y
|
0 if x or y is 0
|
OR
|
||
|
x||y
|
1 if x or y is 1
|
NOT
|
!
|
!x
|
1 if x=0, 0 if x=1
|
The Left side of logical operators are always evaluated first.
2.8.5. Bitwise Operators
Used for bitwise operation
And x & y 1, if both x & y are 1
Or x | y 1,If any x or y are 1
Xor x ^ y 1, if x and y are different
Not ~ x 1,if x =0, 0 , if x=1
Shift Left x << y Each bit in x is shifted y positions to the left (Multiplication)
Shift Right x>>y Each bit in x is shifted y positions to the right (Division)
2.8.6. Memory Access Operators
& Address of &x
* Indirection *x
[ ] Array Element x[i]
. Member of a
Structure/Union s.x x Member of structure s
Structure/Union s->x x Member of structure s (When s is a pointer to a structure)
2.8.7. Other Operators
( ) Function call
(type) Cast
(size of) Size in Bytes
?: Conditional Evaluation
, Sequence Operator
3. Data Types
3.1. Predefined Data Types
Data Types define how much space does a variable take in memory, the range of data it can handle and the type of data. The Predefined Data types are as follows
Integer (int) – Used to store integer type data (15,80…). Storage dependent on the microprocessor user (16 Bit, 32 Bit).
Character(char) – Single character stored. 1 Byte
Floating Point (float) – Decimal Numbers (12.23) . use #include<float.h> . 4 Bytes
Double Precision floating point (double)- 8 Bytes
Void – Used for functions that does not
1) Take in any value
2) Does not return any value