1. | ! |
2. | sizeof |
3. | ~ |
4. | && |
&& Logical AND is a logical operator.
Therefore, 1, 2, 3 are unary operators.
#include<stdio.h> #define MAX 128 int main() { const int max=128; char array[max]; char string[MAX]; array[0] = string[0] = 'A'; printf("%c %c\n", array[0], string[0]); return 0; }
Step 2: const int max=128; The constant variable max is declared as an integer data type and it is initialized with value 128.
Step 3: char array[max]; This statement reports an error "constant expression required". Because, we cannot use variable to define the size of array.
To avoid this error, we have to declare the size of an array as static. Eg. char array[10]; or use macro char array[MAX];
Note: The above program will print A A as output in Unix platform.
#include<stdio.h> int fun(int **ptr); int main() { int i=10, j=20; const int *ptr = &i; printf(" i = %5X", ptr); printf(" ptr = %d", *ptr); ptr = &j; printf(" j = %5X", ptr); printf(" ptr = %d", *ptr); return 0; }
#include<stdio.h> int main() { const int i=0; printf("%d\n", i++); return 0; }
Step 1: const int i=0; The constant variable 'i' is declared as an integer and initialized with value of '0'(zero).
Step 2: printf("%d\n", i++); Here the variable 'i' is increemented by 1(one). This will create an error "Cannot modify a const object".
Because, we cannot modify a const variable.
#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", e1.name); e1.age=85; printf("%d", e1.age); printf("%f", e1.salary); return 0; }
#include<stdio.h> #define MAX 128 int main() { char mybuf[] = "India"; char yourbuf[] = "CURIOUSTAB"; char *const ptr = mybuf; *ptr = 'a'; ptr = yourbuf; return 0; }
Step 2: char yourbuf[] = "CURIOUSTAB"; The variable yourbuf is declared as an array of characters and initialized with string "CURIOUSTAB".
Step 3: char *const ptr = mybuf; Here, ptr is a constant pointer, which points at a char.
The value at which ptr it points is not a constant; it will not be an error to modify the pointed character; There will be an error only to modify the pointer itself.
Step 4: *ptr = 'a'; The value of ptr is assigned to 'a'.
Step 5: ptr = yourbuf; Here, we are changing the pointer itself, this will result in the error "cannot modify a const object".
#include<stdio.h> #define MAX 128 int main() { char mybuf[] = "India"; char yourbuf[] = "CURIOUSTAB"; char const *ptr = mybuf; *ptr = 'a'; ptr = yourbuf; return 0; }
Step 2: char yourbuf[] = "CURIOUSTAB"; The variable yourbuf is declared as an array of characters and initialized with string "CURIOUSTAB".
Step 3: char const *ptr = mybuf; Here, ptr is a constant pointer, which points at a char.
The value at which ptr it points is a constant; it will be an error to modify the pointed character; There will not be any error to modify the pointer itself.
Step 4: *ptr = 'a'; Here, we are changing the value of ptr, this will result in the error "cannot modify a const object".
#include<stdio.h> int main() { const char *s = ""; char str[] = "Hello"; s = str; while(*s) printf("%c", *s++); return 0; }
Step 2: char str[] = "Hello"; The variable str is declared as an array of charactrers type and initialized with a string "Hello".
Step 3: s = str; The value of the variable str is assigned to the variable s. Therefore str contains the text "Hello".
Step 4: while(*s){ printf("%c", *s++); } Here the while loop got executed untill the value of the variable s is available and it prints the each character of the variable s.
Hence the output of the program is "Hello".
#include<stdio.h> int get(); int main() { const int x = get(); printf("%d", x); return 0; } int get() { return 20; }
Step 2: const int x = get(); The constant variable x is declared as an integer data type and initialized with the value "20".
The function get() returns the value "20".
Step 3: printf("%d", x); It prints the value of the variable x.
Hence the output of the program is "20".
#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() { int i = 1; switch(i) { printf("Hello\n"); case 1: printf("Hi\n"); break; case 2: printf("\nBye\n"); break; } return 0; }
Then case 1: statements got executed. so, it prints "Hi". The break; statement make the program to be exited from switch-case statement.
switch-case do not execute any statements outside these blocks case and default
Hence the output is "Hi".
Comments
There are no comments.Copyright ©CuriousTab. All rights reserved.