10 Const Questions and answers section with explanation for various online exam preparation, various interviews, C Language Const online test. If there is any error while opening a file, fopen will return? A constant expression is, roughly, one that can be evaluated at compile time (like 2+2 or 42 ). This means that int foo[NUM_FOO]; is a variable length array, defined as follows from section 6.7.6.2p4: If the size is not present, the array type is an incomplete type. Can I also say: 'ich tut mir leid' instead of 'es tut mir leid'? They are both valid code and they are both equivalent. Red Thing = "can't change the box it's next to" ptrx doesn't know, and doesn't need to know, that x is const (in fact a pointer object doesn't "know" anything), but the compiler is entitled to assume that, since you defined x as const, its value will never change. Is there a place where adultery is a crime? Insufficient travel insurance to cover the massive medical expenses for a visitor to US? After sometime the stack memory will be filled completely. (C++ has different rules. If you want to define a named constant of type int, you can use an enum: or an old-fashioned macro (which isn't restricted to type int): jamesdlin's answer and dbush's answer are both correct. Point out the error in the program? If you run the above code in Linux (32 bit platform), the output will be 24, 5 (because the size of pointer is 4 bytes). Hence stack overflow error will occur. In int * const you have a constant pointer to . Step 3: printf("%d\n", x); It prints the value of variable 'x'. Step 2: const int x=y; The constant variable 'x' is declared as an integer and it is initialized with the variable 'y' value. Step 2:const int *ptrx;The constant variableptrxis declared as an integer pointer. What will be the output of the program?#include<stdio.h>int main () { const int i=0; printf ("%dn", i++); return 0;} 10 11 no output Error: ++needs a value Workspace 3. Declaration Syntax: double sqrt(double x) calculates and return the positive square root of the given number. Copyright 2021 Quizack . Const questions are delivered with accurate answer. Step 1: const int x=5; The constant variable x is declared as an integer data type and initialized with value '5'.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. False, Always the macro is substituted by the given text/expression. const in C does not declare a compile-time constant. How to add a local CA authority on an air-gapped host of Debian. This above process will be repeated in Loop 3, Loop 4, Loop 5. To learn more, see our tips on writing great answers. This is the right place. for(i<=5 && i>=-1; ++i; i>0) scope; such arrays are nonetheless complete types. What is use of placing 'const' in range based for loop at diffrent places? How does the number of CMB photons vary with time? In this example, the value of NUM_FOO cannot be known when compiling bar.c; it is not known until you choose to link foo.o and bar.o. Step 2: const int *ptrx; The constant variable ptrx is declared as an integer pointer. #include int main () { const int x = 5; int *ptrx; ptrx = &x; *ptrx = x + *ptrx; printf ("%d\n", x); return 0; } This problem has been solved! so expression i<=5 && i>=-1 initializes for loop. char *str = "IndiaBIX"; (or) char str[] = "IndiaBIX"; Step 1: char *str[] = {"Frogs", "Do", "Not", "Die", "They", "Croak! #include void main() { int x = 97; char y = x; printf("%cn", y); }, View Answer C. Reposition the file pointer to begining of that line. This will create an error "Cannot modify a const object".Because, we cannot modify a const variable. What will be the output of the program?#include#includeunion 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, 10. operand to the sizeof or _Alignof operator. Correct Answer No error Explanation The output will be (in 16-bit platform DOS): K 75 0.000000 Constants problems Search Results 1. Const Questions with detailed description, explanation will help you to master the topic. int *const. This problem has been solved! Copyright CuriousTab. ungetc(c, stdin); "ungetc()" function pushes character 'a' back into input stream. To acquire clear understanding of Const, exercise these advanced Const questions with answers. I found that if always write const in right, it will becoming easier to read a complex pointer type, for fun. You can use an enum constant instead if you want to avoid using #define and want a symbolic name that can appear in a debugger. Which functions can be used to set the file position indicator to the beginning of the file? #include<stdio.h> int main () { const char *s = ""; char str [] = "Hello"; s = str; while (*s) printf ("%c", *s++); return 0; } Options A. The thing to remember (and yes, this is a bit counterintuitive) is that const doesn't mean constant. Why is this Pointer to Constant Pointer assignment Illegal? I would not want to use such compiler then. 10).Hence the output of the program isBefore modification arr[3] = 4After modification arr[3] = 10, 4. What will be the output of the program? Step 2: const int max=128; The constant variable max is declared as an integer data type and it is initialized with value 128. only be used in declarations or type names with function prototype How can an accidental cat scratch break skin but not damage clothes? Frees the specified query object using the Detour allocator. for(i=1; i<=5; i++) Here the for loop runs 5 times. Winners are those who can use the simplest method for solving a question. Experts are tested by Chegg as specialists in their subject area. To change the value of const variable x we have to use *(int *)&x = 10; Step 1: A macro named MAX is defined with value 128. Computer Science Computer Science questions and answers What is the output of the following code? What will be the output of the program?#includeint fun(int **ptr);int main(){ int i=10; const int *ptr = &i; fun(&ptr); return 0;}int fun(int **ptr){ int j = 223; int *temp = &j; printf("Before changing ptr = %5xn", *ptr); const *ptr = temp; printf("After changing ptr = %5xn", *ptr); return 0;}, 9. Is it true that too many recursive calls may result into stack overflow? By clicking Post Your Answer, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct. It wasn't designed to produce readable sources. extern const int i; If you wish to define an extern variable in a C++ source code file for use in a C source code file, use: extern "C" const int x=10; to prevent name mangling by the C++ compiler. Yes, that is exactly the same. So it prints the character 'a'. the size is * instead of being an expression, the array type Step 2: const int *ptrx; The constant variable ptrx is declared as an integer pointer. char *strnset(char *s, int ch, size_t n); Sets the first n characters of s to ch, string before strnset: abcdefghijklmnopqrstuvwxyz, string after strnset: xxxxxxxxxxxxxnopqrstuvwxyz. True, the header file contains classes, function prototypes, structure declaration, macros. constant size, the array type is not a variable length array I am unable to determine why this C program gives me this answer. What will be the output of the program? If the size of pointer is 4 bytes then What will be the output of the program ? Those values clearly cannot be determined until execution time. ProfileAnswers by bismitapadhyQuestions by bismitapadhy. However, this may not be enforced with some compilers.If compiling with a C compiler, there is a warning, but the assignment discards qualifier const. The rule in C++ is essentially that const applies to the type to its left. For example in int const * you have a pointer to a constant integer. is a variable length array type of unspecified size, which can Get invaluable Interview and Career Tips delivered directly to your inbox. Check out the latest current affairs questions and answers. it is not a matter of compiler it is matter of our fundamental so fundamental is that we can not change constant .so there is error. Is it not the case that C99 allows variable size arrays? Point out the error in the program (in Turbo-C). floating constants that are the immediate operands of casts. Step 3: ptrx = &x; The address of the constant variable x is assigned to integer pointer. Step 1: int get(); This is the function prototype for the funtion get(), it tells the compiler returns an integer value and accept no parameters.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". For solving each and every question, very lucid explanations are provided with diagrams wherever necessary. Because, we cannot modify a const variable. I thought C had no more surprises for me, but this surprised me. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Is it possible for rockets to exist in a world that is only in the early stages of developing jet aircraft? 9. Step 3:ptrx = &x;The address of the constant variablexis assigned to integer pointer variable ptrx. Site design / logo 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA. What will be the output of the program? When following a member function's parameter list, the const keyword specifies that the function doesn't modify the object for which it's . This is also why you can't use addresses in constant expressions except for address constant expressions which are limited to essentially the address of an object plus a constant. expression ++i is the loop condition. I mean: As far as I know, const only get complex whenever it involves pointer. Legend Red Thing = "can't change To change the value ofconstvariablexwe have to use*(int *)&x = 10; Start typing & press "Enter" or "ESC" to close. Following code, the p2 is Integer pointer or Integer? What will be the output of the program (in Turbo C)?#includeint fun(int *f){ *f = 10; return 0;}int main(){ const int arr[5] = {1, 2, 3, 4, 5}; printf("Before modification arr[3] = %d", arr[3]); fun(&arr[3]); printf("nAfter modification arr[3] = %d", arr[3]); return 0;}, Step 1: const int arr[5] = {1, 2, 3, 4, 5}; The constant variable arr is declared as an integer array and initialized toarr[0] = 1, arr[1] = 2, arr[2] = 3, arr[3] = 4, arr[4] = 5Step 2: printf("Before modification arr[3] = %d", arr[3]); It prints the value of arr[3] (ie. C99 does support VLAs. My C++ experience has made me internally deprecate #define as much as possible. So, x = 10.If compiling with a C++ compiler, the above assignment is not allowed. Data This page provides important questions on Const along with correct answers and clear explanation, which will be very useful for various Interviews, Competitive examinations and Entrance tests. if talk to turbo compiler then run this program without error.and i m telling that turbo compiler has sevrak mistakes that confuse a begginer. variable ptrx. Connect and share knowledge within a single location that is structured and easy to search. ro_ptr2 "Const pointer to an int" "Pointer to a const int" ro_refx Tip: Read the declaration "right-to-left" Legend Red Thing = "can't change the box it . 6. A. Reposition the file pointer to a character reverse. #include, In which stage the following code : #include. As for variable length arrays, yes, C added them in C99 (and made them optional in C11). Why would you use the keyword const if you already know variable should be constant? What is the output of the following code? Efficiently match all values of a vector in another vector. const int *p vs. int const *p - Is const after the type acceptable? Step 3: ptrx = &x; The address of the constant variable x is assigned to integer pointer variable ptrx. 3. Because we r using pointer to chane the value of the variable.So it should be ans is 10. if u give ptr=10 in above question then ans is 5 only, here an error because we can not convert const int * to int *. Why do some images depict the same constellations differently? To learn more, see our tips on writing great answers. D. Reposition the file pointer to begining of file. Step 3: ptrx = &x; The address of the constant variable x is assigned to integer pointer variable ptrx. Step 3: ptrx = &x; The address of the constant variable x is assigned to integer pointer variable ptrx. Asking for help, clarification, or responding to other answers. Declares a pointer whose data cannot be changed through the pointer: Declares a pointer who cannot be changed to point to something else: Yes, they are the same. Here p points to the first character in the string "Hello". even if that's IFR in the categorical outlooks? typedef struct data mystruct; struct data { int x; mystruct *b; }; 'The expressions *ptr++ and ++*ptr are same'. Is there a grammatical term to describe this usage of "may be"? What will be the output of the program? 2021 All rights reserved, What is the following program doing? Copyright 2019 Sawaal.com | All Rights Reserved. Step 3: ptrx = &x; The address of the constant variable x is assigned to integer pointer variable ptrx. I have an idea how to interpret (non)const pointers and data. 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". Step 1: const int x=5; The constant variable x is declared as an integer data type and initialized with value '5'. But as jamesdlin's answer pointed out, VS2019 doesn't support C99 or C11. Copyright 2014-2023 GeekInterview.com | All Rights Reserved. Which of the following function sets first n characters of a string to a given character? In July 2022, did China have more nuclear weapons than Domino's Pizza locations? printf("%c", c); prints the character 'a' which is given in the previous "scanf()" statement. In Germany, does an academia position after Phd has an age limit? Electronics and Communication Engineering. What will be the output of the program? int *const is a constant pointer to integer This means that the variable being declared is a constant pointer pointing to an integer. Yes, they are the same. #include int main() { const int x = 5; int *ptrx; ptrx = &x; *ptrx = x + *ptrx; printf("%d\n", x); return 0; }. Continue with Recommended Cookies, https://quizack.com/c/mcq/output-of-following-program-include-stdio-h-int-main-const-int-x-5-const-int-ptrx-ptrx-x-ptrx-10-printf-d-n-x-return-0, What will be the output of following program? For example, you can have in foo.c. Which version of C is supported by VS2019 compiler? True (but C++ does allow this when the initial value is visible; I'm not sure of the exact rules). Men's response to women's teshuka - source and explanations. Just get into the habit to write, You should also include 'int const *' and just for a laugh 'int const * const', @Cogwheel - Precisely. So textual replacement of clrscr() to 100 occurs.The input program to compiler looks like this : Note: 100; is an executable statement but with no action. the following code: int x = 5; int &refx = x; x, refx 5 int *ptrx = &x; Difference between declaring reference and 'address of' operator. an integer constant expression and the element type has a known In for( i <= 5 && i >= -1; ++i; i>0) expression i<=5 && i>=-1 evaluates to one. Which functions can be used to set the file position indicator to the beginning of the file? Because, we cannot use variable to define the size of array. A constant expression is, roughly, one that can be evaluated at compile time (like 2+2 or 42). Returns the minimum of two values. 3. Interpretation, Submit Effectively, this implies that the pointer shouldn't point to some other address. type. Step 2: const int *ptrx; The constant variable ptrx is declared as an integer pointer. Hence the if condition is satisfied and it prints 'Hi'Example: printf("%f\n", sqrt(36.0)); It prints the square root of 36 in the float format(i.e 6.000000). So this one was a real surprise. Why cannot a const struct field be used as an initializer in C? 5. However, there's an exception that if you put it on the extreme left of the declaration, it applies to the first part of the type. enumeration constants, character constants, sizeof expressions LOGIN to continue using GeekInterview website. Each question carries 1 mark; there are no negative marks. Step 1: const int i=0; The constant variable 'i' is declared as an integer and initialized with value of '0'(zero). This will result in an error. Step 1:const int x=5;The constant variablexis declared as an integer data type and initialized with value '5'. ptrx 0x7fff. int x = 5, y = 0; int &refx = x; const int &ro_refx = x; int *ptry = &y; const int *ro_ptr1 = &y; int *const ro_ptr2 = &y; x, refx, 5 0x7fff. scanf("%c", &c); Here we give 'a' as input. The pattern here becomes apparent if the definitions are. What control inputs to make if a wing falls off? Step 4:*ptrx = 10;Here we are indirectly trying to change the value of the constant vaiablex. Should convert 'k' and 't' sounds to 'g' and 'd' sounds when they follow 's' in a word for pronunciation? I can change both the address it points to and the value at at that address. (C++ doesn't support VLAs. int x = 5; int y; const int * const ptr = &x; printf("%d\n", *ptr); *ptr = 15; ptr = &y how many errors are in this coding segment ? Explanation the output of the constant variablexis declared as an initializer in?... The positive square root of the following function sets first n characters of a vector in another....: ptrx = & amp ; x ; the constant vaiablex can Get invaluable Interview and Career delivered! Create an error `` can not modify a const variable VS2019 compiler declared is a constant to! # x27 ; t point to some other address wing falls off pointing to an pointer. Mean: as far as i know, const only Get complex whenever it pointer... Age limit for ( i=1 ; i < =5 ; i++ ) Here for. Const * you have a pointer to integer pointer insurance to cover the massive medical expenses a. Found that if Always write const in right, it will becoming easier to read complex! Is it not the case that C99 allows variable size arrays, it will becoming easier to read a pointer... Share knowledge within a single location that is only in the string `` ''. Complex pointer type, for fun the header file contains classes, prototypes. 5 ' negative marks to use such compiler then run this program without error.and i telling... C added them in C99 ( and made them optional in C11 ) ptrx ; the constant variable is. At at that address int * const you have a constant expression is roughly. Submit Effectively, this implies that the variable being declared is a constant pointer to a pointer. Than Domino 's Pizza locations for help, clarification, or responding other., structure declaration, macros match all values of a vector in another vector valid code and are... Than Domino 's Pizza locations ( and made them optional in C11 ) is supported by VS2019?..., see our tips on writing great answers, https: //quizack.com/c/mcq/output-of-following-program-include-stdio-h-int-main-const-int-x-5-const-int-ptrx-ptrx-x-ptrx-10-printf-d-n-x-return-0, is... You have a pointer to a given character is this pointer to begining of file easy Search... Pattern Here becomes apparent if the size of array directly to your inbox constant x. You already know variable should be constant i=1 ; i 'm not sure of following! Constants that are the immediate operands of casts function prototypes, structure declaration, macros the definitions are for visitor...: const int x=5 ; the address of the exact rules ) is structured and easy to Search will. * you have a constant expression is, roughly, one that can be to... 3, Loop 4, Loop 5 also say: 'ich tut mir '! Has made me internally deprecate # define as much as possible preparation, various interviews, Language... Loop 4, Loop 4, Loop 4, Loop 5 ptrx = & amp ; x the! Of const, exercise these advanced const questions with detailed description, explanation will help you to master the.... Implies that the variable being declared is a constant expression is, roughly, one that can used! Answers section with explanation for various online exam preparation, various interviews, C them. N characters of a string to a constant pointer to begining of file if a wing off... X=5 ; the constant variable x is assigned to integer pointer variable.! The categorical outlooks initializer in C not the case that C99 allows variable size arrays various interviews C! =5 & & i > =-1 initializes for Loop at diffrent places pointer pointing to an pointer! Subject area to describe this usage of `` may be '' sets first n characters of a in. 'Const ' in range based for Loop declare a compile-time constant do some images depict the same constellations differently optional... Interpret ( non ) const pointers and data type and initialized with value ' 5 ' C is by... T point to some other const int x=5 const int *ptrx simplest method for solving each and every question, very lucid are... 2023 stack Exchange Inc ; user contributions licensed under CC BY-SA p - is const after the type acceptable compiler. Memory will be the output of the constant variable ptrx is declared as an data... Phd has an age limit ; there are no negative marks easy to Search provided with diagrams wherever necessary local. Program doing int * ptrx ; the address of the given number file, fopen will return data. Dos ): K 75 0.000000 constants problems Search Results 1 with answers the following code after sometime stack. 2: const int x=5 ; the address it points to the type to its left C supported. Of pointer is 4 bytes then What will be the output of following program doing Phd has age... Other address C ) ; `` ungetc ( ) '' function pushes character ' a ' into... 'S Pizza locations share knowledge within a single location that is structured easy. In July 2022, did China have more nuclear weapons than Domino 's Pizza locations int ptrx.: ptrx = & x ; the address of the exact rules.... Point out the latest current affairs questions and answers section with explanation for various online preparation. Can change both the address it points to and the value of the constant vaiablex to.! Wing falls off each question carries 1 mark ; there are no negative marks visible ; 'm. Expenses for a visitor to US of following program nuclear weapons than 's! Will be repeated in Loop 3, Loop 5 & x ; the constant vaiablex a visitor to?. I < =5 & & i > =-1 initializes for Loop at places. Does the number of CMB photons vary with time have more nuclear weapons than Domino 's Pizza?! At that address to some other address exact rules ) Language const online test error.and... Of pointer is 4 bytes then What will be the output of following program all values of a vector another. Online test ; `` ungetc ( C, stdin ) ; `` ungetc ( ) '' function character! I=1 ; i 'm not sure of the constant variable ptrx that are the operands... Change the value of variable ' x ' variable should be constant p is! A ' as input: ptrx = & x ; the constant variable x is assigned integer. Falls off and they are both valid code and they are both valid code they... A. Reposition the file position indicator to the type acceptable mean: as far as i know const... Why would you use the simplest method for solving a question your inbox rockets exist... A. Reposition the file pointer to constant pointer pointing to an integer pointer a wing off! Detour allocator one that can be used to set the file position indicator the! 'S IFR in the categorical outlooks Reposition the file pointer to constant pointer to a constant integer travel insurance cover... To read a complex pointer type, for fun Loop runs 5 times is 4 then..., stdin ) ; `` ungetc ( ) '' function pushes character a... Points to the first character in the categorical outlooks, Submit Effectively, this implies that pointer... For rockets to exist in a world that is structured and easy to.... Which of the file position indicator to the beginning of the constant variable x is assigned to integer const int x=5 const int *ptrx IFR! The initial value is visible ; i < =5 & & i > =-1 initializes for Loop at diffrent?. Change the value at at that address it involves pointer unspecified size, which can Get Interview! With value ' 5 ' section with explanation for various online exam preparation, various interviews, C const! Arrays, yes, C added them in C99 ( and made optional! This means that the variable being declared is a constant pointer to a constant pointer to have an idea to. To women 's teshuka - source and explanations ; there are no marks. Master the topic C99 ( and made them optional in C11 ) that too many calls! For help, clarification, or responding to other answers in int const * you a. Be determined until execution time for a visitor to US, VS2019 does n't support C99 or C11 it... # define as much as possible the header file contains classes, function prototypes structure... Of Debian tips on writing great answers define as much as possible would not want to use such then! The header file contains classes, function prototypes, structure declaration, macros insufficient travel insurance to the... The case that C99 allows variable size arrays jet aircraft, one can. Pattern Here becomes apparent if the definitions are design / logo 2023 stack Inc. User contributions licensed under CC BY-SA error in the program ( in Turbo-C ) another vector integer means! If the definitions are 2021 all rights reserved, What is the output of following program & ;! Tut mir leid ' tut mir leid ' stage the following code const variable an air-gapped host of Debian if! Constants that are the immediate operands of casts number of CMB photons vary with time the keyword if. Be determined until execution time given character is not allowed Phd has an age limit program. Ptrx = & amp ; x ; the address of the program ( 16-bit. Repeated in Loop 3, Loop 4, Loop 5 of pointer is const int x=5 const int *ptrx bytes then What will be in... Keyword const if you already know variable should be constant const applies to the first character in the categorical?..., the p2 is integer pointer the positive square root of the variablexis. At that address which functions can be used to set const int x=5 const int *ptrx file position to. Cmb photons vary with time method for solving a question the constant declared!