C Notes
C Notes
h>
int main()
{
 
    enum Peoplevar1 {Alex=0,    Tracy,  Kristian} Girls;
    enum Peoplevar2 {William=0, Martin }  Boys;
    switch (Boys)
    {
        case William:
        puts("William");
        break;
 
        case Martin:
        puts("Martin");
        break;
 
        default:
        break;
    }
    return 0;
}
 
           
 
Martin
 
Compiler error
           
 
Runtime error
           
 
William
 
 
 
#include<stdio.h>
union u
{
   int var;
   unsigned char str[4];
};
int main()
{
   unsigned char tmp;
   union u uObj;uObj.var = 3;
   tmp = uObj.str[1];
   uObj.str[1] = uObj.str[0];
   uObj.str[0] = tmp;
   printf("%d", uObj.var);
   return 0;
}
 
           
 
3
           
 
768
           
 
Runtime crash
           
 
876
 
 
 
 
 
 
An entire structure variable can be assigned to another structure variable if __________
           
 
the two variables have the same composition
           
 
the two variables are of same type
           
 
assignment of one structure variable to another is not possible
           
 
None of these
 
 
 
 
 
 
 
What will be the output of the following program?
 
#include<stdio.h>
#define size 3
enum{a,b,c,d,e};
int main()
{
     const cValue[2];
     long lValue[size];
     signed sValue[e];
     printf("%d",sizeof cValue+sizeof lValue+sizeof sValue);
     return 0;
}
 
           
 
Compiler error
           
 
45
           
 
36
           
 
20
 
 
 
What will be the output of the following program?
 
#include<stdio.h>
enum myEnum { AB, BC };
int main()
{
      enum myEnum a =10;
      printf("%d", a);
      return 0;
}
 
           
 
Compiler error
           
 
1
           
 
10
           
 
0
 
 
 
 
 
#include<stdio.h>
int main( )
{
   struct a
   {
       category : 5 ;
       scheme : 4 ;
   } ;
   printf ( "size = %d", sizeof ( struct a ) ) ;
   return 0;
}
 
           
 
size = 0
           
 
size = 1
           
 
size = 4
           
 
Compiler error
 
 
 
#include <stdio.h>
int main()
{
 
    enum Peoplevar1 {Alex=0,    Tracy,  Kristian} Girls;
    enum Peoplevar2 {William=0, Martin }  Boys;
    switch (Boys)
    {
        case William:
        puts("William");
        break;
 
        case Martin:
        puts("Martin");
        break;
 
        default:
        break;
    }
    return 0;
}
 
           
 
Martin
           
 
Compiler error
           
 
Runtime error
           
 
William
 
 
 
           
 
What will be the output of the following program?
 
#include<stdio.h>
union u
{
   int var;
   unsigned char str[4];
};
int main()
{
   unsigned char tmp;
   union u uObj;uObj.var = 3;
   tmp = uObj.str[1];
   uObj.str[1] = uObj.str[0];
   uObj.str[0] = tmp;
   printf("%d", uObj.var);
   return 0;
}
 
 
 
           
 
3
           
 
768
           
 
Runtime crash
           
 
876
 
 
An entire structure variable can be assigned to another structure variable if __________
           
 
the two variables have the same composition
           
 
the two variables are of same type
           
 
assignment of one structure variable to another is not possible
           
 
None of these
 
 
 
 
Consider following code snippet:
 
#include<stdio.h>
int main( )
{
    int i = 5 , j = 2 ;
    fun ( &i, &j ) ;
    printf ( "\n%d %d", i, j ) ;
    return 0;
}
 
Which of the following option is correct about function fun( ) so that value of i and j becomes 25 & 4?
           
 
void fun ( int i , int j )
{
    i = i * i ;
    j = j * j ;
}
 
           
 
void fun ( int &i , int &j )
{
    i = i * i ;
    j = j * j ;
}
 
           
 
