Embedded C Interview

 

Embedded C interview questions

Learn with me series:

Here are some C questions which I formulated for better understanding

  • #include <stdio.h>
    int main()
    {
    int a,b,c;
    float d,e,f;
    a=1;
    b=1234567;
    c=1234;d=14.1;
    e=0.122345;
    f=12345.12345;printf(“|%9d|t|%8d|t|%11d|n|%f|t|%3.5f|t|%10.10f|n”,a,b,c,d,e,f);
    getch();
    }
    What is val of f?
    Ans

  • What is the output?
    #include<stdio.h>
    int main()
    {
    int var=EOF;
    printf("%d",var);
    getch();
    }

    Ans:-1 is defined value of EOF.=>
    #include <stdio.h>
    int main()
    {
    long cn=1;
    char ch;
    while((ch=getchar())!=EOF)
    {
    printf("%ldt%cn",cn++,ch);
    }
    }
    Q1 what happen if I enter word “secret”?
    ans:nothing getchar waits for enter as it has internal buffer,anything will happen after I hit enter,if you expected counter to show immediately as you enter character you should have used getch as it does not have buffer like getchar().Refer http://geeksquiz.com/difference-getchar-getch-getc-getche/Q2 what happens if I hit enter?
    ans:
    1    s
    2    e
    3    c
    4    r
    5    e
    6    t
    7Q3 why this 7 instead of 6?check my question in stack overflow.
    http://stackoverflow.com/questions/33081791/extremly-simple-c-program-why-i-am-getting-output-like-this
  • What is output of this program
    #include<stdio.h>
    int main()
    {
    char ch='c';
    if(ch='e')
    printf("truen");
    else
    printf("flasen");
    getch();
    }
    Ans:
    trueas expression in if has = (assignment operator) & not == (is eqaul to operator)
    so extression turns out to be true.
  • Output of program ?

    #include<stdio.h>
    int main()
    {
    char ch=’n’;
    printf(“%dn”,ch);
    getch();
    }Ans:
    ASCII value of new line charater is 10.
  • This is working program to count newlines tab spaces and p.#include<stdio.h>int main() {
    int c, nl, nt, ns,np;
    nl = 0;
    nt = 0;
    ns = 0;
    np = 0;
    while ((c = getchar()) != EOF)
    {
    if (c == 10)
    ++nl;
    if (c == ‘t’)
    ++nt;
    if (c == ‘ ‘)
    ++ns;
    if (c == ‘p’)
    ++np;
    }
    printf(“Blanks: %dnTabs: %dnNewlines: %dnNoOfP=%d “, ns, nt, nl,np);
    getch();
    }What do you expect once it runs?

Ans:
A blank screen,you need to type a word or may be a paraghaph,
if in windows hit ctrl+z (EOF) and Enter to display counts.

Now some questions on C preprocessor directives.

  • #include <stdio.h>
    int main(int argc,void* argv[] ){

    if(argv[1]==10)
    {
    #define hi 100
    };

    if(argv[1]==20)
    {
    #define bye 200
    };

    printf(“%d”,hi);
    printf(“%d”,bye);

    return 0;

    }

    =>if I run
    ./a.out 10

    result?

Ans:

100 200

Reason they are preprocessor directives ,runs well before running and compiling.

 

  • #include <stdio.h>
    #include <stdlib.h>int main(int argc,void* argv[] )

    {
    int x;
    if(atoi(argv[1])==10)
    {
    x=10;
    };
    if(atoi(argv[1])==20)
    {
    x=20;
    };
    #define hi x

    #if (hi == 20)
    printf(“\n%d\n”,hi);
    #endif

    return 0;

    }

If I run

./a.out 20 , result?

Ans:

No output ,reason there is no value of argv[1] at compile time.


Like what you read?
Do you think you have knowledge to fetch Overseas Jobs and Projects?
Register here and start posting...
The following two tabs change content below.

Embedded Design Engineer by profession .Extremly intrested in Emedded Systems,Linux and Electronics.I truely believe in open source , join me in this mission by registering .

How to Integrate Google “No CAPTCHA reCAPTCHA” on Your Website


2 Comment

  1. Anonymous says:

    test comment

  2. Anonymous says:

    test comment

Leave a Reply