[go: up one dir, main page]

0% found this document useful (0 votes)
30 views223 pages

C Learning

Uploaded by

sourav.nag1996
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)
30 views223 pages

C Learning

Uploaded by

sourav.nag1996
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/ 223

Basics of C :

● Variable and Constant and keyword:

int a=3;

a=5;

so a is variable and 3/5 are constant


Primary Constant: Integer Constant,RealConstant,CharacterConstant
Secondary Constant: Array,Pointer,Structure

IntergerConstant :
Ex.: 426
+782
-8000
-7605
int si, m_hra ;

Range of integer const: -2^n to 2^n-1 (compiler is n bit )

Real constant /floating constant: nothing but decimal constant

float basal ;

Character Constants: single bit character like a,c,c,h, etc

char code ;
Char z=’A’
Keyword=if/else/while etc

● Print the result:

1. Only result

2.result with statement

What is ‘\n’ doing in this statement? It is called newline and it


takes the cursor to the next line. Therefore, you get the output
split over two lines. ‘\n’ is one of the several Escape
Sequences available in C.
● Receiving Input

To make the program general the program itself should ask the user to supply the values of p, n and r
through the keyboard during execution. This can be achieved using a function called scanf( ).This
function is a counter-part of the printf( ) function. printf( ) outputs the values to the screen whereas
scanf( ) receives them from the keyboard. This is illustrated in the program shown below
● Declare variable at 1st :
● Data Analysis
bits vs compliers size

Let say our compiler is 16bits means every things will be 16 bits

Must watch : https://youtu.be/AHP1Yo_FVew


Different Data Type

integers :(4)

for short integer we use short

for long integer we use long

In general we use int when purpose is not known


In 16 bit compiler data stored as

Character :(1)

1 byte data

Decimal Number

for short decimal we use float--4 byte

for long decimal we use double--8 byte

Type of variable

1)Local :scope only inside the function where its defined . Initialize inside of any function

2)Global: scope is every where in all function. Initialize outside of any function .

External variable are global variables with permanent(life time ) storage

3)Static : Here variable initialization will be done once only .

default value of any static variable is 0


o/p=11 12 13 o/p 11 11 11

here static int a=10 ---->it will be execute only once

1st fun () call ---> a=10 then a become 11 and print 11

2nd fun () call ---> "static int a =10 "skipped . a become 12 and print 12

3nd fun () call ---> "static int a =10 "skipped . a become 13 and print 13

default value of any static variable is 0

static function

All the function defined in C are global by default(one file's function can call another file's function ). If
we mention static before a function then that function will not be accessible from another file .It can
only accessible from the file where it written

Key:

every integer and char(ASCII) can be define as unsign and sign

unsign=always indicate +ve number

sign=data can be +ve or -ve depends on MSB 0(+ve) or LSB(-ve)


Integer float conversion

Int to float :

Int(4.5)=4

Float to int:

Float(5)=5.00
k is an integer variable and a is a real variable.

But

Int A;
A=(2.4*4.5)/6;

Right expression mixed with int and float.So return float

As the expression is return float but left hand is int

So expression value is 1.8

It convert to int as 1

So int A=1;

Example2:

int a=1;

float b=1.000

if (a=b) //float become int

if(a==b)

printf("HI");

output ? Ans is HI

Here when we say a (it is int )=b (it is float).Here b converter to int and become 1 and thus a==b
satisfied.

int-char conversion

int a=65;

char c;

c=65 Line1

//In Line1 65 will be consider as ASCII and ascii charact of 65which is A will store. Thus c hold 'A'

int a;

char k='B';
a=k Line1

//In Line1 a store ASCII value of 'B' which is 97

Arithmetic Operation
y*=7--->y=y*7

y+=67--->y=y+67

Steps

1)First find out the Highest priority operator AND perform its task and then next priority operator task

2)If two operator has same priority then see the associatively and perform L-->R or R-->L

Example1:

a=3*4%5/2

Here *,%,/ has same priority .So see the associatively which is L to R.So perform Left to Right

a=((3*4)%5)/2=1

Example2
Type casting
Converting one datatype to another
Decision Control:

● If statement:
Different style of conditions

● in C a non-zero value is considered to be true, where as a 0 is considered to be false.

a.if (3+2)

printf("cool")

Here if is satisfied as 3+2 is true

b. : if (2==5)

printf("cool")

2==5 false always so if statement not performed

● Use multiple condition as


Examples:

----------------------------------------------
● If-else statement :
● Nested if-else:

● Ladder if else
● A Word of Caution
We put i=100

If(i=5) :- its override i and set as 5 so condition is true

Correct : if(i==5)

● Ternary Operator :
-----------------

Loop

While
Infinite while loop:

● Range of integer -2^n to 2^(n-1).Here we assume our complier is 16 bit. So n=16.Max integer can
hold is 32767.Once i reach 32767 and then increment then 32767+1 it's become which falls
outside the valid integer range, so it goes to other side and becomes -32768 which
would certainly satisfy the condition in the while. This process goes on indefinitely
● Here we put a ";" after while

Incremental/Decrement Operator :

● i++🡪i=i+1

First initial value of I check i=0 ;0<10


Second increment i=0+1=1;
● ++i=i+1;

First I will increase i=0+1=1;


Then condition check 1<=10;
● Similay for i - - and - -i;

● i+=2 ----------🡪i=i+2;

