Programming Articles - Page 2676 of 3366

Type difference of character literals in C vs C++

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

194 Views

In C++ the size of the character constants is char. In C the type of character constant is integer (int). So in C the sizeof(‘a’) is 4 for 32bit architecture, and CHAR_BIT is 8. But the sizeof(char) is one byte for both C and C++.Example#include main() {    printf("%d", sizeof('a')); }Output4Example#include using namespace std; main(){    cout

Compile 32-bit program on 64-bit gcc in C and C++

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

1K+ Views

Nowadays the compiler comes with default 64-bit version. Sometimes we need to compile and execute a code into some 32bit system. In that time, we have to use thisS feature.At first, we Shave to check the current target version of the gcc compiler. To check this, we have to type this command.gcc –v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper OFFLOAD_TARGET_NAMES=nvptx-none OFFLOAD_TARGET_DEFAULT=1 Target: x86_64-linux-gnu ........... ........... ...........Here it is showing that Target is x86_64. So we are using the 64-bit version of gcc. Now to use the 32-bit system, we have to write the following command.gcc –m32 program_name.cSometimes this command may generate ... Read More

do…while loop vs. while loop in C/C++

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

7K+ Views

Here we will see what are the basic differences of do-while loop and the while loop in C or C++.A while loop in C programming repeatedly executes a target statement as long as a given condition is true. The syntax is like below.while(condition) {    statement(s); }Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any nonzero value. The loop iterates while the condition is true.When the condition becomes false, the program control passes to the line immediately following the loop.Example#include int main () {    int ... Read More

Variable Length Argument in C

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

2K+ Views

Sometimes, you may come across a situation, when you want to have a function, which can take variable number of arguments, i.e., parameters, instead of predefined number of parameters. The C/C++ programming language provides a solution for this situation and you are allowed to define a function which can accept variable number of parameters based on your requirement. The following example shows the definition of such a function.int func(int, ... ) {    .    .    . } int main() {    func(1, 2, 3);    func(1, 2, 3, 4); }It should be noted that the function func() has ... Read More

mbtowc function in C

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

101 Views

The C library function int mbtowc(whcar_t *pwc, const char *str, size_t n) converts a multibyte sequence to a wide character.Following is the declaration for mbtowc() function.int mbtowc(whcar_t *pwc, const char *str, size_t n)The parameters are −pwc − This is the pointer to an object of type wchar_t.str − This is the pointer to the first byte of a multi-byte character.str − This is the pointer to the first byte of a multi-byte character.n −This is the maximum number of bytes to be checked for character length.The return values are −If str is not NULL, the mbtowc() function returns the number ... Read More

Assigning multiple characters in an int in C language

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

366 Views

The character type data is stored by its ASCII value internally in C or C++. If we want to print a single character as integer, we will get the ASCII value. But when we are trying to print more than one character using a single quote, then it will print some strange output.Please check the following program to get the idea.Example#include int main() {    printf("%d", 'A');    printf("%d", 'AA');    printf("%d", 'ABC'); }Output65 16705 4276803The ASCII of A is 65. So at first it is showing 65 (01000001). Now for AA, it is showing 16705. This is ASCII ... Read More

Address of a function in C or C++

Akansha Kumari
Updated on 14-Jul-2025 17:24:15

3K+ Views

In C and C++, every function is stored in the computer's memory, and each function has a memory address just like all other variables. In this article, our task is to see how we can access the address of a function and display it in both C and C++. Accessing Address of a Function To access the address of a function, we simply use its name without parentheses. When we print a function name with parentheses like hello(), we're calling the function. But if we print just hello, it gives us the memory address where the function is stored. ... Read More

Implicit initialization of variables with 0 or 1 in C

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

301 Views

We know that we need to declare variables before using it in our code. However, we variables can be assigned with 0 or 1 without declaration. In the following example we can see this.Example#include #include x, y, array[3]; // implicit initialization of some variables int main(i) {    //The argument i will hold 1    int index;    printf("x = %d, y = %d", x, y);    for(index = 0; index < 3; index++)       printf("Array[%d] = %d", i, array[i]);       printf("The value of i : %d", i); }Outputx = 0, y = 0 ... Read More

Storage of integer and character values in C

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

1K+ Views

We have used the integer and character variables many times in our program. Here we will see how they are stored in the memory.In C the character values are also stored as integers. In the following code, we shall put 270 into a character type data. So the binary equivalent of 270 is 100001110, but takes only first 8-bits from right. So the result will be (00001110), that is 14. Then stores the value into variable a. It also gives warning for overflow.In the next variable y, we are trying to store negative number say -130. The negative number will ... Read More

Pre-increment and Post-increment in C/C++

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

4K+ Views

Here we will see what is the pre-increment and post-increment in C or C++. The pre-increment and post-increment both are increment operators. But they have little differences.The pre-increment operator increments the value of a variable at first, then sends the assign it to some other variable, but in the case of postincrement, it at first assign to a variable, then increase the value.Example#include using namespace std; main() {    int x, y, z;    x = 10;    y = 10;    z = ++x; //z will hold 11    cout

Advertisements