Sunday, 27 March 2016

Structure


Defination :-

                          Structure is a user defined data type. struct keyword is used to define a structure. Structure  is a collection of different data types elements under a single name and define a structure to hold this information.

                      structure and array are similar the only difference is that array is used to store collection of similar data types while structure can store collection of any type of data

Structure of structure name, variable can be declared as:-

 
struct structure_name
{
    char variable1[100];
    int variable2;
    float variable3;
};

Structure variable declaration


struct structure_name
{
    char variable1[100];
    int variable2;
    float variable3;
}v1 ,v2 ,v[20];

Accessing members:-

  1. Member operator(.)
  2. Structure pointer operator(->)

Program:-

 
#include <stdio.h>
#include <string.h>
struct student
{
        int id;
        char name[30];
        float percentage;
};

int main()
{
    struct student record1 = {1, "paidi", 90.5};
    printf("Records of STUDENT1: \n");
    printf("  Id is: %d \n", record1.id);
    printf("  Name is: %s \n", record1.name);
    printf("  Percentage is: %f \n\n", record1.percentage);
    return 0;
}
printf("struct size------->= %d\n",a);
}

typedef while using structure:


typedef struct structure_name
{
int a;
int b;
int c;
}cov;

by using this typedef we can give number of declare variable of students.

Structures within structures:-


Structures can be nested within other structures.

struct STR
{
int value;
float value1;
};

struct number
{
   struct STR nested1;
   int value2;
}v1,v2;


you want to access value for v2 structure variable then, structure member v1.nested1.value is used
 
#include <stdio.h>
#include <string.h>
struct student_college_detail
{
    int college_id;
    char college_name[50];
};
struct student_detail
{
   int id;
   char name[20];
   float percentage;
  // structure within structure
   struct student_college_detail clg_data;
}stu_data;
int main()
{
  struct student_detail stu_data = {1, "Ramana", 90.5, 71145,"Globalin institute"};
    printf(" Id is: %d \n", stu_data.id);
    printf(" Name is: %s \n", stu_data.name);
    printf(" Percentage is: %f \n\n", stu_data.percentage);
    printf(" College Id is: %d \n", stu_data.clg_data.college_id);
    printf(" College Name is: %s \n",stu_data.clg_data.college_name);
    return 0;
}

Structure Programs :-

 
1. Array using Struct :

#include <stdio.h>
#include <string.h>
struct student
{
     int id;
     char name[30];
     float percentage;
};
struct student record[2];
int main()
{
     int i;
     // 1st student's record
     record[0].id=1;
     strcpy(record[0].name, "Rajana");
     record[0].percentage = 86.5;
     // 2nd student's record        
     record[1].id=2;
     strcpy(record[1].name, "kumar");
     record[1].percentage = 90.5;
     // 3rd student's record
     record[2].id=3;
     strcpy(record[2].name, "vadar");
     record[2].percentage = 81.5;

     for(i=0; i<3; i++)
     {
         printf("Records of STUDENT : %d \n", i+1);
         printf(" Id is: %d \n", record[i].id);
         printf(" Name is: %s \n", record[i].name);
         printf(" Percentage is: %f\n\n",record[i].percentage);
     }
     return 0;
}
2. Function Using Structure:
 
#include <stdio.h>
#include <string.h>
struct student
{
   int id;
   char name[20];
   float percentage;
};
void func(struct student record);
int main()
{
  struct student record;
  record.id=1;
  strcpy(record.name, "Raju");
  record.percentage = 86.5;
  func(record);
  return 0;
}
void func(struct student record)
{
  printf(" Id is: %d \n", record.id);
  printf(" Name is: %s \n", record.name);
            printf(" Percentage is: %f \n", record.percentage);
}
3. Pointer Using Structure:
#include <stdio.h>
#include <string.h>
struct student
{
     int id;
     char name[30];
     float percentage;
};
struct student record1 = {1, "Raju", 90.5};
int main()
{
     int i;
     struct student *ptr;
     ptr = &record1;    
     printf("Records of STUDENT1: \n");
     printf("  Id is: %d \n", ptr->id);
     printf("  Name is: %s \n", ptr->name);
     printf("  Percentage is: %f \n\n", ptr->percentage);
     return 0;
}

4. pointer using Nested Structure:

#include <stdio.h>
#include <string.h>
struct student_college_detail
{
    int college_id;
    char college_name[50];
};
struct student_detail
{
    int id;
    char name[20];
    float percentage;
    // structure within structure
    struct student_college_detail clg_data;
}stu_data, *stu_data_ptr;
int main()
{
  struct student_detail stu_data = {1,"Rajana",90.5,71145,"Network    institute"};
  stu_data_ptr = &stu_data;
  printf(" Id is:%d\n",stu_data_ptr->id);
  printf(" Name is:%s\n",stu_data_ptr->name);
  printf(" Percentage is:%f\n\n",stu_data_ptr->percentage);
  printf("Collgid is:%d\n",stu_data_ptr->clg_data.college_id);
  printf("CollgNameis:%s\n",stu_data_ptr->clg_data.college_name);
}
 
