Monday, 14 November 2016

Input Output Question Practice C Language

Q 1. Print a character with ASCII code using printf function?
Q 2. Try to string input with space using %s?
Q 3. Write down short notes with example on following function
Fclose
Puts
Gets
Fprintf
Scanf

Q 4. Write a program to input more than one character using single scanf function?
Q 5. What are the special character used inside of printf function?
Q 6.  Find the output of following code
Code 1 :
void main()
{
     char ch=65;
     printf("\n\t%c",ch);
}
Code 2:
void main()
{
    int a=1,b=2,c=3;
    printf("\n\t%d %d %d");
   
}
Output : 3 2 1
Because compiler of C maintain global area of variable stack that is why output in decending order
Code 3:
void main()
{
    int a;
    printf("\n%d",scanf(“%d”,&a));
   
}
Output : 1
Because return type of scanf function is one if takes takes place successfully otherwise zero. So when enter number and press enter then input gets complete and scanf returns 1 which is here use for printf function.

Code 4:
void main()
{
    char *str="Girfa:Student Help";
    printf("%s",str+6);
}
Output :  Student Help
Str is a character array pointer. Name of an array is constant pointer which store first element (base) address. When add 6 by base address then its skip 6 characters.

Code 5:

void main()
{
    printf("%d",printf("Girfa:Student Help")); 
}

Output : Girfa:Student  Help 18

Inner printf genrate its output before outer printf which is Girfa:Student Help, Then outer printf print number of character in innner printf which is 18.

Code 6:

void main()
{
    printf("\n\t%c",65);
}

Output : A

%c is design to print a character using ASCII code, 65 is ASCII of A

Code 7:

void main()
{
    char ch=97;
    printf("\n\t%d is ASCII code of %d",ch,ch);
}

Output : 97 is ASCII code of a

Every character of keyboard has a unique code which is known as ASCII code So a ASCII code is 97. %d is use to print ASCII code and %c is use to print related character of 97.

Code 8:

void main()
{
    double a;
    printf("\n\t %d",&a);
}

Output : Garbase

& is known as address operator in C language which returns physcial address of a variable. Normally address value is greater than 32767 and %d is design to print upto 32767.So overflow occur here.

Code 9:

void main()
{
    int a;
    float b;
    char c;
    printf("Enter Integer, Float and character input>> ");
    scanf("%d%f%c",&a,&b,&c);
    printf("\n\tInt=%d\tFloat=%f\tChar=%c",a,b,c);
}

Output : You will not be able to take input in character variable.

You can take more than input though single scanf function , here %c is use in last.when this program will run you’ll press enter after every input. Enter is also a character which ASCII is 13 so variable c will assign enter and you will see anything in character varaible while printing.

Code 10:

void main()
{
    printf("\n\t%d",getch());
}

Output : Print ASCII of pressed character by keyboard
Getch function wait a any key press from keyboard and returns ASCII of pressed character which is print by using %d




No comments:

Post a Comment