Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
16 views

For Beginner C Programming Language Note

This document discusses the C programming language. It provides an introduction to C including what it is, its requirements, and how to write basic C programs. It covers data types, variables, constants, and input/output functions. Examples are provided to demonstrate basic C syntax and concepts.

Uploaded by

Roshan Chaudhary
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

For Beginner C Programming Language Note

This document discusses the C programming language. It provides an introduction to C including what it is, its requirements, and how to write basic C programs. It covers data types, variables, constants, and input/output functions. Examples are provided to demonstrate basic C syntax and concepts.

Uploaded by

Roshan Chaudhary
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

What is C ?

Is : -

Psog ramming language Develop by Dennis M .


Ritchie .

General Purpose Language .

?
= Fast

=p Hardware connection become


easy .

Requirement : -

Der
+7 Ctt

Open Dev Ctt and create new file .

* Format

# include < studio .h >

# include < conio.tn>

int main C) {

seton 0;

save this tile in • c.

* First Program

# include Lstdioh>

int main C) {

used
"

psintf ( Hello World); points


"

Note :- is to p&int data on

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) £

psintf [ Hello World In Hello Mars );


"
setvsn 0;

* 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)
"

(
"

points 01°C In c); Chan A -2 symbol [@ # $ -1-3


iii
=
, a -2 ,
,

Seton 0
; Note : -
olod is used for integer .

y : Hot is used for float

(
-
.

: -
01°C is used for Char .

> 010.2hr is used to show 2 digit number after decimal


.

* Vasriables and constant

# include <stdio.tn>

# define PI 3.14 Note : -


Vasi ables : -
Something that store value ;

Rules : -

cannot start with number


, symbol
int main C) {
int firstnumber = 10 ; constant : -
Once declare cannot changed .

"

psintf ( olod.tn firstnumber ) :


"

The fisst number is ,

psintf ( syntan data type value;


"

The value of pz is : -

van -
name =

''

010.2L .

,
PZ) ;

setorn 0 ;

* Psograns = Calculator

# include < stdio.is >

int main C) K

11 Declaring Variables

int first Number =/00 ;

int second Number = 20 ;

int add sub , Mul dir , modi


, ,
11 Calculating
add = first Number + second Number ;

sub = finest Number -

Second Number ;

mut = firstNumber * second Number;

dir = first Number 1 secondNumber ;

mod = firstNumber of second Number ;

11 printing
1- dwa
" "
add) ;
print L C The sun is

,
"

C
"

olodln with sound


psintt The sub is , sub ); Note : -
la = alot .

"

psintt Ci The mut is ☒ dln , mut);

psintt ( The
" "

dir is 06dm , dir );


"

point (" The mod is 4- dln , mod );

zeturno ;

* User input calculator

A- include dstdio.tn>

int main C)£

11 Declaring Variables

int finest Number ,


second Number ,
add, sub , Mul , dir, mod ;

11 Getting User Input


"

points (
"

Enter tisst Number : In );

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

sub = tisst Number -

Second Number;

mut = firstNumber * second Number;

dir = firstNumber 1 secondNumber ;

mod = firstNumber of second Number ;


11 printing
1- dlnla
" "
add) ;
print L C The sun is

,
" "

psintt C The sub is d- dln , sub );


"

psintt ( ☒ dln
"

The mut is , mut);

points ( The
" "

dir is 06dm , dir );


" "

point ( The mod is 4- dln , mod );

seturno ;

* Wsrite in C that find the of circle


program can asea .

# include <stdio.tn >

# define PZ 3.14

int main C) {

float • adieus ;

):
"

point (
"

Enter Radios : In
"

scant C off $ sardius) ;


"

Asea
=
PZ * • adieus * • adieus ;
" "
f-
pzintf ( The asea of circle is
,
asea );

Seton 0
;

* Operator
# include < stdio.tn>

int main C) £ Note : -

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
"

); : > is greater then


psintf
-

sum is sum
,

i 2 is less then

seturn 0; :-< = is less then or


equal to

} : -
> = 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;

* W- ite a in C which swaps two variables


program , .

Glass A = Water Glass B. = Milk


,

Glass A = Milk ,
Glass D= Water

# include <stdio.tn>

# include Lconio h> -

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 [
"

The value of b is 010 d. In , b) ;

sectors 0 ;

* Condition , Loop

# include Lstdioh > Note If condition : It tells the to


:
perform
-

program
-

