The main purpose of variables is to store data in memory. Unlike constants, it will not change during the program execution. However, its value may be changed during execution.The variable declaration indicates that the operating system is going to reserve a piece of memory with that variable name.Variable declarationThe syntax for variable declaration is as follows −type variable_name;ortype variable_name, variable_name, variable_name;For example, iInt a, b; float c; double d;Here, a, b, c, d are variables. The int, float, double are the data types.Variable initializationThe syntax for variable initialization is as follows −data type variablename=value;For example, int width, height=20; char letter='R'; ... Read More
A backslash ( \ ) that allows a visual representation of some nongraphic characters introduces an escape.One of the common escape constants is the newline character ( ).Backslash CharactersThe backslash characters are as follows −CharacterMeaning‘\a’alert‘\b’backspace‘\f’form feed‘’newline‘\t’horizontal tab‘\r’carriage return‘\v’vertical tab‘\’backslash‘\’ ’single quote‘\" ’double quote‘\?’Question markExample programFollowing is the C program for the backslash character constants −Example Live Demo#include #define PI 3.14 float area; void main(){ double r; r=1.0; area = PI * r * r; printf("Area is %d ", area); // /n is used to enter the next statement in newline }OutputArea is 1492442840Read More
ProblemHow to convert the hexadecimal value to integer value by using the C programming language?Explain the concept.SolutionHexadecimal values represent in 16 symbols 1 to 9 & A to F. Here, A to F decimal equivalent is 10 to 15.ExampleFollowing is the C program for converting hexadecimal to an integer by using functions −#include #include #include int hextodc(char *hex){ int y = 0; int dec = 0; int x, i; for(i = strlen(hex) - 1 ; i >= 0 ; --i)//{ if(hex[i]>='0'&&hex[i]
The value of the pointer address is constant that means we cannot change the value of the address that is pointed by the pointer.A constant pointer is declared as follows −Data_Type const* Pointer_Name;For example, int const *p// pointer to const integerExampleFollowing is the C program to illustrate a pointer to a constant −#include int main(void){ int var1 = 100; // pointer to constant integer const int* ptr = &var1; //try to modify the value of pointed address *ptr = 10; printf("%d", *ptr); return 0; }OutputWhen the above program is executed, it produces the ... Read More
ProblemDeclare int and float variables without initializing and try to print their values in C language. Explain what will happen.SolutionIf a variable is declared but not initialized or uninitialized and if those variables are trying to print, then, it will return 0 or some garbage value.Whenever we declare a variable, a location is allocated to that variable. The only thing is with the help of initialization, we are trying to occupy the memory location which is already allotted while declaration.But in the below program, we are not initializing the values in the memory locations which are reserved. But, by default, ... Read More
Pointers have many but easy concepts and they are very important to C programming.Two of the arithmetic pointer concepts are explained below, which are C pointer addition and subtraction respectively.C pointer additionC pointer addition refers to adding a value to the pointer variable.The formula is as follows −new_address= current_address + (number * size_of(data type))ExampleFollowing is the C program for C pointer addition − Live Demo#include int main(){ int num=500; int *ptr;//pointer to int ptr=#//stores the address of number variable printf("add of ptr is %u ", ptr); ptr=ptr+7; //adding 7 to pointer variable printf("after adding add ... Read More
The pointer is a variable that stores the address of another variable.The syntax for the pointer is as follows −pointer = &variable;ExampleFollowing is the C program for the concept of pointers using C language − Live Demo#include void main(){ //Declaring variables and pointer// int a=2; int *p; //Declaring relation between variable and pointer// p=&a; //Printing required example statements// printf("Size of the integer is %d", sizeof (int));//4// printf("Address of %d is %d", a, p);//Address value// printf("Value of %d is %d", a, *p);//2// printf("Value of next address location of %d is %d", a, ... Read More
Double pointer or pointer to pointer is a variable that holds the address of another pointer.Following is the declaration for a pointer to a pointer −datatype ** pointer_name;For example, int **p; p is a pointer to pointerInitialization − ‘&’ is used for initialization.For example, int a = 10; int *p; int **q; p = &a;Accessing − Indirection operator (*) is used for accessing.ExampleFollowing is the C program for the pointer to pointer − Live Demo#include main ( ){ int A = 10; int *p; int **q; p = &A; q = &p; printf("A =%d", A); ... Read More
ProblemCalculate the sum of all elements of a two-dimensional array by using run-time initialization.SolutionTwo-dimensional Array is used in situations where a table of values have to be stored (or) in matrices applicationsThe syntax is as follows −datatype array_ name [rowsize] [column size];For example, int a[4] [4];Number of elements in an array = rowsize *columnsize = 4*4 = 16ExampleFollowing is the C program to calculate the sum of all elements of a two-dimensional array by using run-time initialization − Live Demo#include void main(){ //Declaring array and variables// int A[4][3], i, j, even=0, odd=0; //Reading elements into the array// ... Read More
ProblemCalculate the sum and product of all elements in an array by using the run-time compilation.SolutionA two-dimensional array is used in situations where a table of values have to be stored (or) in matrices applicationsThe syntax is as follows −datatype array_ name [rowsize] [column size];For example, int a[5] [5];Number of elements in array = rowsize *columnsize = 5*5 = 25ExampleFollowing is the C program to calculate the sum and product of all elements in an array by using the run-time compilation − Live Demo#include void main(){ //Declaring the array - run time// int A[2][3], B[2][3], i, j, sum[i][j], product[i][j]; ... Read More