for Loop:
Point 1:

It is important to note that the initialization, testing and incrementation part of a for loop can be
replaced by any valid expression.

Point 2:
initialization, testing and incrementationcan be done out of for


Point 3:

initiate multiple variables


Odd Loop:
If we don’t know number of time loop execute then we call odd loop

● By do-while loop:
● By for loop:
● By while

Break
In a loop once break seen, control come out of that loop.it always associate with if
--------------------------------------------------------------------------------

Continue:
Once continue encounter then lower part of the continue does not execute and control goes to for ( j = 1 ;
j <= 2 ; j++ )
Do-while loop:
1st execute the body and then check the condition

Example :
Pattern Design :

https://www.youtube.com/watch?v=6irHnysGbSI

pattern1:
my code :
#include<stdio.h>

int main(){

inti,j,a=1,p;

printf("Enter the value :");


scanf("%d",&p);

for(i=1;i<=p;i++)
{
printf("\n");

for(j=1;j<=p-i;j++)
{
printf(" ");
}

for(j=1;j<=a;j++)
{
printf("*");
}

a=a+2;

for(j=1;j<=p-i;j++)
{
printf(" ");
}}
printf("\n");
printf("\n");
printf("\n");
printf("\n");

}
Output:

Pattern2 :

#include<stdio.h>

int main(){

inti,j,a=0,k,m;

for(i=1;i<=4;i++)
{

printf("\n");
printf("\n");

for(j=1;j<=5-i;j++)
{
printf(" ");
}

printf("*");

for(k=1;k<=a;k++)
{
printf(" ");
}

if(i==1)
{
a=1;
}
else
{
a=a+2;
}

if(i!=1){
printf("*");
}

for(j=1;j<=4-i;j++)
{
printf(" ");
}

printf("\n");
printf("\n");

for(m=1;m<=9;m++)
{

printf("*");

}
Case Control Structure

Basics

Example
Tips and Traps:

1) impression that you get till now that u can use only cases arranged in ascending order, 1, 2, 3 and
default. You can in fact put the cases in any order you please
2) You are also allowed to use char values in case and switch as
In fact here when we use ‘v’, ‘a’, ‘x’ they are actually replaced by the ASCII values (118, 97, 120) of
these character constants.

3) Every statement in a switch must belong to some case or the other or always find some case only .
If a statement doesn’t belong to any case the compiler won’t report an error. However, the statement
would never get
executed. Switch always find out case within it
here "Hellow" will never print .

switch(i)

printf("Hellow)

o/p no error and no o/p

4)We can use expression with constants/variable in switch expression .Below are valid
if-else is slower than case ,but in case you can do limited task

12.Switch can't have same case value

90.case level must be constant not variable

5) If we have no default case, then the program simply falls through the entire switch and continues
with the next instruction (if any,) that follows the closing brace of switch.

6) Float is not allowed anyhere in case


We will get error in every float statement

7)Below is error free .We can write case under default as nested case
9)switch(x) and switch ((x)) same
10)Below will not through any error.within switch state its looking for case with 4

no o/p
11)What happened if break is not mentioned ?

first control go to 1 then run all the code below case1 until break is not enountered

12 )Below can be used

same as

go-to and exit()


go to is similar like JMP in uP

goto statement transfers control to the label ‘sos’, causing printf( ) following sos to be
executed.

The exit( ) function is a standard library function which terminates the execution of the program. It is
necessary to use this function since we don't want the statement

Function:

● Basic and Common structure:


If function return nothing then use void instead of int like

● Void Adder(inta,int b)-----🡪return nothing


● Adder(int a,int b)--🡪return int
● Int Adder(inta,int b)🡪return int
● Main()🡪always return int;

default return is int


● Scope within function:
i defined in main function.so it will active within that function but it will not valid in the
other function
● One Dicey Issue:
● Recursion Function :

Within a function its call itself;

Add number :
#include<stdio.h>

summer(int);

main()

int x=0;
summer(x);

summer(int sum)

int a;

printf("Enter the number ");

scanf("%d",&a);

sum=sum+a;

printf("Sum is : %d",sum);

printf("\n");

summer(sum);

KeyPoint:

1.A function can't be written within another function

2.Valid return statement

3. A function can return only one value at a time. Below are invalid
4.Calling convention from right to left

Surprisingly, it outputs 3 3 1.

5.Library Function
Pointers:

Basics:

int i = 3 ;
int *j ;

j = &i ;

&i=address of i=65524;

If int j is holding the address of i y define as

j=&i;

that means j* is the value of the const in j where j is already holding a address;Also we can say that j*
indicate the value whose address is inside j .
More complex :
k=65522=Its a address

*k=65524=value in the address 65522

*(*k)=*(65524)=3= value in the address 65524

KeyPoint

char *temp=str1 //base address of str1 string stored to temp

int *p=89 // address of 89 store to p

int *p={23,56,34,} //base address of array store to p

● Pointer in function :

Main

Adder(input,input,&output);

Print(output)//see the output

Adder(input,input,pointer for output)

Pointer-------------------------------------------------🡪output

Print(pointer)//see the output


}

}}

Here when we call the function then we just pass the address of output variable .In the calling function
evaluate the output (z) and store the result into that output location .So no need to return .

Mane suppose amr barite ( B-11/209) ya sum ta store korte hobe .So ami calling function ya amr barir
address ta B-11/209 ya pathiya di66i.Bol6i je sum evaluate kore amr bari te rekhe dite

