Code of Important Exam
Code of Important Exam
************
************
day-2
1. Arithmetic operators
+
-
*
/
%
2. relational operators
>
>=
<
<=
==
!= (not equal to)
3. logical operators
&& (logical and)
|| (logical or)
! (logical not)
4.bit-wise operator:
| (bitwise or)
& (bitwise and)
^ (bitwise xor)
>> (right shift)
<< (left shift)
5. increment and decrement operator
i=i+1
i++ (post increment)
++i (pre increment)
i-- (post decrement)
--i (pre decrement)
6.conditional operator (?)
exp1 ? exp 2: exp 3
a>b ? printf("a is big") : printf("b is big")
7.assignment operators
a=a+b
a+=b; (a=a+b)
a-=b; (a=a-b)
a*=b; (a=a*b)
a%=b; (a=a%b)
Control Statements:
-> if condition
-> switch case
if condition:
-> simple if
-> if ...else
-> nested if
-> Simple if:
if (i==10)
{
printf("I value is 10");
}
-> if ..else:
if (i==10)
{
printf("I value is 10");
}
else
{
printf(" I value is not 10");
}
-> nested if
if (i<10)
{
if (i>5)
{
printf("I is greater than 5");
}
else
{
printf("I is not grear than 5");
}
}
else
{
printf("I value is grear than 10);
}
switch:
switch case
syntax:
switch (expr)
{
case 1:
break;
case 2:
break;
case 3:
break;
default:
nested switch:
int main()
{
int i,j;
printf("\n Enter the value of i,j");
scanf("%d%d",&i,&j);
switch(i)
{
case 1:
switch (j)
{
case 1:
printf("I am in inside switch case 1");
break;
case 2:
printf("I am in inside switch case 2");
break;
default:
printf("I am in default case");
break;
}
break;
case 2:
break;
default:
printf("Default");
}
return 0;
}
loops:
repetetion of a logic
1. initial value
2. condition
3. increment/decrement (i=i+1)
ex:
print the values from 1 to 100
1,2,3,4,5
2,4,6,8,10,.....50
initial value: 2
condition : 50
increment : i=i+2
for
while
do ...while
1. for loop
for(initial value;condition;increment/decrement)
{
}
print the values from 1 to 100
for(i=1;i<100;i=i+1)
{
printf("\n %d",i);
}
Syntax 3:
for(initial value;condition;)
{
increment/decrement;
}
Syntax 4:
initial value;
for(;condition;)
{
increment/decrement;
}
the loop will be executing infinite times (no limit), it will be keep on executing
While:
initial value
condition
increment/decrement
Syntax:
initial value;
while(condition)
{
increment;
}
========
do -- while
initial value;
condition
increment/decrement;
syntax
do
{
i=i+1;
}
while(condition);
control statements:
1. if conditions,
2. switch case
3. break; -> it will cut the loop execution
4. continue; -> it will skip the execution of the loop
5. goto
1. auto:
By default all the variables are auto.
-> life time (with in a block/function)
-> what is the default value (garbage)
-> what is the scope (with in the block/function)
-> when it will die (once you come out of the block/function
2. Register:
We are telling the compiler that store this variable in CPU register memory.
Frequently used varibales should be store in CPU Register.
Syntax:
register int i;
-> life time (with in a block/function)
-> what is the default value (garbage)
-> what is the scope (with in the block/function)
-> when it will die (once you come out of the block/function
3.Static:
it will store the previous value.
The static keyword in c is used to specify that a variable or a function has static
storage duration.
A variable declared as static inside a function retains its value even after the
function has returned,
which means the variable remains in memory throughout the life of the program
Static variables are initialized only once. The compiler persists with the variable
till the end of the program.
The static variables are alive till the execution of the program.
A static function in C is a function that has a scope that is limited to its object
file.
This means that the static function is only visible in its object file.
int main()
{
static int i=1;
if(i>5)
{
exit(0);
}
else
{
printf("\n The value is %d",i);
i=i+1;
main();
}
return 0;
}
void display()
{
static int i=1;
printf("
The i value is %d",i);
i=i+1;
}
int main()
{
display();
display();
display();
return 0;
}
Extern:
Step 1:
open a project - console application -> main.c
Step 2:
right click on the project -> add files -> right click on main.c -> paste ->
renamed the file (file1.c)
-> the file file1.c will be added to your project
-> double click on the file1.c file
-> remove every thing, and add
int i=100;
void display()
{
printf("\n I am in display function");
}
-> save the file
Step 3:
-> open main.c file
-> extern i;
extern display();
-> in the main function
printf("\n The value of i is %d",i);
display();
Functions:
Piece of code, which can be used many times,
reusable code.
syntax:
returntype functioname(parameters)
ex:
int addition();
int addition(int i,int j);
void display();
Declaration:
we are telling the compiler that that
is the :
-> return type
-> name of the function
-> parameters/arguments
int addition();
int addition(int i,int j);
void display();
Defintion: Body of the function (logic)
ex:
int addition()
{
printf("xyz");
}
2.
int addition(int i,int j)
{
printf("The sum is %d",i+j);
}
Function Call:
we will call the function using the
name of the function itself along with
arguments.
ex:
display();
addition(10,20);
#include <stdio.h>
#include <stdlib.h>
void myfunction()
{
printf("\n My name is Sudhakar");
}
int main()
{
myfunction();
return 0;
}
Call by value:
When you call function using a parameter, then the function
receives the value, and another duplicate local variable
will be created in the function.
in one line:
Pointer:
pointer is a variable which stores the address of another variable
type *var-name;
Ex:
int i=10;
int *j=&i;
Here j is a pointer which stores the address of variable i.
Void pointer is a generic pointer that can be used to point another variable of any
data type.
Void pointer can store the address of variable belonging to any of the data type
int main()
{
int a = 10;
char b = 'x';
int main()
{
int *p; /* wild pointer */
int a = 10;
p = &a; /* p is not a wild pointer now*/
*p = 12; /* This is fine. Value of a is changed */
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=10;
int *j=&i; //Single pointer
int **k=&j; //double pointer
int ***m=&k; //Thriple pointer
printf("%d -> %d -> %d -> %d",i,*j,**k,***m);
return 0;
}
https://www.javatpoint.com/star-program-in-c
Day 6:
int i=10;
i=20;
i=30;
printf("%d",i);
This printf will print 30, because at a time we can store only
one value inside this variable i.
Array:
An array is nothing but collection of homogenious(Similar) group
of values stored in a continuous memory.
or
we can say array is nothing but collection of similar values
10,20,30,40,50,60 -> group of integers
'a','b','c','d','e' -> group of characters
2.34,4.56,7.89,9.0 -> group of floats
Syntax:
initialize an array:
int a[10]={100,200,300,400,500,600,650,700,750,800};
9. Write a program in C to copy the elements of one array into another array.
Important questions:
1. initialize one dimentional array
2. print all the elements of array
3. print all the elements of array in reverse order
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[10]={100,200,300,400,500,600,650,700,750,800};
printf("\n Elements of array");
for(int i=0;i<10;i++)
{
printf("\n %d",a[i]);
}
return 0;
}
pointer:
int i;
int *j=&i;
int a[10];
memory allocation:
memory allocation can happen in 2 ways in C Language:
memory allocation:
memory allocation can happen in 2 ways in C Language:
header files:
stdlib.h;
malloc.h;
Day 7:
memory allocation:
memory allocation can happen in 2 ways in C Language:
The malloc() function takes a single argument, which is the number of bytes to
allocate.
The calloc() function takes two arguments: the number of elements to allocate, and
the size of each element in bytes.
The malloc() function allocates uninitialized memory, which means that the contents
of the allocated memory are undefined.
The calloc() function allocates zero-initialized memory, which means that the
contents of the allocated memory are set to zero.
calloc() allocates zero-initialized memory and takes two arguments, while malloc()
allocates uninitialized memory and takes one argument.
1. Write a C program to scanf integer array and print all the elements.
Use dynamic memory allocation.
2. Write a C program to take a int pointer,assign the value, and print
3. Write a C program to take a float pointer,assign the value, and print
4. Write a C program to take a double pointer,assign the value, and print
5. WCP to allocate memory for an array of 5 intergers ,take the inputs from
keyboard, display the values
6. WCP to find the greatest element of an integer array(using dynamic memory
allocation).
7. WCP to copy the contents of one array into another Array (using dynamic memory
allocation).
8. WCP to print the elements of an array using dynamic memory allocation.
9. WCP to allocate memory for an array of 5 intergers ,take the inputs from
keyboard, display the values,udr calloc for dynamic allocation
10. WCP to copy the contents of one array into another Array (using dynamic memory
allocation, calloc function).
11.WCP to allocate memory for an array of 5 integers(using malloc/calloc), increase
the size of the array to 10 using realloc, print all the values.
12. Write a C program to allocate memory for 2 arrays, add them and display the
results in a function
13. Write a C program to allocate memory for 1D array, find out of the sum of all
the elements in a function
for(int i=0;i<5;i++)
{
for(int j=0;j<5-i-1;j++)
{
if(a[j] < a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
16. WCP to search for an element in the array using linear search
17. WCP to search for an element in the array using binary search
Search:
*****c****
int **p;
p=(int **)malloc(9*sizeof(int));
for(i=0;i<9;i++)
{
p[i]=(int *)malloc(3*sizeof(int)
}
24. Write a C program to allocate memory for 2D array, scan the elements and print
them in matrix format.
25. Write a C program to allocate memory for 2D array, scan the elements and print
them in matrix format.
26. Sort array of 2D strings using dynamic memory allocation
*********************************************************************************
Functions:
-> push the return address to the stack
-> push all the arguments to the stack
-> go to the function definition
-> take out the parameters from stack
-> execute the function
-> pop the address
-> come back to the calling function
Preprocessor directives:
-> macros
-> file inclusions (<stdio.h>,"stdio.h")
-> conditional compilation
-> other directivies
Macro:
-> variables
-> Block (functions)
A macro is a piece of code in a program that is replaced by the value of the macro.
Macro is defined by #define directive.
Types Of Macros
// Macro definition
#define DATE 31
int main()
{
printf("Lockdown will be extended"
" upto %d-MAY-2020",
DATE);
return 0;
}
2.Chain Macros: Macros inside macros are termed as chain macros. In chain macros
first of all parent macro is expand then child macro is expanded
#include <stdio.h>
#define ELE 1, \
5, \
3
int main()
{
int arr[] = { ELE };
printf("Elements of Array are:\n");
for (int i = 0; i < 3; i++) {
printf("%d ", arr[i]);
}
return 0;
}
4.Function-like Macro: These macros are the same as a function call. It replaces
the entire code instead of a function name.
#include <stdio.h>
#define min(a, b) (((a) < (b)) ? (a) : (b))
int main()
{
int a = 18;
int b = 76;
printf("Minimum value between"
" %d and %d is %d\n",
a, b, min(a, b));
return 0;
}
conditional compilation:
#define i 100
#ifdef i
printf("I am good");
#else
printf("I am not good");
#endif
-> #ifdef
-> #else
-> #ifndef
-> #undef
File operations:
1. open a file
2. close a fille
3. open a file for reading
4. open a file for writing
5. Open a file for appending
6. create a new file
7. open a file, copy the content paste in another file.
1. FILE *ptr;
2. fopen:
ptr=fopen("filepath","r");
3. fgetc -> getting a character from a file
Syntax:
ch=fgetc(fptr)
4. fclose
r -> read mode
w-> write
a -> append
r+
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *ptr;
char ch;
ptr=fopen("C://Users/Linus/Downloads/testing.txt","r");
if (ptr == NULL)
{
printf("Not able to open the file");
}
else
{
printf("Able to open the file");
while((ch=fgetc(ptr))!=EOF)
{
printf("%c",ch);
}
fclose(ptr);
}
return 0;
}
Bitwise Operators:
Bitwise Or (|)
Bitwise and (&)
bitwise xor (or+not) ^
Bitwise not (~)
Left shift (<<)
shift right(>>)
int a[]={10,20,30,40,50};
int pos=3;
int val=100;
for(int i=4;i>=pos;i--)
{
a[i+1]=a[i];
}
a[pos]=val;
for(int i=0;i<6;i++)
{
printf("\n %d",a[i]);
}
stucture:
Structures (also called structs) are a way to group several related variables into
one place.
Each variable in the structure is known as a member of the structure.
#include <stdlib.h>
struct student
{
int i;
float f;
};
int main()
{
struct student s;
s.i=100;
s.f=10.25;
#include <stdio.h>
#include <stdlib.h>
struct myStruct {
int field1;
char field2;
};
int main() {
struct myStruct myArray[10];
for (int i = 0; i < 10; i++) {
myArray[i].field1 = i;
myArray[i].field2 = 'a' + i;
}
for (int i = 0; i < 10; i++)
{
printf("\n %d",myArray[i].field1);
printf("\n %c",myArray[i].field2);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct myStruct {
int field1;
char field2;
};
int main() {
struct myStruct myArray[10];
for (int i = 0; i < 10; i++) {
myArray[i].field1 = i;
myArray[i].field2 = 'a' + i;
}
for (int i = 0; i < 10; i++)
{
printf("\n %d",myArray[i].field1);
printf("\n %c",myArray[i].field2);
}
return 0;
}
Array of structures:
A student information -> single structure
multiple students information -> array of structures
s->val1=100;
s->var2=10.25;
C Array of Structures
struct student{
int rollno;
char name[10];
};
int main(){
int i;
struct student st[5];
for(i=0;i<5;i++){
printf("\nEnter Rollno:");
scanf("%d",&st[i].rollno);
printf("\nEnter Name:");
scanf("%s",&st[i].name);
}
printf("\nStudent Information List:");
for(i=0;i<5;i++){
printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);
}
return 0;
}
Union:
it is similar to structure
union student
{
int i;
float f;
};
main()
{
union student s;
}
size of the union is the maximum size of the variable present inside that union
int i=10,j=20;
int k=i+j;
char str[80];
//printf("The value of i is %d j is %d",i,j);
sprintf(str,"The sum of %d and %d is %d",i,j,k);
printf("The str value is %s",str);
enums:
user definied data type.
it is mainly used for assign names to integral constatnts.
#include <stdio.h>
#include <stdlib.h>
enum week{Sunday,monday,tuesday,wednesday,thursday,friday,saturday};
int main()
{
enum week w;
for(int i=Sunday;i<=saturday;i++)
printf("\n %d",i);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
enum week{Sunday=10,monday,tuesday,wednesday=5,thursday=5,friday,saturday};
int main()
{
enum week w;
w=wednesday;
printf("The value of w is %d",w);
return 0;
}
enum month{january,feb,mar,apr,may,june,july,aug,sept};
#include <stdio.h>
#include <stdlib.h>
enum week{Sunday=10,monday,tuesday,wednesday=5,thursday=5,friday,saturday};
int main()
{
enum week w;
w=wednesday;
printf("The value of w is %d",w);
return 0;
}
// Given a & b
int a = 15, b = 2;
char x = 'a';
double div;
linked list:
single node
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
int main()
{
struct node *head;
head=(struct node *)malloc(sizeof(struct node));
head->data=100;
head->next=NULL;
printf("%d",head->data);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
struct node
{
int d;
struct node *next;
};
int main()
{
struct node *head=NULL; -->head node point to structure(first node will
create)
struct node *ptr,*temp;
for(int i=0;i<5;++i)
{
ptr=(struct node *)malloc(1*sizeof(struct node));
ptr->d=100+i;
if(head==NULL)
{
head=ptr;
temp=ptr;
}
else
{
temp->next=ptr;
temp=ptr;
}
}
temp=head;
while(temp!=NULL)
{
printf("%d->",temp->d);
temp=temp->next;
}
}
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
int main()
{
struct node *head;
int i;
for(i=0;i<5;i++)
{
head=(struct node *)malloc(sizeof(struct node));
head->data=100+i;
head->next=NULL;
printf("%d->",head->data);
}
return 0;
}
Linear Data Structures:
Stack
Queue
Arrays
Stack:
push (element)
pop()(removing element)
top - index
=====
top=-1
void push(int x)
{
if(top==size)
{
printf("Size is full or stack over flow");
}
else
{
top++;
a[top]=x;
}
}
====
int pop()
{
if(top==-1)
{
printf("Stack is empty");
}
else
{
x=a[top];
top--;
return x;
}
}
strncpy copies upto null or specified no. of bytes, whichever is less. But memcpy
overlooks null, and copies all specified no. of bytes.
None of them adds null in destination string by itself.
memcpy() function is used to copy a specified number of bytes from one memory to
another. Whereas, strcpy() function is used to copy the contents of one string into
another string.
memcpy() function acts on memory rather than value. Whereas, strcpy() function acts
on value rather than memory.
Because not all data ends with a null character, you must provide the memcpy ( )
function with the number of bytes you want to copy from the source to the
destination. The following program shows examples of both the strcpy ( ) and the
memcpy ( ) functions:
Instead of using the value, we can use macro which will replace the value in a
program.
Void pointer is a generic pointer that can be used to point another variable of any
data type.
Void pointer can store the address of variable belonging to any of the data type.
Memory leak occurs when programmers create a memory in heap and forget to delete
it.
The consequences of memory leak is that it reduces the performance of the computer
by reducing the amount of available memory.
void f()
{
int *ptr = (int *) malloc(sizeof(int));
/* Do some work */
free(ptr);
return;
}
The memory leak occurs, when a piece of memory which was previously allocated by
the programmer. Then it is not deallocated properly by programmer.
Constant:
Constants refer to fixed values that the program may not alter during its
execution. These fixed values are also called literals.
In C programming language, a constant is a value that cannot be changed during the
execution of a program. Constants can be used to represent fixed values,
Numeric constants:
const int MAX = 100;
Symbolic constants:
#define PI 3.14159
Defining Constants
?
There are two simple ways in C to define constants −
Typedef:
The C programming language provides a keyword called typedef, which you can use to
give a type a new name.
typedef unsigned char BYTE;
You can use typedef to give a name to your user defined data types as well.
typedef struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
} Book;
fprintf: fprintf is used to print the string content in file but not on the stdout
console.
fprintf(FILE *fptr, const char *str, ...);
fprintf(fptr, "%d.%s\n", i, str);
quik-sort
Array
#include <stdio.h>
int main() {
int n;
double arr[100];
printf("Enter the number of elements (1 to 100): ");
scanf("%d", &n);
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
int i, j, k;
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
int i, j, k;
return 0;
}
return 0;
}
}
}
cout <<"values"<<endl;
for(i=0;i<n;i++)
{
cout <<a[i]<<endl;
}
return 0;
}
// Bubble sort in C
#include <stdio.h>
// print array
void printArray(int array[], int size) {
for (int i = 0; i < size; ++i) {
printf("%d ", array[i]);
}
printf("\n");
}
int main() {
int data[] = {-2, 45, 0, 11, -9};
bubbleSort(data, size);
void main()
{
char str[100]; /* Declares a string of size 100 */
int l,i;
for(i=l;i>=0;i--)
{
printf("%c ", str[i]);
}
printf("\n");
}
void main()
{
char str[100]; /* Declares a string of size 100 */
int l=0;
}
printf("\n");
}
#include <stdio.h>
int main() {
int a[10], n;
int largest1, largest2, i;
scanf("%d", &n);
printf("enter elements");
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
largest1 = a[0];
for (i = 0; i < n; i++) {
if (a[i] > largest1) {
largest1 = a[i];
}
}
largest2 = a[0];
for (i = 1; i < n; i++) {
if (a[i] > largest2 && a[i] < largest1)
largest2 = a[i];
}
printf("First and second largest number is %d and %d ", largest1, largest2);
}
// A tree node
struct Node {
int data;
struct Node *left, *right;
};
// Driver code
int main(void)
{
struct Node* NewRoot = NULL;
struct Node* root = newNode(2);
root->left = newNode(7);
root->right = newNode(5);
root->left->right = newNode(6);
root->left->right->left = newNode(1);
root->left->right->right = newNode(11);
root->right->right = newNode(9);
root->right->right->left = newNode(4);
// Function call
printf("Maximum element is %d \n", findMax(root));
return 0;
}
1010
10100
=
00000
c=0;
1010
10100
=11110
a=11110
a=30
b=000000 <<1
Revers a bit
for(i=0;i<8;i++)
{
// bitwise left shift 'rev' by 1
rev <<= 1;
int main()
{
unsigned int n =10;
printf("%u", reverseBits(n));
return 0;
}
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = rows; i >= 1; --i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
return 0;
}
#include <stdio.h>
int main() {
int i, j, n, k = 0;
printf("Enter the number of rows: ");
scanf("%d", &n);
for (i = 1; i <= n; ++i, k = 0) {
for (j = 1; j <= n - i; ++j) {
printf(" ");
}
while (k != 2 * i - 1) {
printf("* ");
++k;
}
printf("\n");
}
return 0;
}
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
key difference between memcpy() and strncpy() is that memcpy() copies a specified
number of bytes from the source buffer to the destination buffer,
while strncpy() copies a specified number of characters or until a null character
is encountered in the source buffer.
If you need to copy a fixed number of bytes and do not need null termination,
memcpy() is the appropriate choice.
If you need to copy a fixed number of characters and want null termination,
strncpy() is the appropriate choice.
The main difference between inline and macro functions is that inline functions are
parsed by the compiler,
whereas macros in a program are expanded by the preprocessor.
The keyword "inline" is used to define an inline function, whereas "#define" is
used to define a macro.
2. Macro :
It is also called preprocessors directive. The macros are defined by the #define
keyword. Before the program compilation,
the preprocessor examines the program whenever the preprocessor detects the macros
then preprocessor replaces the macro by the macro definition.
Pre-processing
Compilation
Assembly
Linking
The process of name mangling is compiler dependent. Compilers can use different
strategies.
The name mangled by a compiler may not be same as mangled by other compilers.
looking at the mangled name we can observe the pattern. The pattern looks like: -
_Z numberOfCharsInOriginalFunctionName OriginalFunctionName parameter_names_encoded
Union in C is a special data type available in C that allows storing different data
types in the same memory location.
You can define a union with many members, but only one member can contain a value
at any given time.
Unions provide an efficient way of using the same memory location for multiple
purposes.
Struct Union
The struct keyword is used to define a structure. The union keyword
is used to define union.
When the variables are declared in a structure, the compiler allocates memory to
each variables member.
The size of a structure is equal or greater to the sum of the sizes of each data
member.
The static_cast operator converts variable j to type float . This allows the
compiler to generate a division with an answer of type float .
All static_cast operators resolve at compile time and do not remove any const or
volatile modifiers.
const_cast
can be used to remove or add const to a variable. This can be useful if it is
necessary to add/remove constness from a variable.
static_cast
This is used for the normal/ordinary type conversion. This is also the cast
responsible for implicit type coersion and can also be called explicitly.
You should use it in cases like converting float to int, char to int, etc.
dynamic_cast
This cast is used for handling polymorphism. You only need to use it when you're
casting to a derived class.
This is exclusively to be used in inheritence when you cast from base class to
derived class.
reinterpret_cast
This is the trickiest to use. It is used for reinterpreting bit patterns and is
extremely low level.
It's used primarily for things like turning a raw data bit stream into actual data
or storing data in the low bits of an aligned pointer.
Global variables have global scope, which means that they can be accessed from any
part of the program, including other files.
Static variables have block scope, which means that they can only be accessed from
within the block where they are defined.
Global variables have a lifetime that extends for the entire duration of the
program.
Static variables have a lifetime that extends for the entire duration of the
program, but are only initialized once when the program starts.
Global variables can be accessed by any part of the program, even if they are
defined in a different file.
Static variables can only be accessed within the block where they are defined.
Overall, global variables are useful when you need to define a variable that can be
accessed from multiple parts of the program,
while static variables are useful when you need to define a variable that is only
accessed within a specific block.
By making a variable static, you can limit its scope to the current file.
This can help prevent naming conflicts and improve encapsulation, since the
variable is only accessible within the file where it is defined.
By making a variable static, you can hide its implementation details from other
parts of the program.
This can help improve the security and maintainability of your code.
In C, a static function is a function that is only visible within the file where it
is defined.
A static function cannot be called from other files using the function name, and it
does not conflict with functions of the same name in other files.
#include<stdio.h>
int main()
{
char c[]="Helloo woorlod";
char a='o';
for(int i=0;c[i]!='\0';)
{
if(c[i]==a)
{
for(int j=i;c[j]!='\0';j++)
{
c[j]=c[j+1];
}
}
else
{
i++;
}
}
printf("%s",c);
return 0;
}
struct Node {
int data;
struct Node* next;
};
if (n == 1) {
// Insert the new node at the head of the list
newNode->next = *headRef;
*headRef = newNode;
} else {
// Traverse the list to find the (n-1)th node
struct Node* prevNode = *headRef;
for (int i = 2; i < n; i++) {
if (prevNode == NULL) {
// n is greater than the length of the list, so insert at the end
break;
}
prevNode = prevNode->next;
}
int main() {
struct Node* head = NULL;
return 0;
}
A circular linked list is a linked list where the last node points back to the head
node, forming a loop.
To check if a linked list is a circular linked list in C,
we can use the "Floyd's cycle-finding algorithm" (also known as the "tortoise and
hare algorithm"),
which involves traversing the list with two pointers at different speeds. Here's
how the algorithm works:
Initialize two pointers, slow and fast, to the head of the list.
Traverse the list using the slow pointer, moving one node at a time, and the fast
pointer, moving two nodes at a time.
If the fast pointer encounters a NULL node, the list is not circular.
If the fast pointer ever points to the same node as the slow pointer, the list is
circular.
In C++, "class" is used to define a new data type. A class is a user-defined type
that can contain data members (variables)
and member functions (methods). The syntax for defining a class in C++ is as
follows:
class ClassName {
private:
// private data members and functions
public:
// public data members and functions
};
gradle
Copy
struct Node {
int data;
struct Node* next;
};