Define vs declare a variable, formal vs actual arguments, getchar vs scanf, break vs continue

by Madhulika Reddy on April 26, 2009


c interview questionsQuestion.61 (main function), Question.62 (define vs declare),Question.63 (formal vs actual arguments) test whether the candidate knows basics of C programming. Question.64 (getchar vs scanf) tests basics of io. Question.65 (break vs continue) tests whether candidate knows the subtle differences using ‘break’ and ‘continue’ in loops.

  1. What is the purpose of main() function?
  2. In C, program execution starts from the main() function. Every C program must contain a main() function. The main function may contain any number of statements. These statements are executed sequentially in the order which they are written.

    The main function can in-turn call other functions. When main calls a function,it passes the execution control to that function. The function returns control to main when a return statement is executed or when end of function is reached.

    In C, the function prototype of the ‘main’ is one of the following:
    int main(); //main with no arguments
    int main(int argc, char *argv[]); //main with arguments

    The parameters argc and argv respectively give the number and value of the program’s command-line arguments.

    Example:

    #include <stdio.h>/* program section begins here */
    int main() {// opening brace - program execution starts here
    printf("Welcome to the world of C");
    return 0;
    }// closing brace - program terminates here

    Download Code

    Output:
    Welcome to the world of C.
    Back to top

  3. What is the difference between declaring a variable and defining a variable?
  4. Declaration of a variable in C hints the compiler about the type and size of the variable in compile time. Similarly, declaration of a function hints about type and size of function parameters. No space is reserved in memory for any variable in case of declaration.

    Example: int a;
    Here variable ‘a’ is declared of data type ‘int’

    Defining a variable means declaring it and also allocating space to hold it.
    We can say “Definition = Declaration + Space reservation”.

    Example: int a = 10;
    Here variable “a” is described as an int to the compiler and memory is allocated to hold value 10.
    Back to top

  5. What are the differences between formal arguments and actual arguments?
  6. Argument:An argument is an expression which is passed to a function by its caller (or macro by its invoker) in order for the function(or macro) to perform its task. It is an expression in the comma-separated list bound by the parentheses in a function call expression.

    Actual arguments:
    The arguments that are passed in a function call are called actual arguments. These arguments are defined in the calling function.

    Formal arguments:
    The formal arguments are the parameters/arguments in a function declaration. The scope of formal arguments is local to the function definition in which they are used. Formal arguments belong to the called function. Formal arguments area copy of the actual arguments. A change in formal arguments would not be reflected in the actual arguments.

    Example:

    #include <stdio.h>
    void sum(int i, int j, int k);/* calling function */
    int main() {
    int a = 5;      // actual arguments
    sum(3, 2 * a, a);
    return 0;
    } /* called function */
    
    /* formal arguments*/
    void sum(int i, int j, int k) {
    int s;
    s = i + j + k;
    printf("sum is %d", s);
    }

    Download Code

    Here 3, 2*a, a are actual arguments and i,j,k are formal arguments. Read more about actual arguments vs formal arguments.
    Back to top

  7. What is the difference between getchar and scanf functions for reading strings?
  8. Differences between getchar and scanf functions for reading strings:

    scanf getchar
    1. Entering of each character should be followed by return key. 1. Need not type return key.
    2. Continuous stream of characters cannot be directly supplied using scanf function. 2. Continuous stream of characters can be directly supplied using getchar function
    3. Scanf function can be used to provide data at execution time irrespective of its type(int, char, float).
    Example:

    #include<stdio.h>
    int  main() {
    char a[10];
    printf("Enter a: \n");
    scanf("%s",a);
    return 0;
    }
    3. getchar() function is used only with character type.
    Example:

    #include<stdio.h>
    int main() {
    char a;
    printf("Enter any character: \n");
    a = getchar();
    printf("Character entered:%c \n",a);
    return 0;
    }
    4. scanf() returns the number of items read successfully. A return value 0 indicates that no fields were read. EOF(end of file) is returned in case of an error or if end-of-file/end-of-string character is encountered. 4. getchar() returns the character entered as the value of the function. It returns EOF in case of an error. It is recommeded to use getchar instead of scanf.

    Back to top

  9. What is the difference between ‘break’ and ‘continue’ statements?
  10. Differences between ‘break’ and ‘continue’ statements:

    break continue
    1. break is a keyword used to terminate the loop or exit from the block.The control jumps to next statement after the loop or block. 1. continue is a keyword used for skipping the current iteration and go to next iteration of the loop
    2.Syntax:

    {
    Statement 1;
    Statement 2;
    Statement n;
    break;
    }
    2.Syntax:

    {
    Statement 1;
    continue;
    Statement 2;
    }
    3. break can be used with for, while, do- while, and switch statements.When break is used in nested loops i.e. within the inner most loop then only the innermost loop is terminated. 3. This statement when occurs in a loop does not terminate it but skips the statements after this continue statement. The control goes to the next iteration. continue can be used with for, while and do-while.
    4. Example:

    i = 1, j = 0;
    while(i<=5){
    i=i+1;
    if(i== 2)
    break;
    j=j+1;
    }
    4. Example:

    i = 1, j = 0;
    while(i<=5){
    i=i+1;
    if(i== 2)
    continue;
    j=j+1;
    }

    Back to top