Example :
#include<stdio.h>

void adder(int,int,int*);
main()

inta,b,c;

printf("enter 1st number:");

scanf("%d",&a);

printf("enter 2nd number:");

scanf("%d",&b);

adder(a,b,&c);

printf("sum is = %d",c);

void adder(intx,inty,int *z)

*z=x+y;

Advantage :
1.no need of return ,we can print the output from called/calling function both

2.we are passing the address of output so this is known as call by reference .

Array:

● Basic Structure:
a[i]
All elements must be same type int or floats

Int take 2 byte each float take 4 byte each.

Size must be declared at the initialization phase

● Array Initialization

Size must be initialized

● Entering Data into an Array

Case1:

for ( i = 0 ; i<= 29 ; i++ )


{
printf ( "\nEnter marks " ) ;
scanf ( "%d", &marks[i] ) ;
}

Case2:
● Reading data from Array:

Case1:

Int a[]={1,3,2,1,23}

We can access any particular element in array as

a[2]=2;

a[3]=1

we can simply print a[2]/a[3];

case2:

● Basics of pointer in Array:


We conclude that:
For array if p=624

after p++ , p become =626

● Thus how pointer increment in array :


● Pass array to function(base address,num of ele)

By normal way (call by value)


Pass one by one
By pointer (call by reference ):-------------(pass base address and num of elem)
2 D array :

● Basics:

Practically array stored as :


● How to put data in 2D array:

Type1:

Type2:
Type3:

Type 4:

● Get data from2D array:


Type0:

Type1:

S[3][4] is stored as:


*(s+1)---> indicate the 1st row value--->{1212,33}

*(s+2)---> indicate the 1st row value--->{1434,80}

print of *(s+0) or s[0] give base address of 0th row

print of sizeof(*s) or sizeof(s[0]) give num of column *element size


Type2:

For single di array s[i] is know as *(s+i);

implicitly indicate s=&s[0]

*(s+0)--->element in 0 index--->address of 0 element 65508

*(s+1)--->element in 1 index--- address of 0 element 65510(increment by 2 )

*(s+2)--->element in 2nd index--- address of 0 element 65512(increment by 2 )

s[2][1]
* ( s[2] + 1 )
*(*(s+2)+1)

s[i]-------------------------compiler know it as--------------🡪*(s+i);


s[2][1]-----------------------compiler know it as -------------🡪-* ( * ( s + 2 ) + 1 )

● Pass 2D array to function(base address,rownum,colnum,)


o/p:
Explain:

(i,j)=position of row and column index

String:

● Basics:

Pass base address of any string for print or scan


1D array: name
2D array: &name[2][0]

Each character in the array occupies one byte of memory and the
last character is always ‘\0’.

Character --------‘a’

String -------------“HellowBoss”

“%c”-------------🡪print only character

“%s”------------🡪print a entire string

To print anything string by %s then we have to pass the o index’s address of the string

Char name[25]=”Shuvankar”;

So to print name by %s we have pass the &name[0]=name;


● Print by %c:

By pointer(name --🡪&name[0])
● Take and print data by %s

Name=&name[0]

If we entered “shuvankar halder”

o/p shuvankar

string should not contain space;


take input by %s ----------------🡪 pass address of 0 index of string

print any string by %s---------🡪pass address of 0 index of string

● Take and print by get and put:

gets/%s for scanf---------------🡪pass the base address(index 0) of string;

puts/%s for printf-------------🡪pass the name of string

name=&name[0];

● Advantage of Pointer:

● we cannot assign a string to another, whereas, we can assign a char pointer to another char
pointer
● Also, once a string has been defined it cannot be initialized to another set of characters. Unlike
strings, such an operation is perfectly valid with char pointers.

● Use of Const:
● Library Function:

● Strlen:( Finds length of a string)(return integer lenght)


● strcpy( no return)
This function copies the contents of one string into another
● strcat( )(no return)
This function concatenates the source string at the end of the target
string. For example, “Bombay” and “Nagpur” on concatenation
would result into a string “BombayNagpur”. Here is an example of

● strcmp( )(match: return 0 match)


In the first call to strcmp( ), the two strings are identical—“Jerry”
and “Jerry”—and the value returned by strcmp( ) is zero. In the
second call, the first character of “Jerry” doesn't match with the
first character of “Ferry” and the result is 4, which is the numeric

difference between ASCII value of ‘J’ and ASCII value of ‘F’. In


the third call to strcmp( ) “Jerry” doesn’t match with “Jerry boy”,
because the null character at the end of “Jerry” doesn’t match the
blank in “Jerry boy”. The value returned is -32, which is the value
of null character minus the ASCII value of space, i.e., ‘\0’ minus
‘ ’, which is equal to -32.
The exact value of mismatch will rarely concern us. All we usually
want to know is whether or not the first string is alphabetically
before the second string. If it is, a negative value is returned; if it
isn’t, a positive value is returned. Any non-zero value means there
is a mismatch. Try to impleme
Basically passing the base address of two string;
● 2D String Data include/print by normal:

Key :Always pass base address of each row for both include and print

Data Include
● Type 1:(mention in code)

Here if u print masterlist[1] by %s ---> "parag"

Here if u print masterlist[1] +2 by %s ---> "rag" (omit first 2 digit )

Memory allocation:
● Type 2:(take from input)

