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

Sunday, 22 March 2015

Login Program in C?

#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
   char userid[]="admin",password[]="123",p[15],u[15];
   int n=1,a,b;
   printf("\nEnter USER ID and PASSWORD below (You have only three chances to enter)");
   getch();
   while(n<=3)
   {
      clrscr();
      printf("\nUSER ID: ");
      scanf("%s",u);
      printf("\nPASSWORD: ");
      scanf("%s",p);
      a=strcmp(u,userid);
      b=strcmp(p,password);
      if(a==0&&b==0)
      {
         printf("\nYou have logged in successfully.");
         break;
      }
      else
      {
         printf("\nWrong PASSWORD and/or USER ID. Now you have % d more chance/s.",3-n);
      }
      getch();
      n++;
   }
   if(n==4)
      printf("\nYou can't log in.");
   getch();
}

Wednesday, 25 February 2015

Call by Value and Call by Reference

Passing Argument to Function :

  1. In C Programming we have different ways of parameter passing schemes such as Call by Value and Call by Reference.
  2. Function is good programming style in which we can write reusable code that can be called whenever require.
  3. Whenever we call a function then sequence of executable statements gets executed. We can pass some of the information to the function for processing calledargument.

Two Ways of Passing Argument to Function in C Language :

  1. Call by Reference
  2. Call by Value
Let us discuss different ways one by one –

A.Call by Value :

#include<stdio.h>

void interchange(int number1,int number2)
{
    int temp;
    temp = number1;
    number1 = number2;
    number2 = temp;
}

int main() {

    int num1=50,num2=70;
    interchange(num1,num2);

    printf("\nNumber 1 : %d",num1);
    printf("\nNumber 2 : %d",num2);

    return(0);
}

Output :

Number 1 : 50
Number 2 : 70

Explanation : Call by Value

  1. While Passing Parameters using call by value , xerox copy of original parameter is created and passed to the called function.
  2. Any update made inside method will not affect the original value of variable in calling function.
  3. In the above example num1 and num2 are the original values and xerox copy of these values is passed to the function and these values are copied into number1,number2 variable of sum function respectively.
  4. As their scope is limited to only function so they cannot alter the values inside main function.
Call by Value in C Programming Scheme

B.Call by Reference/Pointer/Address :

#include<stdio.h>

void interchange(int *num1,int *num2)
{
    int temp;
    temp  = *num1;
    *num1 = *num2;
    *num2 = temp;
}

int main() {

    int num1=50,num2=70;
    interchange(&num1,&num2);

    printf("\nNumber 1 : %d",num1);
    printf("\nNumber 2 : %d",num2);

    return(0);
}

Output :

Number 1 : 70
Number 2 : 50

Explanation : Call by Address

  1. While passing parameter using call by address scheme , we are passing the actual address of the variable to the called function.
  2. Any updates made inside the called function will modify the original copy since we are directly modifying the content of the exact memory location.
Call by Pointer or Address or Reference in C Programming Scheme

Summary of Call By Value and Call By Reference :

PointCall by ValueCall by Reference
CopyDuplicate Copy of Original Parameter is PassedActual Copy of Original Parameter is Passed
ModificationNo effect on Original Parameter after modifying parameter in functionOriginal Parameter gets affected if value of parameter changed inside function

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();
}

Wednesday, 16 April 2014

Command line arguments in C:

Command line arguments in C:

      It is possible to pass some values from the command line to your C programs when they are executed. These values are called command line arguments and many times they are important for your program specially when you want to control your program from outside instead of hard coding those values inside the code.
The command line arguments are handled using main() function arguments where argc refers to the number of arguments passed, and argv[] is a pointer array which points to each argument passed to the program. 
argc      - Number of arguments in the command line including program name
argv[]   – This is carrying all the arguments
  • In real time application, it will happen to pass arguments to the main program itself.  These arguments are passed to the main () function while executing binary file from command line.
  • For example, when we compile a program (test.c), we get executable file in the name “test”.
  • Now, we run the executable “test” along with 4 arguments in command line like below.
this is a program
Where,
argc             =       5
argv[0]         =       “test”
argv[1]         =       “this”
argv[2]         =       “is”
argv[3]         =       “a”
argv[4]         =       “program”
argv[5]         =       NULL

