SlideShare a Scribd company logo
Sushila Devi Bansal College of Technology   Indore Practicing “C” Skills Prepared By Mr. Tejalal Choudhary Lect. CS/IT Deptt.
Note: All the programs are tested under Turbo C/C++ compilers. It is assumed that, Programs run under DOS environment, Program is compiled using Turbo C/C++ compiler. The program output may depend on the information based on this assumptions (for example sizeof(int) == 2 may be assumed).
Pointer   pointers are variable that contain addresses, addresses are always whole numbers, pointers would always contain whole numbers
Pointer int a=5 Reserve space in memory to hold the integer Associate the name  a  with this location Store the value  5  at this location a 65524 & Operator &a -65524 Printf(“address of a=%u”,&a); Printf(“value of a=%d”,a); 5
Scanf(“%d%d”,&a,&b); Value at address/indirection operator  ( * ) Address of operator ( & ) Printf(“value of a=%d” *(&a) );  output-5 Pointer delcaration Int *b; b=&a; a  b 65524  65522 5 65524
Int *b  would mean, the value at address contained in  b  is an  int. Printf(“%u”,&a); Printf(“%u”,b); Printf(“%u”,&b); Printf(“%d”,*(&a)); Printf(“%d”,*b);
Pointer to pointer Int a=5 Int *b; Int **c; b=&a; c=&b; A  b  c 65524  65522  65520 5 65524 65522
&a b *c &b c &c b c *(&a) *b **c
&a  -  65524 b-  -  65524 *c  -  65524 &b  -  65522 c  -  65522 &c  -  65520 b  -  65524 c  -  65522 *(&a) -  5 *b  -  5 **c  -  5
//Find Output  void fun(int *i,int *j){ *i=*i**i; *j=*j**j; } void main(){ int i=5,j=2; fun(&i,&j); printf("%d%d",i,j); getch(); }
Passing array elements to function One by one int marks[ ]={10,20,30,40}; for(i=0;i<=3;i++) display(marks[i]); Display(int n) { printf(“marks=%d”,n); }
Call by reference int marks[ ]={10,20,30,40}; for(i=0;i<=3;i++) display( & marks[i]); Display(int  *n ) { printf(“marks=%d”, *n ); }
Find Output int i=10,*x; Float f=10.5,*y; Char c=‘k’ *z, x=&I; y=&f; z=&c; x++;  y++;  z++; What will be the values of x,y and z ?
Every time a pointer variable is incremented it points to the immediately next location of its type. Pointer variable can also be decremented.
//Find Output Int a[]={10,20,30,40,50,60,70}; Int *I,*j; J=&a[1]; K=&[5]; printf (“ %d %d”, j-I, *j-*i); base address is 65502
Each integer in the array occupies 2 bytes. i  and  j  are pointing to locations that are 4 integer apart. *j - *I  would be 40
Don’t try on pointer Addition of two pointer Multiplication of a pointer with constant Division of pointer with a constant
Accessing array with pointer int a[]={10,20,30,40,50,60}’ j=&a[0]; for(i=0;i<=5;i++) { printf(“%d”,*j); j++; }
Passing entire array to a function Int a[]={10,20,30,40,50,60}; display(&a[0],6); or display(a,6);
Real thing  *a  is equivalent to *(a+0) When we say  a[i]   C compiler internally converts it to  *(a + i) below all notations are valid a[i] *(a+i) *(i+a)   i[a]
2D array and pointer int a[][4]={100,10 200,20 300,30 400,40 }; a[0]  will give the address of the zeroth 1D array a[1]  will give the address of the first 1D array…. How to print a[2][1] element of array using pointer?
Memory map 65508 -100  65510 -10 65512 -200  65514 -20 65516 -300  65518 -30 65520 -400  65522 -40
a[2] would give the address 65516 So 65516+1 would give the address 65518 Or (a[2]+1) will give the same result 65518 So to find value ?
We know that  a[i]  is equivalent to *(a+i) So *(a[2]+1) is equivalent to  *(*(a+2)+1) Or  a[2][1] *(a[2]+1)
Int a[]={10,20,30,40,50} for(i=0;i<=4;i++) printf(“%d”,*(a+i)); Find output
Find output Int a[]={10,20,30,40,50}; Int I, j, m, K=1; i=++a[1]; j=a[1]++; m=a[i++]; Value of I, j, m ?
Find output void main() { int const * p=5; printf(&quot;%d&quot;,++(*p)); }
Answer: Compiler error : Cannot modify a constant value. Explanation : p is a pointer to a &quot;constant integer&quot;. But we tried to change the value of the &quot;constant integer&quot;.
Find output main() { static int var = 5; printf(&quot;%d &quot;,var--); if(var) main(); }
Answer:  5 4 3 2 1 Explanation: When  static  storage class is given, it is initialized once. The change in the value of a  static  variable is retained even between the function calls. Main is also treated like any other ordinary function, which can be called recursively.
Find output main() { int c[ ]={2.8,3.4,4,6.7,5}; int j,*p=c,*q=c; for(j=0;j<5;j++) { printf(&quot; %d &quot;,*c); ++q; } For(j=0;j<5;j++){ printf(&quot; %d &quot;,*p); ++p; } }
Answer:  2 2 2 2 2 2 3 4 6 5 Explanation: Initially pointer c is assigned to both  p  and  q . In the first loop, since only  q  is incremented and not c , the value 2 will be printed 5 times. In second loop  p  itself is incremented. So the values 2 3 4 6 5 will be printed.
Find output main() { char *p; printf(&quot;%d %d &quot;,sizeof(*p),sizeof(p)); }
Answer:  1 2 Explanation: The sizeof() operator gives the number of bytes taken by its operand. P is a character pointer, which needs one byte for storing its value (a character). Hence sizeof(*p) gives a value of 1. Since it needs two bytes to store the address of the character pointer sizeof(p) gives 2.
void f(int *,int); void main(){ int a[5],i,b=16; for(i=0;i<5;i++) a[i]=2*i; f(a,b); for(i=0;i<5;i++) printf(&quot;\n%d&quot;,a[i]); printf(&quot;\n%d&quot;,b); getch(); } void f(int *x,int y){ int i; for(i=0;i<5;i++) *(x+i)+=2; y+=2; }
output 2 4 6 8 10 16
Find output main() { int i=3; switch(i) { default:printf(&quot;zero&quot;); case 1: printf(&quot;one&quot;); break; case 2:printf(&quot;two&quot;); break; case 3: printf(&quot;three&quot;); break; } }
Answer :  three Explanation : The default case can be placed anywhere inside the loop. It is executed only when all other cases doesn't match.
Find output main() { int i=-1,j=-1,k=0,l=2,m; m=i++&&j++&&k++||l++; printf(&quot;%d %d %d %d %d&quot;,i,j,k,l,m); }
Answer:  0 0 1 3 1 Explanation : Logical operations always give a result of  1 or 0  . And also the logical AND (&&) operator has higher priority over the logical OR (||) operator. So the expression ‘ i++ && j++ && k++’  is executed first. The result of this expression is 0 (-1 && -1 && 0 =0). Now the expression is  0 || 2  which evaluates to 1 (because OR operator always gives 1 except for ‘0 || 0’ combination- for which it gives 0). So the value of m is 1. The values of other variables are also incremented by 1.
Find output main() { int c=- -2; printf(&quot;c=%d&quot;,c); }
Answer:  c=2; Explanation: Here unary minus (or negation) operator is used twice. Same maths rules applies, ie. minus * minus= plus. Note: However you cannot give like --2. Because -- operator can only be applied to variables as a  decrement  operator (eg., i--). 2 is a constant and not a variable.
Find output #define int char main() { int i=65; printf(&quot;sizeof(i)=%d&quot;,sizeof(i)); }
Answer:  sizeof(i)=1 Explanation: Since the #define replaces the string  int  by the macro  char
Find output main() { int i=10; i=!i>14; Printf (&quot;i=%d&quot;,i); }
Find output Answer:  i=0 Explanation: In the expression  !i>14  , NOT (!) operator has more precedence than ‘ >’ symbol.  !  is a unary logical operator. !i (!10) is 0 (not of true is false). 0>14 is false (zero).
#include<stdio.h> main() { char s[]={'a','b','c','\n','c','\0'}; char *p,*str,*str1; p=&s[3]; str=p; str1=s; printf(&quot;%d&quot;,++*p + ++*str1-32); }
Answer:  77 Explanation: p is pointing to character '\n'. str1 is pointing to character 'a' ++*p. &quot;p is pointing to '\n' and that is incremented by one.&quot; the ASCII value of '\n' is 10, which is then incremented to 11.The value of ++*p is 11. ++*str1, str1 is pointing to 'a' that is incremented by 1 and it becomes 'b'. ASCII value of 'b' is 98. Now performing (11 + 98 – 32), we get 77(&quot;M&quot;); So we get the output 77 :: &quot;M&quot; (Ascii is 77).
Find output main() { int i=5; printf(&quot;%d%d%d%d%d&quot;,i++,i--,++i,--i,i); }
Answer:  4 5 5 4 5 Explanation: The arguments in a function call are pushed into the stack from left to right. The evaluation is by popping out from the stack. and the evaluation is from right to left, hence the result.
Find output #define square(x) x*x main() { int i; i = 64/square(4); printf(&quot;%d&quot;,i); }
Answer:  64 Explanation: the macro call square(4) will substituted by 4*4 so the expression becomes I = 64/4*4 . Since / and * has equal priority the expression will be evaluated as (64/4)*4 i.e. 16*4 = 64
Find output include <stdio.h> #define a 10 main() { #define a 50 printf(&quot;%d&quot;,a); }
Answer:  50 Explanation: The preprocessor directives can be redefined anywhere in the program. So the most recently assigned value will be taken.
Find output #define clrscr() 100 main() { clrscr(); printf(&quot;%d\n&quot;,clrscr()); }
Answer:  100 Explanation: Preprocessor executes as a separate pass before the execution of the compiler. So textual replacement of clrscr() to 100 occurs
Find output enum colors {black,red,green}; main() { printf(&quot;%d..%d..%d&quot;,black,red,green); return(1); }
Answer:  0..1..2 Explanation: enum assigns numbers starting from 0, if not explicitly defined.
Find output main() { int i=0; for(;i++;printf(&quot;%d&quot;,i)) ; printf(&quot;%d&quot;,i); }
Answer:  1 Explanation: before entering into the for loop the checking condition is &quot;evaluated&quot;. Here it evaluates to 0 (false) and comes out of the loop,and i is incremented (note the semicolon after the for loop).
Find output main() { show(); } void show() { printf(&quot;I'm the greatest&quot;); }
Answer: Compier error: Type mismatch in redeclaration of show. Explanation: When the compiler sees the function show it doesn't know anything about it. So the default return type (ie, int) is assumed. But when compiler sees the actual definition of show mismatch occurs since it is declared as void. Hence the error. The solutions are as follows: 1. declare void show() in main() . 2. define show() before main(). 3. declare extern void show() before the use of show().
What will be the o/p of next program.
main( ) { static int a[ ] = {0,1,2,3,4}; int *p[ ] = {a,a+1,a+2,a+3,a+4}; int **ptr = p; ptr++; printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr); *ptr++; printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr); *++ptr; printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr); ++*ptr; printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr); }
output 111 222 333 344

