Nothing Special   »   [go: up one dir, main page]

C Output

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

1. What will be the output of following program?

Input:
13
#include<stdio.h>
int main()
{
int a, b;
if(scanf("%d%d", &a, &b)==2)
printf("true");
else
printf("false");
return 0;
}

OUTPUT:-True
Explanation:
# Scanf function returns integer value (i.e no. of variables scanned)as its return type is integer.
# In the above question, we have scanned two integer values, so it will return 2. => “if condition” becomes true

2. What will be the output of following program?


Input: 1 c

#include<stdio.h>
int main()
{
    int flag = 0, flag1 = 0, n;
    char s;
    flag = scanf("%d", &n);
    flag1 = scanf("%d", &s);
    printf("Value of flag is %d", flag);
    printf("\nValue of flag is %d", flag1);
    return 0;
}

Output:
Value of flag is 1
Value of flag is 0

Explanation: 
#In the above question Scanf function will return integer value.
#In the above question, if it get executed successfully it will return 1 otherwise return 0
# Therefore flag=scanf(“%d”, &n) will return 1 because it scans an integer value successfully
# flag1=scanf(“%d”, &s) will return 0 because it fail to scan character value.
# And if you will not insert any value then it will return -1.
3.) What will be the output of the following code?
Input:
12 10

#include <stdio.h>
int main(void)
{
    int y, z;
    int x = scanf("%d %d", &y, &z);
    printf("%d", x);
    return 0;
}

Output:- 2
Explanation:- scanf() returns the number of variables it successfully stored.

Question 4
#include<stdio.h>
int main()
{
    int i;
    for (i = 0; i<5; i++)
    {
        int i;
        i = 10;
        printf("%d ", i) ;
    }
    return 0;
}
Output:
10 10 10 10 10
Explanation:
Here, it would seem that, as ‘i’ got assigned a value 10, so the loop will run only one time because the loop cond
But the output is quite different than what we expected. The reason behind this is the scope of variable is within t
As soon as the loop exits , i is counted according to i of for loop which makes the condition true upto 5 times (0-
Here, the loop runs according to the ‘i’ declared outside the loop, and hence it will run five times and the ‘i’ insid

Question 4
#include<stdio.h>
int main()
{
    int i;
    for (i = 0; i<5; i++)
    {
        
        i = 10;
        printf("%d ", i) ;
    }
    return 0;
}
Output:
10
Explanation:
Compare with previous

Question 5
#include <stdio.h>
#include <string.h>
  
int main()
{
    printf("GEEKS size = %ld \n\n", sizeof("GEEKS"));
    printf("GEEKS length = %ld \n", strlen("GEEKS"));
    return 0;
}
Output:
GEEKS size = 6
GEEKS length = 5

Explanation:
#sizeof() function returns the size of the string including null character while
# strlen() function returns length of the string excluding null character.

Q6.
#include<stdio.h>
void main()
{
     int i = 10;
     static int x = i;
     if (x == i)
         printf("equal");
     else if (x < i)))
         printf("less than");
     else
         printf("greater than");
}

Output :- Compiler Error


Explanation :-  Here ‘x’ is a static variable and ‘i’ is an auto variable.
Auto variables are run time entities, compared to static, which are load time entities.
Run time variables cannot be initialized with load time variables.

Q7.
#include<stdio.h>
void main()
{
    printf("%s", "i" "am" "good");
}
Options:
(a)i am good
(b)i
(c)good
(d)iamgood
(e)Compiler error

Output :- iamgood
Explanation: In C, string constant “ab” is same as “a” “b”

Q8.
#include<stdio.h>
void main()
{
    if (printf("cisgood"))
        printf("i know c");
    else
        printf("i know c++");
}

Output :- c is good I know c


Explanation :- printf returns an intege

Q9.
#include<stdio.h>
#include <string.h>
void main()
{
    printf("%d ,%d", sizeof("program"), strlen("program"));
}

Output:- 8,7
Explanation:#sizeof() function returns the size of the string including null character while
# strlen() function returns length of the string excluding null character.
Q10.
#include<stdio.h>
void main()
{
    int colour = 2;
    switch (colour) {
    case 0:
        printf("Black");
    case 1:
        printf("Red");
    case 2:
        printf("Aqua");
    case 3:
        printf("Green");
    default:
        printf("Other");
    }
}

Output :- AquaGreenOther
Explanation:-  There are no break statements in switch case, so all statements after case 2, including the defaul

Q11.
#include <stdio.h>
int main()
{
     int a = 5;
     int b = 10;
     {
         int a = 2;
         a++;
         b++;
     }
     printf("%d , %d", a, b);
     return 0;
}

