What is the output of printf("%d")?

1 Answer

Answer :

1. When we write printf("%d",x); this means compiler will print the value of x. But  as here, there is nothing after �%d� so compiler will show in output window  garbage value.  
2. When we use %d the compiler internally uses it to access the argument in the  stack (argument stack). Ideally compiler determines the offset of the data  variable depending on the format specification string. Now when we write  printf("%d",a) then compiler first accesses the top most element in the argument  stack of the printf which is %d and depending on the format string it calculated  to offset to the actual data variable in the memory which is to be printed. Now  when only %d will be present in the printf then compiler will calculate the correct  offset (which will be the offset to access the integer variable) but as the actual  data object is to be printed is not present at that memory location so it will print  what ever will be the contents of that memory location.  
3. Some compilers check the format string and will generate an error without the  proper number and type of arguments for things like printf(...) and scanf(...).

Related questions

Description : printf() Function- What is the difference between "printf(...)" and "sprintf(...)"?

Last Answer : sprintf(...) writes data to the character array whereas printf(...) writes data to the standard output device.

Description : "union" Data Type What is the output of the following program? Why?

Last Answer : #include main() { typedef union { int a; char b[10]; float c; } Union; Union x,y = {100}; x.a = 50; strcpy(x.b,"hello"); x.c = 21.50; printf("Union x : %d %s %f n",x.a,x.b,x.c); printf("Union y : %d %s %f n",y.a,y.b,y.c); }

Description : Trace the error: void main( ) { int *b, &a; *b = 20 printf(“%d, %d”, a, *b) } (A) No error (B) Logical error (C) Syntax error (D) Semantic error

Last Answer : (C) Syntax error

Description : String Processing --- Write out a function that prints out all the permutations of a string. For example, abc would give you abc, acb, bac, bca, cab, cba.

Last Answer : void PrintPermu (char *sBegin, char* sRest) { int iLoop; char cTmp; char cFLetter[1]; char *sNewBegin; char *sCur; int iLen; static int iCount; iLen = strlen(sRest); if (iLen == 2) { iCount++; ... printf("\nEnter a string:"); scanf("%s%*c",sIn); memset(s,0,255); PrintPermu(s, sIn); }

Description : Linked Lists -- Can you tell me how to check whether a linked list is circular?

Last Answer : Create two pointers, and set both to the start of the list. Update each as follows: while (pointer1) { pointer1 = pointer1->next; pointer2 = pointer2->next; if (pointer2) pointer2=pointer2->next ... before pointer1, or the item before that. Either way, its either 1 or 2 jumps until they meet.

Last Answer : Size of the final executable can be reduced using dynamic linking for libraries.

Description : malloc() Function- What is the difference between "calloc(...)" and "malloc(...)"?

Last Answer : 1. calloc(...) allocates a block of memory for an array of elements of a certain size. By default the block is initialized to 0. The total number of memory allocated will be (number_of_elements * ... calls malloc(...) in order to use the C+ + _set_new_mode function to set the new handler mode.

Description : What is C language?

Last Answer : The C programming language is a standardized programming language developed in the early 1970s by Ken Thompson and Dennis Ritchie for use on the UNIX operating system. It has since spread to ... language for writing system software, though it is also used for writing applications. ...

Description : Write the output for the following commands: (i) ls –a (ii) date “+%D”

Last Answer : i) ls –a : list all files including hidden files. These are files that start with “.”. ii) date “+%D” : Display date as mm/dd/yy. output: 11/26/19

Description : What will be the output of the following segment of the program? main( ) { char *s = “hello world”; int i = 7; printf(“%, *s”, i, s); } (A) Syntax error (B) hello w (C) hello (D) o world

Last Answer : Answer: Marks given to all

Description : What will be the output of the following ‘C’ code ? main ( ) { int x = 128; printf (“\n%d”, 1 + x++); } (A) 128 (B) 129 (C) 130 (D) 131

Last Answer :  (B) 129

Description : What is the output of the following program ? (Assume that the appropriate pre-processor directives are included and there is no syntax error) main() { char S[ ] = "ABCDEFGH"; printf ("%C",* (& ... Base address of S is 1000 */ } (A) ABCDEFGH1000 (B) CDEFGH1000 (C) DDEFGHH1000 (D) DEFGH1000

Last Answer : (D) DEFGH1000

Description : What is wrong in this statement? scanf(“%d”,whatnumber);

Last Answer : An ampersand & symbol must be placed before the variable name whatnumber. Placing & means whatever integer value is entered by the user is stored at the “address” of the variable name. This is a common mistake for programmers, often leading to logical errors.

Description : Write a C program Reading and Writing data using Scanf() and Printf() statement -Technology

Last Answer : #include int main(){ char ch; char str[100]; printf('Enter any character '); scanf('%c', &ch); printf('Entered character is %c ', ch); printf('Enter any string ( upto 100 ... , str);}OUTPUT:Enter any characteraEntered character is aEnter any string ( upto 100 character )haiEntered string is hai

Description : printf ("%" d,% x ", & b); Explain the statement ?

Last Answer : printf ( % d,% x , & b); Here , the printf statement displays the output function printf () function as a variable data result of different data types. The C program is used to ... b respectively which indicates the memory location of integer type a and b variables where data will be stored.

Description : What kind of mistake is it to write print f instead of printf and why ?

Last Answer : Write print f instead of printf, the syntax is wrong. If the syntax is wrong then the language in which the program is written is grammatically incorrect. As can be seen , the word Printf ... error. This error is easily detected when translating with a compiler and sends a message to the computer.

Description : What are the functions of Printf () and Getch () function ?

Last Answer : The most commonly used function as an output statement in C language is printf (), which can be used to display various types of data (such as int, Char, float, etc.), including strings or strings ... programming . That is, both print () and getch () act as essential functions for C programming .

Description : State the use of printf( ) & scanf( ) with suitable example.

Last Answer : printf( ) & scanf( ): printf() and scanf() functions are library functions in C programming language defined in stdio.h . printf() function is used to print the character, string, float, integer, octal and hexadecimal ... number"); scanf("%d",&i); printf("Entered number is: %d",i); getch(); }

Description : How many times the word 'print' shall be printed by the following program segment? for(i=1, i≤2, i++) for(j=1, j≤2, j++) for(k=1, k≤2, k++) printf("print/n") (A) 1 (B) 3 (C) 6 (D) 8

Last Answer : (D) 8