Found 1401 Articles for C

Using range in switch case in C/C++

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

6K+ Views

In C or C++, we have used the switch-case statement. In the switch statement we pass some value, and using different cases, we can check the value. Here we will see that we can use ranges in the case statement.The syntax of using range in Case is like below −case low … highAfter writing case, we have to put lower value, then one space, then three dots, then another space, and the higher value.In the following program, we will see what will be the output for the range based case statement.Example#include main() {    int data[10] = { 5, ... Read More

Difference between char s[] and char *s in C

Samual Sam
Updated on 30-Jul-2019 22:30:26

2K+ Views

We have seen sometimes the strings are made using char s[], or sometimes char *s. So here we will see is there any difference or they are same?There are some differences. The s[] is an array, but *s is a pointer. For an example, if two declarations are like char s[20], and char *s respectively, then by using sizeof() we will get 20, and 4. The first one will be 20 as it is showing that there are 20 bytes of data. But second one is showing only 4 as this is the size of one pointer variable. For the ... Read More

Difference between #define and const in C

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

728 Views

The #define is preprocessor directives. So when we define some macro using #define, it replaces into the code with its value before compilation. So when the compiler does not know anything about the code, in that time also the macro values are replaced.The constant is actually a variable. By declaring this variable, it occupies memory unit. But we cannot update the value of constant type variable directly. We can change it using some pointer values.Sometimes programmer may think that using macro is better than const, as this is not taking any additional space into the memory, but for some good ... Read More

Variable length arguments for Macros in C

Samual Sam
Updated on 30-Jul-2019 22:30:26

822 Views

We know that we can use variable length arguments for functions in C. For that we have to use ellipsis (…). Similarly for macros, we can use variable length arguments. Here also we have to include ellipsis, The ‘__VA_ARGS__’ is used to handle variable length arguments. Concatenation operator ‘##’ is used to concatenate the variable arguments.In this example, the Macro will take variable length argument like the printf() or scanf() function. In this macro, we will print the filename, line number, and error messages. The first argument is pr. This is used to determine the priority i.e. whether it is ... Read More

Difference between “int main()” and “int main(void)” in C/C++?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

3K+ Views

Sometimes we see that there are two types of main function definition. The int main() and int main(void). So is there any difference?In C++, there is no difference. In C also both are correct. But the second one is technically better. It specifies that the function is not taking any argument. In C if some function is not specified with the arguments, then it can be called using no argument, or any number of arguments. Please check these two codes. (Remember these are in C not C++)Example#include void my_function() {    //some task } main(void) {    my_function(10, "Hello", "World"); ... Read More

What is long long in C/C++?

Samual Sam
Updated on 30-Jul-2019 22:30:26

6K+ Views

In some cases we use long long in C or C++. Here we will see what is basically long long is? The long long takes twice as much memory as long. In different systems, the allocated memory space differs. On Linux environment the long takes 64-bit (8-bytes) of space, and the long long takes 128-bits (16-bytes) of space. This is used when we want to deal with some large value of integers.We can test the size of different types using this simple program.Example#include using namespace std; main() {    int a;    long b;    long long c;    cout

How to check if an input is an integer using C/C++?

Samual Sam
Updated on 30-Jul-2019 22:30:26

3K+ Views

Here we will see how to check whether a given input is integer string or a normal string. The integer string will hold all characters that are in range 0 – 9. The solution is very simple, we will simply go through each characters one by one, and check whether it is numeric or not. If it is numeric, then point to the next, otherwise return false value.Example#include using namespace std; bool isNumeric(string str) {    for (int i = 0; i < str.length(); i++)       if (isdigit(str[i]) == false)       return false; //when one ... Read More

Generate random numbers following a normal distribution in C/C++

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

2K+ Views

Here we will see how to generate random numbers, which are following in a normal distribution. For the normal random, the formula is like below.𝑧 = √−2 ln 𝑥1 cos (2𝜋𝑥2)Here x1 and x2 are chosen randomly.Example#include #include #include #include using namespace std; double rand_gen() {    // return a uniformly distributed random value    return ( (double)(rand()) + 1. )/( (double)(RAND_MAX) + 1. ); } double normalRandom() {    // return a normally distributed random value    double v1=rand_gen();    double v2=rand_gen();    return cos(2*3.14*v2)*sqrt(-2.*log(v1)); } main() {    double sigma = 82.0;    double Mi = 40.0;    for(int i=0;i

Anything written in sizeof() is never executed in C

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

110 Views

The sizeof function (Sometimes called operator) is used to calculate the size of the given argument. If some other functions are given as argument, then that will not be executed in the sizeof.In the following example we will put one printf() statement inside the loop. Then we will see the output.Example#include double my_function() {    printf("This is a test function");    return 123456789; } main() {    int x;    x = sizeof(printf("Hello World"));    printf("The size: %d", x);    x = sizeof(my_function());    printf("The size: %d", x); }OutputThe size: 4 The size: 8The printf() is not executed which is ... Read More

Check input character is alphabet, digit or special character in C

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

2K+ Views

In this section, we will see how to check whether a given character is number, or the alphabet or some special character in C.The alphabets are from A – Z and a – z, Then the numbers are from 0 – 9. And all other characters are special characters. So If we check the conditions using these criteria, we can easily find them.Example#include #include main() {    char ch;    printf("Enter a character: ");    scanf("%c", &ch);    if((ch >= 'A' && ch = 'a' && ch = '0' && ch

Advertisements