Output :- 5,11
Explanation :- Default storage class of local variable is auto. Scope and visibility of auto variable is within the bl
In C, if there are two variables of the same name, then we can access only local variable.
Hence inside the inner block variable a is local variable which has declared and defined inside th
When control comes out of the inner block local variable a became dead.

Q12.
#include <stdio.h>
int main()
{
    char str[10] = "Hello";
    printf("%d, %d\n", strlen(str), sizeof(str));
    return 0;
}

Output :- 5,10
Explanation :- sizeof(str) = total memory occupied by str = sizeof(char[10]) = 1*10 = 10
strlen(str) = returns length of the string excluding null character

Q13.
#include <stdio.h>
int main()
{
    if (0)
        ;
    printf("Hello");
    printf("Hi");
    return 0;
}

Output :- HelloHi
Explanation :- There is a semicolon after the if statement, so this statement will be considered as separate stat
and here printf(“Hello”); will not be associated with the if statement…….so Both printf statemen

Q13.
#include <stdio.h>
int main()
{
    float f = 3.4e39;
    printf("%f", f);
    return 0;
}

Output :- inf
Explanation :- If you will assign value beyond the range of float data type to the float variable it will not show a
It will store infinity.

Q13.
#include <stdio.h>
int main()
{
    register int i, x;
    scanf("%d", &i);
    x = ++i + ++i + ++i;
    printf("%d", x);
    return 0;
}

Output :- Compiler Error


Explanation :- In C, register variable stores in CPU it doesn’t store in RAM.
So register variable do nor have any memory address. So it is illegal to write &i.

Q13.
int main()
{
    int array[3] = { 5 };
    int i;
    for (i = 0; i <= 2; i++)
        printf("%d ", array[i]);
    return 0;
}

Output :- 5,0,0
Explanation :- Storage class of an array which initializes the element of the array at the time of declaration is st
Default initial value of static integer is zero……1st element is 5….rest 0

Q13.
#include <stdio.h>
int main()
{
    int a = 5;
    float b;
    printf("%d ", sizeof(++a + b));
    printf("%d ", a);
    return 0;
}

Output :- 4
Explanation :- sizeof(++5 + garbage_floating_pt_value ) = sizeof( 6 + garbage_floating_pt_value ) = sizeof ( garb

Q14.
#include <stdio.h>
int main()
{
    int a = 1, b = 2, c = 3;
    c = a == b;
    printf("%d", c);
    return 0;
}

Output :- 0
Explanation :- c = a==b  c = (a==b)  c = (false)  c = 0
“==” is relational operator which returns only two values, either 0 (if false) or 1(if true).

Q14.
#include <stdio.h>
int main()
{
     int a = 20;
     ;
     ;
     printf("%d", a);
     ;
     return 0;
}
Output :- 20
Explanation :-  ; (statement terminator) and no expression/statement is available here, so this is a null stateme

Q14.
#include <stdio.h>
void main()
{
    int a = 1, b = 2, c = 3;
    char d = 0;
    if (a, b, c, d)
    {
        printf("EXAM");
    }
}
Output :- Nothing printed
Explanation :-  if ( a,b,c,d) = if (1,2,3,0) = if(leftmost) = if (0) = if (false) = so no input

Q14.
#include <stdio.h>
int main()
{
    char* str = "Mustafa";
    printf("%c\n", *&*str);
    return 0;
}
Output :- M
Explanation :-   & is a reference operator, * is de-reference operator. We can use these operators any number o
str points the first character of IncludeHelp, *str points “I”, & again reference value of str.
*str again deferences the value of str

Q14.
#include <stdio.h>
int main()
{
     int iVal;
     char cVal;
     void* ptr;
     iVal = 50;
     cVal = 65;
  
     ptr = &iVal;
     printf("value =%d, size= %d\n", *(int*)ptr, sizeof(ptr));
  
     ptr = &cVal;
     printf("value =%d, size= %d\n", *(char*)ptr, sizeof(ptr));
     return 0;
}
Output :- value =50, size= 4
value =65, size= 4
Explanation :-   void pointer can be type casted to any type of data type, and pointer takes 4 bytes (On 32 bit co
To print value using void pointer, you will have to write like this *(data_type*)void_ptr;.

Q14.
#include <stdio.h>
int main()
{
    char ch = 10;
    void* ptr = &ch;
    printf("%d, %d", *(char*)ptr, ++(*(char*)ptr));
    return 0;
}
Output :- 11,11
Explanation :-   (printf evaluates right to left) and (printf prints left to right)

Q14.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
    char x = 'A';
    char* y = (char*)malloc(sizeof(char));
    y = &x;
    for (int i = 0; i < 26; i++)
{
        printf("%c", x);
        y[0] += 1;
    }
}
Output :- ABCDEFGHIJKLMNOPQRSTUVWXYZ
Explanation :-
#
Initial:
00001010 (Binary form of 'A')

In loop
00001010 + 1 = 00001011 = B
00001011 + 1 = 00001100 = C
00001100 + 1 = 00001101 = D

and goes on up till Z.

C | Operators | Question 11
#include <stdio.h> 
int main() 

  int a = 10, b = 20, c = 30; 
  if (c > b > a) 
    printf("TRUE"); 
  else
    printf("FALSE"); 
  return 0; 
}
Answer: False