void fun ( int *i , int *j )
{
    *i = *i * *i ;
    *j = *j * *j ;
}
 
           
 
void fun ( int *i , int *j )
{
    i = i * i ;
    j = j * j ;
}
 
 
 
 
 
What will be the output of the following program?
 
#include<stdio.h>
int function(int i)
{   
    if(i<=1)
    return 1;
    if(i%2==0)
          return function(i/2);
    else
         return function((i-1)/2);
}
int main()
{
   extern int  function(int);
   int i;
   i = function(5);
   printf("%d",i);
   return 0;
}
 
           
 
Compiler error
           
 
2
           
 
0
           
 
1
 
 
 In this value of i is geven has 5 and v do the operaion i%2 value is one so else art s executed as (i-1)/2..
value s 2………………
 
 
What will the following program print?
 
#include<stdio.h>
 
int main(void)
{
    unsigned int c;
    unsigned x=0x3;
    scanf("%u",&c);
    switch(c&x)
    {
         case 3: printf("Hello!\t");
         case 2: printf("Welcome\t");
         case 1: printf("To All\t");
         default:printf("\n");
    }
    return 0;
}
           
 
It will Print Hello
           
 
Runtime error
           
 
Compiler error
           
 
No out put
It do not print any values…….
 
 
 
What will be the output of the following program?
 
void main( )
{
    int i = 138, a = 138, k ;
    k = fun ( !++i, !++a ) ;
    printf ( "%d %d %d", i, a, k ) ;
}
 
fun( int j, int b )
{
    int c ;
    c = j + b ;
    return ( c ) ;
}
 
           
 
138 138 276
           
 
138 138 0
           
 
139 139 276
           
 
139 139 0
 
 
What will be the output of the following program?
 
#include<stdio.h>
int main()
{
int iaddr;
for(iaddr=0;iaddr<3;iaddr++)
{
    int iaddr=100;
    iaddr--;
    printf("%d..",iaddr);
}
return 0;
}
 
           
 
99..99..99..
           
 
0..1..2.
           
 
100..100..100..
           
 
99..98..97.
 It print 100 100 100 because iaddr value is over ridding with 100
 
What will be the output of the following program?
 
#include<stdio.h>
int main()
{
      char a[5][5],flag;
      a[0][0]='A';
      flag=((a==*a)&&(*a==a[0]));
      printf("%d\n",flag);
      return 0;   
}
           
 
Runtime error
           
 
Compiler error
           
 
1
           
 
0
 
 
 
What will be the output of the following program?
 
#include<stdio.h>
#define a 5
void foo();
int main()
{
     printf("%d..",a); 
     foo(); 
     printf("%d",a);
     return 0;
}
void foo()
{ 
      #undef a
      #define a 50
}
 
           
 
50..50
           
 
50..5
           
 
5..5
           
 
Compiler error
 
 
 
What will be the output of the following program?
 
volatile int i;
main()
{       
    i = 8<10&1&&10>8;
    i<<2;
    printf("%d",i);
}
 
           
 
1
           
 
0
           
 
4
           
 
None of these
 
 
 
 
What would be the output of the following program?
 
main()
{       
    int i = 10;
    goto label2;
    while(i)
    {
        switch(i-1)
        {
             case 1:
                 label2:
            printf("%d", i);
        }
    }
    printf("%s", "hello");
    getch();
}
 
           
 
Infinite loop
           
 
10 hello
           
 
Compiler error
           
 
hello
 
 
 the out put of above program is 10 hello
 
#include<stdio.h>
int main()
{
  const char *callCausal();
  *callCausal()='A';
  return 0;
}
 
const char *callCausal()
{
  return "Causal Call";
}
 
           
 
No errors
           
 
Syntax error
           
 
callCausal() returns a constant string pointer which cannot be modified
           
 
None of these
 
 
 
What will be the output of the following program?
 