Memory allocation:
Disadvantage :

Lot of memory are wastage

Data include and print


As we are printing by %s so we have to pass the 1st address of each string /name

Sample code:
#include<stdio.h>
main()
{
char name[34][20];
int i;
printf("enter the name\n");

for(i=0;i<=4;i++)
{
scanf("\n%s",&name[i][0]);
}
printf("yoour name\n");

for(i=0;i<=4;i++)
{
printf("%s\n",&name[i][0]);
}

another example
● Pass 2D string to function
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void searchname ( char [][12] ,int ,int );
int main()
{

char name[4][12],searchnm[12];
int row=4,col=12,i;

for (i=1;i<=4 ;i++)

{
printf ("Enter the %d Names",i);
scanf ("%s",&name[i][0]);
}
searchname(name,row,col);
}

void searchname ( char p[][12],int r,int c)


{
char searchnm[12];
int i;
printf ("Whom you want ?");
scanf("%s",searchnm);

for (i=1;i<=r ;i++)

{
printf ("Names are the %s Names ",p[i]);

● 2D String Data include/print by pointer :


Include:
to overcome that memory wastage below came
In this declaration names[ ] is an array of pointers. It contains base addresses of respective names.
That is, base address of “akshay” is stored in names[0], base address of “parag” is stored in
names[1]
and so on.
name[i] store the base address of each name .

*name[i] point the each name

Include and print


Sample code:
Disadvantage :
data can't be taken from user
*name used to store address of string .if
Char *name[6]
It take lots of garbage values so always we have initialize the
strings at the beginning.
Summary for array and string :
1D string

2D String ;
Summary for Array and String:

1.Pass array whole array to another function :


Pass base address and number of elements( for 1D num of data ,for 2D num of row and column)
2.For String we don't pass
3.For array $a[i][j] for scan and for a[i][j] for print
4.For string always mention base address for both print and scan as &name[2][0]

Global variable :

https://www.youtube.com/watch?v=SZY-8d1Rzkw
global variable is permanent storage

File :

● Basics :
● Whenever we open a file then file came to buffer memory and then it work
● File must be kept where main c file are stored
● Once file came to buffer then its’ end with EOF.
Operation of Mode:
Open and Read File :

● Open file in read mode---fopen


● Pass the address for cha and read it -----fgetc
● Close file--fclose

Read a file
● FILE *fp ;

Take a file pointer

● fp = fopen ( "PR1.C", "r" ) ;

PR1.C=file name which you want to open(in )


r=read mode
a)Firstly it searches on the disk the file to be opened.
b)Then it loads the file from the disk into a place in memory
called buffer.
c)It sets up a character pointer that points to the first
character of the buffer.

So fp is holding the address of 1stcharac of file.

If file is not present then it retuen NULL;

● Char ch = fgetc ( fp ) ;

1.Infgets() we pass the address of charac which retrn the charac on that address and then we print .

2.It also increment the pointer


● fclose ( fp ) :

Once we close the file we can no longer read from it using getc( ) unless we reopen the file. Note that
to close the file we don’t usethe filename but the file pointer fp. On closing the file the buffer
associated with the file is removed from memory.

Write in File:

Open file in

w mode=override existing and insert new(fp point from the begining)

a mode=append the string (fp point to last address of charc in the text)

Just use fputs(string_name,file pointer)

Just use fputc(charac_name,file_pointer)


o/p:
we have to open the file and check whether inserted or not
Existing text will be removed.
A File-copy Program

Read/Write different datatype:

https://www.youtube.com/watch?v=sdkvKOvqwAA

when ever there is a different type of data present like int/char/string then we use .here in the file all the
datatypes are consider as charac or string .like 1234 it occupy 4byte as its string
fprintf( )=write data in file(open file in w mode)=just write the values in the variable in the
file(variable ---🡪file)

fscanf()=read from file.(open file in r mode)=just put the data from variable to file

fprintf:

fscanf:
(file--🡪variables)
Roll name are written in the file

Fscanf read the data from file and store in roll/name which we just print
Read/Write record of data:

Insert lots of data:


Read lots of data:
Text Files and Binary Files
Text File:

A text file contains only textual information like alphabets, digits and special symbols. In actuality
the ASCII codes of these characters are stored in text files.

Key:
In text mode, a special character, whose ASCII value is 26, is inserted after the last character in the
file to mark the end of file. If this character is detected at any point in the file, the read function
would return the EOF signal to the program.

Binary File:
As against this, a binary file is merely a collection of bytes. This collection might be a compiled
version of a C program (say PR1.EXE), or music data stored in a wave file or a picture stored
in a graphic file.

To open this mode will be wbrbetc

Structures:

● Basics and initialization:

It's nothing but a concept of table where we can store data of different type

Or,
In memory:

● Insert data in structure:

Type1(only some few instances)

Type2(insert number of records as array format);


● Fetch data from structure:
● Some basic features:

a) copy one element to other:


b) Nested structure:
c) Pass to a function:
d) Pointer in Structure:
Basic pointer
We can’t use ptr.name or ptr.callno because ptr is

not a structure variable but a pointer to a structure, and the dot operator requires a structure variable
on its left. In such cases C provides an operator ->, called an arrow operator to refer to the structure
elements. Remember that on the left hand side of the ‘.’ structure operator, there must always be a
structure variable, whereas on the left hand side of the ‘->’ operator there must always be a pointer to
a structure. The arrangement of the structure variable and pointer to structure in memory is
shown in the