Explanation: Let us consider the condition inside the if statement. Since there are two greater than (>) ope
associativity of > is considered. Associativity of > is left to right. So, expression c > b > a is evaluated as ( (c
so if(c>b>a)=>if(0)
C | Functions | Question 3

Which of the following is true about return type of functions in C?


(A) Functions can return any type
(B) Functions can return any type except array and functions
(C) Functions can return any type except array, functions and union
(D) Functions can return any type except array, functions, function pointer and union

Answer: (B)
Explanation: In C, functions can return any type except arrays and functions.
We can get around this limitation by returning pointer to array or pointer to function.

C | Functions | Question 4
#include <stdio.h>
int main()
{
   printf("%d", main);  
   return 0;
}

(A) Address of main function


(B) Compiler Error
(C) Runtime Error
(D) Some random value

Answer: (A)
Explanation:  Name of the function is actually a pointer variable to the function and prints the address of the function.

C | Functions | Question 5
Predict output
#include <stdio.h>
int fun(int n)
{
for(;n > 0; n--)
printf("Ali ");
return 0;
}

int main()
{
int (*ptr)(int ) = fun;
(*ptr)(3);
return 0;
}

OUTPUT:-Ali Ali Ali

C | Functions | Question 7
Predict the output?

#include <stdio.h>
int main()
{
     void demo();
     void (*fun)();
     fun = demo;
     (*fun)();
*fun();
     fun();
     return 0;
}
void demo()
{
     printf("GeeksQuiz ");
}
(A) GeeksQuiz
(B) GeeksQuiz GeeksQuiz GeeksQuiz
(C) Compiler Error
(D) Blank Screen

Answer: (B)

Explanation: This is a simple program with function pointers.


fun is assigned to point to demo.
So the three statements “(*fun)();” and “*fun();” and “fun();” mean the same thing.

C | Functions | Question 8
What is the meaning of using extern before function declaration?
For example following function sum is made extern

extern int sum(int x, int y, int z)


{
return (x + y + z);
}

(A) Function is made globally available


(B) extern means nothing, sum() is same without extern keyword.
(C) Function need not to be declared before its use
(D) Function is made local to the file.

Answer: (B)
Explanation: extern keyword is used for global variables.
Functions are global anyways, so adding extern doesn’t add anything.

C | Functions | Question 10
In C, what is the meaning of following function prototype with empty parameter list
void fun()
{
   /* .... */
}

(A) Function can only be called without any parameter


(B) Function can be called with any number of parameters of any types
(C) Function can be called with any number of integer parameters.
(D) Function can be called with one integer parameter.

Answer: (B)
Explanation: Empty list in C mean that the parameter list is not specified and function can be called with any pa
In C, to declare a function that can only be called without any parameter, we should use “void fun
In C++, empty list means function can only be called without any parameter. In C++, both void fun

ILL 1.
#include <stdio.h>

void demo()
{ OUTPUT:- Ali Ali Ali Ali Ali Ali
printf("Ali ");
}
int main()
{
demo(1,2);
demo(2,3,4,5,6);
demo('a','b');
demo(1.0f,2.0f);
demo(1.2,1.3);
demo();
}

ILL 2.
#include <stdio.h>
void demo(void)
{
printf("Ali ");
}
int main()
{
demo(1,2);//ERROR:- Too many arguments to function demo()
}

Implicit return type int in C


Predict the output of following C program.

#include <stdio.h>
fun(int x)
{
     return x*x;
}
int main(void)
{
     printf("%d", fun(10));
     return 0;
}
Output: 100
EXPLANATION:-The important thing to note is, there is no return type for fun(), the program still compiles and r
In C, if we do not specify a return type, compiler assumes an implicit return type as int.

C | Storage Classes and Type Qualifiers | Question 7

#include<stdio.h>
int main()
{
    typedef int i;
    i a = 0;
    printf("%d", a);
    return 0;
}

Output :- 0
Explanation: There is no problem with the program. It simply creates a user defined type i and creates a v

You might also like