Example program for argc() and argv() functions in C:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])   //  command line arguments
{
if(argc!=5) 
{
   printf("Arguments passed through command line " \
          "not equal to 5");
   return 1;
}

   printf("\n Program name  : %s \n", argv[0]);
   printf("1st arg  : %s \n", argv[1]);
   printf("2nd arg  : %s \n", argv[2]);
   printf("3rd arg  : %s \n", argv[3]);
   printf("4th arg  : %s \n", argv[4]);
   printf("5th arg  : %s \n", argv[5]);

return 0;
}

 Output:

Program name : test
1st arg : this
2nd arg : is
3rd arg : a
4th arg : program
5th arg : (null)
IN OTHER WORDS:
C provides a fairly simple mechanism for retrieving command line parameters entered by the user. It passes an argv parameter to the main function in the program. argv structures appear in a fair number of the more advanced library calls, so understanding them is useful to any C programmer.
Enter the following code and compile it:
#include <stdio.h>

int main(int argc, char *argv[])
{
    int x;

    printf("%d\n",argc);
    for (x=0; x<argc; x++)
        printf("%s\n",argv[x]);
    return 0;
}
In this code, the main program accepts two parameters, argv and argc. The argv parameter is an array of pointers to string that contains the parameters entered when the program was invoked at the UNIX command line. The argc integer contains a count of the number of parameters. This particular piece of code types out the command line parameters. To try this, compile the code to an executable file named aaa and type aaa xxx yyy zzz. The code will print the command line parameters xxx, yyy and zzz, one per line.
The char *argv[] line is an array of pointers to string. In other words, each element of the array is a pointer, and each pointer points to a string (technically, to the first character of the string). Thus, argv[0] points to a string that contains the first parameter on the command line (the program's name), argv[1] points to the next parameter, and so on. The argc variable tells you how many of the pointers in the array are valid. You will find that the preceding code does nothing more than print each of the valid strings pointed to by argv.
Because argv exists, you can let your program react to command line parameters entered by the user fairly easily. For example, you might have your program detect the word help as the first parameter following the program name, and dump a help file to stdout. File names can also be passed in and used in your fopen statements.

Thursday, 3 April 2014

Last Year C Questions??

1. // program to count characters,spaces,tabs,new lines in a file //
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
int c;
int b=0,t=0,nl=0;
clrscr();
fp=fopen("good.txt","w");
printf("enter the string:");
while((c=getchar())!=EOF)
{
if(c==' ')
++b;
if(c=='\t')
++t;
if(c=='\n')
++nl;
}
fprintf(fp,"input has %d blanks,%d tabs and %d newlines\n",b,t,nl);
fclose(fp);
getch();

}





2. what do you mean by structure? How they are declared and initialized? How they are different from arrays?

STRUCTURES—these are the collection of heterogeneous elements in simple words we can say that is a collection of different data types. Each element in structure is called member. If you want to access structure members in C language ,structure variable should be declared. Many structure variable can be declared for same structure and memory will be allocated for each separately.

STRUCTURE DECLARATION—
A structure declaration specifies the grouping of variables of different types in a single unit. It simply acts as a temple or blueprint that may be used to create structure variables.
The syntax for declaring a structure in C is:

Struct struct_name
{
data_type member1;
data_type member2;
……………………
data_type membern;
};

STRUCTURE INITIALIZATION—
Lets us consider an example of employee structure which is declared as follows’
Struct employee
{
int emp_no;
char emp_name[20];
int dapt_code;
double salary;
}
we can define and initialize structure variable emp1 of type employee as follows,
employee emp1={101,”raghav”,05,23453.50};

PROGRAM TO ILLUSTRATE—
#
#
struct employee
{
int empid;
char name[50];
char add[50];
float salary;
char email[50];
}s1;
void main()
{
clrscr();
scanf(“%d”,&s1.empid);
scanf(“%s”,&s1.name);
scanf(“%s”,&s1.add);
scanf(“%f”,&s1.salary);
scanf(“%s”,&s1.email);
printf(“%d”,s1.empid);
printf(“%s”,s1.name);
printf(“%s”,s1.add);
printf(“%f”,s1.salary);
printf(“%s”,s1.email);
getch();
}

DIFFERENCE BETWEEN STRUCTURES AND ARRAYS

STRUCTURES                                                      ARRAYS
1.they are a group of heterogeneous               1.they are defined as a finite set
elements.                                                             of homogeneous elements.
 2.syntax                                                                                                         
 struct struct_name                                                2.syntax:
{                                                                             storage_class datatype arrayname[size];
datatype  variable name;
----------------------------
----------------------------
};
3.initialization:                                                      3.initialization:
employee emp1={101,”raghav”,05,23453.50};    float digit[3]={0.25,0,0.50};