Call by reference
Miscellaneous for Placement

Data Structure
Infix A+B operator in middle

Prefix : +AB operator in left

Postfix : AB- operator in right

Infix to prefix/postfix

power operator ^ has high priority then /,*


1)Took the highest priority operator in the expression and perform that

prefix :operator came to left

postfix : operator came to right

2)If 2 operator has same priority then see the associatively and perform accordingly

There / and * has same priority .But associatively is L to R so we move L to R and evaluate the first
operator's operation which we encounter first while moving L to R.
Examples:
Infix expression calculation
https://www.youtube.com/watch?v=zl-iv3dLKIA
https://youtu.be/84BsI5VJPq4

Steps

1)First find out the Highest priority operator and perform its task and then next priority operator task

2)If two operator has same priority then see the associatively and perform L-->R or R-->L.when we move
L to R or vice versa then first operator we encounter from that same priority operator list will be
performed first .
Example1:

a=3*4%5/2

Here *,%,/ has same priority .So see the associatively which is L to R.So perform Left to Right

a=((3*4)%5)/2=1

Key issue in C code


Postfix Expression cal
steps

1)Scan left to right

2)When u encounter a operation then do as follow

Example
Prefix calculation cal

Steps

1)Scan right to left

2) When u encounter a operation then do as follow


case1 : sizeof
sizeof() give us size of any variable/constant etc. Its return the size

o/p=1

o/p=20

case2: typedef
Let we have very length data type which we have to repeat number of time .Using typedef we can
provide it's small nick name and use that nickname everywhere

exp1

exp2
We can also define global variable before main

case3 Dynamic Memory Allocation

Two type of mem possible

)Static Memory Allocation

int a=23;

float y=8.09

Here we have to predefine the memory size

char name[20];

Let user provide name[20]="Ram"=3 bit

So 17 bit is wastage

To avoid these type of wastage Dynamic Mem required

2)Dynamic Memory Allocation

Here we don't know the memory required

Malloc:
Return : Base adress:if memory allocation successful

NULL=If memory is not available to allocate

Here 10 block of 2 byte (int) is allocated and base address is returned in void pointer form void
pointer form

It return void pointer which we have to typecast .so it must be type casted to respective pointer .

Example1:
Key: Here we provide n from user .Means user as per user's requirement mem is allocating .

Memory allocated by this contain garbage values

Calloc:
return base address with void pointer

Here 2=each block size

10= number of block

Other things is same as malloc

Memory allocated by this contain zero values

Free()

Realloc()

To resize the data type which earlier created

case4 Bit operation

Priority are
In C we can perform all binary operation

These operators can operate upon ints and chars but not on floats and doubles.

Number of bit is =compiler bit=16 (default )

showbits()

showbits(5)=0000000000000101
Right shift

sign bit insert ------------------>> data

ch=11010111,

ch >> 1 would give 01101011 ---shift 1 sign bit left to right

ch >> 2 would give 00110101--- shift 2 sign bit left to right

f<<=1 means f=f<<1


f>>=1 means f=f>>1

caution:
Left shift

data-------------------<< insert 0

5225=0001010001101001

0001010001101001<<2=0101000110100100 (insert 0 from right to left )

Other are same as we studied in number system

Mixed of operation:

If so many operator is present then perform as per you did in inflex expression caculation
16^1=17(ans)

here due ++i i become 1 so second time loop will not execute

case5:Void pointer problem


https://youtu.be/-5IPZIHAvIA?list=PL7ersPsTyYt3EtkX5jIpmfLddaYtBYsmF
Here *p is void pointer. Means p can hold address of any datatype.So what datatype he can point out is
not mentioned. So pointer will be confused how much data allocate .So there will be error.

we have to type cast the p only

Here address(p) will be type cast not data *p

case6: macro
Steps of execution of any programming is as
Before compilation preprocessor execute the C code and run the macros
It globally declare that upper is 25

case 7 : Static variable

Type of variable

1)Local :scope only inside the function where its defined . Initialize inside of any function

2)Global: scope is every where in all function. Initialize outside of any function

3)Static : Here variable initialization will be done once only


o/p=11 12 13 o/p 11 11 11

here static int a=10 ---->it will be execute only once

1st fun () call ---> a=10 then a become 11 and print 11

2nd fun () call ---> "static int a =10 "skipped . a become 12 and print 12

3nd fun () call ---> "static int a =10 "skipped . a become 13 and print 13

Pointers

Pointers to 1D Array :
case1

int arr[]={23,4,6,7};

arr--->1000 *arr ---->*(arr+0)--->arr[0]--->23

arr+1---->1004 *(arr+1) ------>arr[1]--->4

case2:

https://youtu.be/ibj_AKOxpHo?list=PLBlnK6fEyqRggZZgYpPMUxdY1CYkZtARR

By * we are moving inside and by & we are moving out

int a[5]={1,2,3,4,5}

int (*p)[5]=&a;
Here (*p)[5] indicate a whole 5 elements array at a single quantity .that single entity's base address is
same as a

p[0] or *(p+0) is 1000 *p[0] or **p is 1

p[1] or *(p+1) is 1004 *p[1] or **(p+1) is 2

o/p 1

case3:

int *p[5]

create a 5 elements array where each elements is pointers