int main C) { certain task only .

int age ; syntan :-


if ( condition)

( Enter );
" "
Your Age
psintf :
statement;
" "

scarf ( odd , $ age ) :


it Case > 22) {
"

psintf ( You );
"
are voter .

}
else {

psintt (
"
You a re not voter
"
);

se torn 0;

* Program that find the entered number is odd or even .

# 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

# include < stdio.tn >

int main C) {

int nv m 2--200 ,
mom 2=30 , mum -5=406

it ( nvm 27 nom 2 51$ nvm 2> mum 's ) {

);
"
and
psintfl then
"
Num 2 is greater mom 2 nom >

else it ( nuns 2 > mum 2 51$ numz > mum 's) {

point (
" "

Mum 2 is greater then mum 2 and mum -3 );

else {
"

psrintf (
' '

Num ] is greater then nun 2 and numz ):

y
seturn 0 ;

3
* Waite in C which tells weather the input
a
program , user

character is vowel letter or not .

# include < stdio.hn

int main C) {

char letter ;
"

( Enter character: ) ;
"

printf
"

scant ( ope $ letter) ;


"
,

int is Lowercase Vowel ,


is Uppercase Vowel ;

11 Return true if something is available


'

( 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 {
" ' '

psintf ( 010C is not vowel .

,
letter);

y
Seton 0 ;

* ternary Operator
syntan : -
condition ? statement if true : statement

# include < stdio.tn > if false .

int main C) {

int age ;

( Enter );
" "
Your Age
psintf :

" "

scarf ( odd , $ age ) :

"

psintfl ) not
" ' '
? You voter psintt [ voter );
'

age > = 22 use .


: you axe .

eturn o ;

y
* Switch case

# include Lstdio.tn >

int main C) £

int number ;
"
);
psintf
"

( Enter Number :

"

scant
"
( Hd , $ number) ;

11 Check the number using switch case

switch ( number) {

case 1 :

" "

psintf ( Dag is Sunday );

bseak ;

case 2 :

);
" "

psintf ( Day is Monday .

bseak ;

case 3 :

psintf (
" "

Day is tuesday . );

bseak ;

default :

"

points (
"
Invalid Option ); .

7
seturno ;

* Loop ( For Loop , While Loop , Do while 2-op )

# include 2stdio.tn> Note : -


For Loop

int main C) { for ( init ; condition ; inc /Dec ) L

int i ; statement

for [ i=o ; i < 100 ; itt ) y


{

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 ;

for Ci=o ; i 2=100 ; i + t )


{
"
points ( otdln
"
i );
,

y
setom 0 ;

* while Loop

# include Lstdio.tn>

int main ( 1£ syntan : while ( condition ) {

int i=2; statement }


while ( i 2=100 ) £
"

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

# include Lstdioh> condition .

int main C) {
int i =L ; Syntan : -
do L
do { statement ;
"

( olodln }
"

psintf ,
i ); while ( condition ) ;

itt ;

y
while ( i < = 100 ) ;

Seton 0;

# include < stdio.tn>

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 ;

for ( i=o ; i< 100 ; itt ) {


"

points (
"
4-dln ,
i) ;

if (i== 101£

bseak ;

}
setorn 0;

y
* Continue

A- include < stdioh>

int main C) {

int i ;

for ( i=o ; i< 100 ; itt ) {


if (i== 10 )
continue ;
"

piintf ( olodln
"

, )
i ;

}
setorn 0;

y
* Pattern = * * * * *

* * * * *

* * * * *

* * * * *

# include zstdio.tn>
int main C) L

int i
,
K ;

for ( i =L ; i < = u; itt) £

for ( 1<=2 ; 14<=5 ; 1<+1-32


" "

psintl ( * );

}
);
" "

points ( In

}
seturno ;
y

* Patteson = *

* *

* * *

* * * *

* * * * *

# include Lnstdio .h>

int main C) L

int i
,
K ;

for (i=5 ; i > =L; i -

-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

* include < stdio.tn>

int add ( int a


,
int b);
int subtract ( int a
,
int b) ;

int main C) {

int 1-n ,
Sn ;

"
);
"

psintf ( Enter 1st Number :

"

scant (
"
odd ,
$ Gn ) ;
"
);
"

psintf ( Enter 2nd Number :

scarf L odd
" "

, $ sn );

"

points ( The sum is 010dm "

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

int subtract ( int a


,
int b) L
int sub = a - b;

- eturn sub ;
* Vasi able scope .

stdio.tn?voidsagHe11oCchannameEJj;-
# include

↳ Function definition

int main C) L
"

say
Hello ( Bishwora; ); "

seturn 0 ;

void Hello ( char name [ 7) {


say
=

psintf L
"
Hello ÑÉÉ→ Function parameter .

↳ function sectors type


is

# include Lstdio . h
>

Void say Hello ( char name []);


int age
= 23 ; -7 Global scope

int main C) {

);
"
"

psrintf L Age is oft , age

L Raushan );
" "

say Hello

nocturn 0
;

>

void (Chan [7) {


say Hello
name

'

(
"
4- s 's );
points Hello name

"

(
"

psintf Age is X-D , age);

}
* static

# include < stdio.tn>

void say Hello C);

int main C) {

int = i ;

for (i=o ; into ;i++){


[ Raushan 'D; }
'

say Hello

setornoj
y

void C) {
say Hello

static int nom =L ;

"

( from 1-dln
"

psintf Hello number



, );
nuns

numtt ;

* Function Recursion : -

Call to same function .

# include < stdio.tn>

int main C) £ Note :-O ! =L

int noms = 5; 11=1*0 ! =L

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 .

Assay always start with 0 .

t
# include 2stdio.tn> In simple way

int main C) & # include Lstdioh>

float psice 1=30-0's int main C) L

float psice 2--45.05 int n = 5;

float psice 3=450-2 ; f


float
psice
4=5000-0; float psice[n];
for ( int i=O;iLn ; itt ) {
" "

psintf ( Brice points ( for d- d :\n ,i+1 ) ;


"
p&ice2);
"
2 is off / Enter psice
☆price [ i ]);
" "

seturn 0
; scarf ( Hod ,

✗ .
for ( int i=0 ; ion ; )h
itt

[i]);
"

psintfl
"
Price 4- d is -4L ,
i +2 , price

nocturn 0 ;

* Write ( WAP) in C that accept n number of assay and sort them in


a
psogram
ascending .

# include < stdio.tn>

int main C) {

int n ,
nom
,
i I 11 size of array .
I for loop .

);
"

number :\n
pssinttl Enter
"

the
"

scant ("%d , 51ns;

int nom [n ]; 11 define assay

// store value in variable

for (i=o ; ion ; itt )L


"

↳ diln
"

psintf ( Enter number ,i+I) ;


"

( Hd
"
scarf ,
☆ nuns []) ; i

for (i=o; :< niitt) {


"

Tod its ,numEiJ);


psintt (
"

Value of Hd is ,

y
saturn 0 ;

y
# include < stdio.tn>

int main C) {

int n ,
nom
,
i
;

In ) ;
"

( Enter
"

pssintt the number :

"

scant ("%d , 15ns;

int nom [n ]i 11 define assay

// store value in variable

for (i=O ; ion ; it f) L


"

↳ diln
"

psintf ( Enter number ,i+I) ;


"

( Hd
"
scarf ,
$ nuns [i]) ;

for (i=o; :< n


; i ++ ){
[i]);
" "

psintt ( to din ,
nom

y
saturn 0 ;

# include < stdio.tn>

int main C) {

int n ,
nom
,
i
,
-1, temp ;

In ) ;
"

( Enter
"

pssintt the number :

"

scant ("%d , 15ns;

int nom [n ]i 11 define assay

// store value in variable

for (i=O ; ion ; it f) L


"

↳ diln
"

psintf ( Enter number ,i+I) ;


"

( Hd
"
scarf ,
$ nuns [i]) ;

11 sorting value

b- ( i=o ; ion ; it +1L


for (i=o ; jinn ; → + f) L

it (n om E) < nom
[i ] )L
temp = rum [i];

nom [i ] num[j]; =
nom
[i] = temp ;

:
✗ pointing value

for (i=o; :< niitt) {


psinttl "%d\n [i]);
"

nom
,

y
saturn 0 ;

* Multi Dimensional Assay

# include < stdio.tn>


int main C) &

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

" "

points ( The value of a is %dln , a) ;


The
psintf (
"

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

" "

points ( The value of a is %dln , a) ;


The
psintf (
"

,P);
"

value of p is -1-2
" "

psintf ( The value of n is Hd / n );

sreturn 0;

* Pointer Addition

* sum

# include < stdio.tn>

int main 1) {

int fn ,
sn , *a
,
*
b. sum ;
"

In ) ;
"

psinttl Enter first number :

"

scant ( Hd
"

, $fn ) ;

psintf (
"

Enter second number : Iri);

[
"

scant ◦ Xd , Isn);

a = 51 fn ;

b = Issn;

sum = * a
+
* b ;
"
old
"

prints ( The );
sum
sum is ,

set ur n 0;

y
* subtraction

# include < stdio.tn>

int main 1) {

int fn ,
sn , *a
,
*
b. sum ;
"

In ) ;
"

psinttl Enter first number :

"

scant [ Hd , $fn ) ;

psintf (
"

Enter second number : Iri);

[
"

scant ◦ Xd , Isn);

a = $ fn ;

b = $ sn
;

sub = * a - * b ;
"
Hd sub );
"

points ( The sub is ,

set ur n 0;

* Multiplication
# include < stdio.tn>

int main 1) {

int fn ,
sn , *a
,
*
b. sum ;
"

In ) ;
"

psinttl Enter first number :

"

scant [ Hd , $fn ) ;

psintf (
"

Enter second number : Iri);

[
"

scant ◦ Xd , Isn);

a = $ fn ;

b = Issn;

Mutt = * a * * b ;
"
Hd ,muH );
"

points ( The molt is

set ur n 0;

* Division

# include < stdio.tn>

int main 1) {

int fn ,
sn , *a
,
*
b. sum ;
"

In ) ;
"

psinttl Enter first number :

"

scant [ Hd , $fn ) ;

psintf (
"

Enter second number : Iri);

[
"

scant ◦ Xd , Isn);
a = 51 fn ;

b = Isn;

dir = * a
/ * b ;
"
Hd
"

points ( The dir is / dir );

set ur n 0;

* Pointer Swap i -

Variable swap

# include < stdio.tn>

int main C) &

int a = 20;

int b = 30 ;

"

points ( odd -4dm , b);


"

The value of a is and value of b is a ,

swap
( $915b);

psintf (
"
Hd
"

The value of a is and value of b is odd / a. b)I

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

" "

points ( The value of P is -4dL x-p ) ;


,

Ptt ;

psintf (
" "

The value of P is 06th ,


*
p) ;

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) {
" "

points ( The value oh P is



lodln x-p ) ;
,

Ptt ;

seturn 0 ;

* stains : -

# include < stdio.tn>

# include < st-ins.tn>

int main C) &

chase name [25] ;


In ) ;
"

prints Enter your name :

gets (name ) I 11 Scant only hose string .

"
4- S );
psinttl
"
Hello ,
name

seturn 0;

* string Length : -

# include 2stdio.tn>

int main C) £

