Found 1401 Articles for C

Standard Size of character ('a') in C/C++ on Linux

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

4K+ Views

In C++ the size of the character literal is char. In C the type of character literal 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 Code Live Demo#include main() {    printf("%d", sizeof('a')); }Output1Example Code Live Demo#include using namespace std; main() {    cout

Find out the current working directory in C/C++

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

6K+ Views

In this section, we will see how to get the current working directory using C or C++. We have defined some flags for the current operating system.Example Code Live Demo#ifdef WINDOWS #include #define GetCurrentDir _getcwd #else #include #define GetCurrentDir getcwd #endif #include using namespace std; std::string get_current_dir() {    char buff[FILENAME_MAX]; //create string buffer to hold path    GetCurrentDir( buff, FILENAME_MAX );    string current_working_dir(buff);    return current_working_dir; } main() {    cout

String tokenisation function in C

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

5K+ Views

In this section, we will see how to tokenize strings in C. The C has library function for this. The C library function char *strtok(char *str, const char *delim) breaks string str into a series of tokens using the delimiter delim.Following is the declaration for strtok() function.char *strtok(char *str, const char *delim)It takes two parameters. The str - The contents of this string are modified and broken into smaller strings (tokens), and delim - This is the C string containing the delimiters. These may vary from one call to another. This function returns a pointer to the first token found ... Read More

Wrapping C/C++ for Python using SWIG

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

1K+ Views

There are multiple ways to wrap your existing C or C++ functionality in Python. In this section, we are going to see how we can wrap C/C++ functionality with SWIG. Below are other options to wrap c/c++ functionality in python.Manual wrappingUsing pyrex to wrap C code.CtypesSIPBoost PythonSWIG (Simple Wrapper Interface Generator) is capable of wrapping C code with numerous other languages including Perl, Python, PHP, Ruby, Tcl, C#, Common Lisp (CLISP, Allegro, CL, UFFI, CFFI), Java, Modula-3 and OCAML. Swig also supports multiple interpreted and compiled Scheme implementations (like Guile, MzScheme, Chicken) are supported.But we will discuss its implementation with ... Read More

Sum of array using pointer arithmetic in C

Nitya Raut
Updated on 30-Jul-2019 22:30:25

843 Views

In this program, we need to find sum of array elements using pointer arithmetic.Here we use * which denotes the value stored at the memory address and this address will remain stored in the variable. Thus “int *ptr” means, ptr is a variable which contains an address and content of the address is an integer quantity.*p means it is a pointer variable. Using this and sum() we will find out sum of the elements of array.Example Code#include  void s(int* a, int len) { int i, s_of_arr = 0; for (i = 0; i 

4 Dimensional Array in C/C++

Nitya Raut
Updated on 30-Jul-2019 22:30:25

690 Views

A 4 dimensional array is an array of 3Darrays.AlgorithmBegin. Declare the variables. Declare the array elements. Take the no of elements as input. Take the elements as input. Print the elements stored in array. End.Here is an example of 4D array.#include using namespace std; int main() {    int a[2][2][3][2];    cout > a[i][j][k][l];             }          }       }    }    cout

Why C/C++ array index starts from zero?

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

600 Views

As Array index starts with 0, so a[i] can be implemented as *(a + i).If Array index starts with 1 then a[i] will be implemented as *(a+i-1) which will be time consuming during compilation and the performance of the program will also be effected.So, it is better to start index of the array from 0.A simple program of array is given -Example Codeint main() {    int array[5] = {7, 7, 7, 6, 6};    for (int i = 0; i < 5; i++)       cout

Pointer vs Array in C

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

390 Views

Pointers and array most of the time are treated as same in c. Some differences are:&operator:&pointer = returns the address of pointer.&array = returns the address of first element.sizeof operator:sizeof(array) = returns the total memory consumed by the all the elements of the array.sizeof(pointer) = returns the only memory consumed by the pointer variable itself.Array variable can’t be re-assigned a value whereas pointer variable can.Declaration:int a[]; //array Int *p; //pointerLet us consider that there is an integer pointer variableint *i;Now let us consider the outcome of the following assignments –a = &i; //illegal assignment. a variable can not be updated ... Read More

An Uncommon representation of array elements in C/C++

Nitya Raut
Updated on 30-Jul-2019 22:30:25

79 Views

This is a simple C++ program of an uncommon representation of array elements.#include using namespace std; int main() { int array[5] = {7,7,7, 6, 6}; for (int i = 0; i < 5; i++) cout

Why C treats array parameters as pointers?

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

230 Views

C treats array parameter as pointers because it is less time consuming and more efficient. Though if we can pass the address of each element of the array to a function as argument but it will be more time consuming. So it’s better to pass the base address of first element to the function like:void fun(int a[]) { … } void fun(int *a) { //more efficient. ….. }Here is a sample code in C:#include void display1(int a[]) //printing the array content {    int i;    printf("Current content of the array is: ");    for(i = 0; i < ... Read More

Advertisements