Best Industrial Training in C,C++,PHP,Dot Net,Java in Jalandhar

Wednesday, 28 January 2015

MCQ on C

What is the output of this C code?
  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int i = 0;
  5.         int x = i++, y = ++i;
  6.         printf("%d % d\n", x, y);
  7.         return 0;
  8.     }
a) 0, 2
b) 0, 1
c) 1, 2
d) Undefined

What is the output of this C code?
  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int x = 97;
  5.         int y = sizeof(x++);
  6.         printf("X is %d", x);
  7.     }
a) X is 97
b) X is 98
c) X is 99
d) Run time error

What is the output of this C code?
  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int x = 4, y, z;
  5.         y = --x;
  6.         z = x--;
  7.         printf("%d%d%d", x,  y, z);
  8.     }
a) 3 2 3
b) 2 3 3
c) 3 2 2
d) 2 3 4

What is the output of this C code?
  1.  #include <stdio.h>
  2.     void main()
  3.     {
  4.         int a = 5, b = -7, c = 0, d;
  5.         d = ++a && ++b || ++c;
  6.         printf("\n%d%d%d%d", a,  b, c, d);
  7.     }
a) 6 -6 0 0
b) 6 -5 0 1

c) -6 -6 0 1
d) 6 -6 0 1


. What is the output of this C code?
  1.  #include <stdio.h>
  2.     void main()
  3.     {
  4.         int a = -5;
  5.         int k = (a++, ++a);
  6.         printf("%d\n", k);
  7.     }
a) -4
b) -5
c) 4
d) -3

 What is the difference between the following 2 codes?
  1.     #include <stdio.h> //Program 1
  2.     int main()
  3.     {
  4.         int d, a = 1, b = 2;
  5.         d =  a++ + ++b;
  6.         printf("%d %d %d", d, a, b);
  7.     }
  1.     #include <stdio.h> //Program 2
  2.     int main()
  3.     {
  4.         int d, a = 1, b = 2;
  5.         d =  a++ +++b;
  6.         printf("%d %d %d", d, a, b);
  7.     }
a) No difference as space doesn’t make any difference, values of a, b, d are same in both the case
b) Space does make a difference, values of a, b, d are different
c) Program 1 has syntax error, program 2 is not
d) Program 2 has syntax error, program 1 is not



What is the output of this C code?
  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int a = 1, b = 1, c;
  5.         c = a++ + b;
  6.         printf("%d, %d", a, b);
  7.     }
a) a = 1, b = 1
b) a = 2, b = 1
c) a = 1, b = 2
d) a = 2, b = 2

Tuesday, 27 January 2015

Formatted I/O functions in C

printf() function:

  • printf() function is used to print the “character, string, float, integer, octal and hexadecimal values” onto the output screen.
  • We use printf() function with %d format specifier to display the value of an integer variable.
  • Similarly %c is used to display character, %f for float variable, %s for string variable, %lf for double and %x for hexadecimal variable.
  • To generate a newline,we use “\n” in C printf() statement.

Example program for C printf() function:

#include <stdio.h>
int main()
{
char ch = ‘A’;
char str[20] = “fresh2refresh.com”;
float flt = 10.234;
int no = 150;
double dbl = 20.123456;
printf(“Character is %c \n”, ch);
printf(“String is %s \n” , str);
printf(“Float value is %f \n”, flt);
printf(“Integer value is %d\n” , no);
printf(“Double value is %lf \n”, dbl);
printf(“Octal value is %o \n”, no);
printf(“Hexadecimal value is %x \n”, no);
return 0;
}                                                                                                                                                                                                   .
Output:
Character is A
String is fresh2refresh.com
Float value is 10.234000
Integer value is 150
Double value is 20.123456
Octal value is 226
Hexadecimal value is 96                                                                                                                                                                   .
You can see the output with the same data which are placed within the double quotes of printf statement in the program except
  • %d got replaced by value of an integer variable  (no),
  • %c got replaced by value of a character variable  (ch),
  • %f got replaced by value of a float variable  (flt),
  • %lf got replaced by value of a double variable  (dbl),
  • %s got replaced by value of a string variable  (str),
  • %o got replaced by a octal value corresponding to integer variable  (no),
  • %x got replaced by a hexadecimal value corresponding to integer variable
  • \n got replaced by a newline.


C – printf and scanf


  • printf() and scanf() functions are inbuilt library functions in C which are available in C library by default. These functions are declared and related macros are defined in “stdio.h” which is a header file.
  • We have to include “stdio.h” file as shown in below C program to make use of these printf() and scanf() library functions.

1. C printf() function:

  • printf() function is used to print the “character, string, float, integer, octal and hexadecimal values” onto the output screen.
  • We use printf() function with %d format specifier to display the value of an integer variable.
  • Similarly %c is used to display character, %f for float variable, %s for string variable, %lf for double and %x for hexadecimal variable.
  • To generate a newline,we use “\n” in C printf() statement.
Note:
  • C language is case sensitive. For example, printf() and scanf() are different from Printf() and Scanf(). All characters in printf() and scanf() functions must be in lower case.

Example program for C printf() function:

