Example:
#include <stdio.h>
#include <string.h>
int main()
{
char str[30] = "12345678910111213";
printf("The last position of '2' is %d.\n",
strrchr(str, '2') - str);
return 0;
}
Output: The last position of '2' is 14.
#include<stdio.h> int main() { const c = -11; const int d = 34; printf("%d, %d\n", c, d); return 0; }
Step 2: const int d = 34; The constant variable 'd' is declared as an integer and initialized to value '34'.
Step 3: printf("%d, %d\n", c, d); The value of the variable 'c' and 'd' are printed.
Hence the output of the program is -11, 34
#include<stdio.h> int main() { const int x=5; const int *ptrx; ptrx = &x; *ptrx = 10; printf("%d\n", x); return 0; }
Step 2: const int *ptrx; The constant variable ptrx is declared as an integer pointer.
Step 3: ptrx = &x; The address of the constant variable x is assigned to integer pointer variable ptrx.
Step 4: *ptrx = 10; Here we are indirectly trying to change the value of the constant vaiable x. This will result in an error.
To change the value of const variable x we have to use *(int *)&x = 10;
#include<stdio.h> #include<stdlib.h> union employee { char name[15]; int age; float salary; }; const union employee e1; int main() { strcpy(e1.name, "K"); printf("%s %d %f", e1.name, e1.age, e1.salary); return 0; }
The output will be (in 16-bit platform DOS):
K 75 0.000000/* Prints a random number in the range 0 to 99 */
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(void)
{
randomize();
printf("Random number in the 0-99 range: %d\n", random (100));
return 0;
}
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int num1 = 12345;
float num2 = 5.12;
char str1[20];
char str2[20];
itoa(num1, str1, 10); /* 10 radix value */
printf("integer = %d string = %s \n", num1, str1);
sprintf(str2, "%f", num2);
printf("float = %f string = %s", num2, str2);
return 0;
}
// Output:
// integer = 12345 string = 12345
// float = 5.120000 string = 5.120000
Comments
There are no comments.Copyright ©CuriousTab. All rights reserved.