Programming in C in 7 Days - Sarmady
Programming in C in 7 Days - Sarmady
"
• “Programming in C in 7 days!” includes only the first 7 lessons of the more complete e-book “Quickly
Learn Programming in C”. You can obtain the more complete e-book on Learnem.com website.
• Support for the free e-book “Programming in C in 7 days!” is provided on Learnem.com discussion
boards.
QUICK START WITH C ................................................................................................................. 3
GETTING INPUT, ARRAYS, CHARACTER STRINGS AND PREPROCESSORS ....................................12
OPERATORS, LOOPS, TYPE CONVERSION ..................................................................................18
CONDITIONAL COMMAND EXECUTION .....................................................................................24
SELECTION USING SWITCH STATEMENTS ..................................................................................30
MORE ON FUNCTIONS ..............................................................................................................38
FUNCTION ARGUMENTS ...........................................................................................................44
STRUCTURES .............................................................................................................................50
WORKING WITH CHARACTER STRINGS ......................................................................................54
MORE STRING FUNCTIONS ........................................................................................................60
FILES..........................................................................................................................................66
MORE ON FILES .........................................................................................................................71
RANDOM ACCESS TO FILES ........................................................................................................77
DYNAMIC MEMORY ALLOCATION .............................................................................................81
PROGRAM ARGUMENTS AND RANDOM NUMBERS ..................................................................88
%
! "
# # $# $ # # # % &' (
)
* $ $ ( + , # - +./0 # %
1 # # - $ # 2 $ # $ ( $
- # %$ - # $ -%
% $ $ $ % % 3 - $ # 45 6 % 75 3 7 $
1 # $ - # - $ # $
% $
8$ 9 / - % % - % -
# 6 % $ ( # 51 ( - 6 $
- % $ - $ # # #
2 $ $ - % # 1
• : + 4 4 #
• 8 ( ); # 1 -% %
#
• < $ ) 1 ##= 1 # 1 !/) " 1 #
# # 1 / % $ <3 :6= <: = #
$ $ - $
# $
? % - #
#include <stdio.h>
main()
{
printf("Hello World!\n");
system("pause"); //this line is only needed under windows
}
2 #- $ # 4 - ( 1 $ $ -
# 4 - @ 1 $ 4 - % $ 4
8$ # $ 8$ - # # 4 ( - $
/- 4 # + 4> 4 # # 1 $ %$ $
( A 6 %
$ ' ()% (
8 # # + 4 # $
$ cc test.c
4 #
$ gcc test.c
8$ 4 ( - - 8 $ 4 ( #
$./a.out
# ##
Hello World!
8 1 #5 # $ # $ 1 - %( % # /- $ 1
# ( % % $ # # ( ## -
& * ( $ <. % B A =- 2 / $ ## % %
- # A !2 & &" , < ,## =5 < A = <3 -
= $ - ##
&
2/:+* & & %# A $ )1
,% %% # - # 1 $ # A 6 $ #
- $# A 1 $ - ( 4 % $ $ $ ! % 5 1 % $ $ " ,# A
- # - / $ % A $1 - # A ( (
# A $1 1 $ - -
2/:+* & 1 # A $ )1
C )1 $ # A # - - )1 $ -
# # % $ $ - < = @ $ $ $ $ -
4 # & & !2 &C & D" ,- $ $ 5# < 1 2 =( 8$ % 1 $
## $ $ - < =- %$ 1 - # - @ $ %
1 $ - % $ $ 4 # ( ! $ " 1
-
2/:+* & C . %# A $ )1 #
.
D < # * =( ! # 2' " 8$ % # $ $ 4 (
- $ # !2 & E" 8 $ # % %# # % %
2/:+* & E ; # % % $ % $ - $ #
/- $ %$ $ 1 # A 5 % - E -- -
! # - "
! ! # - < (A - ="
" # $ !3 - ( )1 # - # A "
!% & ' ( ! A 2 )1 - # A "
!% & ! 4 ( - %$ $ ( # - )1 # "
8$ - # - # $ % % 4 ( ! 4"- @ % ( $
4 ( - -% # $ !F " - %
* $
• ) *' +,&' ! -.
8 # $ - G $G $ # - # ( -
# # 8$ G - = 1 - 5
• /0
# - - 2 ( ( - #
!" - -- - $ - ( $ $ # -#
/
4 ; # - %$ # #
$
• 1
;# ( $ - ( ( % 8$
!" -
• &#/23 ! $! '4205
8$ - # $ ( % 9 # H
# % #
• $ % $ GIG $ 4 # @ %
( ( %$ I -
• ,6,& /7 *, 805
8$ # % % % 6 % 75 - # 4 $ (
- $ / $ % % ( ( - $ 4 ! $ ## 1 - "
6 $ 1 # $ # $ % % % - ( - $
% % 9!* !( &- , # ! !* , # 6!* '! !& *, : '!$,
! & ; ,6,& 8$ $ <# = % % #
% % $ <# = $ # 6 % ( $
• <
!" -
8$ # - %$ # # 1 -
* 1$ '2
1 # - 8$ $ 5 ( - ( /
1 ( ( - 1 ( (
$ - 4 # % # # - #
# #
3 +
/ ( $ - - 1 ( 1 - $ $ (
, $ $ 1 ,1 ( G ;+.8 *G -- -
1 ( G G
2 1 @ 5
J $ 1 ( K ( ( # C& $
,. /
0
8$ -1 ( # A - $ # ( - ( 2 4 # %
1 ( - !" - ( %
main()
{
int count;
float sum,area;
.
.
.
}
2 $ 1 ( ( $ / (
# 1 % ! # - - " , 4 #
- 1 ( ( %
#include<stdio.h>
main()
{
int sum;
sum=12;
sum=sum+5;
printf("Sum is %d",sum);
system("pause");
}
: - - 1 (
8 # I
8$ L MEI / 1 - ( E 6 % $ MLEI
# # -- % # $ - %
Sum is 17
/ - N $ # $ - 1 ( 1 $ - ( 9
int integer
long long integer
float float number
double long float
char character
;$ # $
4
%d decimal integer
%ld decimal long integer
%s string or character array
%f float number
%e double (long float)
# - !" - $ 4 # % 2 (
9 / - / # - - # --
G1 ( G
6 # $ - $1 ( 1 ( # # -
# 4 -# --
"
, % # 1 4 # H $ % # ;$
$
\n New line
\t tab
\r carriage return
\f form feed
\v vertical tab
6 $
$ 4 #
#include<stdio.h>
main()
{
printf("I am going inside test function now\n");
test();
printf("\nNow I am back from test function\n");
system("pause");
}
test()
{
int a,b;
a=1;
b=a+100;
printf("a is %d and b is %d",a,b);
}
5
/ $ 4 # % $ 1 % - 6 $ 1 $ - -
- 6$ % $ - 5# !" - - $
- 5 $ # A - !" - !" @ -
5 O # $ % GIG %$ % $ - ( %$
% %
6 - $ $ $ . % 4 % $ # $ %
$ - # 4
7(
6 5 # # < $ )1 =
% % : 4>+./0
( $ 4 1 #
4 % ( 1 ( 1 #
/- $1 ( $ 5 $ %
9 ## - ! ( "
#include <stdio.h>
main()
{
printf("Hi\nthere\nWhat is the output\n?");
}
test$var
my counter
9count
Float
? 7 3 ? 3 @ # @ 3 ? A
%
? $ @ 1 @ "
' $
/
# 1 ( 1 ( # # # 8$
( 1 ( # # -1 ( 5 -
$ # # 6 % # 1 -
# # - # - 9 # ,- $
$ ( ( 1 ## %$ $ # - 5 # # $
$ #
+ $ + 1 '
? $ 1 4 # $ 1 1 - (
#include<stdio.h>
main()
{
int a,b,c;
printf("Enter value for a :");
scanf("%d",&a);
printf("Enter value for b :");
scanf("%d",&b);
c=a+b;
printf("a+b=%d\n",c);
system("pause");
}
; #
, - - % $ - 1 # 5% % $ %
( - - 4 1 $ # --
? 7 3 ? 3 @ # @ 3 ? A
: - - --
scanf("Format string",&variable,&variable,...);
2 # $ - 1 ( $ % 1 - ( , ?P?
( - $1 ( $ 1 ( $ 4 # -
$ 8$ % % $ $ ( - $ 6 % ( $ $
!& &= @ % $ - $ $
# $ # $ 1 # $ $ %
# % $ % ( 4# A
# $ $ --
8$ - % 4 # 1 # 1 ( - (
float a;
int n;
scanf("%d%f",&n,&a);
$ -- $ $ # ( ( # (
- 1 # ! # 5 " # 1
2 1
, $ $ # 1 ( - $ # , - # $
1
int scores[10];
8$ G G - & 1 6 $ ( - (
# - 41 3 ( - ( 1 Q R5 5 Q'R % % % $ $
1 ( $ 1 (
scores[0]=124;
scores[8]=1190;
* 1 C - - $ 1
#include<stdio.h>
main()
{
int scores[3],sum;
float avg;
? 7 3 ? 3 @ # @ 3 ? A
; #
Enter Score 1 : 12
Enter Score 2 : 14
Enter Score 3 : 15
Sum is = 41
Average = 13.000000
"
/ % $ 5 #$ $ $ - $
$ ( - - $ $ $ 4 #
#include<stdio.h>
main()
{
char name[20];
; # *
/- G G $ $ - ( - % ? ?5 % ??
- ( S 1 $ 6 $ $ G.+ G $
$ % % $ ?H ? $ !/ ? $ 1 5$ % 1 % $ % % $ %
$ ( $ # 5 "
&
? 7 3 ? 3 @ # @ 3 ? A
9 % $ ( $ 1 $ (
name[0]='B';
name[1]='r';
name[2]='i';
name[3]='a';
name[4]='n';
name[5]=0; //or name[5]='\0';
, % % ( 1 4 # # $ - 1 ( N , $ $ !& *, >?> , ; #!
( ; ,& ; ( * , 2 %( ( $ - % % $
-
$
# $ % $ ?T? , 4 # T U $B
$ % $$ - #
# # ( # # # ( - # #
# ,- # # $ - $ A (5 # %
B' $ $ '
T - - $ 4 #
#include<stdio.h>
#define PI 3.14
#define ERROR_1 "File not found."
#define QUOTE "Hello World!"
main()
{
printf("Area of circle = %f * diameter", PI );
printf("\nError : %s",ERROR_1);
printf("\nQuote : %s\n",QUOTE);
system("pause");
}
; #
-
? 7 3 ? 3 @ # @ 3 ? A
# # # - ( - # % $ $ ( %
# % $ # ( %
#include<stdio.h>
main()
{
printf("Area of circle = %f * diameter", 3.14 );
printf("\error : %s","File not found.");
printf("\nQuote : %s","Hello World!\n");
system("pause");
}
/ ( -T - % - ( 6 ## - T -
1 ( $ % ?I? - # #
2
K (
- $ 1 $ - # /
--# # -- - - # - 1 (
#
2 4 # # ( ); ( D( % % 1 -
1 ( # # /- # 1 $ S -
1 ( !% % 4 # 4 "5 % $ -4 S - $ 6 $
S -!" - S - 1 ( 1 ( # ! % $ "
#include<stdio.h>
main()
{
int i;
float f;
@ % ( $ 1 ( 1 ( # # S -!" - % (
$ % 1 ( -8 ( 3 - ); # 4 #
.
? 7 3 ? 3 @ # @ 3 ? A
6 $ 1 % -1 ( - $ - $ ( 1 # #
1 ( $ 1 1 1 %$ 1 ## # 1
(
/- 1 ( 5$ $( % ( 1 ( % 8$ ( % 1 $
1 ( 1 # 1 ( 2 4 # ! C V 5MC V "
@ 1 ( < = < =( G G G G % ( -
#
4 #
signed int a;
unsigned int b;
a=32700;
b=65000;
6 % 1 $ C V 1 ( G(G ( 1 ( G G
# # - % ( # % % 4#
, 1 % 1 ( G G( G(G
) - - # % % -% % 1 ( (
7(
6 5 # # < $ )1 =
% % : 4>+./0
( $ 4 1 #
4 % ( 1 ( 1 #
/- $1 ( $ 5 $ %
9 ## - ! ( "
/
D 7 D @ % D D @ # 7 D 3 2 7 D 3
%
D$ @% $ @ 1$ +
/
# 1 ( 5 # # / $ % % (
$ # 3 $ # 9 1 $ # (
$ !M F>W" 8$ -- %$ $ % % $ $ %1
# # # -# / $ % % $ % 1
1 ( - # - # 1 ( % $ $ #
D$
8$ - # $# 6 - $ #
( $
() Parentheses
+ Add
- Subtract
* Multiply
/ Divide
8$ $ # %$ $ % --
% Modulus
++ Increase by one
-- Decrease by one
= Assignment
S -! " 1 $ S - 1 ( # # $ ( / $ S
$ 1 (
4 #
0
D 7 D @ % D D @ # 7 D 3 2 7 D 3
int a=10;
c=sizeof(a); c will be 2 in dos and 4 in windows as the size of integer
is different in different Os.
% $
% % # - ( 4 $ 6 $ # $
# # / (1 $ -- 4 # % 4 # - -
$ # # $ , 1 % # #
8$ -% - # # 6 % $ 4
" $
%$ # - ( - $
# , % $ 1 ( - - %
# (
while( condition )
command;
while( condition )
{
block of commands
}
# ( 4# , ( 4# %$ $ $
/ $ 1 -& - $ 1 1 - 2
4 # $ !CBD" 1 $ - $ 1 - 6$ $
!& LL& " 1 $ - 1 &
#include<stdio.h>
main()
{
int i=0;
4
D 7 D @ % D D @ # 7 D 3 2 7 D 3
while( i<100 )
{
printf("\ni=%d",i);
i=i+1;
}
system("pause");
}
/ ( 1 4 # L M& & $ # 1 , % %
5 $ # # # $ $ $ 6 $
4# MM - L M&
6 % ( # 4
1$ +
2 % 1 # - 1 1 ( 1 ( -
$ # 8$ # - 1 ( - -- 5- 4 # %$ %
1 # - 1 ( ( # ( % $ - % $ -- # -
# # - $ # #
• , # 1 # $ 1
+ % ( 1
• /- $ % -- # 4# $ ( $% 1 ( #
• / 5- - % ( 1 $ # - $
1 ( %$ $ % $ $ - $ ! 4 $ 1 ( < = $
L M&I "
2 4 # - % 1 - - # ( # 1 ( 5
% ( (
$ ( $
6 # # - # - $ $ 1 #
$
#include<stdio.h>
main()
{
int count=0;
5
D 7 D @ % D D @ # 7 D 3 2 7 D 3
float num=0,sum=0,avg=0;
avg=sum/count;
printf("\nAverage=%f",avg);
printf("\nSum=%f\n",sum);
system("pause");
}
/ $ 4 # % - ( $ $ # 6 % #
1 $ /- 1 % $ % % # 8; 1
X $ # - #
Average=13.000000
Sum=26.000000
6$ & $ 1 - 5 4# # ( - ! 1 "
BL 1
O ( $ <%$ #= % $ # $
( - ! $ "
E $
, % 5 $ - # # 6 % ( -
# $
<2 #= $ %$ #( # 4 <2 #= -
$ $ % $ #%
$ ( -
-$ $ #
/ S # # - <- #= 6 S #1 ( $ 8
$ # # - $ # #% - $ 1
!8 " /- $ ( 1 !- " $ $ #%
Y* 1 % ( # - 1 # 6 $ # $ $
- - $ # 2 4 # % #1 ( 1
% $ - # - ( - $ # ( 1 <- #=
, $ #% % 4 # C&% $- # O # $ % /L/M& -
#1 ( / % <- #= #$ ! MM"
#include<stdio.h>
main()
{
int i=0;
for(i=0;i<100;i++ )
printf("\ni=%d",i);
system("pause");
}
6 # $ # -% 1 # - $ %
#include<stdio.h>
main()
{
int count=0;
float num=0,sum=0,avg=0;
for(count=0;count<7;count ++)
{
printf("Enter temperature : ");
D 7 D @ % D D @ # 7 D 3 2 7 D 3
scanf("%f",&num);
sum=sum+num;
}
avg=sum/7;
printf("\nAverage=%f\n",avg);
system("pause");
}
/ 4 % % 4 # ( # $
7(
6 5 # # < $ )1 =
% % : 4>+./0
( $ 4 1 #
4 % ( 1 ( 1 #
/- $1 ( $ 5 $ %
9 ## - ! ( "
&
' ' 7(
,
, % % # 1 % G- G # - 4
( - / $ % % ( <- = # 6
% $ %
6 K K $
8$ $ # #$ / S S 1 (
# $ 1 ( / -
8$ % - $ - # 4 , %
- $ ( 1 !- " # 4 %
$ <- #= # $ %$ $ 4 $ #
/ # 1 4 # % MM MM 8$ % $ 1 -
1 ( $ # 4 / $ 1 ( $ $ #
1 1 !- "5 - $ # ( $ 1 ( % 4 # % #
# $ !- &F& 'F'" * $ # $
#include<stdio.h>
main()
{
int i,j;
for(i=1;i<10;i++)
{
for(j=1;j<10;j++)
printf("%3d",i*j);
printf("\n");
}
system("pause");
&
7 % 7 D 3 3 ? 7 6 7 3
K K '
( - ( 4 %$ 4
%$ 4 8$ $
%$ # , $ 1 !$ 1 " 1 !- "
if(condition)
command;
if(condition)
{
block of commands;
}
</- = ( $ ( # 1 % # $- 1 4
# $ # /- $ ( - % ( 4
6$ $ # [
#include<stdio.h>
main()
{
int n;
printf("Enter a number: ");
scanf("%d",&n);
if(n>=0)
printf("Number is positive !\n");
if(n<0)
printf("Number is negative !\n");
system("pause");
}
. % ? # 4 4 #
Example 4-3: example4-3.c
6 # 1 9 4 M(4M L
#include<stdio.h>
#include<math.h>
main()
{
float delta,a,b,c,x1,x2;
-
7 % 7 D 3 3 ? 7 6 7 3
printf("Enter a : ");
scanf("%f",&a);
printf("Enter b : ");
scanf("%f",&b);
printf("Enter c : ");
scanf("%f",&c);
delta=b*b-(4*a*c);
if(delta<0)
{
printf("Equation has no answer !\n");
system("pause");
exit(0);
}
if(delta==0)
{
x1=-b/(2*a);
printf("Equation has two equal answers !\n");
printf("x1=x2=%f\n",x1);
system("pause");
exit(0);
}
x1=(-b+sqrt(delta))/(2*a);
x2=(-b-sqrt(delta))/(2*a);
printf("\nX1=%f",x1);
printf("\nX2=%f\n",x2);
system("pause");
}
; # 5( 5 # !$ 1 $ 9 %$ $ L "
# ( % - $ 4 ( ( % 9
6 $ 1 $ 9 !" - - $ < $= ( ( ( $ ( $ $
% $ 1 5 $ < $ $= - $ $ -
6 $ (K K
# - - G- = 1 $ $ - 4 ## ( -
/- # 4 # - %$
4 $ %$ - 5% $ % % # G -G
- $ $
If(condition)
Command;
If(!condition)
.
7 % 7 D 3 3 ? 7 6 7 3
command
1 $ 1 - 4# /- $ % ( - % $??
1 1
@ 1 - - <- = 1 % G- =$
# - % - $ $ - - G -G
If(condition)
Command;
else
command
/ $ - 5 G G $ ( 6$ - !
( - " 4 $ % G G % ( % 4 # $ 1 -
- 4 # D 8$ < -W W= - < -W =
#include<stdio.h>
main()
{
int n;
printf("Enter a number: ");
scanf("%d",&n);
if(n>=0)
printf("Number is positive !\n");
else
printf("Number is negative !\n");
system("pause");
}
( $ ' $ (K K
.4 4 # 1 # 4- -<- = 8$ $ 1
#include<stdio.h>
#include<stdlib.h>
main()
{
int choice;
while(1)
{
/
7 % 7 D 3 3 ? 7 6 7 3
printf("\n\nMenu:\n");
printf("1- Math Program\n2- Accounting Program\n");
printf("3- Entertainment Program\n4- Exit");
printf("\n\nYour choice -> ");
scanf("%d",&choice);
if(choice==1)
printf("\nMath Program Runs. !");
else if(choice==2)
printf("\nAccounting Program Runs. !");
else if(choice==3)
printf("\nEntertainment Program Runs. !");
else if(choice==4)
{
printf("\nProgram ends.\n");
exit(0);
}
else
printf("\nInvalid choice");
}
}
,( 1 4 # 4 # - 1 # , # %$ $ - 1 5
# % - % 1 % 5 # #
# - $ $ % # $ $
# - 1 D 8$ % $ ? 4 ! "I?
- %$ $ $ #
, # 4- - G -G ( 1 4 #
if(choice==1)
command;
else if(choice==2)
command;
else if(choice==3)
command;
else if(choice==4)
{
block of commands;
}
else
command;
8$ - G -G $ - # # , $
- $ <- = $
0
7 % 7 D 3 3 ? 7 6 7 3
7 '3
/ % $ ## 1 $ - $ , $
# $ 9 # 5 % ( (
( - $ 8$ % ( $ # $ @
1 # % # % $ 4 % $ #$
/- $ 5 -- % ( %$
/ % 9 $ # 2 ! ! @ ! , !; $ &-!*&
!; ;2
7(
6 5 # # < $ )1 =
% % : 4>+./0
( $ 4 1 #
4 % ( 1 ( 1 #
/- $1 ( $ 5 $ %
9 ## - ! ( "
4- Exit
1$ ' $ ! '$ 1
" " 8 ( & " 8
4
7 % 7 D 3 3 ? 7 6 7 3
%
-
! "
/
# 1 % %$ %% G -G # %$ $ $
# - 1 1 8$ 1 # %$ $
( % $ < % $W = (
K ! " 888 K
6 G -G ( ( G % $G %$ $ -
$ $ 1 $
switch(...)
{
case ... : command;
command;
break;
default:
command;
}
/ $ ( 1 % $ % % ( ( -- - % $ $ --
* % 4 # DE -# 1 % $ - G -G
#include<stdio.h>
#include<stdlib.h>
main()
{
int choice;
while(1)
5
7 % 7 D 3 3 ? 7 6 7 3
{
printf("\n\nMenu:\n");
printf("1- Math Program\n2- Accounting Program\n");
printf("3- Entertainment Program\n4- Exit");
printf("\n\nYour choice -> ");
scanf("%d",&choice);
switch(choice)
{
case 1 : printf("\nMath Program Runs. !");
break;
default:
printf("\nInvalid choice");
}
}
}
/ G % $W G 5 $< = # ( , ( # #
%$ $ 4 - $ % $ % $ - < =
( - %$ $ 4 - $ # % 8$ # % 4
$ <( =
G( G $ 1 1 % $ /- 1 $ 5 #
4 % 4 $ - G % $G
( % ( 4 !%$ - $ % A % < = ( "
, % 5 $ ( $< = A ( 8$ % 4 % $(
( $ - $< =
/ - % $ 1 G( G 8$ ( % $ 1
G 4 ! "G !%$ $ # 4 " ( %
--
G - G % ( 4 - - $ $ % $ #
7 % 7 D 3 3 ? 7 6 7 3
case i: something;
break;
L
6 G( G <% $ = # 1 # - $ 6
G( G # # 4 !% $ # - "
while (num<20)
{
printf("Enter score : ");
scanf("%d",&scores[num]);
if(scores[num]<0)
break;
}
/ ( 1 4 # # 4 $ BL 1 . %
$ 4 #
Example 5-3: example5-3.c
#include<stdio.h>
#include<stdlib.h>
main()
{
int choice;
while(1)
{
printf("\n\nMenu:\n");
printf("1- Math Program\n2- Accounting Program\n");
printf("3- Entertainment Program\n4- Exit");
printf("\n\nYour choice -> ");
scanf("%d",&choice);
switch(choice)
{
case 1 : printf("\nMath Program Runs. !");
break;
default:
printf("\nInvalid choice");
if(choice==4) break;
}
}
/ ( 1 4 # % $ 1 ( - 4 # 1 4 #
- $ $ 5% ( %$ # % $(
/- $ $ D $ $ <( =% ( %$ # % $ $ -
- %$ $ - - #
ch=getchar();
8$ - - $ #
putchar(ch);
#include<stdio.h>
#include<conio.h>
main()
{
char ch;
while(ch!='.')
{
ch=getchar();
putchar(ch);
}
system("pause");
7 % 7 D 3 3 ? 7 6 7 3
2 # $ -
,( 1 # $ $ $ 1 # ( - ?? $ #
$ $ # % ( ( $ $ % ( -- 8$ ( -- % (
1 ? $? 1 ( - # ! ( - $ " # $ ( --
% # $ # 4 $ - %
$ !" - !8$ # "
/- $ ?? $ ( -- $ 5 # 4 $
% $# $ !" - $ ?? - $ # $ # - %$ #
/- ?? $ $ - #5 %$ $ !" -
% - #
2 ? ? ## # $ - # ? ? # (
# $ - #
/ # # 5 $ $ $
# - G $G 8$ ( -- # $ 1 $
1 $ ! $ $ - U $B $ %
- % "
#include<stdio.h>
main()
{
char ch;
while(ch!='.')
{
ch=getch();
putch(ch);
}
system("pause");
}
&
7 % 7 D 3 3 ? 7 6 7 3
6 $ $ - % $ 1 - $ # ( - # /-
1 % O # $ # ## $!" !
$ $ - $ ( "
( % 4 #
#include<stdio.h>
#include<stdlib.h>
main()
{
char choice;
while(1)
{
printf("\n\nMenu:\n");
printf("1- Math Program\n2- Accounting Program\n");
printf("3- Entertainment Program\n4- Exit");
printf("\n\nYour choice -> ");
choice=getch();
switch(choice)
{
case '1' : printf("\nMath Program Runs. !");
break;
}
}
/ ( 1 4 # % $ 1 % 4 # E & 8$ % $ 1 $!" - -
-!" - /- - ( 4 # % $ $ 1
% /- 1 $ ! A - 4 # "5
# $ / $- 5 $
$ $ - $ - $ $
-
7 % 7 D 3 3 ? 7 6 7 3
/ 4 # $ % $ 1 # G - G <% $ = - $
- ( 4 # % ( $ $ %$ $ %
$ # , # 1 % % 1 ( <% $ =
( < = X % $ $ <% $W = $ $ $ #
% $1 ( - $ # 1 ( # ?&?5 ? ?5 ?C?5 ?D? (
$ $ # !?&? $ -- - $ 1 &"
/- # 1 %$ #% % $ -G G / 1
# % ( 1 # % ( #
K K
( # ( G G $ - % - #
/ $ #$ % 1 / A # $ - - $ #
# - $ #
#include<stdio.h>
main()
{
while((ch=getchar())!='\n')
{
if(ch=='.')
continue;
putchar(ch);
}
system("pause");
}
/ ( 1 4 # 5# # #
( $ ?? $ - 8$
4 % ( $
( $ # % ( # - # $ !%$ $ 9
GH G $ " # , % $ ( $ !" - ( --
# -
7(
6 5 # # < $ )1 =
% % : 4>+./0
( $ 4 1 #
4 % ( 1 ( 1 #
/- $1 ( $ 5 $ %
9 ## - ! ( "
.
7 % 7 D 3 3 ? 7 6 7 3
a -> c
f -> g
n -> l
k -> r
/
6 D 7 D 3 E 3 D 3
%
.
6 E
/
. &% $ $ # - - !" # -
( # 4 -
,- ( - ( - $ ( %$ $ # (
$ - - % $ Y\Y % $ YZ 8$ $
- # 1 # 4 # ( % $ % $ %% % # -
Example 6-1: example6-1.c
#include<stdio.h>
/*Function prototypes*/
myfunc();
main()
{
myfunc();
system("pause");
}
myfunc()
{
printf("Hello, this is a test\n");
}
/ ( 1 4 # % $ 1 # -# # - 2 ( ( 1
# 4 $ $ ,- - % 2 $
$ ,- 1 - 8$ 1 $
! 1 $ # - #" 2 $ ( - -
-
$ % # <- # # = / # %
$ - % # ( 1 $ # / - (
<- # # =
0
6 D 7 D 3 E 3 D 3
3 - # - 2 4 # <# -= - $
# # - $ - - ( -
%$ $ # 6$ - 5 # ( !%$ $
- $ - 4 " # $ $ - 4 ( #
2 # # ! ( - " - - $ - ! $5 W"
%$ $ $ # - #
8$ - -
• @ # - -- # - #
• + - 5# % ( 1 # ( $( % # - A (
) 5 ( -
• ,( - ( 4 % $ -- ( - # 8$ #
# - % $-
, % % - - - 5 $ 1 # /- %
% $ # A !" - ( % % $ 1 - -%
# $ # - -- - 5% % $ 1 !" - 1
- %$ $ # - $ # -
main()
{
Read_scores();
calculate()
write_to_file();
print_results()
}
. % $ 4 #
#include<stdio.h>
#include<stdlib.h>
add();
subtract();
multiply();
main()
{
char choice;
while(1)
{
printf("\nMenu:\n");
4
6 D 7 D 3 E 3 D 3
switch(choice)
{
case '1' : add();
break;
default:
printf("\nInvalid choice");
}
}
}
add()
{
float a,b;
printf("\nEnter a:");
scanf("%f",&a);
printf("\nEnter b:");
scanf("%f",&b);
printf("a+b=%f",a+b);
}
subtract()
{
float a,b;
printf("\nEnter a:");
scanf("%f",&a);
printf("\nEnter b:");
scanf("%f",&b);
printf("a-b=%f",a-b);
}
&5
6 D 7 D 3 E 3 D 3
multiply()
{
float a,b;
printf("\nEnter a:");
scanf("%f",&a);
printf("\nEnter b:");
scanf("%f",&b);
printf("a*b=%f",a*b);
}
E
2 ( # # # $ - -1 ( 8$ # # 1 (
$ ( - (
#include<stdio.h>
main()
{
sayhello(4);
system("pause");
}
sayhello(int count)
{
int c;
for(c=0;c<count;c++)
printf("Hello\n");
}
/ ( 1 4 # % $ 1 $ !" - % $ $ # <D= 8$ - 1
# 1 < =1 ( ( - 4 -- ( $ !"
- % $ # $ Y $
, % - # # ( - % - !" $ -
# , $ # # # - # # !$ % $T "5
( - !" - $ # - # @ # - $ - $ - -
- $ # # X % 1 - $ - # #
%$ - -
&
6 D 7 D 3 E 3 D 3
/- # # # - - # $ - %
# 8$ $ # # - $ - -
E +
/ $ % 4# - 1 / #
( % 1
y=f(x)
y=f(x)=x+1
y=f(x)=1 (arguments are not received or not important)
/ # % 4# - 1 8$ 1 $ #
$ 1 / ( 5- 5 $ $ 8 # - $ 1
# - -
) - # -- /- # -- $ 5 % (
- #
, % 1 - 1 6 $ % $
Sum()
{
int a,b,c;
a=1;
b=4;
c=a+b;
reurn c;
}
,( 1 - $ 1 -1 ( < = $ 1 -- 6
4# 2 4 # % # % -- % $Y M(I
/- - 1 - % % - # 8$
% % $ - 1 6 ## 4
( #
/ # 1 4 # % 1 - 2 4 #
1 !" -
main()
{
.
.
.
return 0;
}
&
6 D 7 D 3 E 3 D 3
) - 1 - # - /- Y $ 1
!" - 1 % ( /- # 1
- 5 # - $ 1 - $
;+ '< +
8$ $ <1 = # -- K # - - $
1 @ - - $ 1 <1 =
void test ()
{
/* fuction code comes here but no return value */
}
1 - ( 1 ( ( 1 %
a=test();
+ ( 1 %
/ 4 % % - 8$ % -
7(
6 5 # # < $ )1 =
% % : 4>+./0
( $ 4 1 #
4 % ( 1 ( 1 #
/- $1 ( $ 5 $ %
9 ## - ! ( "
&
E 3 D 3 ? 6 7 3
%
/
E
/
# 1 %$ %% 1 - - /
$ % % # # - 6 % ( ( 1
( - < # =
6 $
/ - $ - 4 # &% $ % $ %
$
#include<stdio.h>
system("pause");
return 0;
}
, 1 ( 6 $ - # #
# -- $ % $ $ -- $ # # % $ YI ( -
$
&&
E 3 D 3 ? 6 7 3
1+
# - 5 ( 1 $ 4 #
$ (A (
#include<stdio.h>
main()
{
int m;
m=2;
printf("\nM is %d",m);
test(m);
printf("\nM is %d\n",m);
system("pause");
return 0;
}
void test(int a)
{
a=5;
}
/ !" - 5% $ 1 1 ( 6 $ 1 1 Y 6 % -
- ( $ 1 -Y % $ 1 - 1 - # !"
- 6$ $ [
M is 2
M is 2
- $ $ $ 1 - 8$ ( -
$ <1 -1 ( = $ - 1 ( - ,
# 1 -1 ( $ - 1 $ 1
% $ $ 1 1 ( - 8$ %$ % $ $ - < ( 1 =
1
8$ $ $ - 1 ( ( < ( - = 8$ $
( - - 1 - 1 ( -
&-
E 3 D 3 ? 6 7 3
6 % - 4 # $ % % (
#include<stdio.h>
void test(int *ptr);
main()
{
int m;
m=2;
printf("\nM is %d",m);
test(&m);
printf("\nM is %d\n",m);
system("pause");
return 0;
}
8 ( ( - $ 1 - 1 ( - 5- %
- $ 1 ( !%$ 4 $ 1 ( "
/ # YP # 1 $ %$ $ $ 1 ( 2 4 #
-Y 1 ( - # Y $ YP % 1 $ - 1 ( 6
$ Y #
ptr=&m;
/ ( 1 # 1 ( % -1 ( 8$ $
- - 2 4 # -- $ $ ( ( 1 1
- ( # 1 ( / - # 1 1 - $
1 ( - . %% $ %$ % YP ( - 1 (
-1 (
Scanf(“%d”,&a);
. % $ % $ 1 -1 ( % # $ ( 1
1 $
, % 5% - -1 ( Y YP #
ptr=&a;
&.
E 3 D 3 ? 6 7 3
. %% - 1 1 ( Y YF #
6 1 - $ 1 $
*ptr=5;
4 # 6 $ 1 # # ! " - . %-
( - 1 1 ( /- $ # 5 % $
M is 2
Modifying the value inside memory address 2293620
M is 5
$ % $ 1 $ 1 - 1 ( $ - !(
- $ 1 $ - 1 ( "
( $ @L
/ $ % % % # %$ $ - ( - ( ((
$ 8$ $ < % #= - %$ $ % # 1 %
% $ $ $
(( # $ % - $ - $ 5 % # $ /-
% $ ( 5 $ $ ( ( 9 # 1
$ - $ ( $ $ /- % # - $ # % # - <
&= $ 5 % ( #
#include<stdio.h>
void swap(int *a,int *b);
main()
{
int ar[5],i,j,n;
ar[0]=7;ar[1]=3;ar[2]=9;ar[3]=2;ar[4]=11;
for(i=0;i<n-1;i++)
&/
E 3 D 3 ? 6 7 3
for(j=0;j<n-1;j++)
{
if(ar[j]>ar[j+1])
swap(&ar[j],&ar[j+1]);
}
system("pause");
return 0;
}
temp=*a;
*a=*b;
*b=temp;
}
8$ #
ar[0]=7
ar[1]=3
ar[2]=9
ar[3]=2
ar[4]=11
Array after sort:
ar[0]=2
ar[1]=3
ar[2]=7
ar[3]=9
ar[4]=11
7 '3
6 $ 1 - -# $ - ( $ - $
%
/ 4 % % ( 5# 5 5- 5 5
$- %
&0
E 3 D 3 ? 6 7 3
7(
6 5 # # < $ )1 =
% % : 4>+./0
( $ 4 1 #
4 % ( 1 ( 1 #
/- $1 ( $ 5 $ %
9 ## - ! ( "
&4