chase to/Inane [50J ;

psintf ( :L );
"
full
"
Enter your name

gets Chllname ) ;

11 Finding Length

total Length = stolen (tollname );


"

psintf [ The length of -4s is odd ,


fullName ,
total Length ) 's

saturn 0;

y
* string Copy : -

# include < stdio.tn>

# include < storing .h>

int main C) £

chase finest Name [257;

cha - copy
First Name [25 ];

psintf [Enter
"

finest name : -

);

gets (first Name ) ;


stscpy ( copyfirst Name ,
first Name );
"
Copy First Name) ;
"
( First name 04s
psintf is /

return 0;

* String concatenation [ Concert = Join ) : -

# include <stdio.tn>

# include < string .h>

int main C) L

chase first Name [25];


chase last Name [25];
Chas fullName [50] ;

prints [Enter );
"

Lisst name :
-

gets ( tiost
Name );

psintf (
"

Enter last );
"

name :
-

gets ( last Name );

psintf ( Full stscat ( firstName


"
4s
"

name is / lastname )) ;
,

setusn 0
;

y
* String Compasision : -

# include Lstdio.tn >

# include < string .h>

int main C) £

chase first Name [25] ;


chase last Name [ ]; 25

point [ Enter );
"

first name :

gets ( first Name) ;

