2. 🞂 Developed by Dennis Richie
🞂 Developed at Bell Lab in U.S.A. in 1972
🞂 Derived from a language known as BCPL
(Basic Combined Programming Language)
Introduction
3. 🞂 Creating Source Code
🞂 Compiling Source Code
🞂 Linking the source code
🞂 Running the executable file
Program Development Cycle
5. 🞂 Documentation Section
🞂 Link Section
🞂 Definition Section
🞂 Global Declaration Section
🞂 Function Section
🞂 Main Function()
🞂 {
🞂 Declaration
🞂 Executable Part
🞂 }
🞂 Sub Program Section
🞂 {
🞂 Function 1()
🞂 …
🞂 Function n ()
🞂 }
Structure of C program
7. 🞂 Character Set
🞂 Upper case and lowercase characters, 0-9
digits, special characters, white spaces
1. Alphabets A-Z, a-z
2. Digits 0-9
3. Special Characters #, @, &, %, _,-
4. White Space Characters blank space, tab,
new line
Variables, Data Types, Operators, Expression
9. Identifier is a user defined name given to a
program element – variable, functions,
symbolic constants
Rules for naming identifier :-
1) Must be sequence of alphabets & digits
2) Must begin with alphabet
3) No special symbol except (_) is allowed
4) Reserve word should not used as identifier
5) C is case sensitive
6) For naming maximum 32 characters are
allowed
Identifiers and Keywords
10. Keywords are reserve words & predefined by language. The
can not be used by programmer in any other than specifie
by syntax. C provides 32 keywords.
Keywords
auto
break
case
char
const
continue
default
do
double
else
enum
extern
float
for
goto
if
int
long
register
return
while
volatile
short
signed
sizeof
static
struct
switch
typedef
union
unsigned
void
_Alignas (C11)
_Alignof (C11)
_Atomic (C11)
_Bool (C99 beyond)
_Complex (C99 beyond)
_Generic (C11)
_Imaginary (C99
beyond)
_Noreturn (C11)
_Static_assert (C11)
_Thread_local (C11)
inline (C99 beyond)
restrict (C99 beyond)
11. Constants refers to fixed value that do not
changes during program execution. They can
be classified as :
1) integer constants
2) floating point constants
3) character constants
4) string constants
Constants
12. int y = 123; //here 123 is a decimal integer constant
int x = 0123; // here 0123 is a octal integer constant
int x = 0x12 // here Ox12 is a Hexa-Decimal integer constant
float x = 6.3; //here 6.3 is a double constant.
float y = 6.3f; //here 6.3f is a float constant.
float z = 6.3 e + 2; //here 6.3 e + 2 is a exponential constant.
float s = 6.3L ; //here 6.3L is a long double constant
char p ='ok' ; // p will hold the value 'O' and k will be omitted
char y ='u'; // y will hold the value 'u'
char k ='34' ; // k will hold the value '3, and '4' will be omitted
char e =' '; // e will hold the value ' ' , a blank space
chars ='45'; // swill hold the value ' ' , a blank space
14. A variable name is an identifier or symbolic name assigned
the memory location where data is stored. A variable ca ha
only one value assigned to it at any given time during
program execution. Its value may change during execution
the program.
Variables
20. Arithmetic Operators
Operator Symbol Action Example
Addition + Adds operands x + y
Subtraction - Subs second from first x - y
Negation - Negates operand -x
Multiplication * Multiplies operands x * y
Division / Divides first by second x / y
(integer quotient)
Modulus % Remainder of divide op x % y
21. Assignment Operator
x=3
– = is an operator
– The value of this expression is 3
– = operator has a side effect -- assign 3 to x
The assignment operator =
– The side-effect is to assign the value of the right hand side (rhs) to the left hand side
(lhs).
– The value is the value of the rhs.
For example:
x = ( y = 3 ) +1; /* y is assigned 3 */
/* the value of (y=3) is 3 */
/* x is assigned 4 */
22. Compound Assignment Operator
Often we use “update” forms of operators
– x=x+1, x=x*2, ...
C offers a short form for this:
– Generic Form
variable op= expr equivalent to variable = variable op expr
– Update forms have value equal to the final value of expr
i.e., x=3; y= (x+=3); /* x and y both get value 6 */
Operator Equivalent to:
x *= y x = x * y
y -= z + 1 y = y - (z + 1)
a /= b a = a / b
x += y x = x + y
y %= 3 y = y % 3
23. Increment and Decrement
Pre Increment [++x]:
the value of a variable is first incremented by 1, and then the
updated value is used in the expression.
Post Increment [x++]:
the variable's original value is used in the expression first, and
then the post-increment operator updates the operand value
by 1.
Pre Decrement [--x]:
the value of a variable is first decremented by 1, and then the
updated value is used in the expression.
Post Decrement [x--]:
the variable's original value is used in the expression first, and
then the post-decrement operator updates the operand value
by substracting 1.
24. Pre Increment and Post Increment
#include <stdio.h>
int main() {
int x = 7;
int y = ++x;
printf("x is %dn", x);
printf("y is %dn", y);
return 0;
}
Output :
x is 8
y is 8
#include <stdio.h>
int main() {
int x = 7;
int y = x++;
printf("x is %dn", x);
printf("y is %dn", y);
return 0;
}
Output :
x is 8
y is 7
25. Pre Decrement and Post Decrement
#include <stdio.h>
int main() {
int x = 7;
int y = --x;
printf("x is %dn", x);
printf("y is %dn", y);
return 0;
}
Output :
x is 6
y is 6
#include <stdio.h>
int main() {
int x = 7;
int y = x--;
printf("x is %dn", x);
printf("y is %dn", y);
return 0;
}
Output :
x is 6
y is 7
26. Relational Operators
Relational operators allow you to compare variables.
– They return a 1 value for true and a 0 for false.
Operator Symbol Example
Equals == x == y NOT x = y
Greater than > x > y
Less than < x < y
Greater/equals >= x >= y
Less than/equals <= x <= y
Not equal != x != y
There is no bool type in C. Instead, C uses:
– 0 as false
– Non-zero integer as true
27. Relational Operators
#include <stdio.h>
int main() {
int p = 5, q = 5, r = 10;
printf(“%d == %d is %d n”, p, r, p == r);
printf(“%d == %d is %d n”, p, q, p == q);
printf(“%d > %d is %d n”, p, r, p > r);
printf(“%d > %d is %d n”, p, q, p > q);
printf(“%d < %d is %d n”, p, r, p < r);
printf(“%d < %d is %d n”, p, q, p < q);
printf(“%d != %d is %d n”, p, r, p != r);
printf(“%d != %d is %d n”, p, q, p != q);
printf(“%d >= %d is %d n”, p, r, p >= r);
printf(“%d >= %d is %d n”, p, q, p >= q);
printf(“%d <= %d is %d n”, p, r, p <= r);
printf(“%d <= %d is %d n”, p, q, p <= q);
return 0;
}
The output generated here would be:
5 == 10 is 0
5 == 5 is 1
5 > 10 is 0
5 > 5 is 0
5 < 10 is 1
5 < 5 is 0
5 != 10 is 1
5 != 5 is 0
5 >= 10 is 0
5 >= 5 is 1
5 <= 10 is 1
5 <= 5 is 1
28. Logical Operators
&& Logical AND Operator
If both operands are non zero then the condition becomes true. Otherwise, the result has a value
of 0.
// C program for Logical AND Operator
#include <stdio.h>
int main()
{
int a = 10, b = 20;
if (a > 0 && b > 0) {
printf("Both values are greater than 0n");
}
else {
printf("Both values are less than 0n");
}
return 0;
}
Output :
X Y X && Y
1 1 1
1 0 0
0 1 0
0 0 0
Both values are greater than 0
29. Logical Operators
|| Logical OR Operator
The condition becomes true if any one of them is non-zero. Otherwise, it returns false i.e., 0 as the
value.
// C program for Logical OR Operator
#include <stdio.h>
int main()
{
int a = -1, b = 20;
if (a > 0 || b > 0) {
printf("Any one of the given value is greater than 0n");
}
else {
printf("Both values are less than 0n");
}
return 0;
}
Output :
X Y X || Y
1 1 1
1 0 1
0 1 1
0 0 0
Any one of the given value is greater than 0
30. Logical Operators
! Logical NOT Operator
If the condition is true then the logical NOT operator will make it false and vice-versa.
// C program for Logical NOT Operator
#include <stdio.h>
int main()
{
int a = 10, b = 20;
if (!(a > 0 && b > 0)) {
// condition returned true but logical NOT operator changed it to false
printf("Both values are greater than 0n");
}
else {
printf("Both values are less than 0n");
}
return 0;
}
Output :
Both values are less than 0
X !X
0 1
1 0
31. Operating on Bits (1)
C allows you to operate on the bit
representations of integer variables.
– Generally called bit-wise operators.
All integers can be thought of in binary form.
– For example, suppose ints have 16-bits
6552010 = 1111 1111 1111 00002 = FFF016 = 1777608
In C, hexadecimal literals begin with 0x, and octal
literals begin with 0.
x=65520; base 10
x=0xfff0; base 16 (hex)
x=0177760; base 8 (octal)
32. Operating on Bits (2)
Bitwise operators
The shift operator:
– x << n
Shifts the bits in x to n positions to the left, shifting in zeros
on the right.
If x = 1111 1111 1111 00002
x << 1 equals 1111 1111 1110 00002
– x >> n
Shifts the bits in x to n positions right.
– shifts in the sign if it is a signed integer (arithmetic shift)
– shifts in 0 if it is an unsigned integer
x >> 1 is 0111 1111 1111 10002 (unsigned)
x >> 1 is 1111 1111 1111 10002 (signed)
33. Operating on Bits (3)
Bitwise logical operations
– Work on all integer types
& Bitwise AND
x= 0xFFF0
y= 0x002F
x&y= 0x0020
| Bitwise Inclusive OR
x|y= 0xFFFF
^ Bitwise Exclusive OR
x^y= 0xFFDF
~ The complement operator
~ y= 0xFFD0
– Complements all of the bits of X
X Y X ^ Y
1 1 0
1 0 1
0 1 1
0 0 0
34. Shift, Multiplication and Division
Multiplication and division is often slower than shift.
Multiplying 2 can be replaced by shifting 1 bit to the left.
n = 10
printf(“%d = %d” , n*2, n<<1);
printf(“%d = %d”, n*4, n<<2);
……
Division by 2 can be replace by shifting 1 bit to the right.
n = 10
printf(“%d = %d” , n/2, n>>1);
printf(“%d = %d”, n/4, n>>2);
35. Operator Precedence and Associativity
Operator precedence
– It determines the order in which different operators are evaluated in an expression.
– This is important because it can change the outcome of an expression if not
understood correctly.
Operator associativity
– It determines the order in which operators of the same precedence are evaluated when
they appear in a sequence without parentheses.
– There are two types of associativity: left-to-right and right-to-left.
36. Operator Precedence
Operator Precedence level
( ) 1
~, ++, --, unary - 2
*, /, % 3
+, - 4
<<, >> 5
<, <=, >, >= 6
==, != 7
& 8
^ 9
| 10
&& 11
|| 12
=, +=, -=, etc. 14
We’ll be adding more to this list later on...
39. Operator Precedence example
#include <stdio.h>
int main()
{
int x = 5;
int y = 3;
int result = x+++y
printf("%d %d %d",x,y,result);
return 0;
}
OUTPUT:
6 3 8
#include <stdio.h>
int main()
{
int x = 5;
int y = 3;
int result = ++x+y
printf("%d %d %d",x,y,result);
return 0;
}
OUTPUT:
6 3 9
40. An Example
What is the difference between the two lines of output?
#include <stdio.h>
int main ()
{
int w=10,x=20,y=30,z=40;
int temp1, temp2;
temp1 = x * x /++y + z / y;
printf ("temp1= %d;nw= %d;nx= %d;ny= %d;nz= %dn",
temp1, w,x,y,z);
y=30;
temp2 = x * x /y++ + z / y;
printf ("temp2= %d;nw= %d;nx= %d;ny= %d;nz= %dn",
temp2, w,x,y,z);
return 0;
}
41. Conditional Operator
The conditional operator essentially allows you to embed an “if” statement into an
expression
Generic Form
exp1 ? exp2 : exp3 if exp1 is true (non-zero)
value is exp2
(exp3 is not evaluated)
if exp1 is false (0),
value is exp3
(exp2 is not evaluated)
Example:
z = (x > y) ? x : y;
This is equivalent to:
if (x > y)
z = x;
else
z = y;
42. Comma Operator
An expression can be composed of multiple
subexpressions separated by commas.
– Subexpressions are evaluated left to right.
– The entire expression evaluates to the value of the
rightmost subexpression.
Example:
x = (a++, b++);
a is incremented
b is assigned to x
b is incremented
– Parenthesis are required because the comma operator has
a lower precedence than the assignment operator!
The comma operator is often used in for loops.
43. Comma Operator and For Loop
Example:
int i, sum;
for (i=0,sum=0;i<100;i++){
sum += i;
}
printf(“1+...+100 = %d”, sum);
56. 🞂 Input means to provide the program with some data to be
used in the program and Output means to display data on the
screen or write the data to a printer or a file.
🞂 The C programming language provides standard library
functions to read any given input and to display data on the
console.
Input/Output Statement in C
57. The functions used for standard input and output are
present in the stdio.h header file. Hence to use the
functions we need to include the stdio.h header file
#include<stdio.h>
58. Following are the functions used for standard input and
output:
printf() function - Show Output
scanf() function - Take Input
getchar() and putchar() function
gets() and puts() function
59. 🞂 The printf() function is the most used function in the C
language. This function is defined in the stdio.h header
file and is used to show output on the console (standard
output).
🞂 This function is used to print a simple text
sentence or value of any variable which can be
of int, char, float, or any other datatype.
printf() function
60. 🞂 To print values of different datatypes using
the printf() statement, we need to use format
specifiers, like we have used in the code examples
above.
🞂 Also, when we take input from user using
the scanf() function, then also, we have to specify
what type of input to expect from the user using
these format specifiers.
Format Specifiers
61. Datatype Format Specifier
int %d, %i
char %c
float %f
double %lf
short int %hd
unsigned int %u
long int %li
unsigned long int %lu
signed char %c
unsigned char %c
long double %Lf
62. 🞂 When we want to take input from the user, we use
the scanf() function. When we take input from the user, we store the
input value into a variable.
🞂 The scanf() function can be used to take any datatype input from user,
all we have to take care is that the variable in which we store the value
has the same datatype.
scanf()
63. 🞂 The getchar() function reads a character from the terminal
and returns it as an integer. This function reads only a sin
character at a time.
🞂 The putchar() function displays the character passed to it o
the screen and returns the same character. This function t
displays only a single character at a time.
🞂 In case you want to display more than one character, use
putchar() method in a loop.
getchar() & putchar()
64. 🞂 The gets() function reads a line from stdin(standard
input) into the buffer pointed to by str pointer, until
either a terminating newline or EOF (end of file)
occurs.
🞂 The puts() function writes the string str and a trailing
newline to stdout.
gets() & puts()