About the Author:  This post was written by Madhulika Reddy. You can connect with Madhulika on Orkut.


  • Anonymous

    How can I write a syntax checker that will read a text file of C codes and check the basic data type and basic i o function and keywords and report if there any error…?

  • Mike Prentice

    getchar and scanf table headers are reversed

  • http://www.interviewmantra.net Sridhar

    Hi Mike,
    Thanks for pointing out. I have edited the table headers getchar, scanf. Now they are right.

  • ankit

    thanks

  • Ganesh

    thanks for sharing this..

  • Haresh Ravaliya

    THANKS A LOT FOR PROVIDING THIS SITE…..
    I FOUND THE ANSWER OF THE QUE.
    THAT I COULD NOT FIND ON THE NET.
    QUE:-
    WRITE A PROG TO PRINT HELLO WITHOUT USING SEMICOLON IN CODE?
    THANKSSSSSSSSSSSSSSSSSSSSSSS
    THANKSSSSSSSSSSSSSSSSSSS
    FOR THE ANSWERRRRRRR
    HARESH

  • Sridhar Jammalamadaka

    @Haresh, That’s interviewmantra.net my dear friend!!! Here you will find things that are not available anywhere in the internet. Keep visiting this blog. Have a nice day!!!

  • http://google bala

    Thank you very much for sharing this C question and answer.it is very useful for read and understand basic things….. :) :)

  • Ric

    You missed a space between “are” and “a” in “Formal arguments area copy of the actual arguments.”

  • Priya Thomas

    This site is extremely useful…

    It covers most of the interview questions…

  • puja

    this is best

  • venkat

    good site

  • sandy

    on whole i can say dat this was an awesome job…..wish i cud have found this site much earlier bt i just found it the night before my final cse exam…..but it helped me a lot for my last minute revision…….thanxxxx a lot……hats off to this site…..:))

  • shruti

    Good compilation..
    One correction in your question bank (pdf format): Chap2,Qs9
    “struct sample s1″ ….. local variable of return3val(), scope of the variable ends at the end of function call, the structure should not be returned.

  • kaya

    very good and usefull site

  • mari

    i want program for ….
    get 3 strings and output will be like…
    if i get 3 strings are mango , orange , apple
    the output is …
    moaarpnapggloee….
    please give me that program i want this

  • Dinesh kumar

    printf this jalebi program

    8 9 10 11 12 13 14
    1 2 3 4 5 6 7
    15 16 17 18 19 20
    PLZ PRINT THIS

  • Mallikarjun480

    how to write this program
    6+28+726+…………………….
    hint(6=1+2+3)
     

  • Girishr4u

    #include
    #include

    void main()
    {
     if(puts(hello))
     {
     }
    }

  • Manish Prajapati

    syntax error will be detected by compiler ?

  • Karthik

    Excellent questions  Thank you…..

  • Avesh

    very easy yar…..loop laga de teen barr………….. 

  • Rohan

    what is code of following output:
    x x x
    x     x
    x x x

  • reza

    Write a
    program that show the output                                   1
    2 3 4 5

                                                                                                                                    6
    7 8 9

                                                                                                                                    11
    12 13

                                                                                                                                    16
    17

                                                                                                                                    21

     

Previous post:

Next post: