Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Server Side Programming Articles
Page 980 of 2109
How will implement Your Own sizeof in C
In C, the sizeof operator returns the size in bytes of a variable or data type. We can implement our own version of sizeof using pointer arithmetic. The concept is based on the fact that when we increment a pointer by 1, it moves by the size of the data type it points to. Syntax #define my_sizeof(variable) (char *)(&variable+1)-(char*)(&variable) How It Works The implementation uses pointer arithmetic − &variable gives the address of the variable &variable+1 gives the address after one complete element Casting both to (char*) makes the subtraction return bytes ...
Read MorePHP Program to Count Number of Binary Strings without Consecutive 1’s
In PHP, counting binary strings without consecutive 1's is a classic dynamic programming problem. A binary string contains only 0's and 1's, and we need to count valid strings where no two 1's appear next to each other. Understanding the Problem Let's analyze binary strings of length 3 to understand the pattern − All possible binary strings of length 3: 000, 001, 010, 011, 100, 101, 110, 111 Valid strings (without consecutive 1's): 000, 001, 010, 100 Invalid strings (with consecutive 1's): 011, 110, 111 So for length 3, we have 5 valid binary strings ...
Read MoreHow will you show memory representation of C variables?
In C programming, understanding the memory representation of variables is crucial for debugging and low-level programming. This involves examining how different data types are stored in memory at the byte level. Syntax typedef unsigned char *byte_pointer; void display_bytes(byte_pointer ptr, int length); Algorithm To display memory representation of C variables, follow these steps − Get the address and size of the variable using & operator and sizeof() Typecast the address to unsigned char* to access individual bytes Loop through each byte and print its hexadecimal value Example: Memory Representation of Different ...
Read MoreImplicit return type int in C
In C, if a function is declared without an explicit return type, the compiler implicitly assumes the return type to be int. This behavior was allowed in older C standards (C89/C90) but is deprecated and not recommended. The C99 standard requires explicit return types for all functions. Syntax function_name(parameters) { /* Function body */ return value; /* Implicitly returns int */ } Example: Implicit Return Type Here's an example demonstrating implicit return type behavior − #include my_function(int x) { ...
Read MorePHP Program to check for Anagram
PHP (Hypertext Preprocessor) is a widely used server-side scripting language for web development. In PHP, you can check if two strings are anagrams by comparing their character frequencies using built-in functions. What is an Anagram? Anagrams are words or phrases formed by rearranging the letters of another word or phrase. In an anagram, all the original letters must be used exactly once, with no additional or missing letters. For example, "listen" and "silent" are anagrams because they use the same letters rearranged in different order. Using count_chars() Function The count_chars() function is a built-in PHP ...
Read MoreDifference between char s[] and char *s in C
In C programming, there are two common ways to declare strings: char s[] and char *s. While both can hold strings, they have fundamental differences in memory allocation, mutability, and behavior. Syntax char s[] = "string literal"; // Array declaration char *s = "string literal"; // Pointer declaration Key Differences Aspect char s[] char *s Type Array of characters Pointer to char Memory Location Stack (local array) Pointer on stack, string in read-only section Mutability Modifiable Read-only ...
Read MorePHP Program for Subset Sum Problem
The Subset Sum Problem is a classic problem in computer science and dynamic programming. Given a set of positive integers and a target sum, the task is to determine whether there exists a subset of the given set whose elements add up to the target sum. Using Recursive Solution The recursive approach explores all possible combinations by either including or excluding each element ? Found a subset with the given sum No subset with the given sum. In this example, the set is [1, 7, 4, 9, 2]. For sum ...
Read MoreVariable length arguments for Macros in C
In C, we can use variable length arguments for macros, similar to how we use them with functions. This is achieved using ellipsis (…) and the special __VA_ARGS__ identifier. The concatenation operator ## is used to handle cases where no variable arguments are provided. Syntax #define MACRO_NAME(fixed_params, ...) macro_body __VA_ARGS__ Where __VA_ARGS__ represents all the variable arguments passed to the macro, and ## can be used for token pasting. Example: Logging Macro with Variable Arguments This example demonstrates a logging macro that accepts variable arguments like printf(). The macro prints the filename, line ...
Read MorePHP Program for Rabin-Karp Algorithm for Pattern Searching
The Rabin-Karp algorithm is a string pattern matching algorithm that efficiently searches for occurrences of a pattern within a larger text. It was developed by Michael O. Rabin and Richard M. Karp in 1987. The algorithm utilizes a hashing technique to compare the hash values of the pattern and substrings of the text. It works as follows: Calculate the hash value of the pattern and the first window of the text. Slide the pattern over the text one position at a time and compare the hash values. If ...
Read MoreDifference between "int main()" and "int main(void)" in C/C++?
In C programming, you might notice two different ways to declare the main function: int main() and int main(void). While both are valid, there is a subtle but important difference in how C treats them. Syntax int main() int main(void) Key Difference In C, int main() means the function can accept any number of arguments, while int main(void) explicitly specifies that the function takes no arguments. In C++, both forms are equivalent and mean "no arguments". Example 1: Function Without void When a function is declared without void, C allows it to ...
Read More