5. example program of structure inside union:
 
#include <stdio.h>
#include <string.h>
struct student_college_detail
{
    int college_id;
    char college_name[50];
};
union student_detail
{
    int id;
    char name[40];
    int num;
    struct student_college_detail clg_data;   // structure within structure
}stu_data, *ptr;
int main()
{
    union student_detail stu_data;
    ptr = &stu_data;
    ptr->id = 2;
    printf(" Id is: %d \n", ptr->id);
    strcpy(ptr->name,"Rajana");
    printf(" Name is: %s \n",ptr->name);
    ptr->num = 9;
    printf(" num is: %d \n\n", ptr->num);
    printf(" Id is: %d \n", ptr->id);
    printf(" Name is: %s \n",ptr->name);
    printf(" num is: %d \n\n", ptr->num);
}
-----------------------------------------------------------------------------------------
More details about C interview questions and programs: http://learnclanguage4.blogspot.com/

Memmove & Memcopy & Memset programs

Memcpy():-

               memcpy() function copies n bytes from memory area src to memory area dest. The memory areas may not overlap
 
Syntax:

            void *memcopy(void * destination, void *source, size_t count);
 
#include<stdio.h>
main()
{
   char str1[100]=”these is string”;
   char str2[100];
   memcpy(str2,str1,14);
   printf(“%s”,str2);
}

Memmove():-

               memmove() function copies n bytes from memory area src to memory area dest. The memory areas may overlap.
 
Syntax:

            void *memmove(void *destination,void *source,size_t count);
 
#include<stdio.h>
main()
{
  char str[]=”programming”;
  memmove(str+1,str+3,4);   out put=gramramming.
}

Memset():-

               memset() function fills the first n bytes of the memory area pointed to by s with the constant byte c.
 
Syntax:
              
             void *memset(void destination, int set_value,size_t count);
 
#include<stdio.h>
main()
{
    char arr[]=”god gift program”;
    memset(arr+2,*,10);
    printf(“%s”,arr);           output-->=go**********gram.
}


Memcmp():-

              memcmp() function compares the first n bytes of the memory areas s1 and s2. It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match,...

Syntax:-

                 int memcmp(const void *s1, const void *s2, size_t n);
 
-----------------------------------------------------------------------------------------
More details about C interview questions and programs: http://learnclanguage4.blogspot.com/

String copy program

STRCPY():-

                       String copy function copies the string from source to destination. The strings may not overlap, and the destination string dest must be large enough to receive the copy. Beware of buffer overruns. string copy return a pointer to the destination string dest.
 

syntax:-

     char *strcpy(char *to, const char *from);

 

using strcpy :-

 

#include<stdio.h>

#include<string.h>

main()

{

  char a[]="Google";
  char b[15];
  int i=0;
  strcpy(b,a);
  for(i=0;b[i]!=0;i++)
   {
        printf("%c",b[i]);
   }
}
 
 
With out using Strcpy:-
 
#include<stdio.h>
#include<string.h>
int main()
{
    char str1[12]="Google";
    char str2[20];
    int i=0;
    while(str1[i]!='\0')
      {
        str2[i] = str1[i];
        i++;
      }
    str2[i]='\0';
    printf("copied string=\t%s\n",str2);
}
Out Put:-
 
      copied String = google
 

-----------------------------------------------------------------------------------------

More details about C interview questions and programs: http://learnclanguage4.blogspot.com/

Saturday, 26 March 2016

Command Line Arguments Program


Command line arguments are two types

1.   Argc
2.   Argv[]

you can pass these two arguments that are expected by the program. Argc parameter is the count of total command line arguments passed to executable on execution. Integer data type has to use for first parameter and for second parameter use character array. 

int main(int argc, char *argv[])
 
Argv parameter is the array of character string of each command line argument passed to executable on execution

If you are new to C programming, you should first understand how array works.


#include<stdio.h>
{
int main(int argc, int *argv[])
 int i;
 printf(“no arguments %d”,argc);
 for(i=0;i<argc;i++)
 {
    printf(“%s”,argv[i]);
 }
}
 
at the time of execution we have to give the input for command line arguments
 
$ ./a.out input1 input2

 

Out Put :

 
The argument is input1 input2
 
-----------------------------------------------------------------------------------------
More details about C interview questions and programs: http://learnclanguage4.blogspot.com/

Structure

Defination :-                           Structure is a user defined data type. struct keyword is used to define a structure. Structur...