Programming Articles - Page 2678 of 3366

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

Const Qualifier in C

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

2K+ Views

We use the const qualifier to declare a variable as constant. That means that we cannot change the value once the variable has been initialized. Using const has a very big benefit. For example, if you have a constant value of the value of PI, you wouldn't like any part of the program to modify that value. So you should declare that as a const.Objects declared with const-qualified types may be placed in read-only memory by the compiler, and if the address of a const object is never taken in a program, it may not be stored at all. For ... Read More

Need of long data type in C

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

407 Views

In C or C++, there are four different datatypes, that are used for integer type data. These four datatypes are short, int, long and long long. Each of these datatypes takes different memory spaces. The size varies in different architecture and different operating systems. Sometimes int takes 4-bytes or sometimes it takes 2-bytes. This also happen for the compilers. So we can use cross compilers.The cross compilers are basically a compiler, which is capable of compiling for a platform other than current platform.So if we want to compile the following code in 32bit system, and 64-bit system, it will generate ... Read More

Benefits of C over other languages

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

2K+ Views

The C programming language was developed by Dennis Ritchie during early 1970. It was developed to redesign UNIX operating system.Earlier the B language, which was used for UNIX system, it has different drawbacks. It does not support structures, and did not understand datatypes. For this reason, the C language was introduced. C has high level functionality, and detailed feature for OS programming. The UNIX kernel was developed by using C.Advantages of C languageC is medium level language. It has both, the lower level and higher level functionality. We can use C to make driver or kernel level programs as well ... Read More

Macros and Preprocessors in C

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

3K+ Views

The C Preprocessor is not a part of the compiler, but is a separate step in the compilation process. In simple terms, a C Preprocessor is just a text substitution tool and it instructs the compiler to do required pre-processing before the actual compilation. We'll refer to the C Preprocessor as CPP.All preprocessor commands begin with a hash symbol (#). It must be the first nonblank character, and for readability, a preprocessor directive should begin in the first column. The following section lists down all the important preprocessor directives −Sr.NoDirectives & Descriptions1#defineSubstitutes a preprocessor macro.2#includeInserts a particular header from another ... Read More

enum vs. const vs. #define in C/C++

Akansha Kumari
Updated on 16-Jun-2025 17:23:14

757 Views

The #define, const, and Enum are all used for declaring constant values, but their behaviours and use cases differ. The #define is a preprocessor directive, which is used to define a macro (it is a string or name, in which you can assign a constant value), whereas const is a keyword with which you can declare a variable with a constant value. Whereas, an Enum is a special user-defined data type that represents a group of constants. In the following article, we will learn about all three in detail. Enumeration (Enum) An Enumeration (or Enum) is a user-defined data type ... Read More

How to write a short literal in C++?

Akansha Kumari
Updated on 15-Jul-2025 17:35:04

1K+ Views

In the following article, we will learn about short literals in C++. In both C and C++, different data types have different literals. A literal is a fixed constant value, which is assigned to the variables of different data types. For example, here is a list of different data types with their literals. Datatypes Literals ... Read More

Advertisements