More Related Content

DOC
C tech questions
PDF
C aptitude scribd
DOCX
C interview question answer 2
DOC
Captitude 2doc-100627004318-phpapp01
DOC
C aptitude.2doc
DOCX
Maharishi University of Management (MSc Computer Science test questions)
PPT
Introduction to AspectJ
PDF
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
C tech questions
C aptitude scribd
C interview question answer 2
Captitude 2doc-100627004318-phpapp01
C aptitude.2doc
Maharishi University of Management (MSc Computer Science test questions)
Introduction to AspectJ
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...

What's hot (20)

PDF
Programming Fundamentals Arrays and Strings
PPT
An imperative study of c
PPT
Pointers+(2)
PDF
C++ Programming - 1st Study
PPTX
Chapter 7 functions (c)
PPT
Unit 5 Foc
PDF
2014 computer science_question_paper
PPTX
CSE240 Pointers
PPT
PPT
Fp201 unit5 1
PDF
C programs
PPTX
USER DEFINE FUNCTIONS IN PYTHON
PPTX
Unit 3
PPSX
C programming pointer
PDF
C program
PPTX
Function recap
PPT
Boost.Interfaces
PDF
Implementing stack
PDF
Programming with GUTs
PDF
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Programming Fundamentals Arrays and Strings
An imperative study of c
Pointers+(2)
C++ Programming - 1st Study
Chapter 7 functions (c)
Unit 5 Foc
2014 computer science_question_paper
CSE240 Pointers
Fp201 unit5 1
C programs
USER DEFINE FUNCTIONS IN PYTHON
Unit 3
C programming pointer
C program
Function recap
Boost.Interfaces
Implementing stack
Programming with GUTs
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Ad

