Found 1339 Articles for C

Converting Strings to Numbers in C/C++

Ayush Gupta
Updated on 16-Mar-2020 09:52:05

273 Views

In this tutorial, we will be discussing a program to understand how to convert strings into numbers in C/C++.C/C++ provides two ways to convert strings into numbers.Example Live DemoUsing sscanf()#include int main(){    const char *str = "12345";    int x;    sscanf(str, "%d", &x);    printf("The value of x : %d", x);    return 0; }OutputThe value of x : 12345Using stoi() Live Demo#include #include using namespace std; int main(){    string str1 = "45";    string str2 = "3.14159";    string str3 = "31337 geek";    int myint1 = stoi(str1);    int myint2 = stoi(str2);    int myint3 = stoi(str3);    cout

Convert a String to Integer Array in C/C++

Ayush Gupta
Updated on 16-Mar-2020 09:50:25

3K+ Views

In this tutorial, we will be discussing a program to understand how to convert a string into integer array in C/C++.For this we will create a new array. Traverse through the given string, if the character is a comma “, ”, we move on to the next character else add it to the new array.Example Live Demo#include using namespace std; //converting string to integer array void convert_array(string str){    int str_length = str.length();    int arr[str_length] = { 0 };    int j = 0, i, sum = 0;    //traversing the string    for (i = 0; str[i] != ... Read More

Thread functions in C/C++

Ayush Gupta
Updated on 02-Mar-2020 11:16:59

2K+ Views

In this tutorial, we will be discussing a program to understand thread functions in C/C++.Thread functions allow users to implement concurrent functions at the same time, which can either be dependent on each other for execution or independent.Example#include #include #include void* func(void* arg){    //detaching the current thread    pthread_detach(pthread_self());    printf("Inside the thread");    pthread_exit(NULL); } void fun(){    pthread_t ptid;    //creating a new thread    pthread_create(&ptid, NULL, &func, NULL);    printf("This line may be printed before thread terminates");    if(pthread_equal(ptid, pthread_self())       printf("Threads are equal");    else       printf("Threads are ... Read More

How to sum two integers without using arithmetic operators in C/C++ Program?

Ayush Gupta
Updated on 02-Mar-2020 11:02:56

605 Views

In this tutorial, we will be discussing a program to understand how to sum two integers without using arithmetic operators in C/C++.For adding two integers without using arithmetic operators, we can do this with either using pointers or using bitwise operators.ExampleUsing pointers#include using namespace std; int sum(int a, int b){    int *p = &a;    return (int)&p[b]; } int main() {    int add = sum(2,3);    cout

How to print a semicolon(;) without using semicolon in C/C++?

Ayush Gupta
Updated on 02-Mar-2020 10:38:18

328 Views

In this tutorial, we will be discussing a program to understand how to print a semicolon(;) without using a semicolon in /C++.This can be done in two possible ways, either by using the ascii value of semicolon or using user-defined macros for the same.Example Live DemoUsing putchar() method#include int main(){    //ASCII value of semicolon is equal to 59    if (putchar(59)){    }    return 0; }Output;Example Live DemoUsing Macros :#include #define POINT printf("%c",59) int main(){    if (POINT) {    } }Output;

How to add “graphics.h” C/C++ library to gcc compiler in Linux

Ayush Gupta
Updated on 25-Feb-2020 07:25:35

2K+ Views

In this tutorial, we will be discussing a program to understand how to add “graphics.h” C/C++ library to gcc compiler in Linux.To do this we are required to compile and install the libgraph package.This includes install build-essential and some external packages>>sudo apt-get install build-essential >>sudo apt-get install libsdl-image1.2 libsdl-image1.2-dev guile-2.0 guile-2.0-dev libsdl1.2debian libart-2.0-dev libaudiofile-dev libesd0-dev libdirectfb-dev libdirectfb-extra libfreetype6-dev libxext-dev x11proto-xext-dev libfreetype6 libaa1 libaa1-dev libslang2-dev libasound2 libasound2-devThen setting the path in the extracted files>>sudo make install >>sudo cp /usr/local/lib/libgraph.* /usr/libExample#include #include #include int main(){    int gd = DETECT, gm;    initgraph(&gd, &gm, NULL);    circle(40, 40, 30);    delay(40000); ... Read More

How does a vector work in C/C++

Ayush Gupta
Updated on 25-Feb-2020 07:20:01

3K+ Views

In this tutorial, we will be discussing a program to understand how vectors work in C/C++.A vector data structure is an enhancement over the standard arrays. Unlike arrays, which have their size fixed when they are defined; vectors can be resized easily according to the requirement of the user.This provides flexibility and reduces the time requirement with arrays to copy the previous elements to the newly created array.Example Live Demo#include #include using namespace std; int main(){    vector myvector{ 1, 2, 3, 5 };    myvector.push_back(8);    //not vector becomes 1, 2, 3, 5, 8    for (auto x : myvector)    cout

How arrays are passed to functions in C/C++

Ayush Gupta
Updated on 17-Feb-2020 10:09:38

160 Views

In this tutorial, we will be discussing a program to understand how arrays are passed to functions.In the case of C/C++, the arrays are passed to a function in the form of a pointer which provides the address to the very first element of the array.Example Live Demo#include //passing array as a pointer void fun(int arr[]){    unsigned int n = sizeof(arr)/sizeof(arr[0]);    printf("Array size inside fun() is %d", n); } int main(){    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};    unsigned int n = sizeof(arr)/sizeof(arr[0]);    printf("Array size inside main() is %d", n);   ... Read More

Predefined Identifier __func__ in C

sudhir sharma
Updated on 04-Feb-2020 07:30:13

2K+ Views

Identifier is the name given to an entity in programming to identify it in the program.Generally, identifiers are created by the programmer for efficient working but there are some predefined identifiers that are inbuilt in programming. For example, cout, cin, etc.Here, we will see one of these predefined identifiers of C programming language which is __func__.The formal definition of __func__ is  −“The identifier __func__ shall be implicitly declared by the translator as if, immediately following the opening brace of each function definition, the declarationstatic const char __func__[] = “function-name”;appeared, where function-name is the name of the lexically-enclosing function.”C program The ... Read More

Print * in place of characters for reading passwords in C

sudhir sharma
Updated on 03-Feb-2020 10:20:11

637 Views

In this problem, we are given a string password. Our task is to print * in place of characters of the password.Let’s take an example to understand the problem,Input: password Output ********To solve this problem, we will traverse the password we have entered and print * in place of characters of the password.ExampleThe below program will show the implementation of our solution Live Demo#include #include int main() {    char password[50] = "password";    int length = strlen(password);    printf("Password : ");    for(int i = 0; i

Advertisements