Pointers to 2D Array :
s[2][1]
* ( s[2] + 1 )
*(*(s+2)+1)

Case1:
https://youtu.be/3fOPOUnkcdQ?list=PLBlnK6fEyqRggZZgYpPMUxdY1CYkZtARR

int a[2][2]={1,2,3,4}.if I printf *p then we will get 1st row

In C data stored as row wise


Here we can see each row has unique address .

enter into each row as

a[0] or *(a+0)---> 1000

a[1] or *(a+1)--->1008

Access each elements


**a --->*a[0]-->a[0][0]--->1

**(a+1)--->*a[1]--->a[1][0]----> 3

Case2:

each element is pointers

case3:

int (*p)[2][3]={4,67,1,2,3,4};
*p point whole array as single quantity

Pointers to 3D Array :
s[2][1]
* ( s[2] + 1 )
*(*(s+2)+1)

https://youtu.be/3fOPOUnkcdQ?list=PLBlnK6fEyqRggZZgYpPMUxdY1CYkZtARR

int a[2][2][2]={1,2,3,4,5,6,7,8} So we have 2 2D array as row wise

We can also think


So addressing as

So access each 2D array


Access each 1D array and elements

Pointers in String:
Pass base address of any string for print or scan
case1:

char name[5][25]={"Hellow","cool","oiii"};

name[i] indicate base address i th string

printf ("%s",name[2])------> 2nd string " cool "

printf ("%s", name[2]+2) ----> printf from index 2 -->ol

case2:

In this declaration names[ ] is an array of pointers. It contains base addresses of respective names.
That is, base address of “akshay” is stored in names[0], base address of “parag” is stored in
names[1] and so on.
name[i] store the base address of i th name .

*name[i] point the each name

printf("%s",name[2])----> raman

Here we actually name[2] indicate base address of raman .for print we have to mention base address

printf("%s",name[2]+2)----> from (base address+2)-->man

Case3:

Double/Multi pointer in string :


https://www.youtube.com/watch?v=HTG3mXmPvCE

key: int *p=base address of any then p=base address

Pointers to Function :
https://youtu.be/BRsv3ZXoHto?list=PLBlnK6fEyqRggZZgYpPMUxdY1CYkZtARR

Base address of function is 4500

*ptr is pointing to that function


Key Points:

0.Interger 4 byte char 1 byte float 4 byte double 8 byte

1.int s[2][3]={{23,56},{45,23,56}}

Here in array data store in serially .{} not matter

2.

char *temp=str1 //base address of str1 string stored to temp

int *p=89 // address of 89 store to p

int *p={23,56,34,} //base address of array store to p

int *p=base address of any then p=base address


int **p=base address of any then p=base address
int arr[]=[23,45,56]
int *p=arr //p indicate the base of arr

int a=90;

int *p=&a //p point to a

3.

int a[]={23,56,78,12,34};

a++ ; //error Here a is constant ++ operator cann't work on him

int *p;

p=&a[0]

p++ //no error

4. Remember

(*p)()--->point to a function

(*p)[][]--->point to a array

5.

its not a error but no o/p as switch always find out case only within it

12.Switch can't have same case value and case value must be constant

case level must be constant


23.In case statement we can only use char and int not float

8.break always use in loop.If we mention outside of loop then error occur

6.

s[2][1]
* ( s[2] + 1 )
*(*(s+2)+1)

7.int arr[23]

Here 0 to 22th index elements will be 0 and after that garbeg values

after inserting data garbeg value will be present after 22th

8.every value in enum is 0 1 2 3 if not mentioned ..Its a array of any type data

9. ‘%u' treats the integer as unsigned, whereas ‘%d' treats the integer as signed.

10."%6.2f" meaning ?
Total digit allocated for whole float number . After .(dot) 2 digit will be taken AND remaining (6-2=4) will
be for before .(dot)

11. b[] is same as *b

14.

char *temp=str1 //base address of str1 string stored to temp

int *p=89 // address of 89 store to p

int *p={23,56,34,} //base address of array store to p

15.
Here int x=010 indicate 10 is in octave =8 in decimal

16.
printf return number of charact it print

scan returns number %d /%s/%c present

17.
All the function defined in C are global by default(one file's function can call another file's function ). If
we mention static before a function then that function will not be accessible from another file .It can
only accessible from the file where it written

18.
&4["Hellow"] means & *("Hellow" +4)

if string is char s[23] ="Hellow";

then that means & *(s +4)

&4["Hellow"]---> & *(s+4)---. it will delete first 4 letter and print remaining-->ow

Also in a print statement only 1st arguments will be print .Others will be ignored

19. Compile time vs run time

compile time is this

Run time error when error came here

20. External variable are global variables with permanent(life time ) storage

21.ternary operator can be written as


22.**p meaning

**p pointing to 3 through another pointer

****p meaning
25.Double pointer

**PPZ <----------------&b means ppz store the address of b

Here p is a base address which is move to ptr

Also if we write

int ***ptr=base address

then ptr hold that base address .ptr--->pointer--->pointer--->value


here p is a address so ptr take that p address

Same for here

16.

24.NULL pointers

https://youtu.be/oPScHNQDCkc
17.always convert address of element not convert element when needed

here char pointer hv to point a short data so address of a which is &a is type cast to char
pointer .p is also address

char is 1 byte so 320 is truncated

18.int arr[n]={23,45,12,3,40}

vallue of n must be define within same function