Viewers also liked (20)

PDF
communication Guide
PPS
Recomenzar
PPT
rampant caries
PPTX
ENFERMEDADES DE TRANSMISIÓN SEXUAL. DRA. LAURA PEREDA CORVERA
PPTX
PPTX
Ud3. El arte griego. Introducción y Arquitectura
PDF
Pruebas bioquimicas para la identificacion de bacterias de importancia clinica
PDF
Características de los virus
PPTX
La Corrupcion
PPTX
Tipos de corrupcion
PDF
θερμομόνωση2010 11
PPTX
Physical Unit Operations
DOC
Programacion de modulos 2015 cetpro2 doris
PPT
Chapter 13 materialism
PPT
El Arte Griego La Arquitectura
PPTX
DOCX
Modelo de Informe de una empresa
PPT
Antropologia Visual
communication Guide
Recomenzar
rampant caries
ENFERMEDADES DE TRANSMISIÓN SEXUAL. DRA. LAURA PEREDA CORVERA
Ud3. El arte griego. Introducción y Arquitectura
Pruebas bioquimicas para la identificacion de bacterias de importancia clinica
Características de los virus
La Corrupcion
Tipos de corrupcion
θερμομόνωση2010 11
Physical Unit Operations
Programacion de modulos 2015 cetpro2 doris
Chapter 13 materialism
El Arte Griego La Arquitectura
Modelo de Informe de una empresa
Antropologia Visual
Ad

Similar to pointers 1 (20)

PPS
C programming session 05
PPTX
C programming
PPT
Pointer
PDF
Chapter 13.1.8
PPTX
UNIT 4 POINTERS.pptx pointers pptx for basic c language
PPT
Ch 7-pointers
PPT
Unit3 C
PPTX
pointers_final.pptxxxxxxxxxxxxxxxxxxxxxx
DOCX
Qust & ans inc
PDF
Pointers [compatibility mode]
PDF
C Recursion, Pointers, Dynamic memory management
PPT
Pointers C programming
PPTX
C++ Pointer | Introduction to programming
PPT
ch08.ppt
PDF
Slide set 6 Strings and pointers.pdf
PDF
Lec23-CS110 Computational Engineering
PPT
l7-pointers.ppt
PPTX
Array, string and pointer
PPTX
Input output functions
PPTX
Algoritmos e Estruturas de Dados - Pointers
C programming session 05
C programming
Pointer
Chapter 13.1.8
UNIT 4 POINTERS.pptx pointers pptx for basic c language
Ch 7-pointers
Unit3 C
pointers_final.pptxxxxxxxxxxxxxxxxxxxxxx
Qust & ans inc
Pointers [compatibility mode]
C Recursion, Pointers, Dynamic memory management
Pointers C programming
C++ Pointer | Introduction to programming
ch08.ppt
Slide set 6 Strings and pointers.pdf
Lec23-CS110 Computational Engineering
l7-pointers.ppt
Array, string and pointer
Input output functions
Algoritmos e Estruturas de Dados - Pointers

More from gaurav koriya (8)

PPSX
Intrusion detection system
PDF
Hacking techniques
PPT
ALL ABOUT SQL AND RDBMS
PPT
INDIA N heritage -THE PUNJAB
PPT
ip address
PPT
About ip address
PPT
Katrin Aand Shruti Pps
PPT
Mobile Computing
Intrusion detection system
Hacking techniques
ALL ABOUT SQL AND RDBMS
INDIA N heritage -THE PUNJAB
ip address
About ip address
Katrin Aand Shruti Pps
Mobile Computing

Recently uploaded (20)

