Server Side Programming Articles - Page 2279 of 2650

Macros vs Functions in C

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

2K+ Views

In this section we will see what are the differences between macros and functions in C. The macros are pre-processed, so it means that all the macros will be preprocessed while it is compiled. The functions are not preprocessed, but compiled.In macros no type checking is done, so it may occur some problems for different types of inputs. In the case of functions, this is not done. Also for macros if the inputs are not properly maintained, then it may generate some invalid results. Please check the following program to get the idea about the problem.Example#include #define SQUARE(x) x ... Read More

Convert C/C++ code to assembly language

Smita Kapse
Updated on 30-Jul-2019 22:30:25

7K+ Views

Here we will see how to generate assembly language output from C or C++ source code using gcc.The gcc provides a great feature to get all intermediate outputs from a source code while executing. To get the assembler output we can use the option ‘-S’ for the gcc. This option shows the output after compiling, but before sending to the assembler. The syntax of this command is like below.gcc –S program.cppNow, let us see how to output will be look like. Here we are using a simple program. In this program two numbers are stored into the variables x and ... Read More

How does free() know the size of memory to be deallocated?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

2K+ Views

The free() function is used to deallocate memory while it is allocated using malloc(), calloc() and realloc(). The syntax of the free is simple. We simply use free with the pointer. Then it can clean up the memory.free(ptr);The free() is not taking any size as parameter, but only pointer. So the question comes, that how the free() function know about the size of the block to deallocate?When we use the dynamic memory allocation techniques for memory allocations, then this is done in the actual heap section. It creates one word larger than the requested size. This extra word is used ... Read More

Compound Literals in C

Smita Kapse
Updated on 30-Jul-2019 22:30:25

613 Views

In this section we will see what is the compound literals in C. The compound literals are introduced in C99 standard in C. Using this feature, it can create unnamed objects. In the following example we will see how to use compound literal to generate object without any name.Example#include struct point {    int x;    int y; }; void display_point(struct point pt) {    printf("(%d,%d)", pt.x, pt.y); } main() {    display_point((struct point) {10, 20}); }Output(10,20)

Difference between C structures and C++ structures

Akansha Kumari
Updated on 11-Jun-2025 17:54:19

4K+ Views

A structures (or struct) is a user-defined data type, which is used to group and store variables of different data types inside a single name. But in C, structures can store only data members whereas C++ can store member functions, constructors, destructors, and even inheritance which is similar to classes in C++, but the only difference between struct and classes is that struct members are public by default. In this following article we will see how structure differs in both C and C++ in detail. Structures in C The structures in C is a user-defined data type, which is used ... Read More

Structure Sorting in C++

Akansha Kumari
Updated on 09-Jun-2025 19:27:25

2K+ Views

A structure in C++ is a user-defined data type, which is used to group and store variables of different data types under a single name. In this article we will see how to sort an array of structures using conditions on member variables. For this we will be using the sort() function which is defined under header file. Syntax Here is the following syntax of sort() function. sort(start_index, end_index, comparison_function); start_index is an iterator (or pointer) to the first element of the array of structures.end_index is an iterator (or pointer) to one past the last element (means last_index + ... Read More

Multiline macros in C

Anvi Jain
Updated on 30-Jul-2019 22:30:25

4K+ Views

In this section we will see, how can write multiline macros in C. We can write multiline macros like functions, but for macros, each line must be terminated with backslash ‘\’ character. If we use curly braces ‘{}’ and the macros is ended with ‘}’, then it may generate some error. So we can enclose the entire thing into parenthesis.Please check the following program to get the idea about multiline macros.Example#include #define PRINT(x, str) ({\    printf("The number %d", x);\    printf(" is ");\    printf(#str);\    printf("");\ }) int main() {    int x = 10;    if(x % 2 == 0){       PRINT(x, EVEN);    } }OutputThe number 10 is EVEN

Write a C macro PRINT(x) which prints x

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

2K+ Views

Here we will see how to define a macro called PRINT(x), and this will print whatever the value of x, passed as an argument.To solve this problem, we will use the stringize operator. Using this operator the x is converted into string, then by calling the printf() function internally, the value of x will be printed. Let us see the example to get the better idea.Example#include #define PRINT(x) printf(#x) int main () {    PRINT(Hello);    printf("");    PRINT(26);    printf("");    PRINT(2.354721);    printf(""); }OutputHello 26 2.354721

Modulus of Negative Numbers in C

Smita Kapse
Updated on 30-Jul-2019 22:30:25

1K+ Views

Here we will see what will be the result if we use negative numbers to get the modulus. Let us see the following programs and their outputs to get the idea.Example#include int main() {    int a = 7, b = -10, c = 2;    printf("Result: %d", a % b / c); }OutputResult: 3Here the precedence of % and / are same. So % is working at first, so a % b is generating 7, now after dividing it by c, it is generating 3. Here for a % b, the sign of left operand is appended to the ... Read More

Scansets in C

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

3K+ Views

Let us see, what is the scanset in C. The scanset is basically a specifier supported by scanf family functions. It is represented by %[]. Inside scanset we can specify only one character or a set of characters (Case Sensitive). When the scanset is processed, the scanf() can process only those characters which are mentioned in the scanset.Example#include int main() {    char str[50];    printf("Enter something: ");    scanf("%[A-Z]s", str);    printf("Given String: %s", str); }OutputEnter something: HElloWorld Given String: HEIt ignores the characters which are written in lowercase letters. The ‘W’ is also ignored because there are some ... Read More

Advertisements