#include <stdio.h>
int main()
{
char ch = ‘A’;
char str[20] = “fresh2refresh.com”;
float flt = 10.234;
int no = 150;
double dbl = 20.123456;
printf(“Character is %c \n”, ch);
printf(“String is %s \n” , str);
printf(“Float value is %f \n”, flt);
printf(“Integer value is %d\n” , no);
printf(“Double value is %lf \n”, dbl);
printf(“Octal value is %o \n”, no);
printf(“Hexadecimal value is %x \n”, no);
return 0;
}                                                                                                                                                                                                   .
Output:
Character is A
String is fresh2refresh.com
Float value is 10.234000
Integer value is 150
Double value is 20.123456
Octal value is 226
Hexadecimal value is 96                                                                                                                                                                   .
You can see the output with the same data which are placed within the double quotes of printf statement in the program except
  • %d got replaced by value of an integer variable  (no),
  • %c got replaced by value of a character variable  (ch),
  • %f got replaced by value of a float variable  (flt),
  • %lf got replaced by value of a double variable  (dbl),
  • %s got replaced by value of a string variable  (str),
  • %o got replaced by a octal value corresponding to integer variable  (no),
  • %x got replaced by a hexadecimal value corresponding to integer variable
  • \n got replaced by a newline.

2. C scanf() function:

  • scanf() function is used to read character, string, numeric data from keyboard
  • Consider below example program where user enters a character. This value is assigned to the variable “ch” and then displayed.
  • Then, user enters a string and this value is assigned to the variable ”str” and then displayed.

Example program for printf() and scanf() functions in C:

#include <stdio.h>
int main()
{
char ch;
char str[100];
printf(“Enter any character \n”);
scanf(“%c”, &ch);
printf(“Entered character is %c \n”, ch);
printf(“Enter any string ( upto 100 character ) \n”);
scanf(“%s”, &str);
printf(“Entered string is %s \n”, str);
}                                                                                                                                                                                                    .
Output:
Enter any character
a
Entered character is a
Enter any string ( upto 100 character )
hai
Entered string is hai                                                                                                                                                                        .
  • The format specifier %d is used in scanf() statement. So that, the value entered is received as an integer and %s for string.
  • Ampersand is used before variable name “ch” in scanf() statement as &ch.
  • It is just like in a pointer which is used to point to the variable.

Variations in Output for integer an floats

Integer and floating-points can be displayed in different formats in C programming as:
#include<stdio.h>
int main(){
    printf("Case 1:%6d\n",9876);      
/*  Prints the number right justified within 6 columns  */
    printf("Case 2:%3d\n",9876);      
/* Prints the number to be right justified to 3 columns but, there are 4 digits so number is not right justified  */
    printf("Case 3:%.2f\n",987.6543);
/* Prints the number rounded to two decimal places */
    printf("Case 4:%.f\n",987.6543);
/* Prints the number rounded to 0 decimal place, i.e, rounded to integer */
    printf("Case 5:%e\n",987.6543);
/* Prints the number in exponential notation(scientific notation) */
    return 0;
}
Output
Case 1:  9876
Case 2:9876
Case 3:987.65
Case 4:988
Case 5:9.876543e+002


Unformatted Input/Output Functions with Example: C Language

There are several standard library functions available in this category - those that can deal with a single character and those that can deal with a string of characters. The various unformatted input/output functions in C are shown below:



getchar( ) and putchar( )

Even though getchar( ) and putchar( ) looks like functions, they are not. They are the macros that are used to read and display a character. The syntax to read a character shown below:

“ch = getchar( )” will reads a character from the keyboard and copy it into memory area which is identified by the variable ch. No arguments are required for this macro. Once the character is entered from the keyboard, the user has to press Enter key.

“putchar(ch)” outputs a character stored in a variable on the monitor. The variable should be passed as parameter as shown in the above syntax.

Example1
main()
{
 char ch;
 clrscr();
 ch = getchar();
 putchar(ch);
 getch();
}

getch(), getche() and putch()

The functions getch() and getche() are used to read a character from the keyboard, similar to getchar(). Both functions don’t need a return key pressed to terminate the reading of a character. A character entered will itself terminates reading. 
In case of getch(), the character entered is not displayed or echoed on the screen, whereas in getche(), the character entered is echoed or displayed on the screen. Syntax for both the macros
ch = getch();            /*Typed character will not be displayed on the screen*/
ch = getche();            /*Typed character will be displayed on the screen*/

Now, we have to display the inputted character on the screen, putch() macro is introduced.
“putch(ch)” This function outputs a character stored in the memory, on the monitor. The variable should be passed as parameter to the functions. In the above syntax ‘ch’ is used as an argument and its value is displayed on the screen.
Example2:
main()
{
  char ch;
  clrscr();
  ch = getche();
  putch(ch);
  getch();
}

gets() and puts()

These functions are used to read a set of characters (string) from the keyboard and display a set of characters (string) on the screen.
char str[5];
gets(str);
        /* Reads a set of characters into memory area str */
puts(str);         /* Displays a set of characters from memory area str */
Example3
main()
{
  char str[10];
  clrscr();
  gets(str);
  puts(str);
  getch();
}