"
);
"

psintfl Enter last name


:

gets ( last Name) ;

if ( stscmp ( first Name ,


lastName) ==o)L

Equal );
"

psintt [ Both Stains Are .

y
else {
points (
"
Both Equal ) ;
"
String Are Not .

y
setosn 0 ;

>

* string Lower : _

# include < stdio.tn


# include <stains h> -

int main C)
2

Chas firstName [25];

points ( :L );
" "

Enter first name

gets ( firstName ) ;

Sts /Ws ( firstName ));


point [ 04s ?
"

Lower case of First name is

setusn 0;

y
* string Uppercase:
-

# include Lstdio - h>

# include <stains h> -

int main C)
L

Chas firstName [25];

points ( :L );
" "

Enter first name

gets ( firstName ) ;

point [ 04s 'S stoups ( firstName ));


"

Upper case of First name is

sretusn 0;

* storing Reverse

# include Lstdio - h>

# include <stains h> -

int main C)
L

Chas firstName [25];

points ( :L );
" "

Enter first name

gets ( firstName ) ;

stssev ( firstName ));


point [ 04s ?
"

Reverse case of First name is

sretusn 0;

* Waite a
program in C which find length of storing using pointer .

# include < stdio.tn>

# include <storing h>


-

int main C) £

Chas hostname [25];

