For Beginner C Programming Language Note
For Beginner C Programming Language Note
Is : -
?
= Fast
Requirement : -
Der
+7 Ctt
* Format
int main C) {
seton 0;
* First Program
# include Lstdioh>
int main C) {
used
"
seturn 0; screen
Note : -
In = new line .
: -
stdio.tn = standard input , output
: -
int =
Data type
main = Main Function
7
# include Lstdio h
-
>
int main C) £
* Data Type
# include Lstdio .h
>
int main C) { Note :-|| = Comment L Single Line Comment }
int a = to; : -
/* . .
.
* I { Multi Line Comment }
float b= 10.3 ;
' '
char c- -
H :
Data type in C : -
"
psintf ("°bd\n ,
a
); iz int =
1,213,4,
-10 -
,
too - , (5+5.5--10)
"
[ 01°F In b) ;
'
psintf ,
>
ii float = 1.010.32 ,
i. 2 (1.0+13.2--14.2)
"
(
"
Seton 0
; Note : -
olod is used for integer .
(
-
.
: -
01°C is used for Char .
# include <stdio.tn>
Rules : -
"
The value of pz is : -
van -
name =
''
010.2L .
,
PZ) ;
setorn 0 ;
* Psograns = Calculator
int main C) K
11 Declaring Variables
Second Number ;
11 printing
1- dwa
" "
add) ;
print L C The sun is
•
,
"
C
"
"
psintt ( The
" "
zeturno ;
A- include dstdio.tn>
11 Declaring Variables
points (
"
$ fisst Number );
"
scarf [ of d
'
Note ! -
scarf is used to input user input
,
.
"
In ) ;
psintf ( second
"
Enter Number :
"
scarf ( "
• 1- d , $ second Number ) ;
11 Calculating
add = first Number + second Number ;
Second Number;
,
" "
psintt ( ☒ dln
"
points ( The
" "
seturno ;
# define PZ 3.14
int main C) {
float • adieus ;
):
"
point (
"
Enter Radios : In
"
Asea
=
PZ * • adieus * • adieus ;
" "
f-
pzintf ( The asea of circle is
,
asea );
Seton 0
;
* Operator
# include < stdio.tn>
Operator = +, -
, * , 1,04
i -
= is Assignment operator .
int :
=3 ; + t is Increment operator
-
a .
: is Decrement
int b Operator
-
2;
- -
: .
int -5;
c- : -
51$ is AND
: -
11 is OR
int sum =
a + btc ; : -
! is NOT
(
"
The Hd
"
sum is sum
,
i 2 is less then
} : -
> = is greater then or equal to
i. -
= = is equal to
# include < stdio.tn>
27
int main C) L
int a =3 ;
int b
-
-
2;
int c = 5;
int sum =
@ + b) * C;
"
(
"
The Hd );
psintf sum is ,
sum
seturno;
Glass A = Milk ,
Glass D= Water
# include <stdio.tn>
int main C) {
11 Declaring Variable
int a =D ;
int b = 20;
int C ;
11 Calculation to swap
c=a ;
a :b ;
b- -
C;
11 Point value
"
(
"
The
psintf value of a is 4-d. In ,
a );
psintf [
"
sectors 0 ;
* Condition , Loop
program
-
( Enter );
" "
Your Age
psintf :
statement;
" "
psintf ( You );
"
are voter .
}
else {
psintt (
"
You a re not voter
"
);
se torn 0;
# include Lstdio .
>
h
int main C) L
int number ;
"
points ( );
' '
Enter number :
51 number ) ;
"
scarf ( Hd
"
if ( numbers of 2 = = 0 ) {
"
psintt ( Even );
"
Number
y
else {
);
"
psintfl
"
odd Number
}
• etum 0;
>
* G- eater Number
int main C) {
int nv m 2--200 ,
mom 2=30 , mum -5=406
);
"
and
psintfl then
"
Num 2 is greater mom 2 nom >
point (
" "
else {
"
psrintf (
' '
y
seturn 0 ;
3
* Waite in C which tells weather the input
a
program , user
int main C) {
char letter ;
"
( Enter character: ) ;
"
printf
"
( letter
'
a' letter II letter
'
O' 11
'
);
Lowercase Vowel = = = 11 = e = =
' i
II letter ==
'
letter = =
'
v
is
' ' '
( letter
'
U');
' '
II
' '
II letter 11 letter
'
A II letter E letter I O
Uppercase Vowel -_ = = = = = = =
= ==
is
Vowel
if ( isLowercase 11 is Uppercase vowel ) £
"
vowel
psintf (
"
01°C is .
,
letter) ;
}
else {
" ' '
,
letter);
y
Seton 0 ;
* ternary Operator
syntan : -
condition ? statement if true : statement
int main C) {
int age ;
( Enter );
" "
Your Age
psintf :
" "
"
psintfl ) not
" ' '
? You voter psintt [ voter );
'
eturn o ;
y
* Switch case
int main C) £
int number ;
"
);
psintf
"
( Enter Number :
"
scant
"
( Hd , $ number) ;
switch ( number) {
case 1 :
" "
bseak ;
case 2 :
);
" "
bseak ;
case 3 :
psintf (
" "
Day is tuesday . );
bseak ;
default :
"
points (
"
Invalid Option ); .
7
seturno ;
int i ; statement
psintf (
" "
Raushan Kumar In );
y
Seton 0 ;
y
# include dstdio.tn> A point 1 to 100 by using
for loop
int main C) {
int i ;
y
setom 0 ;
* while Loop
# include Lstdio.tn>
idaushanln ) ;
"
psintt (
itt ;
b
seturno ;
# include Lstdio.tn>
int main C) £
int i =L ;
while ( i 2=100 ) £
"
"
i)
psintf ( olodln , ;
itt ;
b
seturno ;
* Do While Loop
Note : -
Do while loop ; point first the - check the
int main C) {
int i =L ; Syntan : -
do L
do { statement ;
"
( olodln }
"
psintf ,
i ); while ( condition ) ;
itt ;
y
while ( i < = 100 ) ;
Seton 0;
int main C) {
int i = 100 ;
do {
"
( olodln
"
psintf , );
i
i -
-
}
while ( i > =\ );
Seton 0;
* B-eak : -
B-eat the entire Loop .
Continue : -
Bypass
* Break
A- include < stdioh>
int main C) {
int i ;
points (
"
4-dln ,
i) ;
if (i== 101£
bseak ;
}
setorn 0;
y
* Continue
int main C) {
int i ;
piintf ( olodln
"
, )
i ;
}
setorn 0;
y
* Pattern = * * * * *
* * * * *
* * * * *
* * * * *
# include zstdio.tn>
int main C) L
int i
,
K ;
psintl ( * );
}
);
" "
points ( In
}
seturno ;
y
* Patteson = *
* *
* * *
* * * *
* * * * *
int main C) L
int i
,
K ;
-12
for ( 1<=5 ; K > =i ; K - -
)&
"
);
"
points ( *
y
seturn 0 ;
* Function : -
A set of task that perform costain operation .
# include { stdio.tn>
int main C) £
C) ;
point Name
• eturn 0;
y
C) {
void point Name
" "
Raus han In );
psintf (
y
☒ Types of Function
int main C) {
int 1-n ,
Sn ;
"
);
"
"
scant (
"
odd ,
$ Gn ) ;
"
);
"
scarf L odd
" "
, $ sn );
"
,
add lfn , sn ) );
"
psintfl
"
The diff is 4- dln ,
subtract lfn.sn)) ;
• etusn 0;
t
int add ( int a
,
int b) {
int sum = atb ;
zeturn sum ;
- eturn sub ;
* Vasi able scope .
stdio.tn?voidsagHe11oCchannameEJj;-
# include
↳ Function definition
int main C) L
"
say
Hello ( Bishwora; ); "
seturn 0 ;
psintf L
"
Hello ÑÉÉ→ Function parameter .
# include Lstdio . h
>
int main C) {
);
"
"
L Raushan );
" "
say Hello
nocturn 0
;
>
'
(
"
4- s 's );
points Hello name
"
(
"
}
* static
int main C) {
int = i ;
say Hello
setornoj
y
void C) {
say Hello
"
( from 1-dln
"
numtt ;
* Function Recursion : -
2 ! = 2*1
"
(
"
The factorial otd 51=5*4*3*2 # 1
psintf is ,
factorial ( nuns ) );
seturn 0;
y
int factorial ( int nom )d
if ( nom =
-
2) L
setorn 1;
y
* Assay = It can store multiple value of same type in same variable .
t
# include 2stdio.tn> In simple way
seturn 0
; scarf ( Hod ,
✗ .
for ( int i=0 ; ion ; )h
itt
[i]);
"
psintfl
"
Price 4- d is -4L ,
i +2 , price
nocturn 0 ;
int main C) {
int n ,
nom
,
i I 11 size of array .
I for loop .
);
"
number :\n
pssinttl Enter
"
the
"
↳ diln
"
( Hd
"
scarf ,
☆ nuns []) ; i
Value of Hd is ,
y
saturn 0 ;
y
# include < stdio.tn>
int main C) {
int n ,
nom
,
i
;
In ) ;
"
( Enter
"
"
↳ diln
"
( Hd
"
scarf ,
$ nuns [i]) ;
psintt ( to din ,
nom
y
saturn 0 ;
int main C) {
int n ,
nom
,
i
,
-1, temp ;
In ) ;
"
( Enter
"
"
↳ diln
"
( Hd
"
scarf ,
$ nuns [i]) ;
11 sorting value
it (n om E) < nom
[i ] )L
temp = rum [i];
nom [i ] num[j]; =
nom
[i] = temp ;
:
✗ pointing value
nom
,
y
saturn 0 ;
int a [2)[3) =L
{ 112,3} ,
{ 74,5}
Y;
points (
"
Hd
"
,a[2][I]) ;
Saturn 0
;
* Pointer : -
It is a variable that contain address of other variable .
tdio.tn
# include >
int main C) {
int a= 10
; 11 Normal vasriable
int X-p =
Stai A pointer variable
" "
,P);
"
address of p is -1-2
return 0 ;
y
tdio.tn
# include >
int main C) {
int a= 10
;
int x-p =
§a ;
int n ;
n=*p;
" "
,P);
"
value of p is -1-2
" "
sreturn 0;
* Pointer Addition
* sum
int main 1) {
int fn ,
sn , *a
,
*
b. sum ;
"
In ) ;
"
"
scant ( Hd
"
, $fn ) ;
psintf (
"
[
"
scant ◦ Xd , Isn);
a = 51 fn ;
b = Issn;
sum = * a
+
* b ;
"
old
"
prints ( The );
sum
sum is ,
set ur n 0;
y
* subtraction
int main 1) {
int fn ,
sn , *a
,
*
b. sum ;
"
In ) ;
"
"
scant [ Hd , $fn ) ;
psintf (
"
[
"
scant ◦ Xd , Isn);
a = $ fn ;
b = $ sn
;
sub = * a - * b ;
"
Hd sub );
"
set ur n 0;
* Multiplication
# include < stdio.tn>
int main 1) {
int fn ,
sn , *a
,
*
b. sum ;
"
In ) ;
"
"
scant [ Hd , $fn ) ;
psintf (
"
[
"
scant ◦ Xd , Isn);
a = $ fn ;
b = Issn;
Mutt = * a * * b ;
"
Hd ,muH );
"
set ur n 0;
* Division
int main 1) {
int fn ,
sn , *a
,
*
b. sum ;
"
In ) ;
"
"
scant [ Hd , $fn ) ;
psintf (
"
[
"
scant ◦ Xd , Isn);
a = 51 fn ;
b = Isn;
dir = * a
/ * b ;
"
Hd
"
set ur n 0;
* Pointer Swap i -
Variable swap
int a = 20;
int b = 30 ;
"
swap
( $915b);
psintf (
"
Hd
"
seturn 0;
✗
void swap ( int *
a
,
int * b) L
int temp =
*a
;
*
a =
*
b;
*
b =
temp ;
* Pointer Assay : -
# include LStdio.tn>
int main 1) L
[5 ] { 10,20, -3940,507,
int a =
int x-p ;
p=§a[0] ;
" "
Ptt ;
psintf (
" "
seturn 0 ;
y
By using Loop
# include 2stdio.tn>
int main 1) L
[5 ] { 10,20, -3940,503,
int a =
int x-p , i ;
p=§a[0] ;
for (i=0; i< 5 ; it f) {
" "
Ptt ;
seturn 0 ;
* stains : -
"
4- S );
psinttl
"
Hello ,
name
seturn 0;
* string Length : -
# include 2stdio.tn>
int main C) £
psintf ( :L );
"
full
"
Enter your name
gets Chllname ) ;
11 Finding Length
saturn 0;
y
* string Copy : -
int main C) £
cha - copy
First Name [25 ];
psintf [Enter
"
finest name : -
);
return 0;
# include <stdio.tn>
int main C) L
prints [Enter );
"
Lisst name :
-
gets ( tiost
Name );
psintf (
"
Enter last );
"
name :
-
name is / lastname )) ;
,
setusn 0
;
y
* String Compasision : -
int main C) £
point [ Enter );
"
first name :
"
);
"
Equal );
"
y
else {
points (
"
Both Equal ) ;
"
String Are Not .
y
setosn 0 ;
>
* string Lower : _
int main C)
2
points ( :L );
" "
gets ( firstName ) ;
setusn 0;
y
* string Uppercase:
-
int main C)
L
points ( :L );
" "
gets ( firstName ) ;
sretusn 0;
* storing Reverse
int main C)
L
points ( :L );
" "
gets ( firstName ) ;
sretusn 0;
* Waite a
program in C which find length of storing using pointer .
int main C) £
"
first
prints [ Enter name :L );
>
int count = 0;
10 ){
' '
while (*p ! =
count + +;
ptt ;
}
seton count ;
* structure : -
# include <stdio.tn>
struct student {
int id;
is;
int main C) L
struct student 51 ;
↳ Name of vasiable
51 -
id =L ;
51 mask . = 60 ;
" "
52 - id =3;
52 . mask = 25;
"
);
"
SI mask
: Hd SI 1-s
' '
51 id I. mask 52 .name );
pssintf (
"
Hd name : ◦ s
SIZD
.
: , , ,
);
"
'
Kd Obd : %s id , mask , sz.name
psintf [ 52 ID - : ◦
52 mask : 52 name / 52 .
52 .
setusn 0;
y
* Assay of structure :-(Assay = Collection of same element)
# include {stdio.tn>
struct student {
int mask ;
3;
int main C) {
scant (
"
Hs Hd Hd .n ame
,
-
11 For Psinting
for (i=o ; i< 5 ; itt) {
SE]
"
(
"
student 1-dln -1s, 8011 : Hd mark X-D its -011 n o , $s[i] mask ) ;
psintf
◦
name : :
/
.
.
, ,
Seton 0;
* Nesting structure : -
A- include {stdio.tn>
struct student {
chase name
[20] ;
int so //n o ;
int mask;
};
struct address {
struct student 5;
y;
int main 1) {
struct address a ;
"
psintf ( address :X ) ;
"
Enter name,
soll , mask and
"
04s old 04s
"
5) a. ☆ a. s.si/1no,5a.s.mask,a.addsess);
scant ( ◦ bd ,
5. n a m e ,
"
s.ro/1no, mask , address)
\n%s
pointf. [
◦ Kd odd 04s ,
a. 5. n a m e , a. a. s . a.
;
setusn 0 ;
* Union : -
# include <stdio.li
union student {
int soll ;
float fee ;
int main C) {
union student at ;
91.2011 = too;
Hd ,aI soll) ;
" "
psintf (
-
a 1. fee = 2000.03
"
(
"
off , atlee) ;
psintf
stscpy ( at name
, Raushan );
"
a-2.name ) ;
"
ks
psinttr (
-
,
setusno ;
"
* Dynamic Memos> Allocation : -
We allocate memory we have to use
"
stdlib.tn in header file .
# include {stdio.tn>
int main C) {
int a ;
"
of (a));
ps.in/-f(''/-d ,
size
11 Answer is U Byte .
return 0;
y
* # include <stdio.hn
int main C) {
int as - [to];
1- d
"
of Cass)) Byte
ps.in/-f( I/ Answer
"
◦ size ; is 40
, .
return 0;
* Ma /Ioc in C : -
E. g :
pts = (int *) malloc ( too * size of (float));
# include {stdio.hr>
# include <stdlib.tn>
int main C) {
int a
,
i
, x-p ;
"
( Enter :L );
"
scant (
"
Hd ,$a);
p = ( int *
) make (a* size of ( int));
if ( P = = HULL) {
"
);
"
else L
p[%d ]
"
Enter value it
,
$p[i];
"
scant (
"
old
,
6
for (i=o ; iaa ; ittjk
"
points ( value is i
is
,
:
Saturn Oj
is
* Ca /Ioc in C : -
Allocates memory and initializes all bits to ze ro .
E.g :
pts = (float *) Cadoc ( 25, sized (float) ) ;
# include <stdio.tn>
# include <stdlib - h> A standard liabsay
int main C) {
int a
,
i
, x-p ;
"
for In );
psintf (
"
Enter size assay
:
( Hd $a);
" "
scant ,
= ( int *
) cake ( a , sized (int));
p
if ( P = = HULL) {
"
);
"
else L
p[%d ]
"
value it
psintf ,
$p[ ];
"
scant (
"
i
old
,
y
for (i=o ; iaa ; ittjk
"
points ( value is i
is
,
y
tree @) ;
Y seturno ;
* Real/0C in C : -
syntan :
pts = sea /Ioc (pts, n) ;
pts =
pointer
int main C) {
chase x-p ;
p = ( Chas *
) make (4* sized ( chare) );
"
);
"
psrintf ( obv );
"
Hs is value and address is , P, P
if ( P = = HULL)
{
);
''
fails
"
allocation
psintf ( Memory
.
y
else {
"
tree ( P);
y
Saturn 0;
11 Cseate a file
11 Read a file -
s
11 Write a file - w
11 Update a file
-
s = seat mode = St
-
w = wsite mode = wt
- a = appoint = at
* Read File : -
11 fgetcc ) ; fputc () ;
int main C) {
FILE *
fp ; 11 Lp = File Open
chase ch ;
11 Create a tent file and write something and s ave it in same folder /place (Filename
.
=
.tn/-)if(fp----
"
data
fp topers [ data
" "
= - tut ,
8 );
HULL ) {
);
"
psintfl
"
No File Found .
i
while ( ch ! = EOF )k
ch = fgetccfp);
points ( ich);
" "
HC
tclose ( fp);
setuon 0;
* fputcl ) ;
int main C) {
FILE *
fp ; 11 Lp = File Open
chase ch ;
11 Create a tent file and write something and s ave it in same folder /place (Filename
.
=
.tn/-)psintfEEntes
"
data
fp topers '(
" "
);
'
= data .tnt ,
w
you
save
9 .
while (2) h
Ich);
"
scant ( Okc
"
,
)L
' '
it ( ch =
-
_ 9
breaks
>
fputc ( ch , fp) ;
b
fclose Cfp);
0;
setorn