0% found this document useful (0 votes)
14 views

Programming in C in 7 Days - Sarmady

The document provides an overview of the first 7 lessons in an e-book titled "Programming in C in 7 days!": 1) The first lesson covers getting input, arrays, character strings, and preprocessors. 2) Lesson 2 discusses operators, loops, and type conversion. 3) Lesson 3 explains conditional command execution and selection using switch statements. Support for the free e-book is provided on the Learnem.com discussion boards, and the full e-book can be purchased on their website.

Uploaded by

fadlyyy934
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Programming in C in 7 Days - Sarmady

The document provides an overview of the first 7 lessons in an e-book titled "Programming in C in 7 days!": 1) The first lesson covers getting input, arrays, character strings, and preprocessors. 2) Lesson 2 discusses operators, loops, and type conversion. 3) Lesson 3 explains conditional command execution and selection using switch statements. Support for the free e-book is provided on the Learnem.com discussion boards, and the full e-book can be purchased on their website.

Uploaded by

fadlyyy934
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

!

"

• “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= <: = #
$ $ - $

6 < $ ) 1 ##= - $ % < $ ) 1 ##=


- ( % - $ % ( $ # >>%%% ( $ ! $ +*
$ # >>%%% ( $ > 1> 1 ## $ "

# $
? % - #

Example 1-1: example1-1.c

#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 #

2/:+* & D $ $ 4 # &&

.
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 ( ( %

Example 1-2: example1-2.c

#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 #

Example 1-3: example1-3.c

#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 ## - ! ( "

8 " " ( $ " ' 9

#include <stdio.h>
main()
{
printf("Hi\nthere\nWhat is the output\n?");
}

8 $ " ' ! 8 : " ! " + 8 " $


" ' $ ! $ 8

8 " $ ; $ ( $ < " = " $ '


>8

&8 "1 " + + '9

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 - (

Example 2-1: example2-1.c

#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");
}

; #

Enter value for a : 10


Enter value for b : 20
a+b=30

, - - % $ - 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;

Example 2-2: example2-2.c

* 1 C - - $ 1

#include<stdio.h>
main()
{
int scores[3],sum;
float avg;
? 7 3 ? 3 @ # @ 3 ? A

printf("Enter Score 1 : ");


scanf("%d",&scores[0]);
printf("Enter Score 2 : ");
scanf("%d",&scores[1]);
printf("Enter Score 3 : ");
scanf("%d",&scores[2]);
sum=scores[0]+scores[1]+scores[2];
avg=sum/3;

printf("Sum is = %d\nAverage = %f\n",sum,avg);


system("pause");
}

; #

Enter Score 1 : 12
Enter Score 2 : 14
Enter Score 3 : 15
Sum is = 41
Average = 13.000000

"
/ % $ 5 #$ $ $ - $
$ ( - - $ $ $ 4 #

Example 2-3: example2-3.c

#include<stdio.h>
main()
{
char name[20];

printf("Enter your name : ");


scanf("%s",name);

printf("Hello, %s , how are you ?\n",name);


system("pause");
}

; # *

Enter your name : Brian


Hello, Brian, how are you ?

/- 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 #

Example 2-4: example2-4.c

#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");
}

; #

Area of circle = 3.140000 * diameter


Error : File not found.
Quote : Hello World!

-
? 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 ( # ! % $ "

Example 2-5: example2-5.c

#include<stdio.h>
main()
{
int i;
float f;

printf("Integer type uses %d bytes of memory.\n", sizeof(i));


printf("float type uses %d bytes of memory.\n", sizeof(float));
system("pause");
}

@ % ( $ 1 ( 1 ( # # S -!" - % (
$ % 1 ( -8 ( 3 - ); # 4 #

Bytes used Range


char 1 256
int 2 65536
short 2 65536
long 2 4 billion
float 4 6 digits * 10e38
double 8 10 digits * 10e308

.
? 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 ## - ! ( "

8 $ " ! " @! $ " ' ( ' " $ $ 1


1 $ 8

8 1 ! $ " + ( @ ! " ' ! $ " !


$ @ + ! " " $ 1 ' " '$ $ 1
1 " " 8

8 $ " 1 ' $ " @ " $


8 C $ " ' " $ 8

/
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 #

c=4%3; c will be equal to 1 after execution of this command.


i=3;
i=i*3; i will be equal to 9

f=5/2; if f is integer then it will be equal to 2. If it


is a float type variable its value will be 2.5

j++; Increases the value of j by one.

0
D 7 D @ % D D @ # 7 D 3 2 7 D 3

j--; Decreases value of j by one

sizeof(int) returned value is 2 in dos and 4 in windows

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 % # #

while(not a hundred times)


{
code
}

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 &

Example 3-1: example3-1.c

#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 #
$

Example 3-2 : example3-2.c

#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;

printf("Enter score (-1 to stop): ");


scanf("%f",&num);
while(num>=0)
{
sum=sum+num;
count++;
printf("Enter score (-1 to stop): ");
scanf("%f",&num);
}

avg=sum/count;
printf("\nAverage=%f",avg);
printf("\nSum=%f\n",sum);

system("pause");
}

/ $ 4 # % - ( $ $ # 6 % #
1 $ /- 1 % $ % % # 8; 1
X $ # - #

Enter score (-1 to stop): 12


Enter score (-1 to stop): 14
Enter score (-1 to stop): -1

Average=13.000000
Sum=26.000000

6$ & $ 1 - 5 4# # ( - ! 1 "
BL 1

O ( $ <%$ #= % $ # $
( - ! $ "

E $
, % 5 $ - # # 6 % ( -
# $

<2 #= $ %$ #( # 4 <2 #= -
$ $ % $ #%
$ ( -

for( control statement )


command;
D 7 D @ % D D @ # 7 D 3 2 7 D 3

for( control statement )


{
block of commands
}

-$ $ #

for ( initialization; test condition; run every time command )

/ S # # - <- #= 6 S #1 ( $ 8
$ # # - $ # #% - $ 1
!8 " /- $ ( 1 !- " $ $ #%

Y* 1 % ( # - 1 # 6 $ # $ $
- - $ # 2 4 # % #1 ( 1
% $ - # - ( - $ # ( 1 <- #=

, $ #% % 4 # C&% $- # O # $ % /L/M& -
#1 ( / % <- #= #$ ! MM"

Example 3-3: example3-3.c

#include<stdio.h>
main()
{
int i=0;

for(i=0;i<100;i++ )
printf("\ni=%d",i);

system("pause");
}

Example 3-4: example3-4.c

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 ## - ! ( "

8 !" $F ! $ " $ $" G H " " G:H $ 8 C


1 8# " 1$ + ! " II $ '8

8 $ " $ ; ' < ' $ ' ' 8


E ( $ 1 04 $ J J 4 =1 C ' $ $
" '+ >

8 $@ ! $ " $ " $ " ' 18 "


" " ' ' C- ( ' 1 ; + ' 1 < (
' ' 8 " 8
7 % 7 D 3 3 ? 7 6 7 3
%

&
' ' 7(

,
, % % # 1 % G- G # - 4
( - / $ % % ( <- = # 6
% $ %

6 K K $
8$ $ # #$ / S S 1 (
# $ 1 ( / -
8$ % - $ - # 4 , %
- $ ( 1 !- " # 4 %

for( initialization; test condition; run every time command)


command;

$ <- #= # $ %$ $ 4 $ #
/ # 1 4 # % MM MM 8$ % $ 1 -
1 ( $ # 4 / $ 1 ( $ $ #
1 1 !- "5 - $ # ( $ 1 ( % 4 # % #
# $ !- &F& 'F'" * $ # $

Example 4-1: example4-1.c

#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

Example 4-2: example4-2.c

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 =

Example 4-4: example4-4.c

#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

Example 4-5: example4-5.c

#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 ## - ! ( "

8 $ " $ 5 ! 5 ' 5 " ' 8 = K K $>


" + " ' 8 ! ' $" + '
" ' ' ! 8

A 16-20 B 12-16 C 10-12 D 7-10 E 4-7 F 0-4

8 ! ( $ &,- ' " ! J

1- Add two numbers

2- Subtract two numbers

3- Multiply two numbers

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;

case ... : command;


break;

default:
command;
}

/ $ ( 1 % $ % % ( ( -- - % $ $ --

Example 5-1: example5-1.c

* % 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;

case 2 : printf("\nAccounting Program Runs. !");


break;

case 3 : printf("\nEntertainment Program Runs. !");


break;

case 4 : printf("\nProgram Ends. !");


exit(0);

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

< % $= ( - # < $ = %$ 1 ( <


= % 8$ $ % ( %
% $(

case i: something;
break;

L
6 G( G <% $ = # 1 # - $ 6
G( G # # 4 !% $ # - "

Example 5-2: example5-2.c

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;

case 2 : printf("\nAccounting Program Runs. !");


break;
7 % 7 D 3 3 ? 7 6 7 3

case 3 : printf("\nEntertainment Program Runs. !");


break;

case 4 : printf("\nProgram Ends. !");


break;

default:
printf("\nInvalid choice");

if(choice==4) break;

}
}

/ ( 1 4 # % $ 1 ( - 4 # 1 4 #
- $ $ 5% ( %$ # % $(

/- $ $ D $ $ <( =% ( %$ # % $ $ -
- %$ $ - - #

" => ' "=>


$ !" - 1 $ %$ % $ - # 8$ -
% $ - # ( 1 ( 4# #

ch=getchar();

8$ - - $ #

putchar(ch);

Example 5-4: example5-4.c

#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 # $ -

test <--This is typed by us


test <--output of putchar after we pressed enter
again.testing <--Typed string includes a '.' character
again. <--Loop terminates when it reaches '.' char

,( 1 # $ $ $ 1 # ( - ?? $ #
$ $ # % ( ( $ $ % ( -- 8$ ( -- % (
1 ? $? 1 ( - # ! ( - $ " # $ ( --
% # $ # 4 $ - %
$ !" - !8$ # "

/- $ ?? $ ( -- $ 5 # 4 $
% $# $ !" - $ ?? - $ # $ # - %$ #
/- ?? $ $ - #5 %$ $ !" -
% - #

2 ? ? ## # $ - # ? ? # (
# $ - #

/ # # 5 $ $ $
# - G $G 8$ ( -- # $ 1 $
1 $ ! $ $ - U $B $ %
- % "

Example 5-5: example5-5.c

#include<stdio.h>
main()
{
char ch;

while(ch!='.')
{
ch=getch();
putch(ch);
}

system("pause");
}

Testing. <--program terminates immediately after we use the


character ‘.', also characters are sent to output once

&
7 % 7 D 3 3 ? 7 6 7 3

6 $ $ - % $ 1 - $ # ( - # /-
1 % O # $ # ## $!" !
$ $ - $ ( "

( % 4 #

Example 5-6: example5-6.c

#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;

case '2' : printf("\nAccounting Program Runs. !");


break;

case '3' : printf("\nEntertainment Program Runs. !");


break;

case '4' : printf("\nProgram Ends. !");


exit(0);

}
}

/ ( 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 # $ - - $ #
# - $ #

Example 5-7: example5-7.c

#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

8 $ " ' $ 1 $ ' =MN M ' $ > ' $


$" ' $ " $ 8 " => ' $
" 8= ( $ $ J alphabet: 34 spaces: 10 >

8 $ " ' $ ' $ ! " ! " " " "


" 8 " ! " $ 1 8

a -> c
f -> g
n -> l
k -> r

" => ' ! "888 ! " $ 8

/
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 #

Example 6-2: example6-2.c

#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

printf("1- Add\n2- Subtract\n");


printf("3- Multiply\n4- Exit");
printf("\n\nYour choice -> ");
choice=getch();

switch(choice)
{
case '1' : add();
break;

case '2' : subtract();


break;

case '3' : multiply();


break;

case '4' : printf("\nProgram Ends. !");


exit(0);

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 (
$ ( - (

Example 6-3: example6-3.c

#include<stdio.h>

/* use function prototypes */


sayhello(int count);

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 ## - ! ( "

8 $ " $ ' + '$ 1+ 8 " '" +


!" " $ ' '$ " 1+ 8

8 $ " ' ' ! "


+ 1$ 8

&
E 3 D 3 ? 6 7 3
%

/
E

/
# 1 %$ %% 1 - - /
$ % % # # - 6 % ( ( 1
( - < # =

6 $
/ - $ - 4 # &% $ % $ %
$

Example 7-1: example7-1.c

#include<stdio.h>

int min(int a,int b);


main()
{
int m;
m=min(3,6);
printf("Minimum is %d",m);

system("pause");
return 0;
}

int min(int a,int b)


{
if(a<b)
return a;
else
return b;
}

, 1 ( 6 $ - # #
# -- $ % $ $ -- $ # # % $ YI ( -
$

&&
E 3 D 3 ? 6 7 3

1+
# - 5 ( 1 $ 4 #
$ (A (

Example 7-2: example7-2.c

#include<stdio.h>

void test(int a);

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 # $ % % (

Example 7-3: example7-3.c

#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;
}

void test(int *ptr)


{
printf("\nModifying the value inside memory address %ld",&m);
*ptr=5;
}

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 #

Val=*ptr; /* finding the value ptr points to */

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 % ( #

Example 7-4 (Bubble sort): example7-4.c

#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;

printf("Array before sort:\n\n");


for(i=0;i<5;i++)
printf("ar[%d]=%d\n",i,ar[i]);

n=5; /*numberof items in sort array*/

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]);
}

printf("Array after sort:\n\n");


for(i=0;i<5;i++)
printf("ar[%d]=%d\n",i,ar[i]);

system("pause");
return 0;
}

void swap(int *a,int *b)


{
int temp;

temp=*a;
*a=*b;
*b=temp;
}

8$ #

Array before sort:

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 ## - ! ( "

8 ! " ! 8 E ; "< " 1$ ' '


+ ; $ < 1$ 8 " 1 $ " ' $ = > $
" ; $ < 8

8 $ " + " ' 18 '' '$


' $ + 18

8 * = 1 8> " ! " ! $ ( $ ! 8

&4

You might also like