C Articles

Page 84 of 96

Macros vs Functions in C

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 3K+ Views

Macros and functions are two different mechanisms in C for code reuse, but they work fundamentally differently. Macros are preprocessed before compilation, meaning they are textually replaced in the code. Functions are compiled as separate code blocks that are called during execution. Syntax #define MACRO_NAME(parameters) replacement_text return_type function_name(parameters) { // function body return value; } Example: Macro vs Function Problem This example demonstrates a common issue where macros and functions produce different results due to the way macros expand − #include ...

Read More

Convert C/C++ code to assembly language

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 7K+ Views

Assembly language is a low-level programming language that provides a human-readable representation of machine code. The GNU Compiler Collection (GCC) allows us to convert C/C++ source code into assembly language for analysis and optimization purposes. Syntax gcc -S source_file.c gcc -S source_file.cpp Note: To use gcc, you need to install it on your system. On Ubuntu/Debian: sudo apt install gcc, on Windows: install MinGW or use WSL. Parameters -S − Generate assembly code and stop before assembling source_file − The C/C++ source file to convert Example: Simple C ...

Read More

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

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 2K+ Views

The free() function is used to deallocate memory that was allocated using malloc(), calloc(), or realloc(). The syntax of free() is simple − it only takes a pointer as an argument and deallocates the memory. Syntax void free(void *ptr); The interesting question is: how does free() know the size of the memory block to deallocate when it only receives a pointer? How free() Determines Memory Block Size When you allocate memory using dynamic memory allocation functions, the memory management system stores metadata about each allocated block. This metadata typically includes the size of ...

Read More

Compound Literals in C

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 692 Views

In C, compound literals are a feature introduced in the C99 standard that allows you to create unnamed objects with automatic storage duration. This feature enables you to initialize arrays, structures, and unions directly in expressions without declaring a separate variable. Syntax (type_name) { initializer_list } Example 1: Compound Literal with Structure Here's how to use compound literals to create an unnamed structure object − #include struct point { int x; int y; }; void display_point(struct point pt) { ...

Read More

Multiline macros in C

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 4K+ Views

In C programming, multiline macros allow you to define complex operations that span multiple lines. Unlike single-line macros, each line in a multiline macro must be terminated with a backslash '' character to indicate line continuation. When using multiline macros, it's recommended to enclose the entire macro definition in parentheses to avoid potential syntax errors. Syntax #define MACRO_NAME(parameters) ({\ statement1;\ statement2;\ statement3;\ }) Example 1: Basic Multiline Macro Here's an example demonstrating how to create and use a multiline macro for printing ...

Read More

Write a C macro PRINT(x) which prints x

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 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. Syntax #define PRINT(x) printf(#x) Example Let us see the example to get the better idea − #include #define PRINT(x) printf(#x) int main() { PRINT(Hello); ...

Read More

Modulus of Negative Numbers in C

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 2K+ Views

In C programming, the modulus operator (%) can be used with negative numbers, but the result depends on the sign of the dividend (left operand). The sign of the result always matches the sign of the dividend, regardless of the divisor's sign. Syntax result = dividend % divisor; Rule for Sign Determination The sign of the modulus result follows this rule − If dividend is positive, result is positive If dividend is negative, result is negative The divisor's sign does not affect the result's sign Example 1: Positive Dividend, Negative ...

Read More

Scansets in C

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 4K+ Views

Scansets in C are special format specifiers supported by the scanf() family of functions. They allow you to specify which characters to accept or reject when reading input. Scansets are represented by %[] and provide fine-grained control over input validation. Syntax %[characters] // Accept only specified characters %[^characters] // Accept all characters except specified ones %[character-range] // Accept characters within a range Example 1: Basic Character Set This example demonstrates how to read only uppercase letters using a character ...

Read More

Benefits of C over other languages

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 2K+ Views

The C programming language was developed by Dennis Ritchie during early 1970. It was developed to redesign the UNIX operating system. Earlier the B language, which was used for UNIX system, had different drawbacks. It did not support structures, and did not understand datatypes. For this reason, the C language was introduced. C has high level functionality, and detailed features for OS programming. The UNIX kernel was developed using C. Advantages of C Language Medium Level Language: C is a medium level language. It has both, the lower level and higher level functionality. We can use ...

Read More

C/C++ Struct vs Class

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 484 Views

In C programming, there is no class keyword − only structures (struct) are available. However, understanding the conceptual differences between struct and class is important for C programmers who may work with C++. In C, structures are used to group related data together. Syntax struct structure_name { data_type member1; data_type member2; // ... more members }; Example: Basic Structure in C In C, all structure members are accessible by default − #include struct my_struct { ...

Read More
Showing 831–840 of 953 articles
« Prev 1 82 83 84 85 86 96 Next »
Advertisements