here the size of array cann't be define outside of function

19.
#include "stdio.h"
int foo(int a)
{
---------
}

int main()
{
foo;
-
}

Here no error but no o/p

20. if u declare a pointer outside of any function (globally) then its default value is NULL

21.WE can call main() recursively

it will run until its stack overflow

23. But a variable name has to start with either letter or underscore.
23.Remember below

o/p 0
24.starting with 0x is hexadecimal number

25.Staring with 0 is octal

Find output of Program :


Problem 89:

Ans -gh

similar way

Problem 90:
Problem34:

https://youtu.be/U8Ps0XSQdJw?list=PLBlnK6fEyqRggZZgYpPMUxdY1CYkZtARR
Thus we got 15
Problem 89:
Problem 89:

when f(5,5) called

&x=5

here actually address of 5 is &x .means x is 5

c=5

so x is 5 and c is 5

similar way

Ans =3024

Problem 35:

https://youtu.be/alEjARaJ-Fo?list=PLBlnK6fEyqRggZZgYpPMUxdY1CYkZtARR
Ans 19

Problem 45:

https://youtu.be/Mgygikl0Ym8?list=PLBlnK6fEyqRggZZgYpPMUxdY1CYkZtARR
Problem 89:

https://youtu.be/zt2Z8U_1kmw?list=PLBlnK6fEyqRggZZgYpPMUxdY1CYkZtARR
2356

(*ptr)[3]=x[3] (Let x=(*ptr)) =*(x+3)=*((*ptr+0)+3)=ptr[0][3]

Problem1

https://youtu.be/qzjgv1UKtQc?list=PL7ersPsTyYt3EtkX5jIpmfLddaYtBYsmF
o/p=5

p is holding a address of x let it 1000

Here *p++ first p will be increment to 1004(let int is 4 byte) then value of *(1004) will be considered

but

++*p here value of *p will be incremented

Problem2:

https://youtu.be/_5N-ScdbAw4?list=PL7ersPsTyYt3EtkX5jIpmfLddaYtBYsmF
output=6

++*p here value of *p which is 5 will be incremented

Problem 3:

https://www.youtube.com/watch?v=u6LuwLuLSrM&list=PL7ersPsTyYt3EtkX5jIpmfLddaYtBYsmF&index=3
8
o/p=error at ++a

++a means a=a+1

Here ++ operator only work on variable ,not constant .So ++5 will give error

a=&a[0]=constant

So you will get error

Problem4:

https://youtu.be/lgG4RFfiNCs?list=PL7ersPsTyYt3EtkX5jIpmfLddaYtBYsmF
30 30

A array is created

f1 print---------------------- 30

f2:

b[] also act as pointer like f1.


b[]=*b

it also give 30

Key Point

int a[]={23,56,78,12,34};

a++ ; //error Here a is constant ++ operator cann't work on him

int *p;

p=&a[0]

p++ Here p is variable ++ operator can work on him

Problem 5:

print of *(p+0) or p[0] give base address of 0th row


print of sizeof(*p) or sizeof(s[0]) give num of colum *element size

print of *(s+0) or s[0] give base address of 0th row

print of sizeof(*s) or sizeof(s[0]) give num of column *element size

1223

Here every block is a pointer .Means every block store a address.

p[1][0]=1223

* p[1][0]=*1223=value in 1223 address

for %d

*p=*(p+0) all the element in 0 th row =size is 20*int size=80

every row has 10 element

for %u

0th address of each row

Problem 6:

https://youtu.be/LIL86pdKnY4?list=PL7ersPsTyYt3EtkX5jIpmfLddaYtBYsmF
ans-800byte

Here p pointer point a matrix of [10][20] where p holding the base address of it .

Like for a normal case

int a;

int *p;

p=&a;

*p indicating a interget so sizeof(*p)=4

But in the question *p indicating a matrix of 10x20.Its size is 10*20*4=800(each element on that array is
int)

So sizeof(*p)=800

Problem 12

https://youtu.be/N9v27OKHimM?list=PL7ersPsTyYt3EtkX5jIpmfLddaYtBYsmF
o/p-200 sq cm

Here (*p)()--->point to a function .Here as there is no return in function so use void

*p--------------------------point---->unit()

once p() encounter then function get called

int (*p)()-->point to a function where return type is int

Key

(*p)()--->point to a function

(*p)[][]--->point to a array
Problem 7:

https://youtu.be/aVO1nz6wZok

ans=11 byte

Problem 8:

https://youtu.be/bPzGScJJgbQ?list=PL7ersPsTyYt3EtkX5jIpmfLddaYtBYsmF
ans - no swapping taken place

solution --see video

KeyPoint

char *temp=str1 //base address of str1 string stored to temp

int *p=89 // address of 89 store to p

int *p={23,56,34,} //base address of array store to p

Problem 16
https://youtu.be/GN6Vd0Oygm0?list=PL7ersPsTyYt3EtkX5jIpmfLddaYtBYsmF

10 20
Problem 9:

https://youtu.be/JBBuXl6H1Sw?list=PL7ersPsTyYt3EtkX5jIpmfLddaYtBYsmF
ans=4

char* will be consider as ptr

so ptr=char*

p1 is holding a address which is integer

Problem 10

https://youtu.be/TxwwpGclYm8?list=PL7ersPsTyYt3EtkX5jIpmfLddaYtBYsmF

2+3*2+3

