You are on page 1of 31

C/C++ Programming language tutorial in Hindi: Introduction

C computer language ; . C/C++


, . .
. Hindi C/C++ programming language ,
Hindi C/C++ programming language , C/C++
.
C/C++ programming language hindi tutorial
; computer language C/C++ .
C/C++ , C/C++
. . .
Compiler
computer (computer program) English computer
. computer . software
program computer language convert , computer . software compiler .
compiler
Note: compiler Install C/C++ program Run "Run Program Here" click .
Winodows OS devcpp Linux gcc compiler .
windows . Downloads section 9mb
. . .

Important Notice
C/C++ computer program run online, compiler Install . "Run Program Here" click
. compiler . .
.
program save (execution) . .
program
===Windows ===
1 . dev-c++ open . (Start -> All Programs -> Dev-C++)
2 . Dev-C++ "Tip of the Day" window . ( tips
)
3 . File ( ) New Source File .
. program .
?
1

#include <stdio.h>

2
3

int main() {

printf("Namaskar");

5
6

scanf("%s");

return 0;

4 . menu Execute ->Compile .


: program spelling Compile Error .

5 . menu Execute -> Run . window Namaskar. program output .


.
==Ubuntu Linux ==
1 . text editor (vim , gedit , emacs ...) .
2 . program . namaskar.c .
C program .c .
?
1

#include <stdio.h>

2
3

int main() {

printf("Namaskar");

5
return 0;

6
7

3. terminal cd command . 4 command


.
. ls : current .
. cd .. : current .
. cd < >: current
. pwd : current full location .
4 .
gcc namaskar.c Enter . error compile .
Hindi C/C++ programming language tutorial switch case statement . if else
statement . switch case statement if else
. example . example integer . variable value decide
...

#include <stdio.h>

int main() {

float v1 = 23;
float v2 = 9;

float result;

char c = 's';

switch(c) {
case 'a':
result = v1 + v2;
printf("Result of addition is %f \n", result);
break;
case 's':
result = v1 - v2;
printf("Result of subtraction is %f \n", result);
break;
case 'm':
result = v1 * v2;
printf("Result of multiplication is %f \n", result);
break;
default:
printf("No operation selected.\n");
}

scanf("%s");
return 1;
}

. variable c define type char value s . (char ) code


. switch variable c case compare . example c s .
case a case statements execute . . switch(c) c
case 's': s match result variable v1 v2 difference screen print Result of
substraction is 14. break statement run switch() { } block . break case match
case execute switch() variable match . case match default
statement execute . program c program run output .
float print printf %f use .
for loop .
Hindi C/C++ programming language tutorial for loop . for loop operator
.
%: . example .

int a = 15 % 2; a 1 15 2 1 .

int b = 254 % 2; b 0 254 2 0 .

int c = 37 % 10; c 7 37 10 7 .

for loop example . example 1 10 square print .


?
1
2
3
4
5
6
7
8
9
10
11
12
13

#include <stdio.h>
int main() {
int i;
int sq;
for(i=1; i<=10; i = i+1) {
sq = i*i;
printf("square of %d is %d.\n", i, sq);
}
scanf("%s");
return 1;
}

2 variable i, sq declare . for() . 2 ; 3


. program .

initialization statement . statement ; . statement


execute . example statement i = 1 i value 1 .

boolean statement output true false . execute check .


true for { } statement execute . example {} sq 1 i
1 , screen print square of 1 is 1.

execute , check true {} statement


execute . run check true {} statement execute
.. false . example i 2 i <= 10 true {} sq
4 i 2 . i {} statement execute . i 11 i <= 10
false for loop .
program run output . value change run output . program . 1
10 print .
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

#include <stdio.h>
int main() {
int i;
int sq;
for(i=1; i<=10; i = i+1) {
switch(i%2) {
case 0: printf("%d is even.\n", i);
break;
case 1: printf("%d is odd.\n", i);
break;
}
}
scanf("%s");
return 1;
}

program for loop i 1 10 for {} block


statement execute . {} switch case statement . for switch case
i 1 i%2 1 case 1 match print 1 is odd. for switch case
i 2 i%2 0 case 0 match print 2 is even. .
array runtime input . program scanf("%s") .
Hindi C/C++ programming language tutorial runtime program run
input . example . example user input print
.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

#include <stdio.h>
int main() {
printf("Please enter a number: ");
int i = 0;
scanf("%d", &i);
printf("You entered %d \n", i);
if(i%2 == 0) {
printf("Number is even\n");
} else {
printf("Number is odd\n");
}
scanf("%d", &i);
}

program run . screen print Please enter a number: . type enter .


print type (even) (odd). .
program printf("Please enter a number: "); screen print .
variable i define input . line scanf("%d", &i);
scanf printf . argument "%d" printf , input int .
argument &i input variable store . ( variable &
pointer .) screen 25 enter i 25 . program
- screen print enter . % .
2 0 print Number is even print Number is odd.
program scanf : windows program run window output
program window . scanf window input wait
. program run window program output .
Array .
variable variable value store variable define type
value store integer, character etc. Array value store . 100 int store array
. example variable Array store .
example 0 9 square array store print . program run .

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

#include <stdio.h>
int main() {
int i = 0;
int arr[10];
for(i = 0; i < 10; i++) {
arr[i] = i*i;
}
for(i = 0; i < 10; i++) {
printf("square of %d is %d\n", i, arr[i]);
}
scanf("%d", &i);
}

. int arr[10]; arr variable define 10 int store . int store . int
number store . numbering 0 start 10 int Array arr int arr[0] , arr[1] ...
arr size 10 10 int(arr[0] arr[9]) store . program . for loop
( 10 run ) i 0 arr[0] (arr array int) 0 for loop
array arr int(arr[9]) 81 . for loop arr print .
array access for loop .
while loop .

!! link
facebook twitter Google Buzz Share .
Hindi C/C++ programming language tutorial while loop . boolean statement
. value true loop statement run . example . while loop
1 10 square print

#include <stdio.h>

int main() {
int i=1;
int sq;
while(i<=10) {
sq = i*i;
printf("square of %d is %d.\n", i, sq);
i = i+1;
}

scanf("%d", &i);
return 1;
}

program i value 1 . while loop boolean statement i<=10 true i value 1 ,


loop statement execute . loop i value 1 . boolean statement true
i value 2 . . i value 11 boolean statement false while loop
. program . 1 10 square print .
string use interesting example .
!! link
facebook twitter Google Buzz Share .
C language tutorial in hindi string . Array char type variable .
What is String
char variable variable (letter) store . word(
) store ? char array , array
variable store help word sentence store . examples help word "hindi" store
print 3 important .

#include <stdio.h>

int main() {
char w[6];

w[0] = 'h';
w[1] = 'i';
w[2] = 'n';
w[3] = 'd';
w[4] = 'i';
w[5] = '\0';
printf("Word we stored is %s \n", w);
printf("1st letter of array is %c \n", w[0]);

scanf("%s", w);
return 0;
}
program run output . print
Word we stored is hindi
1st letter of array is h
. char array 6 char . array values
h i value . w[5] \0 2 char 1 char ,
word letter . \n line break character print \0 letter
read print . 6 char array hindi 5 char . string
print %s use . w string w[0], w[1]... char . printf statement string w print
%s use printf statement char w[0] print %c use .
program .

#include <stdio.h>

int main() {
char w[] = {'h', 'i', 'n', 'd', 'i', '\0'};
printf("Word we stored is %s \n", w);
printf("1st letter of array is %c \n", w[0]);

scanf("%s", w);
return 0;
}
difference line array define values variable
int line define value int x = 1; . array define array length
( array element )
char w[6] = {'h', 'i', 'n', 'd', 'i', '\0'};
char w[] = {'h', 'i', 'n', 'd', 'i', '\0'};
program .

#include <stdio.h>

int main() {

char w[] = "hindi";


printf("Word we stored is %s \n", w);
printf("1st letter of array is %c \n", w[0]);

scanf("%s", w);
return 0;
}

program 1 line difference char array shortcut use . shortcut char array
use .
char w[] = "hindi";
char array w . array element w[0] 'h', element w[1] 'i' .... . element w[5] '\0' .
syntax() \0 .
string , . recall .
blog . Reply . function
important .
!! link
facebook twitter Google Buzz Share .
Hindi C/C++ programming language tutorial function .
function use . if else statement topic program total marks obtained
marks percent division print . 5 students percentage division
program total marks obtained marks modify program compile run .
time . program percentage division print 5 copy-paste .
function use. function total marks obtained marks
percentage . percentage print program , division print program .
program function define . run run . function call run
.

int get_percent(int total marks, int obtained marks) {


int percent = obtained_marks*100/total_marks;
return percent;
}
int function return . example function int type return .
function , get_percent . () function input variable parameter argument
input variable type function access . input int .
function input , input () . 2 input . {}
function . input percent . return .
int function int type value (return ). return percent return int
type . return statement run , return function value return
function value return .
function use . program run .

#include <stdio.h>
int get_percent(int total_marks, int obtained_marks) {
int percent = obtained_marks*100/total_marks;

return percent;
}
int main() {
int percent;
percent = get_percent(500, 360);
printf("Percent is %d\n", percent);
percent = get_percent(500, 340);
printf("Percent is %d\n", percent);
scanf("%d", &percent);
return 0;
}
function define get_percent, int type value return 2 int type parameter
.
main() program run start . main int type percent variable define .
get_percent function call define . function 2 parameter 500 360 . parameter
important . function call function run , total_marks value 500
obtained_marks value 360 . values basis get_percent percent value 80 return .
percent = get_percent(500, 360);
get_percent return percent store . main() percent print .
get_percent function call value percent variable store print .
note main function parameter ( main() () ) int return
return 0; . C program run main function call . main
function call sequence call . main function program finish .
program run main function error main method not found.
program division print . run .

#include <stdio.h>
void get_percent(int total_marks, int obtained_marks) {
int percent = obtained_marks*100/total_marks;
if(percent >= 60) {
printf("Congrats!! You passed in 1st division.\n");
printf("Your percentage is %d.\n",percent);
}
else if(percent >= 45) {
printf("You passed in 2nd division.\n");
printf("Your percentage is %d.\n",percent);
}
else if(percent >= 33) {
printf("You just passed in 3rd division.\n");
printf("Your percentage is %d.\n",percent);
}
else {
printf("Sorry! you failed.\n");

printf("Your percentage is %d.\n",percent);


}
}
int main() {
get_percent(500, 360);
get_percent(500, 340);

int abc;
scanf("%d", &abc);
return 0;
}
example get_percent function division print . function difference function print
main print . void function return , main
function call , example value function return variable store .
Exercise:
1. function print_month int parameter number function print .
10 October print . function return , print . function main
parameter call .
2. program main scanf use input input parameter print_month call .
example .
!! link
facebook twitter Google Buzz Share .
Hindi C/C++ programming language tutorial function examples variable scope .
C function C variables .
What is scope
C variable scope variable declare define value .
function variable x define function variable read/write function
. loop variable define loop variable read/write .
. {...} define variable read/write .
example .
program register add print . program run
Menu register name add register print .

#include <stdio.h>
char list[10][20];
int length = 0;
void add_name() {
if(length >= 10) {
printf("list is full\n");
return;
}

printf("Enter the name: ");


scanf("%s", list[length]);
length = length + 1;
printf("name added\n");
}
void print_list() {
int i = 0;
for(i=0; i < length; i++) {
int serial_no = i + 1;
printf("%d : %s\n", serial_no, list[i]);
}
}
int main() {
int input = 0;
while(input != 3) {
if(input == 0) {
printf("0 - Print this menu\n");
printf("1 - add name to list\n");
printf("2 - print list\n");
printf("3 - quit program\n");
}
else if(input == 1) {
add_name();
}
else if(input == 2) {
print_list();
}
else {
printf("Wrong Input, try again\n");
}
scanf("%d", &input);
}
}
program run menu print enter . List add 1 press
enter , add . type enter . add 1 enter . List
print 2 Enter . program run .
. 2 variable list length declare . variable function
function loop read/write . variable {} declare

access , access .
char list[10][20]; . char list[10]; list array 10 char . char
list[10][20]; list array 20 char 10 array . 10 char 10 array array 20
char .
int a[2][3] .

a = {

{1,2,3} ,

{4,5,6}

};

a 2 array array a[0] a[1] . array 3 3 int .


char list[10][20]; use store . string char array store . list 10 char array
10 store . array 20 char store 20 letter
. variable length currently list , 0 .
add_name function call list add . check list 10 return use
function , scanf use list variable read . scanf argument "%s"
string read argument char array . list[0], list[1], list[2] ... list[19] 20 char array .
list add length 1 . list[length] list list list[length]
(length=0) char array . list[0] add length = 1 , list[length] (length = 1) array .
print_list function define , length list for loop print . for loop
variable serial_no define loop access .

main function program run start . if-else statement use input variable value according list
add , list print , menu print . if-else while loop , run input value 3
. while loop input variable scanf use input read , input 3 while loop program
finish otherwise while loop input value according if-else use list add , list print ,
menu print input variable scanf use input read .
function important .
Hindi C/C++ programming language tutorial important keywords break continue use ,
loops (for loop, while loop, switch case) .

use of break in c/c++


c/c++ loop break use . int array
number , for loop use array element check number . for loop
array check , number for loop loop
. break use . for loop while loop break use .
example . 100 200 search 21 .

#include <stdio.h>

int main() {
int i;
for(i=100; i<=200; i++) {
if(i%21 == 0) {
printf("1st such number is %d\n", i);

break;
}
}
scanf("%d", &i);
return 0;
}
for loop if statement . % . i%21
value 0 21 for loop i if ( for loop i
100 200 for loop statement run .) if statement run i value
105(21 ) if break run for loop . finally for loop statement i
100 105 value run i=105 break run for loop .
c/c++ while loop break use . switch case statement break use .

use of continue in c/c++


loop (for loop, while loop) statement run . loop
loop loop run loop iteration start ,
continue use . example . example array number . continue use odd
numbers ( ) print .

#include <stdio.h>
int main() {
int arr[] = {1,4,7,2,0,-5,8,17,5,-10};
int length = 10;
int i;
for(i=0; i<10; i++) {
if(arr[i]%2 == 0) {
continue;
}
printf("Odd number is %d\n", arr[i]);
}
scanf("%d", &i);
return 0;
}
example for loop use array number . number even continue use skip
for loop .
while loop continue use .
blog . Reply . function
important .
Hindi C/C++ programming language tutorial structure(struct) . datatype
int, char, float, double etc. datatype format data store . store
char array store , age store int store . (rectangle) length width

store 2 int variable store . length width store 2


variable . struct use variable . struct use
variable
length width store program struct use , better version struct use
.

#include <stdio.h>

int main() {
int length1 = 12;
int width1 = 8;

int length2 = 20;


int width2 = 11;

printf("Rectangle1: %d %d\n", length1, width1);


printf("Rectangle2: %d %d\n", length2, width2);

scanf("%d", &length1);
}
program . struct use .

#include <stdio.h>

struct Rectangle {
int length;
int width;
};
int main() {
struct Rectangle rect1;
struct Rectangle rect2;
rect1.length = 12;
rect1.width = 8;
rect2.length = 20;
rect2.width = 11;

printf("Rectangle1: %d %d\n", rect1.length, rect1.width);


printf("Rectangle2: %d %d\n", rect2.length, rect2.width);

scanf("%d", &(rect1.length));
}

. struct define Rectangle variable length width .


Rectangle datatype . int datatype store Rectangle datatype
int store length width . . struct
datatype . int datatype Rectangle datatype . {} variable
datatype store . Rectangle datatype variable length width store . ; compile
error .
Rectangle datatype define use . int variable define Rectangle variable
define , Rectangle struct . Rectangle type variable rect1 rect2 define .
variable value length width struct datatype define . rect1 length width
rect1.length, rect1.width access .

rect1 Rectangle datatype datatype value length width store . object


struct store .
. link facebook, twitter, Buzz share .
5 . Run
./a.out
namaskar . Run . .
program Run . (comment ) , C/C+
+ program error .
!!
: C/C++ computer program "Run Program here" click
C/C++ program Run .
Hindi C/C++ programming language tutorial program . program
.
?
#include <stdio.h>
1
int main() {
2
printf("Namaskar");
3
scanf("%s");
4
return 0;
5
}
6
C/C++ program screen Namaskar . program line
.
line printf("Namaskar"); . C printf function " "
screen . line Namaskar program output .

program lines . program 2 line 3 line ,


printf line changes .

program points
1.C program run main() { . program run main() { line run
(execute ) } program . 2 line execute .
2.printf function method ( function use ), function input function
.
3. function semicolon (;) . semicolon compiler function
command .
program .
?
#include <stdio.h>
1
int main() {
2
printf("Namaskar")
3
scanf("%s");
4
return 0;
5
}
6
program compile error
In function `int main()':
error: expected `;' before "scanf"
Execution terminated
scanf ; , . program printf
function ; . compile compile .
program compile . program modify compile .
topic variables program calculation , interest()
.
Hindi C/C++ programming language tutorial variables expressions .
variables in C/C++ programming language
Variable , . variable define . define
C/C++ program variable use . .
?
#include <stdio.h>
1
int main() {
2
int x;
3
x = 1;
4
x = 5;
5
6
scanf("%s");
7
return 1;
8
}
9
program program printf line 3 line add . 3 line
.
1. int main() { line int x;
program x variable define int variable integer
.
int temp; temp variable define integer () .
2. x = 1;
variable x define , . line x 1 . line x 5
.
C/C++ program variable define .
Expression in C/C++ programming language
Expression example .
?
1
2
3
4
5
6
7
8
9
10

#include <stdio.h>
int main() {
int price;
int rate ;
int time;
price = 1000;
rate = 5;
time = 3;

11
int interest;
12
interest = price*rate*time/100;
13
14
scanf("%s");
15
return 1;
16
}
17
program 3 variable: price, rate, time define set . variable interest define . *
(multiplication) / (division). value interest variable (interest)
value .
?
price*rate*time/100;
1
expression . 150 interest value 150 . +, -, *, / ()
expression .
Variable value screen print (printf )
topic printf(""); "" screen . .
line .
printf("Value is %d", 8);
screen print Value is 8
printf function 2 input parameter . printf 1st parameter . 1st parameter %d
paremeter screen %d parameter value print . %d , parameter
value print . .
?
#include <stdio.h>
1
2
int main() {
3
int price;
4
int rate ;
5
int time;
6
7
price = 1000;
8
rate = 5;
9
time = 3;
10
11
int interest;
12
interest = price*rate*time/100;
13
14
printf("Price is %d, Rate is %d, time is %d, calculated interest is %d", price, rate, time, interest);
15
16
scanf("%s");
17
return 1;
18
}
19
program print
Price is 1000, Rate is 5, time is 3, calculated interest is 150
1st parameter %d , paremeter .
Hindi C/C++ programming language tutorial variables expressions .
C/C++ hindi tutorial !

1.

Compiler - C/C++ program language . compile Linux gcc

filename.c terminal a.out binary . a.out (replace)


! Windows build run background !
2.

main() function - C program (execute) . printf() function

expression terminal print . main printf expressions


.
3.

Variable declare define (example int x =10) - variable 3

, datatype, integer. x (value), 10 .


int x; , declare define x value
. variable value compiler x memory
(allocate) memory , (desirable) . function program
variables declare . . = variable
value . variable execute value memory .

computer sequential bytes . address . byte 0 .


1 .. memory address . 1 byte 8 bits . bits value store . 1 bit
0 store 1.
C/C++ computer programming language datatypes ,

1.

int - (integer) . 2,147,483,648 2,147,483,647 (range) value store

.
2.

float - (real number) 1.2, .002 etc.

3.

char - (character a,b ...) store . Computer number store .

a store Computer 97 store . ASCII table character


Computer number store .
datatypes (variant) , 4.

double - float variant . float memory .

5.

short int - int variant , ( 32,768 32,767 ) , int int memory ..

6.

long int - int variant . 32 bit architecture machine 4 byte 64 bit architecture machine

8 bytes . int ( 9,223,372,036,854,775,808 +9,223,372,036,854,775,807 )


. long double .
7.

unsigned int - int variant negative value . 4 bytes positive (range)

0 +4,294,967,295 . unsigned long int unsigned short int .


topic(C/C++ hindi tutorial) program (rate), (time) (price) (interest)
. code , int . ? int ? !
code code replace .

#include <stdio.h>

int main() {

float price;
float rate ;
float time;

price = 1000.0 ;
rate = 5.3;
time = 3.5;

float interest;
interest = price*rate*time/100.0;

printf("Price is %f, Rate is %f, time is %f, calculated interest is %f", price, rate, time,
interest);

scanf("%s");
return 1;
}
printf %d %f , %d int print %f float value .
Note: C/C++ program Linux Run Segmentation Fault error , ignore
scanf .
Hindi C/C++ programming language tutorial if-else statement . comparison operator
. operator ( +, - ...). comparison operator
Boolean output , statement true false

< > : 3 > 1 output true 3 < 1 ka output false. 3 > 3 output false 3 3 . 3 < 3
output false .

<= >= : 3 >= 3 3 <= 3 output true . <= =<


. = > < .

== : value . int, char, compare . = . = assignment


operator variable value , == variable value compare .
if else .
if statement format .
if(boolean expression1) {
statement1;
} else if(boolean expression2) {
statement2;
}
.....
....
....
else {
statementN;
}
boolean expression value true false. boolean expression1 true statement1 execute .
boolean expression1 false boolean expression2 . true statement2 execute . .
boolean expression true statement execute , . boolean expression
true else statement execute . .
example total marks obtained marks percent percent grade print .

#include <stdio.h>

int main() {

int total_marks = 500;


int obtained_marks = 272;

int percent = obtained_marks*100/total_marks;

if(percent >= 60) {


printf("Congrats!! You passed in 1st division.\n");

printf("Your percentage is %d.\n",percent);


}
else if(percent >= 45) {
printf("You passed in 2nd division.\n");
printf("Your percentage is %d.\n",percent);
}
else if(percent >= 33) {
printf("You just passed in 3rd division.\n");
printf("Your percentage is %d.\n",percent);
}
else {
printf("Sorry! you failed.\n");
printf("Your percentage is %d.\n",percent);
}

scanf("%s");
return 1;
}
values percent value 54 .( percent value 54.4
program int . ) percent 60 condition (percent >= 60) result false
condition (percent >= 45) check true print

You passed in 2nd division.


Your percentage is 54
condition true statement execute . C/C++ program print .
printf \n print , \n new line character \n
print line print . \n run output . example obtained_marks
value C/C++ program run output . comment
.
topic if else example .
Hindi C/C++ programming language tutorial if else example .

basic example.

#include <stdio.h>
int main() {
int percent = 45;
if(percent >= 33) {
printf("Congrats!! You passed.\n");
}
else {

printf("Sorry.! you failed\n");


}
scanf("%s");
return 1;
}

example percent value change program run . check percent value 33


[if(percent >= 33)] screen print
Congrats!! You passed.
print (else)
Sorry.! you failed

{ } , . example speed 60
warning print .

#include <stdio.h>
int main() {
int speed = 65;
if(speed > 60) {
printf("Warning: Speed is in danger zone.\n");
}
else {
}
scanf("%s");
return 1;
}

speed value program run print . speed 60 print . program


.

#include <stdio.h>
int main() {
int speed = 65;
if(speed > 60) {
printf("Warning: Speed is in danger zone.\n");
}
scanf("%s");
return 1;
}
else else program .
C/C++ programming hindi tutorial program Ready .
pointer C/C++ . ,
comment .
Pointer Memory Memory Structure . computer
electronic device calculator, micro processor, mobile phone 0 1 , detail .
device Memory array . .
1

...

Memory Array position 0 store 1. position bit . 1 bit


0 store 1. Magnetic memory bit magnetic field direction represent . direction clockwise
computer 0 read , aniclockwise 1. Computer bit read write . 8 bits

. 8 bit 1 byte . computer Memory .

11010001 10011101 00101001 11111001 10000011 00100110

...

Computer memory(array) address 11010001. address 10011101 etc.


Computer Memory address byte(8 bits) , address byte,... byte
bit byte byte bit. directly byte bit , address
byte ( 8 bits) . Computer byte level addressing . byte address , byte
bit direct address .
. Memory variable store pointer . Binary to
Decimal Conversion Binary number .
Hindi C/C++ programming language tutorial Memory .
computer 1 byte(8 bits) read . 1 bit 0 1 store 1 byte 00000000
11111111 (8 binary ) store . 00000000 11111111
decimal convert .
00000000 = 0 (in decimal)
11111111 = 28 -1 = 255 (in decimal)
8 bit 1 byte 0 255 store .
store 2 byte store . 2 byte 00000000 00000000
11111111 11111111 0 65535 store . datatype store .
char: ASCII table (character) Decimal . char variable store
computer store . 'a' store 97(01100001)
store . table store 255 char store
1 byte memory .
short int: short int variable 2 byte(16 bits) store . 2 byte 0 65535(216-1) store . short int
negative value -32768(-215) 32767(215-1) .
unsigned short int: 2 byte negative value 0 65535 .
int: 4 byte negative value -231 231-1 value .
unsigned int: 4 byte positive value 0 232-1 value .
float: store format complex value . 4 byte .
double: 8 byte value .
datatype .
computer, memory byte read . byte address byte
byte address 1... . computer variable value memory byte byte
address 0,
address . variable( int) byte continuous store byte address
. int(4 bytes) 101st byte 104th byte store int address 101 .
pointer ready . pointer .
Hindi C/C++ programming language tutorial pointer C/C++
.
What is Pointer in C/C++ programming language
variable Computer memory store . store address
variable value memory stored . address pointer . C/C++ programming language
variable address (variable address = Memory address/location variable value
stored ). C/C++ programming language variable address & use . variable int x;
x address &x . int, char, float etc variable store variable address
. datatype address store integer store int datatype use
. int variable address store int* datatype use . char variable address store
variable address store
char* datatype use . example show variable
.

int x = 5;
int* p;
p = &x
int variable x define , p variable declare int address store . p variable x
address .( variable address & use .)

Address
0

Memory 100001 111001 001001 00001 011001


...

11
01
10
01
01

p = &x = 3

int x

variable p int* type x address stored - means p print x address print


.( x address 3 time C/C++ program run address
) p Memory address memory stored *p use ( p
memory address x memory x 5 stored *p 5 . example .
example change experiement .
#include <stdio.h>

int main() {
int x = 5;
int* p = &x;
printf("x = %d\n",x);
printf("address of x = %d\n", p);
printf("value at location p = %d\n", *p);

scanf("%d", &x);
return 0;
}
. topic Hindi C/C++ tutorial pointer use .
Hindi C/C++ programming language tutorial pointer .
C/C++ programming language tutorial int pointer example pointer datatype .
struct pointer . struct pointer example . struct use . C/C++
struct hindi click .

#include <stdio.h>

struct rectangle {
int width;
int length;
};

int main() {
struct rectangle r1;
r1.width = 10;
r1.length = 15;

struct rectangle* r2;


r2 = &r1;
printf("Original width=%d, length=%d\n", (*r2).width, (*r2).length);

r1.width = 20;
r1.length = 25;
printf("r1 changed, width=%d, length=%d\n", (*r2).width, (*r2).length);

(*r2).width = 5;
(*r2).length = 10;
printf("*r2 changed, width=%d, length=%d\n", r1.width, r1.length);
return 0;
}
C/C++ program . output . main struct rectangle type variable r1 define
pointer r2 declare struct rectangle address store . r1 address . r1 variable r2
pointer r1 address r1 value change r2 value read changed value . r2
value change r1 read changed value .
program . r2 address(pointer) r1 , *r2 struct rectangle (pointer address value *
*r2 r1 . (*r2).width r1.width change change .)
Important Note about pointer
1. (*r2).width shortcut r2->width . program (*r2).width r2->width (*r2).length r2->length
. program .
2. variable declare value ( int x;) define value ( int
x=1;) pointer declare variable address value read (* use *r2)
program crash segmentation fault .
. pointer .
Hindi C/C++ programming language tutorial pointer .
example print , actual output .

#include <stdio.h>

void add1(int i) {
i = i + 1;
}

int main() {
int x = 5;
printf("before adding x = %d \n", x);
add1(x);
printf("after adding x = %d \n", x);

scanf("%d", &x);
return 1;
}
run output .

before adding x = 5
after adding x = 5
output . add1 function parameter(argument) pass value 1
. main x value 5 x = 5 print . add1 function x pass x value 1
value 5 x = 5 print .
add1 function call pass variable x copy , add1 function copy value
change , original x value change .
function call pass variable copy copy call function . call
function arguments value change original variable value change , copy value change .
C++ program pointer use output

#include <stdio.h>

void add1(int* i) {
*i = *i + 1;
}

int main() {
int x = 5;
printf("before adding x = %d \n", x);
add1(&x);
printf("after adding x = %d \n", x);

scanf("%d", &x);
return 1;
}
run output .

before adding x = 5
after adding x = 6
function call x value . add1 function int int variable address ,
address value 1 .
main add1 function call x address pass . x address copy add1
function . add1 function address change x value change add1 function address store
variable value change . original address address copy, address variable ,
address store variable change original variable change .
pointer . wait .
Hindi C/C++ programming language tutorial array pointer .
array define .

int A[10];
A[0] = 0; A[1] = 10; A[2] = 20; ...
A pointer . A array position int address store . A position int address , A+1
position int address , A+2 position int ...
A address *A 0 ( A[0] = 0 ), *(A+1) 10 , *(A+2) 20 . *(A)+1 *(A+1)
. *(A)+1 A address position stored value 1 , *(A+1) A address
position position stored value. array position(index) value access .
A[n] *(A+n)
example .
?
#include <stdio.h>
1
2
int main() {
3
int A[] = {1,2,3,4};
4
printf("[%d, %d, %d, %d]\n",A[0], A[1], A[2], A[3]);
5
6
*A = 10;
7
*(A+1) = 20;
8
*(A+2) = 30;
9
*(A+3) = 40;
10
printf("[%d, %d, %d, %d]\n",A[0], A[1], A[2], A[3]);
11
12
A[0] = 0; A[1] = 2; A[2] = 4; A[3] = 6;
13
printf("[%d, %d, %d, %d]\n", *A, *(A+1), *(A+2), *(A+3));
14
15
scanf("%d", A);
16
return 0;
17
}
18
run output .
void pointer
pointer define variable address store . int* p p int
address store . void pointer variable address store . void pointer value
value int char float...
example help .
?
#include <stdio.h>
1
2
int main() {
3
void* p;
4
int i = 65;
5
p = &i;
6
printf("int value = %d\n", (int) *p);
7
char c = 'h';
8
p = &c;
9
printf("char value = %c\n", (char) *p);
10
11
scanf("%d", &i);
12
return 0;
13
}
14
example pointer define int* p void* p . pointer datatype variable
address store . program int address store char . int pointer value read
program value int , void pointer value program datatype
value datatype value stored value datatype typecast .
example void pointer int address store print (int) *p stored value int
(typecast) . *p value pointer (int) value int
. char . void pointer struct address store , value struct
typecast .
malloc and free
pointer variable define memory ( variable memory allocate
) pointer use memory address . memory allocate malloc.
malloc(4) call 4 byte memory allocate address return . int 4 byte ,
malloc(4) call int memory .
malloc memory address type data int float string double char array , malloc
void pointer (void*) return . use int store .
?
int* i = (int*) malloc(4); //malloc void* . int int pointer(int*) typecast
1
*i = 1;
2
printf("%d",*i);
3
system int memory , 4 byte . C function sizeof
datatype .

sizeof(int) 4 ( system int 4 byte ). int memory malloc(sizeof(int )) use . 10


int store .
?
int* a = (int*) malloc(10*sizeof(int));
1
int i;
2
for(i=0;i<10;i++) *(a+i) = 12;// *(a+0), *(a+1), *(a+2), *(a+3) ... etc
3
malloc(10*sizeof(int)) 10 int memory memory byte address . byte(a) int
store . int byte (a+1) store . (a+1) byte address . address value store/read
*(a+1) use ( address(pointer) p value store/read *p use )
user input
?
int* a = (int*) malloc(10*sizeof(int));
1
int i;
2
for(i=0;i<10;i++) scanf("Please enter number: %d", a+i);// *(a+0), *(a+1), *(a+2), *(a+3) ... etc
3
variable define memory . variable use memory free
. malloc use memory free . free free call . for
example
?
int* a = (int*) malloc(10*sizeof(int));
1
//use memory
2
free(a);
3
memory free program memory(RAM) . memory system slow hang
. program memory free .
C basic , #include <stdio.h> . related .
program , computer machine . (process)
program compile . compile (steps) . step .
program #include <stdio.h> .
printf function . function stdio.h file defined (declared) . program compile step
compiler #include <stdio.h> stdio.h file content . process pre-processing .
program printf function defined run . error
printf function .
stdio Standard Input and Output. file(stdio.h) function defined input/output . printf
output print scanf input . function stdio.h file defined .
Pre-processing
program line # start pre-processor . Compiler step process program modify
. . #include pre-processing directive file content
. pre-processing directive .
#define ABC 1
#define constant define . #define ABC 1 program ABC
1 compiler step 1 . #define macro .
#define ADD4(a) (a+4)
macro parameter . use program compile 1st step ADD4(x)
(x+4) x , variable . ADD4(5) (5+4) .
#ifdef xyz
...
#endif
#define use xyz define #ifdef #endif program compile
program .
Note: pre-processor line . line pre-processor pre-processor line
, program .
#include example . pre-processor commands example .
?
1

#include <stdio.h>

#define AREA(r) (PI*r*r)

#define PI 3.14159

4
5

int main() {

int rad = 10;

float area = AREA(rad);

printf("Area of circle is %f\n", area);

9
10

scanf("%d", &r);

11

return 0;

12

program AREA(rad) (PI*rad*rad) (3.14159*rad*rad) . function call


. function call function return value area AREA(rad)
(3.14159*rad*rad) function call value calculate area .
Hindi C/C++ programming language tutorial arithmetic operators .
operator . operator function , output
( return ) .

1.

+: sum return . 5+3 8 return , a = 5+3 a value 8

.
2.

-: difference return .

3.

*: product return .

4.

/: (divisor) return .

5.

%: (remainder) return .

operator arithmetic operator .


?
int a = 5;
1
int b = 3;
2
a + b;
3
program line a b add 8 return . a b value change . return value 8
variable save lost . example return value c save .
?
int a = 5;
1
int b = 3;
2
int c = a + b;
3
= operator right side expression value left side variable . c = a+b
right side a+b 8 c value 8 .
?
int a = 5;
1
int b = 3;
2
a = b;
3
example a = b a 3(b value) , b 5 = left side variable right
side value , . left side variable , . a+b = c c
a+b value program error , left side variable a+b . = right side
expression value return .
?
1
2
3
4

int a = 5;
int b = 3;
int c,d;
d = (c = a % b);//% .

example c = a%b c value 2 (c=a%b) 2 return d = (c=a%b) return value d


.
operators input(int,float etc.) output . 30/8 3 . 30 8 int
result int , , . agar 30.0/8.0 3.75 .
operator, binary operator . 2 . unary operators
.
1. x++ ++x : variable x value 1 . x++ x value return ++x x value return . x+
+ ++x use expression . example .
?
int a = 5;
1
int b = a++;
2
int c = ++a;
3
int d = (b++) + (++c);
4
program line run a value 1 6 a++ value return b 5 .
line run a value 6 7 c 7 ++a a value return . line run b
value 5 6, c value 7 8 , d 13 (d = b value + c value = 5+8)
x-- --x x value 1 . operator(++ --) int() . float double
datatype use error .
Binary Number System
c++ programming hindi tutorial logical operators . .
1. Binary Number System - , logical operator .
Binary decimal decimal binary convert .
. .
0 9 . use .
85 86 ( 5 6 ). , 0
1 . 59 60 9 ( 0 9 ) 0
.
0 7 8 . 8 ? . 7 10 ,
0 7 . .
0,1,2,3,4,5,6,7, 10,11,12,13,14,15,16,17,20, 21,22 .... 71,72,73,74,75,76,77,100,101,102 ...
. discussion site
discuss .
************
* 12. 0 7 14(
) .
system(Counting system) 0 9 10 , decimal system( 10,base 10) . 0 7
8 use system use octal system( 8,base 8) .
. Counting system 12 .
12 = 2x100 + 1x101 = 10 + 2
Octal system( 8,base 8) 14 . octal system 8 .
14 = 4x80 + 1x81 = 8 + 4
system . * , decimal system 12 octal system 14 .
0 3 4 . 0,1,2,3,10,11,12,13,20,21,22,23,30,31,32,33,100,101,102 ...
Quaternary system( 4,base 4) . * 30 .

**** * * * * * * * *

decimal(0-9 ) 1 2 3 4 5 6 7 8 9 10 11 12
0-7

1 2 3 4 5 6 7 10 11 12 13 14

0-3

1 2 3 10 11 12 13 20 21 22 23 30

- base 11(base 10) base 8 13, base 4 23 .


base 10 use . base base . base 8 75
= (75)8, base 4 123 = (123)4
base base 10 convert
. example .
(75)8 = 5x80 + 7x81 = 5x1 + 7x8 = 61 (base 10 )
(123)4 = 3x40 + 2x41 + 1x42 = 3x1+ 2x4 + 1x16 = 27 (base 10 )
base 10 base convert
base convert . . base .
. base . process 0 . example .
98 base 8 example:
98/8 = 12(),2() ---> 2()
12/8 = 1(), 4() ---> 4()
1/8 = 0(), 1() --> 1 ()
98 = (142)8
98 base 4 example:

98/4 = 24(),2() ---> 2()


24/4 = 6(), 0() ---> 0()
6/4 = 1(), 2() --> 2 ()
1/4 = 0(), 1() --> 1()
98 = (1202)4
Binary Number System: 2 - 0 1, :
0,1,10,11,100,101,110,111,1000,1001,1010,1011,1100 ... system 2 , Binary system() .
98 base 2 example:
98/2 = 49(),0() ---> 0()
49/2 = 24(), 1() ---> 1()
24/2 = 12(), 0() --> 0()
12/2 = 6(), 0() --> 0()
6/2 = 3(), 0() --> 0( )
3/2 = 1(), 1() --> 1()
1/2 = 0(), 1() --> 1( )
98 = (1100010)2
1100010 base 10 example:
(1100010)2 = 0x20 + 1x21 + 0x22 + 0x23 + 0x24 + 1x25 + 1x26 = 0+2+0+0+0+32+64 = 98
logical operators .
logical and comparision operator
C programming hindi tutorial operators .
Logical operators .
1. ! - uniary operator(NOT) , bool value , !true = false, !false = true
2. || - binary operator(OR) bool value true true false . true || false = true,
false || false = false
3. && - binary operator(AND) bool value false false true . true || false =
false, false || false = false
OR (), (true) result (true) . (false) result
(false) .
AND (), (true) result (true) . (false) result (false) .
Comparison Operators use . example use .
1. == - binary operator(equal) int bool value , bool value true false .
(5 == 5) = true , (1 == 2) == false
2. != - binary operator (not equal) int bool value , bool value true false
. (5 != 5) = false , (1 != 2) == true
3. > - binary operator (greater than) int value , int bool value true false . (5
> 5) = false , (3 > 2) == true
4. < - binary operator (less than) int value , int bool value true false . (5 <
5) = false , (1 < 2) == true
3. >= - binary operator (greater than or equal) int value , int bool value true
false . (5 > 5) = true , (3 > 2) == true
4. <= - binary operator (less than or equal) int value , int bool value true false
. (5 < 5) = true , (1 < 2) == true

example use . example () input print 28 29 .(


4 29 100 400 , 1900,2100... 28
2000 29 )
?
1
2
3
4
5
6
7
8
9

#include<stdio.h>
void main() {
int i;
printf("Enter the year: ");
scanf("%d",&i);
if(i%4==0 && i%100!=0) printf("Feb have 29 days");
else if(i%400==0) printf("Feb have 29 days");
else printf("Feb have 28 days");
}

% . 0 . example check 4 (i
%4 ==0 ) (&&) 100 (i %100 !=0) (1984, 2012) 400 . .
?
1
2
3
4
5
6
7
8

#include<stdio.h>
void main() {
int i;
printf("Enter the year: ");
scanf("%d",&i);
if((i%4==0 && i%100!=0) || i%400==0) printf("Feb have 29 days");
else printf("Feb have 28 days");
}

You might also like