"

first
prints [ Enter name :L );

gets ( tisst Name ) ;


int length = string _
inctisstllame );
Hd 's length);
psintf [ The length of storing is
setusn 0 ;

>

int storing _ in (chase


*
P) {

int count = 0;

10 ){
' '

while (*p ! =

count + +;

ptt ;
}
seton count ;

* structure : -

Group of disimilas element . We can


hold different vasiable in same variable .

# include <stdio.tn>

# include <storing .h>

struct student {

int id;

chase name [30 ];


int mask ;

is;

int main C) L

struct student 51 ;

↳ Name of vagi able


struct student 52 ;

↳ Name of vasiable

51 -
id =L ;

51 mask . = 60 ;

" "

stscopyGI.name , Raus ha > );

52 - id =3;

52 . mask = 25;

"

);
"

51-8 copy (S2 .n ame , Raman

11 Printing Structure Value

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 {

chase name [20];


int so /Ino ;

int mask ;

3;

int main C) {

stout student s[5];


int i;

for (i=o ; i <5


; itt )L
"

soll and mask student Hd 2) ;


"

psintf ( Enter name


,
of ,
it

,$s[i] $s[i ]-8011n o, $s[i] mask );


"

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>

# include <storing h> -

struct student {

chase name
[20] ;

int so //n o ;

int mask;

};
struct address {

chase address [100 ];

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

# include <string .h>

union student {

int soll ;

float fee ;

Chas name [20];


7;

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 .

11 malloch ) , callocc ), sealocl ) ,


tree C)

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

Syntan : pts = ( cast Type *) malloc ( size);

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

psintf size for assay


"

scant (
"
Hd ,$a);

p = ( int *
) make (a* size of ( int));

if ( P = = HULL) {
"
);
"

points ( Memory allocation fails .

else L

for (i=o ; i < a


;
itt ) L

p[%d ]
"

psintf ( for 2);


"

Enter value it
,

$p[i];
"

scant (
"
old
,

6
for (i=o ; iaa ; ittjk
"

Hd and address odd In ,p[i] $p[ ]);


"

points ( value is i
is
,

:
Saturn Oj
is
* Ca /Ioc in C : -
Allocates memory and initializes all bits to ze ro .

pts ( cast Type *) calloc ( n size);


syntan : = ,

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) {
"
);
"

points ( Memory allocation fails .

else L

for (i=o ; i < a


;
itt )L

p[%d ]
"

( Enter for 2);


"

value it
psintf ,

$p[ ];
"

scant (
"
i
old
,

y
for (i=o ; iaa ; ittjk
"

Hd and address odd In ,p[i] $p[ ]);


"

points ( value is i
is
,

y
tree @) ;
Y seturno ;

* Real/0C in C : -

You can change the size of previously allocated memory

syntan :
pts = sea /Ioc (pts, n) ;

pts =
pointer

# include < stdio.tn>

# include < stdlib h> -

# include < string .h>

int main C) {

chase x-p ;

p = ( Chas *
) make (4* sized ( chare) );
"
);
"

stscpy (P, Raus has Chaudhary


"

psrintf ( obv );
"
Hs is value and address is , P, P

if ( P = = HULL)
{
);
''
fails
"
allocation
psintf ( Memory
.

y
else {

sea /10C (P , -300 # size of (chars ));


p
=

"

psint.tl?o-s is value and address is obv ,


P, P );

tree ( P);

y
Saturn 0;

* File Handling :( File = A document that contain something)

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

# include Lstdioih? I/ tgetc E)

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

# include Lstdio h> -

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

value that want to and press for quit );


"

you
save
9 .

while (2) h
Ich);
"

scant ( Okc
"
,

)L
' '

it ( ch =
-
_ 9

breaks

>

fputc ( ch , fp) ;

b
fclose Cfp);

0;
setorn

You might also like