This PPT contains A to Z topics of C programming language. It makes learners to find it easy to learn C. Tutors also can use the PPT for better presentation.
2. History of C
• C is a procedural lang (or) structure oriented language, used
to develop different kinds of app’s.
• ALGOL-1960-International committees.
• CPL-1963-Cambridge University
• BCPL-1967- Martin Richards
• B-1970-Ken Thomson-for UNIX OS
• C-1972-dennis ritchie-Bell Labs.
3. Features of C
• Simple , general purpose language
• Modularity
• Speed
• Flexibility
• Extendability
• Rich set of operators
4. Uses of C
C can be used to develop,
•database systems
•word processors
•spread sheets
•operating systems
•compilers and assemblers etc.
5. Structure of a C program
1. Documentation section
2. Link Section
3. Definition Section
4. Global declaration section
5. Function prototype declaration section
6. Main function
7. User defined function definition section
6. 1 #include <stdio.h> //This is a preprocessor command that includes standard input
//output header file(stdio.h) from the C library
2 void main() //This is the main function from where execution of any C program begins.
3 { //This indicates the beginning of the main function.
4 printf(“Hello_World! “); //Statements terminated by semicolon.
5 } //This indicates the end of the main function.
7. First C program
#include <stdio.h>
void main()
{
/* Our first simple C basic program */
printf(“Hello World! “);
}
8. How to execute a C program
In DOSBOX:
Write the program.
Press alt+f9 to compile the program.
Press ctrl+f9 to execute the program.
9. Internal process of program
execution
Source code
compiler
Machine code/ Object
Linker
executable code
10. C Tokens
• Constants
• Variables
• Keywords
• Identifiers
• Data types
• Operators
• Characters strings
• Special symbols
11. Constants
• A constant is a quantity that doesn’t change.
• A constant can be declared using the keyword 'const'.
• Syntax: const data_type variable_name;
eg: const int x;
12. Types of C constants
1. Integer constants. eg: 5,344, -32,232 etc
2. Real or Floating point constants. eg: 190.1221, 878.56767876 etc
3. Octal & Hexadecimal constants. eg: 016, 0X45
4. Character constants. eg: 'w', 'D' etc.
5. String constants. eg: “hello”,”good”
6. Backslash character constants. eg: 'n','t' etc.
13. Variables
• Name given to a location in memory where any constant is
stored.
• A Variable should be declared before use.
• Syntax: datatype variable_name;
eg: int a; // 2 bytes of memory is
// set aside in memory
Ex: 3X + 2Y = 20
Where 3, 2, 20 can’t change, they are called constants, where as the
quantities X & Y can vary or change , hence are called variables.
14. Keywords
• Also called reserved words.
• Words whose meaning has already been
explained to the C compiler.
• 1)auto 2)break 3)case 4)char 5)const
6)continue 7)default 8)do 9)double 10)else
11)enum 12) extem 13)float 14)for 15)goto
16)if 17)int 18)long 19)register 20)return
21)short 22)signed 23)size of 24)static 25)struct
26)switch 27)typedef 28)union 29) unsigned 30)
void 31)volatile 32)while
15. Identifiers
• Each program elements in a C program are given a name
called identifiers.
• Names given to identify Variables, functions and arrays are
examples for identifiers.
eg: a, x, swap(), m[] etc.
16. Rules for constructing identifier
names in C:
1. First character should be an alphabet or
underscore.
2. Succeeding characters might be digits or letter.
3. Punctuation and special characters aren’t
allowed except underscore.
4. Identifiers should not be keywords.
17. DATA TYPES
•C data types are defined as the data storage format that a
variable can store a data to perform a specific operation.
•Data types are used to define a variable before their use in a
program.
•Size of a variable, a constant and an array are determined by
data types.
18. C-Datatypes
There are four kinds of datatypes in C.
Sl.
no
kind examples
1 PRIMITIVE DATATYPES int, float, double, char
2 DERIVED DATATYPES array, pointer, enum
3 USER DEFINED DATATYPES structure, union
4 VOID void
19. int
• Int datatype is used to store numeric values.
• Keyword is int.
• Its size is 2 bytes.
• Its range is –32,767 to 32,767.
• Declaration and initialization:
int n=12333;
20. float
• Float datatype allows to store decimal point number.
• Keyword is float.
• Its size is 4 bytes.
• Precision is 6.
• Declaration and initialization:
float a=12.223233;
21. double
• Double datatype is used to store a decimal point number .
• Keyword is double.
• Its size is 8 bytes.
• Precision is 10 digits.
• Declaration and initialization:
double a=123.121224;
22. char
• Char datatype is used to store character type of data.
• Keyword is char.
• Its size is 1 byte.
• Declaration and initialization:
char a=‘A’.
24. S.No C Data types storage Size Range
1 Char 1 –127 to 127
2 Int 2 –32,767 to 32,767
3 Float 4 1E–37 to 1E+37 with six digits of precision
4 Double 8 1E–37 to 1E+37 with ten digits of precision
5 long double 10 1E–37 to 1E+37 with ten digits of precision
6 long int 4 –2,147,483,647 to 2,147,483,647
7 short int 2 –32,767 to 32,767
8 unsigned short int 2 0 to 65,535
9 signed short int 2 –32,767 to 32,767
10 long long int 8 –(2power(63) –1) to 2(power)63 –1
11 signed long int 4 –2,147,483,647 to 2,147,483,647
12 unsigned long int 4 0 to 4,294,967,295
13 unsigned long long int 8 2(power)64 –1
25. Operators
• An operator is a symbol , used to perform certain operations
on the operands.
• Eg: int a=10;
int b=20;
int c;
c=a + b;
a and b are called as operands and ‘+’ is the operator.
26. Types of operators
• Arithmetic operators
• Relational operators
• Logical operators
• Assignment operators
• Increment and decrement operators
• Conditional operator
• Bitwise operatorsS
• Special operators
28. Relational operators
OPERATOR MEANING
> Is greater than
< Is less than
<= Is less than or equal to
>= Is greater than or equal to
== Is equal to
!= Is not equal to
29. logical operators
• Logical operators help us to combine two or more relational
expressions . Ex:- (A>B)&&(A>C)
OPERATOR MEANING
&& AND
|| OR
! NOT
30. Assignment operators
• These are used to assign the result of an expression to a
variable.
Operators Example Explanation
Simple assignment operator = sum=10 10 is assigned to variable sum
Compound assignment
operators
+= sum+=10 This_is_same_as_sum=sum+10…………
-= sum-=10 This is same as sum = sum-10
*= sum*=10 This is same as sum = sum*10
/+ sum/=10 This is same as sum = sum/10
%= sum%=10 This is same as sum = sum%10
&= sum&=10 This is same as sum = sum&10
^= sum^=10 This is same as sum = sum^10
32. ...continued
• Prefix:- First, the variable value is increment or
decrement by one then the expression is
evaluated using the new value of the variable.
Syntax: ++var_name;
- -var_name;
• Postfix:- First, the expression is evaluated then
the variable value is increment or decrement by
one.
Syntax: var_name++;
var_name – -;
33. Difference between pre/post increment &
decrement operators in C
S.
no
Operator
type
Operator Description
1
Pre
increment
++i Value of i is incremented before assigning it to
variable i.
2
Post-
increment
i++ Value of i is incremented after assigning it to
variable i.
3
Pre
decrement
– –i Value of i is decremented before assigning it to
variable i.
4
Post_decrem
ent
i– – Value of i is decremented after assigning it to
variable i.
34. Conditional operator
Also called as ternary operator.
Syntax:
Condition ? Expression1 : Expression2
Operator meaning
? : If then
35. Bitwise operator
• Bitwise operators operate upon bits.
OPERATOR MEANING
& Bitwise AND
| Bitwise OR
^ XOR
<< Shift left
>> Shift right
36. Special operators
S.no Operators Description
1 &
This is used to get the address of the variable.
Example : &a will give address of a.
2 *
This is used as pointer to a variable.
Example : * a where, * is pointer to the variable a.
3 Sizeof ()
This gives the size of the variable.
Example : size of (char) will give us 1.
37. Control structures
• Instructions are executed one by one. That is called as
sequential flow of control.
• Control structures alter this flow of control.
• 3 kinds.
• Branching or conditional statements.
• Looping or iteration statements
• Jumping statements
38. Branching statements
• 5 kinds.
• if statement
• if.. else statement
• If..else..if ladder
• Nested if ..else statement
• Switch case statement
39. Simple if statement
• If statement tests a condition , if it is true then a set a
statements are executed.
• Syntax: if(condition)
{
// statement1;
//statement2;
.
.
//statementn;
}
40. if.. else statement
• If statement tests a condition ,if it is true it executes a set of
statements, if it is false then another set of statements.
• Syntax: if(condition)
{
// if part statements;
}
else
{
//else part statements;
}
41. if ..else..if ladder
• syntax: if(condition)
{
// if part statements;
}
else if(condition)
{
//else part statements;
}
else
{
//else part
}
42. Nested if else
• syntax: if(condition)
{
if(condition)
{
// if part statements;
}
else
{
//else part statements;
}
}
else
{
//else part statements;
}
Inner if else stmt
45. For loop
• Syntax:
for(initialization ; test ; increment/decrement)
{
//statements;
}
Steps in execution of for loop
1.Initialization of the loop counter.
2.Condition is tested.
3.If the test is true then loop statements are executed.
4.When the loop ends the loop counter is incremented
5.Again the condition is tested , if it is true, the loop is executed, if it is
false , the loop exits.
49. Goto statement
• Goto statement allows you to jump from one place to another
in a program, unconditionally.
• Syntax: goto labelname;
.
.
labelname: statements;
50. Break statement
• Break statement is used to
• break out of loops
• break out of switch statement
• Syntax: break;
• Break when used in loops , takes you to the end of the loop.
51. Continue statement
• Continue statement allows us to continue the loop, bypassing
some of the statements of the loop.
• Continue when used in loop, takes you to the beginning of the
loop.
• Syntax: continue;
52. Return statement
• Return statement allow us to return a value from a function.
• Syntax: return value;
53. Arrays
• An Array is a collection of similar datatypes.
• An array elements are allocated memory in contigous memory
locations.
• An array is denoted by a pair of square brackets ‘[ ]’.
• Array index starts with zero.
54. ..continued
• Array declaration:
• Syntax: datatype array_variable_name[size];
• Eg : int arr[5]; //array of type int and it
//allocates space for 5 elements.
float arr1[10]; // an array of type float and
//space for 10 float elements.
55. ..continued
• Array initialization: 3 ways
• An element at once
• An array can be initialized when it is declared
• Can be initialized from the keyboard
56. ..continued
1. An element at once :
int arr[5] ;
arr[0]=1;
arr[1]=2;
arr[2]=34;
arr[3]=4;
arr[4]=55;
arr
1
554342
57. ..continued
2. Declaration and initialization:
Initialization can be done at the time of declaration using
flower brackets.
eg : int arr[3]= {1,2,3};
arr
1 2 3
58. ..continued
3. From the keyboard using for loop and scanf() function.
eg : int a[10], i;
printf(“enter values into an”);
for(i=0;i<10;i++)
{
scanf(“%d”,&a[i]);
}
59. Functions
• A function is a sub program, which performs a
particular task when called.
• A C program is a collection of some functions.
• main() is the predefined function from where
the execution of a program starts.
• C program = Main() + user defined functions
60. ..continued
• Uses of functions:
• Re-useability
• Modularity
• Re-usability: C functions are used to avoid rewriting same code again and
again in a program.
• We can call functions any number of times in a program and from any place
in a program for the same task to be performed.
• Modularity: Dividing a big task into small pieces improves understandability
of very large C programs.
• A large C program can easily be tracked when it is divided into functions.
61. Defining a Function
• Syntax:
function definition function declaration
return_type function_name(List of parameters)
{
//set of statements
function body
}
62. ..continued
• Function definition – This contains all the statements to be executed.
Function definition = function declaration +function body
A function body is a group of statements enclosed in
flower brackets.
eg: void main() function declaration
{
int a=10; function definition
function body
printf(“a = %d”,a);
}
63. Function terminology
• Function prototype
• Function definition
• Function call
Function prototype or declaration - This informs compiler about
the function name, function parameters and return value’s data
type.
Syntax: return_type function_name(parameter list);
This is written before main() function. To inform the
compiler that there exists a function in the program.
64. Function Call
• This statement actually calls the function.
• Syntax: function_name( list of parameter values);
• This statement is written in the calling function.
• With this statement, the control is transferred to the function
definition.
• Eg: sum(10,20);
65. Function calls
• Functions can be defined in any one of the following 4
different ways.
No arguments , No return value .
No arguments , With return value .
With arguments , No return value .
With arguments , With return value .
• Arguments:-
These values are passed from main() function to the invoked
function . Generally these values are called as inputs.
• Return value:-
These values are passed from one function to another
function while invoking function. Generally these values are
called as outputs.
66. Ex:- Program to print the sum of given two numbers
using functions?
i)No arguments , No return value
void add ( ) ; //function prototype
void main()
{
add(); //function calling
}
void add() //function definition
{
int a,b,c;
printf(“enter any numbers”);
scanf(“%d%d”, &a , &b) ;
c = a + b ;
printf(“n sum is=%d ” , c ) ;
}
67. ii)No arguments , With return value
int add ( ) ; //function prototype
void main()
{
printf(“nsum is=%d”,add()); // function call
}
int add() ; //function definition
{
int a,b,c;
printf(“enter any numbers”);
scanf(“%d%d”, &a , &b) ;
c = a + b ;
return c ;
}
68. iii)With arguments , No return value
void add ( int , int ) ; //function prototype
void main()
{
int a , b ;
printf(“enter any two numbers”);
scanf(“%d%d”,&a,&b);
add(a,b); //function call
}
void add ( int x , int y ) //function definition
{
int z = x + y ;
printf(“sum is=%d”,z) ; }
69. iv)With arguments , With return value
int add ( int , int ) ; //function prototype
void main()
{
int a , b ;
printf(“enter any two numbers”);
scanf(“%d%d”,&a,&b);
printf(“sum is=%d”,add(a,b)); //function call
}
int add ( int x , int y ) //function definition
{
int z = x + y ;
return z ; }
70. Function calls
• There are two ways that a C function can be called from
a program. They are,
• Call by value
• Call by reference
Editor's Notes
The amount of memory space to be allocated for a variable is derived by modifiers.
Modifiers are prefixed with basic data types to modify (either increase or decrease) the amount of storage space allocated to a variable.
For example, storage space for int data type is 4 byte for 32 bit processor. We can increase the range by using long int which is 8 byte. We can decrease the range by using short int which is 2 byte.
#include &lt;stdio.h&gt;int main(){int a=40,b=20, add,sub,mul,div,mod; add = a+b;sub = a-b;mul = a*b;div = a/b;mod = a%b; printf(&quot;Addition of a, b is : %d\n&quot;, add);printf(&quot;Subtraction of a, b is : %d\n&quot;, sub);printf(&quot;Multiplication of a, b is : %d\n&quot;, mul);printf(&quot;Division of a, b is : %d\n&quot;, div);printf(&quot;Modulus of a, b is : %d\n&quot;, mod);} Output:
Addition of a, b is : 60Subtraction of a, b is : 20Multiplication of a, b is : 800Division of a, b is : 2Modulus of a, b is : 0
#include &lt;stdio.h&gt;
int main(void)
{
int a,b;
a=10; b=20;
printf(&quot;Relational Operators output \n&quot;);
printf(&quot;a &lt; b is : %d \n&quot;,a&lt;b);
printf(&quot;a &lt;= b is : %d \n&quot;,a&lt;=b);
printf(&quot;a == b is : %d \n&quot;,a==b);
printf(&quot;a != b is : %d \n&quot;,a!=b);
printf(&quot;a &gt; b is : %d \n&quot;,a&gt;b);
printf(&quot;a &gt;= b is : %d \n&quot;,a&gt;=b);
return 0;
}
Output :
Relational Operators outputa &lt; b is : 1a &lt;= b is : 1a == b is : 0a != b is : 1a &gt; b is : 0a &gt;= b is : 0
Program to show Prefix and Postfix operators on int value and char value
#include&lt;stdio.h&gt;
void main()
{
int x=5;
char ch=&apos;a&apos;;
printf(&quot;postfix is %d\n&quot;,x++);
printf(&quot;prefix is %d\n&quot;,++x);
printf(&quot;\n\n&quot;);
printf(&quot;postfix is %c\n&quot;,ch++);
printf(&quot;prefix is %c\n&quot;,++ch);
}
If the condition is true, expression1 will be executed. If it is false, then expression2 is executed.