Complete Information till 2D arrays. In this slides you can also find information about loops and control decision....
Best slides for beginners who wants to learn about C programming language..
2. 1972, AT&T Bell’s laboratories by Denis and Ritchie.
Alphabets Keywords
Digits Constants Statements Program
Symbols Variables
Alphabets :- A,B,C,….,Z.
Digits :- 1,2,3,4….9.
Symbols :- ! # $ % ^ & * ( ) _ + : ; ’ , . Etc.
Keywords:- these are reserve words in compiler.
Constants :-
a. Primary Constants:- Integer : without fractional part/whole numbers.
Real : with both fractional and integer parts.
Character: digit/alphabets/symbol enclosed in ‘ ’ .
b. Secondary Constants:- Arrays, Pointers, Structures Etc. 2
3. For Integer Constants:-
a. It must have one digit.
b. May be positive as well as negative.(by default positive)
c. Range : (underflow) -32768 to +32767 (overflow)
For Real Constants:-
a. May be positive as well as negative.(by default positive)
b. It must have one digit.
c. Range : -3.4 X 1038 to +3.4 X 1038 .
For Character Constants:-
a. Maximum length of character constant is 1.
3
4. Variables means, whose value varies and can be changed
easily.
Variable is a named memory location.
Rules for Constructing Variables:
a. Variable have maximum length of 31 characters.
b. Variable name must start with alphabet or underscore.
c. No space, commas or special symbols are allowed.
d. Keyword(reserve words) can’t be used as variable name.
Examples:
Valid Invalid
AbCd A bC D
A44o_ @12f
Age printf 4
5. Keywords means reserve words.
There are about 32 keywords in Dev compiler
Examples:
Auto break if else
While for continue int
Float char scanf printf
dowhile Etc.
5
6. For Programing there are some important tips Like;
Every statement end with semicolon. (;)
There must be execution order.
Use of Pre-processor Directories. Like main()….
6
7. Generally we used three types of Datatypes
that is: integer float and character.
Integer:-
short type reserve 2 bytes in memory location. 4 bytes in Dev compiler.
(-32768 to +32767)
Long type reserve 4 bytes in memory location
( -2147483648 to +2147483647)
Character:- written in single comas(‘ A ’),it reserves 1 byte in memory
location .
Float:-
Simple 1.7 X 10-38 to 1.7X 10 38 reserve 4 byte in memory location
Double 2.3 X 10-308 to 1.7X 10 308 reserve 8 byte in memory location
Long Double 2.4 X 10-4932 to 1.1X 10 4932 reserve 4 byte in memory location
7
8. Variable Declaration Statement.
Use to declare variable in C.
Assignment Statements.
It may be variable initialization statement and also
updation.
Compound Assignment Statements.
Using single variable, same values are assigned.
Variable Initialization Statements.
initially variable’s value like : num1=10;
Variable Increment Decrement Statement.
pre inc / dec : first increase/decrease then execute.
post inc / dec : first execute then increase/decrease.
8
9. Group of operands and operators are called Expressions.
Operands can be constant or variable or other
expressions.
Logical Expressions:
Athematic : + - * / ** % Etc.
Logical : AND (&&) OR (||) NOT (!)
Relational Expressions: for comparing <, =, >, >=,
==.
9
10. There are two type of type conversion:
Implicit :- Automatically convert by compiler.
integer integer = integer
integer float = float
float float = float
Explicit :- According to our wish.
a=int (4.5)
a=4.
10
11. Input :- scanf( ) => float/integer/string.
gets() => string.
Output :- printf() => integer/float/string.
puts() => string.
printf(“format string ” , list of arguments)
/t for tab space.
/d for integer
/s for string
/b for bell
/n for new line.
11
12. In C language Program is written according to proper
syntax:
Pre-processors directories( #include )
Main function ( main() )
Body of the program ( { …… } )
Simple Sum Program
#include<stdio.h>
main()
{
int num1=5,num2=7,sum;
sum=num1+num2;
printf(“SUM OF 2 NUMBER IS = %d “ , sum);
} 12
13. Decision control structure is actually the divide and
conquer approach.
If Condition:-
If is a conditional statement whose body execute when
the condition is true.
Syntax:-
if(condition/relational expr.)
{ body }
Example:
if(10<=11)
printf(“ YES ”);
13
14. If-Else Statement:-
Like if condition if else is used that if the if condition is not
true then what would done…. So we use if else condition.
Syntax:-
if(condition/relational expr.)
{ body }
else{ body }
Example:-
if(10<=11)
printf(“ True ”);
else
printf(“ False ”); 14
15. If else If statement:-
If else if is used when we have multiple conditions. Like if
first condition is not true then one more condition is
applied.
Syntax:-
if(condition/relational expr.)
{ body }
Else if(condition/relational expr.)
{ body }
Example:-
if(11<11)
printf(“ True ”);
Else if(10!=11)
printf(“ Not Equal ”);
15
16. Nested If:-
Nested if statements are used for condition with nested
condition.
Syntax:-
if(condition/relational expr.)
{ if(condition/relational expr.)
{ body }}
Example:-
if(10<=10)
{ if(1==1)
printf(“11 is not Equal to 10”);
printf(“10 is equal to 10”);
} 16
17. Conditional Operators:-
Conditional Operators works same as done by if else
statement that if the if condition is not true then what
would done…. So we use conditional operators.
Syntax:-
Condition ? exp1 : exp2; ALSO
!(condition) ? Exp1 : exp2;
Example:-
A>B ? (A>C ? Printf(“A “) : printf(“C”) ) : (B<C ?
Printf(“B”): printf (“C”) ) ;
Suppose A= 1, B=2, C=3 then
Output:- C
17
18. Switch Statement:-
In computer programming languages, a switch statement is a type of
selection control mechanism used to allow the value of a variable or
expression to change the control flow of program execution via a
multiway branch.
Syntax:
switch ( test ) {
case 0:
Statement/s;
break;
case 1:
Statement/s;
break;
default :
Statement/s;
}
18
19. Repeats a statement or group of statements while a
given condition is true. It tests the condition before
executing the loop body.
Initialization
Condition/ Termination criteria.
Updation / Incrementation.
19
20. C provides us three loops.
While Loop
Do While loop
For loop
20
21. This loop execute until the given condition is true.
In order words, the body of loop execute until the
given condition is true.
Syntax:
while(condition)
{
statement(s);
increment/decrement;
}
21
22. Example:
Let us considered the following program
main()
{
int i=0 , j ;
while(i<=9)
{
j=i;
printf(“nj=%d”,j);
I++;
}
}
22
23. We use this loop when we think the body of loop execute
one time without checking the given condition.
Syntax:
do
{
statement(s);
}while(condition);
Here the keyword do allows the control to enter into the
body of loop without checking the condition.
The condition will checked after the execution of the body
of the loop.
23
25. A for loop is a repetition control structure that
allows you to efficiently write a loop that needs to
execute a specific number of times.
Syntax:
for( init; condition; increment/decrement)
{
statement(s);
}
25
27. o Contiguous memory location.
o Same data type.
o Random Access
If we need to store marks of 100 students, instead
of using 100 variables we simply use array.
Syntax:
type Array_name[Size_of_Array];
Example:
int a[10];
27
32. A function is a self-contained block of statements
that perform a coherent task of some kind.
Every C program begins with a function called
main() function.
Parts:
Function Prototype
Function Call
Function Definition
32
33. Function Prototype
A function prototype is a declaration of a
function that specifies the function's name and
type signature (arity, parameter types, and return
type).
Semicolon should be implemented at the end.
Syntax:
type function_name ( type arg1, type arg2...... );
33
34. Function Call:
In this part we call function in another function to
perform specific task.
Syntax:
F=mark();
Example:
main()
{
mark();
}
34
35. Function Definition:
In function definition, we specify which task will
performed by this function.
Example:
void Mark()
{
printf(“This is a function”):
}
35
36. Return Statement:
It is used to return control and value to the
location from where the function is call.
It returns only one value.
Example:
int marks(int x ,int y)
{
int sum= (x+y) ;
return sum;
}
36
37. Call by value:
The call by value method of passing arguments to a
function copies the actual value of an argument into
the formal parameter of the function.
Example:
int marks(int x ,int y)
{
int sum= (x+y) ;
return sum;
}
37
38. Call by Reference:
The call by reference method of passing
arguments to a function copies the address of an
argument into the formal parameter. We have use
pointers in order to copy address of variable. We
will discuss pointers in next topic.
38
39. Pointers:
Pointers are used to stored address of variables.
How to declare it?
int *y;
y=&x;
Example:
main()
{
int x=3,*y;
y=&x;
printf(“%d”,*y);
}
39
40. Pointer to a Pointer:
It contains address of pointer.
How to declare it?
int **k;
40
42. Structures:
Arrays allow to define type of variables that can hold
several data items of the same kind.
Similarly structure is another user defined data type
available in C that allows to combine data items of
different kinds.
Structures are used to represent a record. Suppose you
want to keep track of your books in a library. You
might want to track the following attributes about each
book −
Title
Author
Subject
Book ID
42
Course Content:
Variables
Datatypes
Keywords
Conditional Operators
Conditional Statements
a . if
b . if- el if
c . Nested if
Switch Statements
Loops
a . For loop
b . While loop
c . Do-while loop
d . Nested loop
Arrays
a . 1-D arrays
b . 2-D arrays
Functions
Structures