as per infix expression calculation * is higher priority

2+(3*2)+3

=11

Problem 11:

https://youtu.be/-5IPZIHAvIA?list=PL7ersPsTyYt3EtkX5jIpmfLddaYtBYsmF
Here *p is void pointer. Means p can hold address of any datatype.So what datatype he can point out is
not mentioned. So pointer will be confused how much data allocate .So there will be error in print .

we have to type cast the p only

p is type cast to int pointer

Problem 13:
o/p 1 2

see static section

Problem 14:

o/p 35 19

see infix expression calculation

x*=3+4

priority of + is more than *=


x*=7

x=x*7=35

similar for y

Problem 15:

https://youtu.be/dEBZ3Fz2BmE?list=PL7ersPsTyYt3EtkX5jIpmfLddaYtBYsmF

ans 2 1 0

see video

Problem 16

https://youtu.be/ek1A5w7IDpo?list=PL7ersPsTyYt3EtkX5jIpmfLddaYtBYsmF

PAL
1000 1001 1002 1003 1004 1005 1006

B H O P A L '\0'

Here when we use %s then we pass base address .From base address to till get '\0' string printed

base address of "BHPAL"+3=1003

string start printing from 1003 till get '0\'

Problem 17

https://youtu.be/N30oLLPdEP4?list=PL7ersPsTyYt3EtkX5jIpmfLddaYtBYsmF

Ans -P

Problem 18

https://youtu.be/AxAtLgNW0Zg?list=PL7ersPsTyYt3EtkX5jIpmfLddaYtBYsmF
Ans 10 10 10

Problem 19

https://youtu.be/TvqKQ3fv3Kw?list=PL7ersPsTyYt3EtkX5jIpmfLddaYtBYsmF

Ans 4 2

Problem 20
8642

Here int x=010 indicate 10 is in octave =8 in decimal

Problem 22

Ans -67

We can print any constant directly mentioning that rather mention variable always

Problem 21

https://youtu.be/6ux6VAtYUUY?list=PL7ersPsTyYt3EtkX5jIpmfLddaYtBYsmF
Ans-2

scanf always return num of "%d" or "%s" mention inside it .Here return value will be printed

Problem 22:

https://youtu.be/PeW1Y4Fndyc?list=PL7ersPsTyYt3EtkX5jIpmfLddaYtBYsmF

printf always return number of charac in his string

Problem 23:

https://youtu.be/oM3W3Unl8GQ?list=PL7ersPsTyYt3EtkX5jIpmfLddaYtBYsmF
Ans 16 4 2

printf always execute from right to left

Problem 24:

https://youtu.be/4_qJEKb8YP8?list=PL7ersPsTyYt3EtkX5jIpmfLddaYtBYsmF

ans 3

(x=3) performed

Here ',' not matter only assign operator work

Problem 25:
ans 5

1st x assign to 3

1st x assign to 4

1st x assign to 5

Problem 26:

https://www.youtube.com/watch?v=JNgdGKODMVw&list=PL7ersPsTyYt3EtkX5jIpmfLddaYtBYsmF&index
=5
error come

Problem 27:

https://youtu.be/IY79fWYkiPQ

each char contain 1 byte mean s 8 bit.after 255 --->0-->1--....

Ans 9

Problem 28:

https://youtu.be/mwmvfNVhIA4
All the function defined in C are global by default(one file's function can call another file's function ). If
we mention static before a function then that function will not be accessible from another file .It can
only accessible from the file where it written

Problem 30
o/p

&4["Hellow"] means & *("Hellow" +4)

if string is char s[23] ="Hellow";

then that means & *(s +4)

&4["Hellow"]---> & *(s+4)---. it will delete first 4 letter and print remaining-->ow

Also in a print statement only 1st arguments will be print .Others will be ignored

Problem 31:

o/p 17

o/p 4

case1:

b>>=2*1++

as its post increment and also as per precedence ++ > * so first ++ should be work
But its post increment operator thus 2*1 performed first then c -->c+1only .

case2:

first c-->c+1 then * operation performed

Problem 29:
Ans is not sure .Do it by u r self

Problem 32:
o/p C
check 4 and 12 of Case tips

Problem 33:

o/p HELLO AB EF

Problem 35:
External variable are global variables with permanent(life time ) storage

persistence meaning temporary

Problem 35:

Here in array data store in serially .{} not matter


Problem 36:

Problem 37 :
consider

PROBLEM 39:
https://www.geeksforgeeks.org/predefined-macros-in-c-with-examples/
Problem 23:
o/p 17

addition or substraction of any char means its ASCCI are add aur subs

Problem 24:

at 47 location placing 47

Problem 24:
Problem 25:
Here size of array cann't be define from out side of function

Problem 90:
if u declare a pointer outside of any function (globally) then its default value is NULL

Problem 22:
initially s=0 when value was not assigned

Ans 6

Problem 78:
Here *r mean r is a pointer which holding a address of integer .

**r mean r pointer---->pointer---> integer

So single or double pointer must be define in declaration before print .

**r is not mention in the code .

so if it was mentioned int **r before print then no erro

Problem 78:

Problem 78:

where is error

here num+num2<== num3 is a expression can have only 0 or 1 value .So cann't be -1 .So error at line 3
Problem 89:

from greekforgreek

105 106 109 111 104 103 102 101

Advance pointers

loops-controls-structures=only switch

You might also like