Sei sulla pagina 1di 133

C Programming

A
S
D

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 Hard disk
processor
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; 4 a
c = a + b; 7 b
printf(“ sum is %d” ,c);
junk c
}
void main()
{
int a = 4,b =7 , c; 4 a
c = a + b; 7 b
printf(“ sum is %d” ,c);
} 11 c
void main()
{
4 a
int a = 4,b =7 , c;
c = a + b; 7 b

printf(“ sum is %d” ,c);


11 c
}

Sum is
void main()
{
int a = 4,b =7 , c; 4 a
c = a + b; 7 b
printf(“ sum is %d” ,c);
} 11 c

Sum is 11
void main()
{
47
4
0 a
int a = 4,b = 7, c;
c = a + b; 11
70 b

printf(“ %d + %d is %d “,a,b,c);
11 c
} junk

4 + 7 = 11
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
}
Every program should have this DISSECTION of
function and only one main a basic C
function. No capital M please program
Function Function braces, simple braces,
body ellipse. This is to hold or receive
begins parameters or arguments.
main() Presently it is holding nothing
{
Function printf(“Welcome to see the sea of C”);
body }
ends printf function
takes/holds necessarily End of an
Only one one argument which is a instruction is
instruction or
statement
string. Starting and signaled by ;
ending by ” ”
#include<stdio.h> printf should have a
prototype ?
main()
{
printf(“\n \tWelcome to see the sea of C”) ;
}}

We can add Statement


Compound escape missing
Statement sequences and
missing format
specifiers in
the string
Welcome to see
the sea of C
See that C is Ecstatic
#include<stdio.h>
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
Let Us C
#include<stdio.h>
void main()
{
printf(“Let Us C”);
}
See Programming in C

#include<stdio.h>
void main()
{
printf(“Let Us \nC”);
}
See Programming in C
Let Us
#include<stdio.h>
void main() C
{
printf(“Let Us \nC”);
}
See Programming in C

#include<stdio.h>
void main()
{
printf(“Le\tt Us C”);
}
See Programming in C
Le t Us C
#include<stdio.h>
void main()
{
printf(“Le\tt 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
Let Us C
#include<stdio.h>
void main()
{
printf(“Let Us C\a”);
}
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> a=a
void main()
{
int a;
printf(“a = a”);
}
Data type followed by The value of a is
variable name 6785
#include<stdio.h> Declaration of
a variable
main()
{
int a;
printf(“\n The value of a is %d “,a);

} format specifier or place


Undefined symbol a
holder and its
Undeclared first use ‘a’
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> a=5
void main()
{
int a;
a = 5;
printf(“a = %d”, a);
}
The value of a is
6
#include<stdio.h> Declaration &
main() Initialization of a
{ variable
int a = 6;
printf(“\n The value of a is %d “,a);

}
See Programming in C: Use of Variables

#include<stdio.h> 5+3= 8
void main()
{
int a=5,b=3,c;
c = a + b;
printf(“%d + %d = %d”,a,b,c);
}
See Programming in C: Use of Variables

#include<stdio.h> 5+3= 8
void main()
{
int a=5,b=3,c;
c = a + b;
printf(“%d + %d = %d”,a,b,c);
}
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()
{ format specifier
int a,b; or place holder
a = 6;
b= 5;
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; 627 a
printf(“%d x 2 = %d”,a,b); 7218 b
}
In memory
See Programming in C: accepting of Variables

#include<stdio.h> Enter value of a


void main()
{
int a,b;
printf(“Enter value of a”);
scanf(“%d”,&a);
b = 2 * a; 627 a
printf(“%d x 2 = %d”,a,b); 7218 b
}
In memory
See Programming in C: accepting of Variables

#include<stdio.h> Enter value of a4


void main()
{
int a,b;
printf(“enter value of a”);
scanf(“%d”,&a);
b = 2 * a; 4 a
printf(“%d x 2 = %d”,a,b); 7218 b
} 4 In memory
See Programming in C: accepting of Variables

#include<stdio.h> Enter value of a4


void main()
{
int a,b;
printf(“enter value of a”);
scanf(“%d”,&a);
b = 2 * a; 4 a
printf(“%d x 2 = %d”,a,b); 8 b
} 4 In memory
See Programming in C: accepting of Variables

#include<stdio.h> Enter value of a4


void main() 4x2=8
{
int a,b;
printf(“enter value of a”);
scanf(“%d”,&a);
b = 2 * a; 4 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:
c = a/b; a % 10 operation
d = c * b; or the last digit of
e = a – d; a
printf(“ e = %d”,e);
}
a 462 b 10 c 46 d 460 e 2
So what is
What is this operation? 659 % 10
void main()
13 % 10
{ 7 % 10
int a=462,b=10,c,d,e; 7%4
c = a/b; 8%4
d = c * b; 5%4
e = a – d; 4%4
printf(“ e = %d”,e); 3%4
1%4
}
a 462 b 10 c 46 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 Enter the value of a to find square 6
1 5681
6 a The square of 6 is 36

2 36
9625 sq #include<stdio.h>
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() 47
4
0 num
{
sum
int num = 47,sum = 0, ld; 11
11
70

while (num > 0) junk


4
7 ld
{
ld = num % 10;
sum = sum + ld;
num = num / 10;
}
printf(“The sum is “+sum);
}
24
576
20
22
23
21 396 86
0
20
63
41
Void main(){ I j sum
int I, j, sum= 0;
for(i=20;i<= 23;i++){ 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
In the memory
Enter the value of a to find square 6
9
25
1 The square of 6 is 36 81
625
5681
a

2 9625
81
625
36 #include<stdio.h>

sq main()
3 5681
2
3
4
1 {
int a,sq,count;
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);
925
6
}

}
In the
Enter the value of a to find square 6
9
1 memory The square of 625 is is
36 62581
5681
a #include<stdio.h>
main()
2 9625
81
625
36 {
sq int a,sq,count;
3 5681
2
3
4
1 for(count=1,icount<=3;count++)
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

Potrebbero piacerti anche