Found 33676 Articles for Programming

Signal Handling in C++

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

582 Views

Signals are the interrupts delivered to a process by the operating system which can terminate a program prematurely. You can generate interrupts by pressing Ctrl+C on a UNIX, LINUX, Mac OS X or Windows system.There are signals which cannot be caught by the program but there is a following list of signals which you can catch in your program and can take appropriate actions based on the signal. These signals are defined in C++ header fileSignalDescriptionSIGABRTAbnormal termination of the program, such as a call to abort.SIGFPEAn erroneous arithmetic operation, such as a divide by zero or an operation resulting in ... Read More

Difftime() C library function

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

174 Views

Here we will see what is the difftime() function in C. The difftime() is used to get the differences between two time values.difftime() takes two time argument, the first one is the lower bound, and the second one is the upper bound. And it returns the differences between these two arguments.Example#include #include #include main() {    int sec;    time_t time1, time2;    time(&time1);    printf("Current Time: %ld",time1);    for (sec = 1; sec

Foreach in C++ vs Java

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

213 Views

In C++ and Java, there is another kind of loop, called the foreach loop. This is basically a modification of for loop. This loop is used to access the data from some container. This can access the elements of some array quickly without performing initialization. This loop is used to do something for each element of a container, not doing things n times.Now let us see how the foreach loop is used in C++ and Java.Example#include using namespace std; int main() {    int arr[] = { 11, 22, 33, 44, 55, 66, 77, 88, 99 };    for (int a : arr) //foreach loop    cout

Difference between Structures in C and C++

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

519 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

Type difference of character literals in C vs C++

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

186 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

96 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

362 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

Advertisements