Explained C Aptitude Interview Questions Series-3

Here are Third batch of frequently asked C aptitude questions & these questions have proper answers given so mnc team suggest you should go over these Apptitude questions before your job interview


If u haven`t read C aptitude questions#1 & #2 in this excellent series then please do so before continuing


Q How can I implement a variable field width with printf? That is, instead of %8d, I want the width to be specified at run time.


A: printf("%*d", width, x) will do just what you want.


Q How can I print numbers with commas separating the thousands? What about currency formatted numbers?


A: The functions in begin to provide some support for these operations, but there is no standard routine for doing either task. (The only thing printf() does in response to a custom locale setting is to change its decimal-point character.)


Q What is wrong with this code?


char c;


while((c = getchar()) != EOF) ...


A: For one thing, the variable to hold getchar's return value must be an int. getchar() can return all possible character values, as well as EOF. By squeezing getchar's return value into a char, either a normal character might be misinterpreted as EOF, or the EOF might be altered (particularly if type char is unsigned) & so never seen.


Q Why does the code


while(!feof(infp)) {


fgets(buf, MAXLINE, infp);


fputs(buf, outfp);


}


copy the last line twice?


A: In C, end-of-file is only indicated *after* an input routine has tried to read, & failed. (In other words, C's I/O is not like Pascal's.) Usually, you should just check the return value of the input routine (in this case, fgets() will return NULL on end- of-file); often, you don't need to use feof() at all.


Q My program's prompts & intermediate output don't always show up on the screen, especially when I pipe the output through another program.


A: It's best to use an explicit fflush(stdout) whenever output should definitely be visible (& especially if the text does not end with \n). a lot of mechanisms attempt to perform the fflush() for you, at the "right time," but they tend to apply only when stdout is an interactive terminal.


Q How can I print a '%' character in a printf format string? I tried \%, but it didn't work.


A: Simply double the percent sign: %% .


\% can't work, because the backslash \ is the *compiler's* escape character, while here our problem is that the % is essentially printf's escape character.


Q Someone told me it was wrong to use %lf with printf(). How can printf() use %f for type double, if scanf() requires %lf?


A: It's true that printf's %f specifier works with both float & double arguments. Due to the "default argument promotions" (which apply in variable-length argument lists such as printf's, whether or not prototypes are in scope), values of type float are promoted to double, & printf() therefore sees only doubles. (printf() does accept %Lf, for long double.)


Q What printf format should I use for a typedef like size_t when I don't know whether it's long or some other type?


A: Use a cast to convert the value to a known, conservatively- sized type, then use the printf format matching that type. For example, to print the size of a type, you might use


printf("%lu", (unsigned long)sizeof(thetype));


Q Why doesn't the call scanf("%d", i) work?


A: The arguments you pass to scanf() must always be pointers. To fix the fragment above, change it to scanf("%d", &i) .


+Some Great DBMS aptitude Questions


+Exhaustive collection of Operating System Concepts


+All the C apptitude questions You want

Remember a lotta these questions with detailed explanation can be found At this Website

No comments:

Take Advantage Of 25 of the finest Articles to help u prepare for the Placement Papers and Interview questions
You’d Never Guess How easy it is to get a job after reading these placement preparation articles

Here are 25 Placement Preparation Articles


"IMPORTANT NOTE"
We Update our site frequently and we want to keep you guys informed so enter your email in the top left hand box and click subscribe,so that whenever we post new papers it gets delivered right to your mail.



 

© New Placement papers | Latest Placement Papers