C Programming Interview / Viva Q&A: 2

This C program has a integer variable and 2 printf functions. Check the source code below and guess the output.

Whats the output of this C Program?

 
#include < stdio.h >

int main()
{
    int a;

    a = printf("IBM");

    printf("\n%d", a);

    return 0;
}

Output 1:
IBM
3

Output 2:
Microsoft
9

Output 3:
Apple
5

Output 4:
Oracle
6

C Programming Interview / Viva Q&A: 2 (printf)


[youtube https://www.youtube.com/watch?v=e9gZ9Vk8lf4]

YouTube Link: https://www.youtube.com/watch?v=e9gZ9Vk8lf4 [Watch the Video In Full Screen.]


Important: printf() function returns the number of characters printed by it.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Using Scanf in C Program

In our previous video tutorial you learnt about Integers, Float and Character data types and their format specifier. In today’s video lets see an example of using all 3 data types along with strings.

You’ll also learn to get user input from console using scanf function/method present in stdio.h header file.

Example: int, float, char

 
#include< stdio.h >

int main()
{
    int a;
    float b;
    char ltr;

    printf("Enter 2 numbers and a single character\n");
    scanf("%d %f %c", &a, &b, <r);

    printf("\nYou entered %d, %f and %c", a, b, ltr);

    return 0;
}

Output:
Enter 2 numbers and a single character
1 2 i

You entered 1, 2.000000 and i

Example for string type

 
#include < stdio.h >

int main()
{
    char c[10];

    printf("Enter a company name\n");
    scanf("%s", c);

    printf("\nYou entered %s", c);

    return 0;
}

Output:
Enter a company name
Microsoft
You entered Microsoft

Using Scanf() To Read Int, Float, Char and String Data Type: C Programming


[youtube https://www.youtube.com/watch?v=myqWGZePUgg]

YouTube Link: https://www.youtube.com/watch?v=myqWGZePUgg [Watch the Video In Full Screen.]


Keyword for Integer is int and its format specifier is %d.
Keyword for Float is float and its format specifier is %f.
Keyword for Character is char and its format specifier is %c.

Note 1: If you input decimal value for a integer variable, it only stores integer part of the value and discards the decimal value.

Note 2: printf and scanf are kind of opposite. Because printf converts all the numbers, characters etc and displays everything in text format on to the console. While, scanf takes all the text entered by the user in the console and converts them into respective data type based on the format specifier present in scanf statement.

Structure of a basic C Program

Lets write our first C program – the typical “Hello World!” program.

In this video tutorial lets learn the structure of a basic C program:
1. Preprocessors – include directive.
2. Main method/function.
3. printf method/function.
4. Semicolon syntax.
5. Indentation for readability of code.

Source Code: A Simple C Program

#include<stdio.h>

int main()
{
    printf("Microsoft\n Apple\n Oracle\n Google\n Yahoo\n");
    return 0;
}

Output:
Microsoft
Apple
Oracle
Google
Yahoo

Structure of a basic C Program


[youtube https://www.youtube.com/watch?v=3Yu6u3tmBS4]

YouTube Link: https://www.youtube.com/watch?v=3Yu6u3tmBS4 [Watch the Video In Full Screen.]


Preprocessor statement

The “include” directive which we see in the first line is a preprocessor directive. Here we are including a standard library file which has some set of useful functions in it, which we are using in our “Hello World” C Program.

stdio stands for “Standard Input Output”. As the name suggests this library file has functions to read and write data to console window, along with other many useful functions. For example, printf() is a function present in stdio.h library file. We simply use it in our program to print data on to the console window. We need not know the implementation details of printf method/function.

main() method

Main method or function is part of all ‘C’ programs. It’s entry point of any C program execution. Function is a way of grouping some code together. We can write any logic/statements inside a function.

It’s a standard that main always returns a integer value. Thus main is preceded by a keyword called int, which means integer. It’s a keyword or reserve word. We’ll know more about keywords(or reserve words) in a separate video tutorial. Since main method needs to return a integer value, we explicitly return 0 at the end.

Readability of code

It’s very important that we write code which is readable. It helps in maintaining the code. Large programs get too clumsy very easily and reading and understanding code often becomes difficult. So we need to indent the code and make sure its readable as far as possible.

Note:
Main method/function doesn’t take any argument.