#include<stdio.h>
char* func()
{
    char str[10] = "Take Care";
    return str;
}
int main()
{
    char* str1 = func();
    printf("%s", str1);
    return 0;
}
 
           
 
Take Care
           
 
Compiler error
           
 
Undefined value
           
 
None of these
 
 
What will be the output of the following program?
 
 
#include<stdio.h>
int main()
{
   int var1,var2,var3,minmax;
   var1=5;
   var2=5;
   var3=6;
   minmax=(var1>var2)?(var1>var3)?var1:var3:(var2>var1);
   printf("%d\n",minmax);
   return 0;
}
 
           
 
0
           
 
5
           
 
6
           
 
None of these
 
 
 
Consider following code snippet:
 
#include<stdio.h>
int main( )
{
FILE *fp, *fs, *ft ;
fp = fopen ( "A.C", "r" ) ;
fs = fopen ( "B.C", "r" ) ;
ft = fopen ( "C.C", "r" ) ;
fclose ( fp, fs, ft ) ;
return 0;
}
 
Which one of the following files gets closed?
           
 
Only A.C
           
 
Only A.C and B.C
           
 
Error: “Undeclared identifier fclose( ) function”
           
 
All the three files
 
 
 
What will be the output of the following program?
 
#include <stdio.h>
 
char *strrev(char *s,int n)
{
       int i=0;
       while (i<n/2)
       {
               *(s+n) = *(s+i);       //uses the null character as the temporary storage.
               *(s+i) = *(s + n - i -1);
               *(s+n-i-1) = *(s+n);
               i++;
       }
       *(s+n) = '\0';
 
       return s;
}
 
int main()
{
       char *str = malloc(10);
 
       bzero(str, 10);
       sprintf(str, "abcde");
       printf("%s\n", strrev(str, 5));
       return 0;
}
 
           
 
Compiler error
           
 
Runtime error
           
 
Compile and print some Garbage value
           
 
None of these
 
 
 
What will be the output of the following program?
 
Assume that the array begins at address 65486, and sizeof int is 2.
 
#include<stdio.h>
int main()
{
  int a[3][4] = {1,2,3,4,
                 4,3,2,1,
                 7,8,9,0
                 };
  printf("%u %u", a+1, &a+1);
  return 0;
}
 
           
 
65480 65484
           
 
65480 65482
           
 
65480 65496
           
 
65480 65488
 
 
 
#include <stdio.h>
int main()
{
  FILE* fp = NULL;
  unsigned char c;
  fp = fopen("MyFile.txt","r");
  while((c = getc(fp)) != EOF)
    putchar(c);
  fclose(fp);
  return 0;
}
 
           
 
Stack overflow
           
 
Runtime error
           
 
abcdefghij followed by infinite loop
           
 
Infinite loop
 
 
 
What will be the output of the following program ?
 
 
#include<stdio.h>
void func(char*str)
{
  int i=0;
  *str=i++;
}
 
int main()
{
  int i=0;
  char str[5];
  for(i=0;i<5;i++)
    func(str+i);
  for(i=0;i<5;i++)
    printf("%c",str[i]);
  return 0;  
}
 
           
 
Runtime error
           
 
Empty array string
           
 
Compiler error
           
 
01234
 
 
 
What will be the output of the following program?
 
#include<stdio.h>
int main()
{
int ch;
ch = 65;  
printf("The character that has numeric value of 65 is:\n");
putc(ch, stdout);
return 0;
}
 
           
 
The character that has numeric value of 65 is:
A
 
           
 
The character that has numeric value of 65 is:
a
 
           
 
The character that has numeric value of 65 is:
B
 
           
 
None of these
 
 
 
 
 
 
 
#include<stdio.h>
int main()
{
  int d;
  double *s=0;
  d=(int)(s+1);
  printf("%d",d);
  return 0;
}
 
           
 
8
           
 
4
           
 
Runtime error
           
 
Compiler error
 
 
 
 
int feof(FILE *stream)
int ferror(FILE *stream);
void clearerr(FILE *stream);
int fileno(FILE *stream);
 
The above functions are used for __________.
 
           
 
low level I/O
           
 
creating temporary file
           
 
stream status enquiries
           
 
reading and writing files
 
 
 
Point out the error if any, in the following program?
 
01   void main(void)
02    {
03      int j,i;
04      int *a[2];
05      int arr[2] = {1,2};
06      int srr[2] = {3,4};
07      a[0] = arr;
08      a[1] = srr;
09      clrscr();
10      for(i=0;i<2;i++){
11        for(j = 0;j<2;j++){
12          printf("%d",a[i]+j);
13        }
14      }
15    getch();
16    }
 
 
           
 
Prints 4 different addresses
           
 
Compiles without any error and the output is 1 2 3 4
           
 
Error in line 07 & 08
           
 
Error in line 13
 
 
 
 
01. int main()
02. {
03. FILE* fp = NULL;
04. long size = 0;
05. fp = fopen("MYFile.txt","b");
06. fseek( fp, 4 , SEEK_SET);
07. size = fwrite("TakeCare",1,3,fp);  
08. return 0;
09. }
 
           
 
AbcdTakeCare
           
 
Empty file
           
 
4 Take
           
 
Runtime error at line #4
 
 
 
 
 
Consider the following code snippet:
 
void main( )
{
    int i, j ;
    int ( *p )[3] ;
    p = ( int ( * )[3] ) malloc ( 3 * sizeof ( *p ) ) ;
}
 
How would you access the elements of the array using p?
 
 
           
 
for ( i = 0 ; i < 3 ; i++ )
{
    for ( j = 0 ; j < 3 ; j++ )
        printf ( "%d", p[ i + j ] ) ;
}
 
           
 
for ( j = 0 ; j < 3 ; j++ )
    printf ( "%d", p[ i ][ j ] ) ;
 
           
 
for ( i = 0 ; i < 3 ; i++ )
    printf ( "%d", p[ i ] ) ;
 
           
 
for ( i = 0 ; i < 3 ; i++ )
{
    for ( j = 0 ; j < 3 ; j++ )
        printf ( "%d", p[ i ][ j ] ) ;
}
 
 
 
 
 
uestion Number 12
 
What will be the output of the following program?
 
#include <stdio.h>
 
#define BUFSIZE 1024
 
