C Questions Answers-C Structure(PartII)


C Structure(PartII)

1. What would be printed from the following C program?

#include<stdio.h>
#include<conio.h>
void printId(int);
void printName(char*);

 typedef struct//define structure student
     {
       //variables
        int id;
        char *name;

      }student;

   int main()
   {

       student st; //structure object
       st.id = 100; //assign 100 to student id
       st.name = "Sok";//assign Sok to student name
       //output student information
       printId(st.id);
       printName(st.name);

       getch();
       return 0;

    }

   void printId(int id)
   {
     printf("%d\n",id);
    }
   void printName(char *name)
   {
      printf("%s\n",name);
    }

 

 
Check answer 2. What would be printed from the following C program?
#include<stdio.h>
#include<conio.h>

 typedef struct
   {
     int x;
     int y;
    }Point;

void makeTriangle(Point p[]);
    int main()
    {

      Point p[3];
      int i;
      makeTriangle(p);
      printf("The triangle consists of the following coordinates:\n");
      for(i=0 ;i<3;i++)
           printf("(%d,%d)\n",p[i].x,p[i].y);     
     
      getch();
      return 0;
    }

   void makeTriangle(Point p[]){
      p[0].x = 200;
      p[0].y = 20;
      p[1].x = 150;
      p[1].y = 100;
      p[2].x = 300;
      p[2].y = 100;

      }



   Check answer 3. What would be printed from the following C program?
#include<stdio.h>
#include<conio.h>


typedef struct
     {
        int nomi;
        int deno;
     
      }Fract;

Fract sum(Fract,Fract);

   int main()
    {


     Fract f1={1, 2};/* 1/2 */
     Fract f2 ={2, 5};/* 2/5 */
     Fract result = sum(f1, f2);//sum the fractions
     printf("Result=%d/%d",result.nomi,result.deno);  //display the result


     getch();
     return 0;

    }

   Fract sum(Fract f1, Fract f2)
    {
     Fract result={(f1.nomi * f2.deno) + (f2.nomi * f1.deno), f1.deno * f2.deno};
     return result;

     }



  Check answer


Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.