PDF
Classroom Observation Tools for Teachers
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
Pharma ospi slides which help in ospi learning
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
01-Introduction-to-Information-Management.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Cell Types and Its function , kingdom of life
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
RMMM.pdf make it easy to upload and study
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Classroom Observation Tools for Teachers
Module 4: Burden of Disease Tutorial Slides S2 2025
Microbial disease of the cardiovascular and lymphatic systems
PPH.pptx obstetrics and gynecology in nursing
Sports Quiz easy sports quiz sports quiz
Pharma ospi slides which help in ospi learning
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Anesthesia in Laparoscopic Surgery in India
VCE English Exam - Section C Student Revision Booklet
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
01-Introduction-to-Information-Management.pdf
TR - Agricultural Crops Production NC III.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Cell Types and Its function , kingdom of life
Abdominal Access Techniques with Prof. Dr. R K Mishra
RMMM.pdf make it easy to upload and study
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx

pointers 1

  • 1. Sushila Devi Bansal College of Technology Indore Practicing “C” Skills Prepared By Mr. Tejalal Choudhary Lect. CS/IT Deptt.
  • 2. Note: All the programs are tested under Turbo C/C++ compilers. It is assumed that, Programs run under DOS environment, Program is compiled using Turbo C/C++ compiler. The program output may depend on the information based on this assumptions (for example sizeof(int) == 2 may be assumed).
  • 3. Pointer pointers are variable that contain addresses, addresses are always whole numbers, pointers would always contain whole numbers
  • 4. Pointer int a=5 Reserve space in memory to hold the integer Associate the name a with this location Store the value 5 at this location a 65524 & Operator &a -65524 Printf(“address of a=%u”,&a); Printf(“value of a=%d”,a); 5
  • 5. Scanf(“%d%d”,&a,&b); Value at address/indirection operator ( * ) Address of operator ( & ) Printf(“value of a=%d” *(&a) ); output-5 Pointer delcaration Int *b; b=&a; a b 65524 65522 5 65524
  • 6. Int *b would mean, the value at address contained in b is an int. Printf(“%u”,&a); Printf(“%u”,b); Printf(“%u”,&b); Printf(“%d”,*(&a)); Printf(“%d”,*b);
  • 7. Pointer to pointer Int a=5 Int *b; Int **c; b=&a; c=&b; A b c 65524 65522 65520 5 65524 65522
  • 8. &a b *c &b c &c b c *(&a) *b **c
  • 9. &a - 65524 b- - 65524 *c - 65524 &b - 65522 c - 65522 &c - 65520 b - 65524 c - 65522 *(&a) - 5 *b - 5 **c - 5
  • 10. //Find Output void fun(int *i,int *j){ *i=*i**i; *j=*j**j; } void main(){ int i=5,j=2; fun(&i,&j); printf(&quot;%d%d&quot;,i,j); getch(); }
  • 11. Passing array elements to function One by one int marks[ ]={10,20,30,40}; for(i=0;i<=3;i++) display(marks[i]); Display(int n) { printf(“marks=%d”,n); }
  • 12. Call by reference int marks[ ]={10,20,30,40}; for(i=0;i<=3;i++) display( & marks[i]); Display(int *n ) { printf(“marks=%d”, *n ); }
  • 13. Find Output int i=10,*x; Float f=10.5,*y; Char c=‘k’ *z, x=&I; y=&f; z=&c; x++; y++; z++; What will be the values of x,y and z ?
  • 14. Every time a pointer variable is incremented it points to the immediately next location of its type. Pointer variable can also be decremented.
  • 15. //Find Output Int a[]={10,20,30,40,50,60,70}; Int *I,*j; J=&a[1]; K=&[5]; printf (“ %d %d”, j-I, *j-*i); base address is 65502
  • 16. Each integer in the array occupies 2 bytes. i and j are pointing to locations that are 4 integer apart. *j - *I would be 40
  • 17. Don’t try on pointer Addition of two pointer Multiplication of a pointer with constant Division of pointer with a constant
  • 18. Accessing array with pointer int a[]={10,20,30,40,50,60}’ j=&a[0]; for(i=0;i<=5;i++) { printf(“%d”,*j); j++; }
  • 19. Passing entire array to a function Int a[]={10,20,30,40,50,60}; display(&a[0],6); or display(a,6);
  • 20. Real thing *a is equivalent to *(a+0) When we say a[i] C compiler internally converts it to *(a + i) below all notations are valid a[i] *(a+i) *(i+a) i[a]
  • 21. 2D array and pointer int a[][4]={100,10 200,20 300,30 400,40 }; a[0] will give the address of the zeroth 1D array a[1] will give the address of the first 1D array…. How to print a[2][1] element of array using pointer?
  • 22. Memory map 65508 -100 65510 -10 65512 -200 65514 -20 65516 -300 65518 -30 65520 -400 65522 -40
  • 23. a[2] would give the address 65516 So 65516+1 would give the address 65518 Or (a[2]+1) will give the same result 65518 So to find value ?
  • 24. We know that a[i] is equivalent to *(a+i) So *(a[2]+1) is equivalent to *(*(a+2)+1) Or a[2][1] *(a[2]+1)
  • 25. Int a[]={10,20,30,40,50} for(i=0;i<=4;i++) printf(“%d”,*(a+i)); Find output
  • 26. Find output Int a[]={10,20,30,40,50}; Int I, j, m, K=1; i=++a[1]; j=a[1]++; m=a[i++]; Value of I, j, m ?
  • 27. Find output void main() { int const * p=5; printf(&quot;%d&quot;,++(*p)); }
  • 28. Answer: Compiler error : Cannot modify a constant value. Explanation : p is a pointer to a &quot;constant integer&quot;. But we tried to change the value of the &quot;constant integer&quot;.
  • 29. Find output main() { static int var = 5; printf(&quot;%d &quot;,var--); if(var) main(); }
  • 30. Answer: 5 4 3 2 1 Explanation: When static storage class is given, it is initialized once. The change in the value of a static variable is retained even between the function calls. Main is also treated like any other ordinary function, which can be called recursively.
  • 31. Find output main() { int c[ ]={2.8,3.4,4,6.7,5}; int j,*p=c,*q=c; for(j=0;j<5;j++) { printf(&quot; %d &quot;,*c); ++q; } For(j=0;j<5;j++){ printf(&quot; %d &quot;,*p); ++p; } }
  • 32. Answer: 2 2 2 2 2 2 3 4 6 5 Explanation: Initially pointer c is assigned to both p and q . In the first loop, since only q is incremented and not c , the value 2 will be printed 5 times. In second loop p itself is incremented. So the values 2 3 4 6 5 will be printed.
  • 33. Find output main() { char *p; printf(&quot;%d %d &quot;,sizeof(*p),sizeof(p)); }
  • 34. Answer: 1 2 Explanation: The sizeof() operator gives the number of bytes taken by its operand. P is a character pointer, which needs one byte for storing its value (a character). Hence sizeof(*p) gives a value of 1. Since it needs two bytes to store the address of the character pointer sizeof(p) gives 2.
  • 35. void f(int *,int); void main(){ int a[5],i,b=16; for(i=0;i<5;i++) a[i]=2*i; f(a,b); for(i=0;i<5;i++) printf(&quot;\n%d&quot;,a[i]); printf(&quot;\n%d&quot;,b); getch(); } void f(int *x,int y){ int i; for(i=0;i<5;i++) *(x+i)+=2; y+=2; }
  • 36. output 2 4 6 8 10 16
  • 37. Find output main() { int i=3; switch(i) { default:printf(&quot;zero&quot;); case 1: printf(&quot;one&quot;); break; case 2:printf(&quot;two&quot;); break; case 3: printf(&quot;three&quot;); break; } }
  • 38. Answer : three Explanation : The default case can be placed anywhere inside the loop. It is executed only when all other cases doesn't match.
  • 39. Find output main() { int i=-1,j=-1,k=0,l=2,m; m=i++&&j++&&k++||l++; printf(&quot;%d %d %d %d %d&quot;,i,j,k,l,m); }
  • 40. Answer: 0 0 1 3 1 Explanation : Logical operations always give a result of 1 or 0 . And also the logical AND (&&) operator has higher priority over the logical OR (||) operator. So the expression ‘ i++ && j++ && k++’ is executed first. The result of this expression is 0 (-1 && -1 && 0 =0). Now the expression is 0 || 2 which evaluates to 1 (because OR operator always gives 1 except for ‘0 || 0’ combination- for which it gives 0). So the value of m is 1. The values of other variables are also incremented by 1.
  • 41. Find output main() { int c=- -2; printf(&quot;c=%d&quot;,c); }
  • 42. Answer: c=2; Explanation: Here unary minus (or negation) operator is used twice. Same maths rules applies, ie. minus * minus= plus. Note: However you cannot give like --2. Because -- operator can only be applied to variables as a decrement operator (eg., i--). 2 is a constant and not a variable.
  • 43. Find output #define int char main() { int i=65; printf(&quot;sizeof(i)=%d&quot;,sizeof(i)); }
  • 44. Answer: sizeof(i)=1 Explanation: Since the #define replaces the string int by the macro char
  • 45. Find output main() { int i=10; i=!i>14; Printf (&quot;i=%d&quot;,i); }
  • 46. Find output Answer: i=0 Explanation: In the expression !i>14 , NOT (!) operator has more precedence than ‘ >’ symbol. ! is a unary logical operator. !i (!10) is 0 (not of true is false). 0>14 is false (zero).
  • 47. #include<stdio.h> main() { char s[]={'a','b','c','\n','c','\0'}; char *p,*str,*str1; p=&s[3]; str=p; str1=s; printf(&quot;%d&quot;,++*p + ++*str1-32); }
  • 48. Answer: 77 Explanation: p is pointing to character '\n'. str1 is pointing to character 'a' ++*p. &quot;p is pointing to '\n' and that is incremented by one.&quot; the ASCII value of '\n' is 10, which is then incremented to 11.The value of ++*p is 11. ++*str1, str1 is pointing to 'a' that is incremented by 1 and it becomes 'b'. ASCII value of 'b' is 98. Now performing (11 + 98 – 32), we get 77(&quot;M&quot;); So we get the output 77 :: &quot;M&quot; (Ascii is 77).
  • 49. Find output main() { int i=5; printf(&quot;%d%d%d%d%d&quot;,i++,i--,++i,--i,i); }
  • 50. Answer: 4 5 5 4 5 Explanation: The arguments in a function call are pushed into the stack from left to right. The evaluation is by popping out from the stack. and the evaluation is from right to left, hence the result.
  • 51. Find output #define square(x) x*x main() { int i; i = 64/square(4); printf(&quot;%d&quot;,i); }
  • 52. Answer: 64 Explanation: the macro call square(4) will substituted by 4*4 so the expression becomes I = 64/4*4 . Since / and * has equal priority the expression will be evaluated as (64/4)*4 i.e. 16*4 = 64
  • 53. Find output include <stdio.h> #define a 10 main() { #define a 50 printf(&quot;%d&quot;,a); }
  • 54. Answer: 50 Explanation: The preprocessor directives can be redefined anywhere in the program. So the most recently assigned value will be taken.
  • 55. Find output #define clrscr() 100 main() { clrscr(); printf(&quot;%d\n&quot;,clrscr()); }
  • 56. Answer: 100 Explanation: Preprocessor executes as a separate pass before the execution of the compiler. So textual replacement of clrscr() to 100 occurs
  • 57. Find output enum colors {black,red,green}; main() { printf(&quot;%d..%d..%d&quot;,black,red,green); return(1); }
  • 58. Answer: 0..1..2 Explanation: enum assigns numbers starting from 0, if not explicitly defined.
  • 59. Find output main() { int i=0; for(;i++;printf(&quot;%d&quot;,i)) ; printf(&quot;%d&quot;,i); }
  • 60. Answer: 1 Explanation: before entering into the for loop the checking condition is &quot;evaluated&quot;. Here it evaluates to 0 (false) and comes out of the loop,and i is incremented (note the semicolon after the for loop).
  • 61. Find output main() { show(); } void show() { printf(&quot;I'm the greatest&quot;); }
  • 62. Answer: Compier error: Type mismatch in redeclaration of show. Explanation: When the compiler sees the function show it doesn't know anything about it. So the default return type (ie, int) is assumed. But when compiler sees the actual definition of show mismatch occurs since it is declared as void. Hence the error. The solutions are as follows: 1. declare void show() in main() . 2. define show() before main(). 3. declare extern void show() before the use of show().
  • 63. What will be the o/p of next program.
  • 64. main( ) { static int a[ ] = {0,1,2,3,4}; int *p[ ] = {a,a+1,a+2,a+3,a+4}; int **ptr = p; ptr++; printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr); *ptr++; printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr); *++ptr; printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr); ++*ptr; printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr); }
  • 65. output 111 222 333 344