Found 1401 Articles for C

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

6K+ 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

Memory Layout of C Programs

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

2K+ Views

The memory layout for C programs is like below. There are few levels. These are −Stack SegmentHeap SegmentText SegmentData segmentNow let us see what are the functionalities of these sections.Sr.NoSections & Description1StackThe process Stack contains the temporary data such as method/function parameters, return address and local variables. It is an area of memory allotted for automatic variables and function parameters. It also stores a return address while executing function calls. Stack uses LIFO (Last- In-First-Out) mechanism for storing local or automatic variables, function parameters and storing next address or return address. The return address refers to the address to return ... Read More

Compound Literals in C

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

458 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

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

3K+ Views

Here we will see what are the differences between structures in C and structures in C++. The C++ structures are mostly like classes in C++. In C structure, all members are public, but in C++, they are private in default. Some other differences are listed below.C StructureC++ StructureStructures in C, cannot have member functions inside structures.Structures in C++ can hold member functions with member variables.We cannot initialize the structure data directly in C.We can directly initialize structure data in C++.In C, we have to write ‘struct’ keyword to declare structure type variables.In C++, we do not need to use ‘struct’ ... Read More

Multiline macros in C

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

3K+ 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

1K+ 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

2K+ 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