Part 1

advertisement
C Programming
A
S
D
F
K
L
;
Computer system
Monitor
CPU
Key Board mouse
Parts of a computer
Processor, memory, hard disk, input, output.
In memory both the program lines and variables
are kept
Action is taking place in processor and results
are stored there in the memory.
Even the output is given in the output window
and flipped back to the edit window
Computer system
Monitor
CPU
Key Board mouse
Computer system
memory
Monitor
processor
Hard disk
CPU
Key Board mouse
Steps in executing a code
Write the code (source code)
Compile the source code
If errors go back to edit and correct and
repeat compiling
If no errors, the compiler has generated
the machine (object code)
Run the (object) code and so get the results
void main()
{
int a = 4,b =7 , c;
c = a + b;
printf(“ sum is %d” ,c);
}
4
a
7
b
junk
c
void main()
{
int a = 4,b =7 , c;
c = a + b;
printf(“ sum is %d” ,c);
}
4
a
7
b
11 c
void main()
{
int a = 4,b =7 , c;
c = a + b;
printf(“ sum is %d” ,c);
}
Sum is
4
a
7
b
11
c
void main()
{
int a = 4,b =7 , c;
c = a + b;
printf(“ sum is %d” ,c);
}
Sum is 11
4
a
7
b
11 c
void main()
{
int a = 4,b = 7, c;
c = a + b;
printf(“ %d + %d is %d “,a,b,c);
}
4 + 7 = 11
47
4
0
a
11
70
b
11
junk
c
What is programming?
Making the computer work as we want it to
work
Getting things done by the computer
We write the program in the memory and
make the processor work it out
What is programming?
Programming is making the computer work
as we want it to. ie. we will have to talk to a
machine. Some syntax is there. Only a few
syntaxes to be byehearted.
A series of instructions to the processor, so
that it can do the entire work, without any
difficulty. Instructions in a tape recorder or
MP3 player to make milk tea!
How do we make it work?
We keep a program in memory and ask the
processor to execute line by line
It is like a boy following the instructions
given to it, systematically and quickly, but
without any common sense or mind reading
capability. Fast unlike us, obedient unlike us
and having no common sense like us?!
Vehicle v/s program
If a new vehicle,
draw,
expert opinion,
(----- )finally build and run.
But this expert (compiler) will build it!
So if there is a modification, show to
expert/(compile) and only then run
The compiler is like an expert
Compiler is the expert who/which tells
whether the syntax is correct! If not, we are to
correct and again submit. Finally build and test
run. Compiler will compile too!
Compiler is also without common sense at
times. Logical errors!
Compiler will not correct logical errors. So to
know what is happening, print the results, in
between or check the values of variables.
In Input and output mechanism
If we are to see the result, project them onto the
screen (printf)
%d is a place holder in that order
But the result screen will flip back to edit and so
get it back by alt f5
order to get values from the key board, scanf
Variables and memory address
All the variables to be used must be declared
(and so given locations). The dissection of a frog!
The memory address is the uniquely identifying
method.
In a college,
register number,
in a town,
the address,
The operations possible
Different operators, + - * / but % is also there!
Integer on integer will give only an integer.
Some suggestions
Meaningful variable names and function names
Do proper indentation
Results printed should be made meaningful
Do hand tracing
Draw flow charts!
Feel free to ask doubts, loud clear and smart
Free to ask. Loud and smart
Please learn blind typing at the earliest
Note that there are two types of slashes in KB
Use the home and end keys wherever required
Make your own changes and see whether the
expected results are obtained
Neither copy nor mug up
Do a hand tracing of the programs for
understanding on the one side and to find out
the mistakes on the other side
Spend some time to plan the program using
algorithm or flow chart
Be sure that it is logic and no magic.
Avoid printing results inside the loops
Hard code first and then go for soft coding
Get the addresses from the computer and
draw a memory map
There are different slashes, \n, \t.... // /* and
*/
The operations possible
Different operators, + - * / but % is also there!
Integer on integer will give only an integer.
The operator precedence issues.
5+2 * 3 = 11 v/s (5+2) * 3 = 21
Right to left and left to right 2/4*3 v/s 2*3/4
X+1 % 5 v/s (x+1) % 5;
See Programming in C
#include<stdio.h>// header file inclusion
void main()
{
printf(“Let Us C”);
}
See Programming in C
#include<stdio.h>
void main()//a must for any C program to work
{
printf(“Let Us C”);
}
See Programming in C
#include<stdio.h>
void main()//see the function braces
{
printf(“Let Us C”);
}
See Programming in C
#include<stdio.h>
void main()//see the function braces
// there is a space between void and main
// main can not be capital! ie. C is case sensitive
{
printf(“Let Us C”);
}
See Programming in C
#include<stdio.h>
void main()//function header
{
printf(“Let Us C”);
}
See Programming in C
#include<stdio.h>
void main()//function header
{
printf(“Let Us C”);
}
If there is a head, there shall be a
body after/below the head
See Programming in C
#include<stdio.h>
void main()//function header
{// body begins
printf(“Let Us C”);
}// body ends
If there is a head, there shall be a
body after/below the head
See Programming in C
#include<stdio.h>
void main()//function header
{
printf(“Let Us C”);// end of an instruction
}
DISSECTION of
Every program should have this
a basic C
function and only one main
program
function. No capital M please
Function braces, simple braces,
Function
ellipse. This is to hold or receive
body
parameters or arguments.
begins
Presently it is holding nothing
main()
Function
body
ends
Only one
instruction or
statement
{
printf(“Welcome to see the sea of C”);
}
printf function
takes/holds necessarily
one argument which is a
string. Starting and
ending by ”
”
End of an
instruction is
signaled by
;
#include<stdio.h>
printf should have a
prototype ?
main()
{
printf(“\n \tWelcome to see the sea of C”) ;
}}
Compound
Statement
missing
We can add
escape
sequences and
format
specifiers in
the string
Statement
missing
#include<stdio.h>
Welcome to see
the sea of C
See that C is Ecstatic
main()
{
printf(“\n \tWelcome to see the sea of C”);
printf(“\n See that C is Ecstatic” );
}
Un-terminated string or
character constant
See Programming in C
#include<stdio.h>
void main()
{
printf(“Let Us C”);
}
See Programming in C
#include<stdio.h>
void main()
{
printf(“Let Us C”);
}
Let Us C
See Programming in C
#include<stdio.h>
void main()
{
printf(“Let Us \nC”);
}
See Programming in C
#include<stdio.h>
void main()
{
printf(“Let Us \nC”);
}
Let Us
C
See Programming in C
#include<stdio.h>
void main()
{
printf(“Le\tt Us C”);
}
See Programming in C
#include<stdio.h>
void main()
{
printf(“Le\tt Us C”);
}
Le
t Us C
See Programming in C
#include<stdio.h>
void main()
{
printf(“Let Us C\bK”);
}
See Programming in C
Let Us K
#include<stdio.h>
void main()
{
printf(“Let Us C\bK”);
}
See Programming in C
#include<stdio.h>
void main()
{
printf(“Let Us C\rG”);
}
See Programming in C
Get Us C
#include<stdio.h>
void main()
{
printf(“Let Us C\rG”);
}
See Programming in C
#include<stdio.h>
void main()
{
printf(“Let Us C\a”);
}
See Programming in C
#include<stdio.h>
void main()
{
printf(“Let Us C\a”);
}
Let Us C
See Programming in C: Use of Variables
#include<stdio.h>
void main()
{
a = 5;
printf(“a = a”);
}
See Programming in C: Use of Variables
#include<stdio.h>
void main()
{
a = 5;
printf(“a = a”);
}
Undefined symbol ‘a’ in main
See Programming in C: Use of Variables
#include<stdio.h>
void main()
{
int a;
//integer: used for counting
printf(“a = a”);
}
Whatever you use should be brought in
beforehand! The dissection of a frog, or brisk
physics practical and instruments are damaged!
See Programming in C: Use of Variables
#include<stdio.h>
void main()
{
int a;
printf(“a = a”);
}
a=a
Data type followed by
variable name
#include<stdio.h>
The value of a is
6785
Declaration of
a variable
main()
{
int a;
printf(“\n The value of a is %d “,a);
}
Undefined
symbol a
Undeclared first use ‘a’
format specifier or place
holder and its
corresponding expression
See Programming in C: Use of Variables
#include<stdio.h>
void main()
{
int a;
//variable declaration
a = 5;
// initialisatioin
printf(“a = %d”,a);
}
See Programming in C: Use of Variables
#include<stdio.h>
void main()
{
int a;
a = 5;
printf(“a = %d”, a);
}
a=5
The value of a is
6
#include<stdio.h>
Declaration &
Initialization of a
variable
main()
{
int a = 6;
printf(“\n The value of a is %d “,a);
}
See Programming in C: Use of Variables
#include<stdio.h>
5+3=
void main()
{
int a=5,b=3,c;
c = a + b;
printf(“%d + %d = %d”,a,b,c);
}
8
See Programming in C: Use of Variables
#include<stdio.h>
5+3=
void main()
{
int a=5,b=3,c;
c = a + b;
printf(“%d + %d = %d”,a,b,c);
}
8
Place holders in printf statement and the
variables listed after the string sit in the
place holders in the same order
The product 6 * 5 = 30
#include<stdio.h>
main()
{
int a,b;
a = 6;
b= 5;
format specifier
or place holder
printf(“\n The product
}
%d * %d = %d “,a, b , a*b);
#include<stdio.h>
The prod of 6 and
5 is 30
main()
{
int a=6,b=5;
printf(“\n The prod of %d and %d is %d = %d”, .
a, b , a*b);
Variable declaration and
initialization can be done
}
together for multiple vars
#include<stdio.h>
The prod of 6 and
5 is 30
main()
{
int a=6,b=5;
printf(“\n The prod of %d and %d is %d = %d”, .
a, b , a*b);
Variable declaration and
initialization can be done
}
together for multiple vars
But what is the big deal if we know the values in
advance? Hard coded values! Why not soft code?
Accept from KB during the run time?
See Programming in C: accepting of Variables
#include<stdio.h>
void main()
{
int a,b;
printf(“enter value of a”);
scanf(“%d”,&a);
b = 2 * a;
printf(“%d x 2 = %d”,a,b);
}
See Programming in C: accepting of Variables
#include<stdio.h>
void main()
{
int a,b;
printf(“enter value of a”);
scanf(“%d”,&a);
b = 2 * a;
printf(“%d x 2 = %d”,a,b);
}
627
a
7218 b
In memory
See Programming in C: accepting of Variables
Enter value of a
#include<stdio.h>
void main()
{
int a,b;
printf(“Enter value of a”);
scanf(“%d”,&a);
627 a
b = 2 * a;
printf(“%d x 2 = %d”,a,b);
7218 b
}
In memory
See Programming in C: accepting of Variables
Enter value of a4
#include<stdio.h>
void main()
{
int a,b;
printf(“enter value of a”);
scanf(“%d”,&a);
4
a
b = 2 * a;
printf(“%d x 2 = %d”,a,b);
7218 b
}
4
In memory
See Programming in C: accepting of Variables
Enter value of a4
#include<stdio.h>
void main()
{
int a,b;
printf(“enter value of a”);
scanf(“%d”,&a);
4
a
b = 2 * a;
printf(“%d x 2 = %d”,a,b);
8
b
}
4
In memory
See Programming in C: accepting of Variables
Enter value of a4
#include<stdio.h>
4x2=8
void main()
{
int a,b;
printf(“enter value of a”);
scanf(“%d”,&a);
4
a
b = 2 * a;
printf(“%d x 2 = %d”,a,b);
8
b
}
4
In memory
What is this operation?
void main()
{
int a=462,b=10,c,d,e;
c = a/b;
d = c * b;
e = a – d;
printf(“ e = %d”,e);
}
a 462
b 10
c 856
d -672
e 0
What is this operation?
void main()
{
int a=462,b=10,c,d,e;
c = a/b;
d = c * b;
e = a – d;
printf(“ e = %d”,e);
}
a 462
b 10
c 46
d -672
e 0
What is this operation?
void main()
{
int a=462,b=10,c,d,e;
c = a/b;
d = c * b;
e = a – d;
printf(“ e = %d”,e);
}
a 462
b 10
c 46
d 460
e 0
What is this operation?
void main()
{
int a=462,b=10,c,d,e;
c = a/b;
d = c * b;
e = a – d;
printf(“ e = %d”,e);
}
a 462
b 10
c 46
d 460
e 2
What is this operation?
void main()
{
int a=462,b=10,c,d,e;
c = a/b;
d = c * b;
e = a – d;
printf(“ e = %d”,e);
}
a 462
b 10
c 46
d 460
e 2
What is this operation?
void main()
did a mod
{
int a=462,b=10,c,d,e; operation:
a % 10 operation
c = a/b;
or the last digit of
d = c * b;
a
e = a – d;
printf(“ e = %d”,e);
}
a 462
b 10
c 46
d 460
e 2
What is this operation?
void main()
{
int a=462,b=10,c,d,e;
c = a/b;
d = c * b;
e = a – d;
printf(“ e = %d”,e);
}
a 462
b 10
c 46
So what is
659 % 10
13 % 10
7 % 10
7%4
8%4
5%4
4%4
3%4
1%4
d 460
e 2
did an a % 10 operation or the last digit of a
#include<stdio.h>
The square of 6 is
main()
36
{
We want the values on
int a;
the run, during execution,
printf(“Enter a num ”);ie from the keyboard !
scanf(“%d”,&a);
printf(“\n The square of %d is %d “,a, a*a);
}
Lest one be blinking,
better give a printf
statement before scanf.
#include<stdio.h>
The square of 6 is 36
main()
We want the values on
{
the run, during execution,
int a;
ie from the keyboard !
printf(“Enter a num ”);
scanf(“%d”,&a);
printf(“\n The square of %d is %d “,a, a*a);
}
Lest one be blinking, better give a
printf statement before scanf.
#include<stdio.h>
The square of 6 is 36
main()
{
int a;
printf(“Enter the value of to find square ”);
scanf(“%d”,&a);
printf(“\n The square of %d is %d “,a, a*a);
We want the values on
}
the run, during
execution, ie from the
Lest one be blinking,
keyboard !
better give a printf
statement before scanf.
In the memory
1 5681
6
a
Enter the value of a to find square 6
The square of 6 is 36
9625 sq
#include<stdio.h>
2 36
main()
{
int a,sq;
printf(“\n Enter the value of a to find square ”);
scanf(“%d”,&a);
sq= a * a;
printf(“\n The square of %d is %d “,a, sq);
6
}
Common error messages
Statement missing
check the semicolon in the previous line
Compound statement missing
check whether the closing curly brace is missing
Declaration not allowed
check whether the opening curly brace is missing
Un-terminated string or character constant
check whether the string which is started is ended or
not: any closing double quote missing
Undefined symbol ‘x’
check whether the variable is declared or not
Undefined symbol ‘xyz’,’pqr’,’apwzyd’….
Check whether a string starting is missing or not
L value expected
check whether some constant is being modified
Connot covert int * to int
check whether an integer pointer value is being
assigned to an integer variable
Too few parameters in the function call
check the shortage of parameters in the calling of a
function, whereas the function definition or prototype
has more parameters
Too many parameters in the function call
check the additional parameters in the calling of a
function, whereas the function definition or prototype
has lesser parameters
The sum is
void main()
{
int num = 47,sum = 0, ld;
while (num > 0)
{
ld = num % 10;
sum = sum + ld;
num = num / 10;
}
printf(“The sum is “+sum);
}
47
4
0
num
11
70
11
sum
junk
4
7
ld
Void main(){
24
576
20
22
23
21
I
396
j
86
0
20
63
41
sum
int I, j, sum= 0;
The sum is 20
and I is 20
sum = sum + i;
The sum is 41
printf(“\n the sum is %d and I is 21
and i is %d “,sum,i);
The sum is 63
and I is 22
The sum is 86
and I is 23
for(i=20;i<= 23;i++){
.
}
}
In the memory
Enter the value of a to find square
The square of 6 is 36 81
625
1
5681
6
9
25
a
2
9625
81
625
36
#include<stdio.h>
sq
3
main()
{
5681
2
3
4
1
int a,sq,count;
for(count=1,count<=3;count++)
{
printf(“\n Enter the value to square ”);
scanf(“%d”,&a);
sq= a * a;
printf(“\n The square of %d is %d “,a, sq);
}
count
925
6
}
In the
1 memory
5681
a
2 9625
81
625
36
sq
3 5681
2
3
4
1
count
Enter the value of a to find square
81
The square of 625
is is
36 625
6
9
#include<stdio.h>
main()
{
int a,sq,count;
for(count=1,icount<=3;count++)
{
printf(“\n Enter the value to square ”);
scanf(“%d”,&a);
sq= a * a;
printf(“\n The square of %d is %d “,a, sq);
}
925
6
}
Problems to be done
Swapping of two variables
Swapping/rotation among n variables
If statement syntax. Only one side
check pass/even
absolute value
If else statement syntax. both sides
pass or fail/odd or even
biggest among two vars
sort 2 variables
senior/junior
last digit is odd/even
last but one digit odd/even
divisible by 7 or not
Nested if else
3 var biggest
3 var sort
grade s, a, b,c…
incentive upto and then on…
special pass criterion
roots of a quadratic equation
sides of a triangle
For loop
With serial numbers
The dynamics of for loop
Printing all the numbers from 1 to 50
Summing up all numbers from 1 to 50
Accepting variables n times and processing them
21 matchstick game
Printing a single star
Printing a line of stars
Printing the digits of a number
Summing up the digits of a number
Convert a number to binary
Check whether a number is palindrome
Check whether a number is sliding down or climbing up
Check whether a given number is prime or not
Check whether a number is Armstrong
Perfect number
Single digit sum number or not
Cup number
Grove number
Platau number
Mountain number
Interchangeability of loops
a statement 10 times
Nested loops
Printing patterns
Dynamic screen or dance on the screen
Generation of numbers
Prime, Armstrong, perfect, ninean,
eighteen...
Cup, mountain, palindrome
Slide numbers, climb up numbers
Oddeven sum equal
Alter sum equal
Triple sum equal
Generation of deadly sevens. Either divisible
by 7 or at least one digit is 7
Arrays
Declare
Initialize
Print all
Sum all
Modify all
Select on basis
Print function
Sort
Bubble
Selection
Insertion
Palarray
Print in reverse order
Reverse array and print
Find out the sum of digits in the array
Arrays and addresses
Pointer concept. Using a pointer, access another var,
change it, accept to it
Pointer arithmetic
Double pointer issues. Proper pictorial representations
Printing an array, given any address of the nth element
in array
Functions
Using built in functions with no arguments clrscr();
Using built in functions with arguments. Printf(“ ab”);
Using built in functions with varying arguments
printf(“%d %d “,a,b)
User defined functions, without arguments
User defined functions with arguments
User defined functions return vals
User defined functions with pass by reference
arguments
Two d arrays
Declaration and initialisation
Printing td arrays
Function to print/use td arrays
16 puzzle
Addresses in two D arrays
Sending any row’s address to a function
Diagonal elements
Major/ minor
Upper major/upper minor
Transpose
Addition
multiplication
Border elements
Inner elements
Steering rotation
Inner steering rotation
Check symmetric
Creation of magic square
Sum of rows/cols in a wrapper row/col
Check quadian matrix if every subsquare matrix
sum are equal
1
9
11
3
8
16
14
6
5
13
15
7
4
12
10
2
Reflections of td arrays
Rotations of td arrays
The cofactor of a matrix
Storage classes in C
Strings
Definition
Declaration
Initilisatioin
Searching
Sorting
Two arrays and strings
Malloc and pointer array of storing strings
Operations
Substring
Length
No. of vowels
No of words
Concatenation
Palindrome
Reversing a string
Change case of string
Upper/lower/toggle
Sorting of strings
Structure
Declaration
Initialisation
Copying
Printing values
Accepting values
By variable
By pointer
Array of structure
Array in a structure
Structure within a structure
Unions
Memory allocation
Pre-processor directives
Download