void usage(char *cmd)
{
  printf("Usage: %s <file to read with complete path>\n", cmd);
  printf("If your file is at /home/Cprogramming/documents/names.txt,
   then the command will be: \n");
  printf("%s /home/ Cprogramming /documents/names.txt\n", cmd);
}
 
int main(int argc, char *argv[])
{
  FILE *fp;
 
  if (argc < 2)
  {
      usage(argv[0]);
      exit(0);
  }
 
  fp = fopen(argv[1], "r");
  if (fp == NULL)
  {
     usage(argv[0]);
     exit(0);
  }
 
  printf("Contents of %s are: \n", argv[1]);
 
  while (!feof(fp))
  {
     char buf[BUFSIZE] = {0};
 
     fread(buf, sizeof(char), BUFSIZE, fp);
     printf("%s", buf);
  }
 
  printf("End of the file\n");
 
  fclose(fp);
  return 0;
}
 
           
 
Compiler error
           
 
Runtime error
           
 
No output
           
 
None of these
 
 
 
 
#include<stdio.h>
int main( )
{
  static char s[25] = "The cocaine man";
  int i = 0 ;
  char ch ;
  ch = s[++i] ;
  printf ( "%c", ch ) ;
  ch = s[i++] ;
  printf ( "%c", ch ) ;
  ch = i++[s] ;
  printf ( "%c", ch ) ;
  ch = ++i[s] ;
  printf ( "%c", ch ) ;
  return 0;
}
 
hhec
           
 
he c
           
 
The c
           
 
hhe!
 
 
 
 
Select the most appropriate functionality of the following declaration in handling errors in C.
 
int feof(FILE *stream);
           
 
Returns a non-zero value if the error indicator is set for the stream
           
 
Returns a non-zero value if the end of file indicator is set for the stream
           
 
Returns all the error messages if the file pointer reaches the end of file
           
 
Returns a zero value if the error indicator is set for the stream
 
 
 
 
Which one of the following statements is correct about the following program?
 
#include<stdio.h>
int main( )
{
     int *c ;
     c = check ( 10, 20 ) ;
     printf ( "c = %u", c ) ;
     return 0;
}
check ( int i , int j )
{
     int *p, *q ;
     p = &i ;
     q = &j ;
     if ( i >= 45 )
         return ( p ) ;
     else
         return ( q ) ;
}
 
 
           
 
Error: ‘Expression syntax in function check( )’
           
 
Error: ‘Non portable pointer conversion’
           
 
Error: ‘Lvalue required’
           
 
No error
 
 
 
 
Which one of the following is the correct option that substitutes // deallocate memory in the following
program?
 
 
#include<alloc.h>
int main( )
{
  struct ex
  {
    int i ;
    float j ;
    char *s ;
  } ;
 
  struct ex *p ;
  p = ( struct ex * )
  malloc ( sizeof ( struct ex ) ) ;
  p -> s = ( char * ) malloc ( 20 ) ;
  // deallocate memory
  return 0;
}
 
 
           
 
free ( p -> s ) ;
           
 
free ( s, p ) ;
           
 
free ( p ) ;
           
 
free ( p -> s ) ; free ( p ) ;
 
 
 
#include<stdio.h>
int main()
{        
unsigned int i = -1;
printf("%s","Welcome");
if(i>-5)
main();
return 0;
}
 
Infinite loop
           
 
Stack overflow
           
 
Calling main inside main is illegal
           
 
Welcome
 
 
 
 
 
#include<stdio.h>
int main()
{
  int  a[5] = {1,2,3,4,5};
  int *ptr = (int*)(&a+1);
  printf("%d %d",*(a+1),*(ptr-1));
  return 0;
}
 
 
21
           
 
22
           
 
25
           
 
None of these
 
 
 
 
 
What will be the output of the following program?
 
#include<stdio.h>
int main()
{
    FILE * fp = NULL;
    char str[100]="abcdefghij";
    fp = fopen("MyFile.txt","w");
    while(!feof(fp))
    {
       fscanf(fp,"%s",str);   
       fprintf (fp, "[%-10.10s]\n",str);
    }
    fclose(fp);
    return 0;
}
 
           
 
MyFile.txt will contain abcdefghij
 
           
 
Infinite loop
           
 
Compiler error
           
 
Runtime error
 
 
 
 
#include<stdio.h>
int bags[5]={20,5,20,3,20};
int main()
{
    int pos=5,*next();
    *next()=pos;
    printf("%d %d %d", pos, *next(), bags[0]);
}
int *next()
{
     int i;
     for(i=0;i<5;i++)
     if (bags[i]==20)
     return(bags+i);
     printf("Error!");
     exit(0);
     return 0;
}
 
           
 
10 20 10
           
 
5 20 5
           
 
10 Garbage value
           
 
20 Garbage value
 
 
 
#include<stdio.h>
int callPoint(void);
void print(int,int(*)());
int i = 10;
int main()
{
  int i=20;
  print(i, callPoint);
}
void print(int i,int (*callPointOne)())
{
  printf("%d\n",(* callPointOne)());
}
int callPoint(void)
{
  return(i-=5);
}
 
           
 
10
           
 
Runtime error
           
 
5
           
 
Compiler error
 
 
 
A. const int *ptr
;
B. int *const ptr
;
C. const * int ptr;