Array I
Array I
Ques 1. what is the output for the below code?
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *arr[4]={"hello", "good", "bad", "yes" };
char *chptr;
chptr=arr;
chptr++;
printf("\n chptr = %s", *chptr);
return 0;
}
options:
A. good
B. hello
C. bad
D. run time error
Ques 2. what is the output for the below code?
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *arr[4]={"hello", "good", "bad", "yes" };
char **chptr;
chptr=arr;
*chptr++;
printf("\n chptr = %s", chptr[1]);
return 0;
}
options:
A. hello
B. bad
C. good
D. run time error
Ques 3. what is the output for the below code?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int iarr[5] = {2,3,4,5,6};
printf("\n chptr = %d %d", *(iarr+1) , *(*(&iarr)+1));
return 0;
}
options:
A. 3 2
B. 2 2
C. 3 3
D. compile time error
Ques 4. what is the output for the below code?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int inum[5] = {2,3,4,5,6};
printf("\n chptr = %d %d", sizeof(inum), sizeof(inum[0]));
return 0;
}
options:
A. 4 4
B. 20 4
C. 20 20
D. compile time error
Ques 5. what is the output for the below code?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int s[5] = {32,23,43,52,16};
int *p=(&s+1);
p-- ;
printf("\n chptr = %d ", *p);
return 0;
}
options:
A. 16
B. 32
C. 23
D. compile time error
Ques 6. what is the output for the below code?
#include <stdio.h>
#include <stdlib.h>
void fun(int ar[])
{
printf("%d\n", ar[4]);
}
int main()
{
int ar[5] = {32,23,43,52,16};
fun(ar);
return 0;
}
options:
A. 16
B. 32
C. 43
D. compile time error
Ques 7. what is the output for the below code?
#include <stdio.h>
#include <stdlib.h>
void fun(int ar[])
{
printf("%d\n", ar[2]);
}
int main()
{
int ar[5] = {32,23,43,52,16};
fun(ar[2]);
return 0;
}
options:
A. 43
B. 16
C. 52
D. run time error
Ques 8. what is the output for the below program?
#include <stdio.h>
#include <stdlib.h>
void fun(int arr[])
{
printf("%d\n", arr[3]);
}
int main()
{
int numarr[5] = {2,3,4,15,23,4,6};
fun(&numarr[1]);
return 0;
}
options:
A. 15
B. compiler error
C. 23
D. 15
Ques 9. what is the output for the below code?
#include <stdio.h>
#include <stdlib.h>
void fun(int arr[])
{
int k;
if(k=1)
printf("%d\n", arr[0]);
else
printf("%d\n", arr[3]);
}
int main()
{
int narr[5] = {2,3,4,15,23,4,6};
fun(narr);
return 0;
}
options:
A. 15
B. 2
C. 4
D. compiler error
Ques 10. what is the output for the below code?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x,y,z,e,f;
int *narr[5] = {&x,&y,&z,&e,&f};
int marr[2][2] ={5,6,7,8} ;
narr[1] = 1000;
printf("%d %d \n", narr[1],marr[1][1]);
return 0;
}
options:
A. compile time error
B. run time error
C. 1000 8
D. 6
Answers
1. D
2. B
3. C
4. B
5. A
6. A
7. D
8. C
9. B
10. C
C Loops & Control – I
C Loops & Control - I
Ques. 1: What will be the output result of below code?
#include
int main()
{
int i = 2048;
for (; i; i >>= 1)
printf("Embedded system training");
return 0;
}
How many times Embedded system training will appear
A. 11
B. 12
C. code will not compile.
D. infinite loop.
Ques. 2: What will be the output result of below code?
#include
int main()
{
int i = 10;
switch (i)
{
case '10': printf("Professional");
break;
case '11': printf("Training");
break;
default: printf("Institute");
}
return 0;
}
A. Professional
B. Training
C. Institute
D. Compile time error.
Ques. 3: consider following code, what is the output?
#include
int add(){
printf("don't add here ");
return 0;
}
int main()
{
int i=1024;
if (add(i))
{
i=9;
}else
{
i=12;
}
printf("%d",i);
return 0;
}
A. don't add here 9
B. don't add here 12
C. error while compile
D. 12
Ques. 4: find the output
#include
main()
{
int check=0;
if (check);
else
{
printf("Hello PTI");
}
printf(" see you soon.\n");
}
A. Hello PTI see you soon.
B. else without a previous if
C. see you soon
D. none of above
Ques. 5: find the output
#include
main()
{
int check=0;
if (check);
{
}
else
{
printf("Hello PTI");
}
printf(" see you soon.\n");
}
A. Hello PTI see you soon.
B. else without a previous if
C. see you soon
D. none of above
Ques 6: What is the output?
#include
#define DISP(i, limit) do \
{ \
if (i++ < limit) \
{ \
printf("PTinstitute\n"); \
continue; \
} \
}while(0)
int main()
{
int i = 0;
DISP(i, 3);
return 0;
}
A. 0
B. PTinstitute
C. 3
D. Compile time error
Ques 7. what is the output?
#include
int main()
{
int i = 4;
switch (i)
{
case 1+0: printf("PTI");
break;
case 2+2: printf("Good");
break;
default: printf("Test");
}
return 0;
}
A. PTI
B. Good
C. Test
D. none of the above
Ques 8: find the outpout?
#include
#define TRUE 1
#define FALSE 0
int main()
{
int i = 3;
switch (i & 1)
{
case FALSE: printf("FALSE");
break;
case TRUE: printf("TRUE");
break;
default: printf("Default");
}
return 0;
}
A. TRUE
B. FALSE
C. Default
D. None of the above
Ques 9: find the outpout?
#include
enum num{FALSE, TRUE };
int main()
{
int i = 3;
switch (i & 1)
{
case FALSE: printf("FALSE");
break;
case TRUE: printf("TRUE");
break;
default: printf("Default");
}
return 0;
}
A. Default
B. FALSE
C. TRUE
D. None of the above
Ques 10: find the outpout?
#include
int main()
{
int m;
if (printf("0"))
m = 3;
else
m = 5;
printf("%d", m);
return 0;
}
Options:
A. 05
B. 03
C. 5
D. Compile time error
Ques 11: find the outpout?
#include
int main()
{
int k;
for (k = 5; k!=0; k--)
printf("n = %d", k--);
return 0;
}
Options:
A. n = 5 n= 3 n=1
B. n= 5, n= 4, n= 3 n=2 n=1
C. Infinite loop
D. 5 4 3 2 1
Ques 12: find the outpout?
#include
int main()
{
int g = 4, num = 8;
do {
num /= g;
} while(g--);
printf ("%d \n", num);
return 0;
}
options:
A. 1
B. 0
C. Runtime error
D. Compile time error
Ques 13: find the outpout?
#include
int main()
{
int g = 4, num = 8, true=1;
do {
num = num / g;
true --;
} while(true);
printf ("%d \n", num);
return 0;
}
options:
A. 1
B. 0
C. 2
D. Compile time error
Ques 14: find the outpout?
#include
int main()
{
int h = 4, y= 8;
if(y/h==2)
printf("\n h=2");
else
printf("\n h!=2");
return 0;
}
options:
A. h=2
B. h!=2
C. Compiletime error
D. none of the above
Ques 15: find the outpout?
#include
int main()
{
int m = 1;
if(m)
{
m--;
printf("\n %d ", m);
if(!m)
printf("%d ",m );
else
printf("%d ",m);
}
else
printf("\n h!=2");
return 0;
}
options:
A. 1 1
B. 0 0
C. Compiletime error
D. 1 0
Answers
1. B
2. C
3. B
4. A
5. D
6. B
7. B
8. A
9. C
10. B
11. C
12. C
13. C
14. A
15. B
Pointer Test I
Pointer Test I
Ques. 1: If a variable is a pointer to a structure, then what is the operator to
access data members of the structure through the pointer variable?
A. dot operator(.)
B. address operator(&)
C. indirection operator(*)
D. -> operator
Ques. 2: What will be the output result of below code?
#include
main()
{
float x = 13.442;
int *w = x;
printf("%d",*w);
}
A. 10
B. 10.2
C. error: incompatible type
D. no output
Ques. 3: The operator used to access the value of variable at address stored in a pointer variable is?
A. indirection operator(*)
B. address operator(&)
C. dot operator(.)
D. logical And operator(&&)
Ques. 4: What will be the output of following code assuming that array begins at location 1002?
#include
main()
{
int m[5] = {5, 6,7, 8, 9};
int *s = m;
printf("%d\t%d\t%d\t%d\t",*s,0[m],m,s);
}
A. error
B. 5 5 1002 1002
C. 5 0 1002 0
D. 5 5 0 1002
Ques. 5: What will be the output of following code?
#include
int iarr[] = {1,2,3};
main()
{
int *qptr;
qptr = iarr;
qptr = qptr+3;
printf("%d",*qptr);
}
A. error
B. 3
C. 2
D. garbage value
Ques. 6: What is the output of below code?
#include
void main()
{
int const *ptr = 5;
printf("%d", ++(*ptr));
}
A. error
B. 5
C. 6
D. print address of p
Ques 7: What will be the output of following code?
#include
main()
{
struct std
{
int a = 3;
char str[] = "hello";
};
struct std *ptr;
printf("%d", ptr->x);
printf("%s", ptr->name);
}
A. 3 hello
B. garbage value
C. error in declaration of pointer to structure.
D. error because of initializing variables in structure declaration
Ques. 8: What will be the output of following code?
#include
main()
{
register a = 5;
char b[] = "welcome";
printf("%s %d", b, a);
}
A. error
B. welcome 5
C. welcome
D. none of the above
Ques. 9: What is the size of char on 32-bit machine?
A. 1
B. 2
C. 4
D. 8
Ques. 10: Is the NULL pointer same as an uninitialised pointer?
A. Yes
B. No
Ques.11: Which of the statement is correct about the code?
int add(int, int);
int (*p)(int, int);
p = add;
A. p is a pointer to a function add which return integer
B. p is a function which return integer pointer
C. p is a function similar to add function
D. none of the above
Ques. 12: What will be the output of following code?
#include
int fun(int *m,int *n)
{
*m = *m+*n;
*n = *m-*n;
*m = *m-*n;
}
main()
{
int a = 10,b = 20;
fun(&a,&b);
printf("a= %d b = %d\n", a, b);
}
A. x=10 y=20 B. x=20 y=10
C. 20 10 D. error
ques 13: What will be the output of the C program?
#include
int main(){
int g = 435;
char *mptr;
mptr = (char *)&g;
printf("%d ",*mptr);
return 0;
}
A. -77
B. Run Time Error
C. Garbage value
D. Compile Time Error
ques 14: What will be the output of the C program?
#include
int main(){
int h = 3;
int *m;
int **p;
m = &h;
p = &m;
p++;
printf("%d ",**p);
return 0;
}
A. Garbage value
B. Compilation Error
C. Run time error
D. Linker Error
Ques. 15. What will be the output of the C program?
#include
int main(){
int a = 24;
int *b;
b = &a;
b++;
printf("%d ",*b);
}
A. Linker Error
B. Run time error
C. Compilation Error
D. Garbage value
Ques 16. What will be the output of the C program?
#include
#include
int main(){
char *str = "welcome";
char b[22];
strcpy(b, "ptinstitute");
printf("\n%s %s",str, b);
return 0;
}
A. welcome ptinstitute
B. Run time error
C. Compilation Error
D. Garbage value
Ques 17. What will be the output of the following C program?
#include
#include
int main(){
char *str1 = "hello";
char str2[22];
*str1 = "world";
printf("\n%s %s",str1, str2);
return 0;
}
A. Linker Error
B. Run time error
C. Compilation Error
D. Garbage value
Ques 18. What will be the output of the C program?
#include
int main()
{
char *str = "welcometoworld";
printf(str + 4);
return 0;
}
A. ometoworld
B. metoworld
C. welc
D. welco
Ques 19. What will be the output of the C program?
#include
int main()
{
char *qptr = "goodmorning";
printf("%c\n",*&*qptr);
return 0;
}
A. Address of g
B. Compilation Error
C. g
D. Run time error
Ques 20. What will be the output of the C program?
#include
#include
int main(){
register y = 23;
int *q;
q = &y;
printf("%u",q);
return 0;
}
A. Address of a
B. Run time error
C. Garbage value
D. Compile time error
Answers
1. D. -> operator
2. C. error: incompatible type
3. A. indirection operator(*)
4. B. 5 5 1002 1002
5. D. garbage value
6. A. error // incrementing the const pointer data *ptr is not allowed
7. D. error because of initializing variables in structure declaration
8. B. welcome 5
9. A. 1
10. B. No // NULL pointer pointing to address 0x0, uninitialised pointer pointing to some unknown location.
11. A. p is a pointer to a function add which return integer
12. B. x=20 y=10
13. A. -77
14. C. Run time error // p is pointer to m, m is a pointer to h. on incrementing the p ,
//it will points to some garbage value, so it is a runtime error.
15. D. Garbage value // b is pointer variable pionting to variable a. on incrementing b
// it points to other location which is not initialised hence gives garbage value.
16. A. welcome ptinstitute
17. C. Compilation Error // str1 is a ponter to character variable, it can be initialised only at declaration.
18. A. ometoworld
19. C. g
20. D. Compile time error // y is a register variable, q is requesting the register address.
Test 1
Test I
Ques 1 : What is the output of the below program.
main()
{
int m=33,n=21;
m=m++ + n++;
n= ++m + ++n;
printf("%d %d",m,n);
}
Options:
(A) 55 78
(B) 57 80
(C) 58 79
(D) 60 78
Ques 2 : What will be output for program below mentioned:
main()
{
int s=5;
printf("%d,%d,%d",s,s<<3,s>>1);
}
Options:
(A) 5,40,2
(B) 5,38,3
(C) 4,44,3
(D) 5,40,4
Ques 3 : For the below program what is the correct answer?
main ()
{
int m, n, p;
m = 2;
n =4*(m++);
p =3*(++m);
}
Options:
(A) n = 8, p = 12.
(B) m = 7, p = 13.
(C) n = 9, p = 12.
(D) m = 8, n = 8.
Ques 4 : How many iterations the below loop will run
main()
{
int k;
k=0;
do
{
k--;
printf("%d",k);
++k;
}
while(k>=0);
}
Options:
(A) 77
(B) Infinite
(C) Compilation Error
(D) 0
Ques 5 : what would be the output if ch = 'D' ?
switch(ch)
{
case 'D' : printf("Wonder");
case 'S' : printf("Good");
case 'R' : printf("Welcome");
break;
}
Options:
(A) Wonder
(B) WonderGood
(C) WonderGoodWelcome
(D) None of the above
Ques 6 : x,y,z are integer variables with values 2,4,6 respectively.
What is the result of the below expression:
!((x+y)>(z+10))
Options:
(A) 1
(B) 4
(C) 12
(D) 0
Ques 7 : For the below program, How many times "www.ptinstitute.in" printed?
main ()
{
int e, r;
for (e=5, r=1; e ==4, r < 10; e --, r++)
printf("\n www.ptinstitute.in");
}
Options:
(A) 5
(B) compilation error
(C) 9
(D) None of the above
Ques 8 : What value will be printed for the program below.
main()
{
int f,m,h;
f=21;
m=32;
h=printf("%d",f) + ++m;
printf("%d",h);
}
Options:
(A) 2133
(B) 2032
(C) 2135
(D) Compilation Error
9 : How many times the below loop will get executed?
main()
{
int s;
for(s=9;s;s=s-4)
{
printf("\n%d",s);
}
}
Options:
(A) 2
(B) 3
(C) Error
(D) Infinite
Ques 10 : What is the result printed for the below statement if x=12
printf("%d %d",x, !x--);
Options:
(A) 11 0
(B) 12 12
(C) 12 0
(D) 0 12
Ques 11 : What shall be the output
main()
{
int e;
e = 13;
if(e == 21 || 20)
{
printf("Correct");
}
else
{
printf("Wrong");
}
}
Options:
(A) Correct
(B) Worng
(C) Compiler Error
(D) Run time Error
Ques 12 : What is the output for the below program?
main()
{
if(22,0,23)
{
printf("True");
}
else
{
printf("False");
}
}
Options:
(A) True
(B) False
(C) Syntax Error
(D) Compiler Error
Ques 13 : What shall be the output for the below code?
main()
{
int g, h, *p, *q`;
g = 10;
h = 10;
p = &g;
q = &h;
if(p == q)
{
printf("one");
}
else
{
printf("zero");
}
}
Options:
(A) one
(B) zero
(C) Compiler Error
(D) Syntax Error
Ques 14 : How many times the for loop will get executed in the below program?
main()
{
int a;
for(a=12, a=8; a>=0; a--)
{
printf("\n %d", a);
}
}
Options:
(A) 1
(B) 8
(C) 9
(D) Syntax Error
Ques 15 : How many iterations main() will be called?
main()
{
printf("\n Main function is Called ");
main();
}
Options:
(A) 100
(B) 1000
(C) 1
(D) Infinite
Ques 16 : What shall be the output for the program below if the base address of array is 0x2300.
main()
{
int arr[] = { 5,4,7,8,9 };
int w,*q ;
q = arr;
for(w = 0; w<=4 ; w++)
{
printf("\n 0x%x", *q++);
}
}
Options:
(A) 5 4 7 8 9
(B) 4 5 8 9
(C) 0x2300 0x2301 0x2302 0x2303
(D) 0x2301 0x2302 0x2303 0x2304
Ques 17 : Which of the following program structure/component/statement is not a concept for implementation of modularization ?
Options:
(A) DLL
(B) Functions
(C) type casting
Ques 18 : What will be output for the below program?
#include
int main()
{
int i = 340;
char *q;
q = (char *)&i;
printf("%d",*q);
return 0;
}
Options:
(A) 1
(B) 340
(C) 84
(D) Syntax Error
Ques 19 : What will be output when you will execute following c code?
#include
{
char str[10]="The American King";
printf("%s", str);
return 0;
}
Options:
(A) The Americ
(B) The
(C) Syntax error
(D) None of the aboce
Ques 20 : What is the output result of the below c program?
#include "stdio.h"
int main()
{
int _ = 3;
int __ = 13;
int ___;
___ = _ + __;
printf("%i", ___);
}
Options:
(A) 3
(B) 13
(C) 16
(D) Compilation Error
Answers
1. (A) 55 78
2. (A) 5,40,2
3. (A) n = 8, p = 12.
4. (B) Infinite
5. (C) WonderGoodWelcome
6. (A) 1
7. (C) 9
8. (C) 2135
9. (D) Infinite
10. (A) 11 0
11. (A) Correct
12. (A) True
13. (B) zero
14. (C) 9
15. (D) Infinite
16. (A) 5 4 7 8 9
17. (C) type casting
18. (C) 84
19. (A) The Americ
20. (C) 16
Structure Pointer
Pointer Variable can also Store the Address of the Structure Variable. Pointer to Array of Structure stores the Base address of the Structure array.
2.1 Write a program to store 1 student information using a structure pointer concept without using array.
MEMORY MAPPING:
1)initially it allocates size of the structure.
2)firstly it allocates 10 memory locations for my name=”priyanka”
3)next it allocates 35 memory locations for mailid,for both age and roll_num it allocates each of 4 byte
for below program i got addressess will be as follow(note address will be changing at each compilation and depending on different PC’s.
/*name=priyanka->0xbf946424
mailid=priyankamn522@gmail.com->0xbf94642e
roll_num=65->0xbf946454
age=22->0xbf946458*/
#include<stdio.h>
#include<string.h>//adding inbuilt for string
library(particularly strcpy)
struct student//structure definition along with tag name
{
char name[10],mailid[35];
int roll_num,age;
};
int main()
{
struct student a,*ptr;//declaration of structure pointer variable
ptr=&a;//pointer variable stores the address of structur variable.
strcpy(ptr->name,”priyanka”);
strcpy(ptr->mailid,”priyankamn522@gmail.com”);
ptr->roll_num=65;
ptr->age=22;
printf(“name=%s->%p\n mailid=%s->%p\n roll_num=%d->%p\n age=
%d->%p\n”,ptr->name,ptr->name,ptr->mailid,ptr->mailid,ptr-
>roll_num,&ptr->roll_num,ptr->age,&ptr->age);
/*printing the one student information using pointers*/
}
2.2 write a program using structure pointer to store 5 students
information without using array.
MEMORY MAPPING:
1st student
name->0xbfd4d7d8
age->0xbfd4d808
rollno->0xbfd4d80c
mailid ->0xbfd4d7e2
marks->0xbfd4d810
2nd student
name ->0xbfd4d814
age ->0xbfd4d844
rollno ->0xbfd4d848
mailid ->0xbfd4d81e
marks ->0xbfd4d84c
3rd student
name ->0xbfd4d850
age ->0xbfd4d880
rollno ->0xbfd4d884
mailid ->0xbfd4d85a
marks ->0xbfd4d888
4th student
name ->0xbfd4d88c
age ->0xbfd4d8bc
rollno ->0xbfd4d8c0
mailid ->0xbfd4d896
marks ->0xbfd4d8c4
5th student
name ->0xbfd4d8c8
age ->0xbfd4d8f8
rollno ->0xbfd4d8fc
mailid ->0xbfd4d8d2
marks ->0xbfd4d900 */
#include<stdio.h>
struct student//structure defination along with data type
declaration of the members
char name[10],mailid[35];
int age,rollno,marks;
int main()
struct student a[10],*ptr;//declaration of structure array along with structure pointer int i;
for(i=0;i<5;i++)//for loop for scannning students information by using pointer concept
{
ptr=&a[i];
printf(“enter the %d student
information:name,age,rollno,mailid,marks\n”,i+1); scanf(“%s %d %d %s %d”,ptr->name,&ptr->age,&ptr >rollno,ptr->mailid,&ptr->marks); ptr++;//for next student information storage location
}
for(i=0;i<5;i++)//for loop for printing the 5 student information using structue pointers and structure array concept
{
ptr=&a[i];
printf(“student %d information is\n name=%s-%p age=%d-
%p rollno=%d-%p mailid=%s-%p marks=%d-%p\n”,i+1,ptr->name,ptr-
>name,ptr->age,&ptr->age,ptr->rollno,&ptr->rollno,ptr-
>mailid,ptr->mailid,ptr->marks,&ptr->marks);
ptr++;//next student information address
}
}
2.3 write a program using structure pointer to store 5 students
information without using array.
MEMORY MAP:
according to my user input
name =priya=0xbf9f84d4
rollno =65=0xbf9f84e0
age =22=0xbf9f84e4
marks =79=0xbf9f84e8
———————————-
name =rita=0xbf9f84ec
rollno =87=0xbf9f84f8
age =23=0xbf9f84fc
marks =67=0xbf9f8500
———————————-
name =sony=0xbf9f8504
rollno =21=0xbf9f8510
age =21=0xbf9f8514
marks =78=0xbf9f8518
———————————-
name =sheela =0xbf9f851c
rollno =71 =0xbf9f8528
age =21 =0xbf9f852c
marks =48 =0xbf9f8530
———————————-
name =smitha =0xbf9f8534
rollno =81 =0xbf9f8540
age =21 =0xbf9f8544
marks =78 =0xbf9f8548
———————————-*/
#include<stdio.h>
struct student//structure defination along with the datatype
declaration of the members
{
char name[10];
int rollno,age,marks;
};
int main()
{
struct student a,b,c,d,e,*p1,*p2,*p3,*p4,*p5;//structure variable declaration along with the structure pointer declaration
p1=&a,p2=&b,p3=&c,p4=&d,p5=&e; printf(“enter the student 1 information name,rollno,age,marks\n”);
//user input of 5 students information one by one using pointer
scanf(“%s %d %d %d”,p1->name,&p1->rollno,&p1->age,&p1- >marks);
printf(“enter the student 2 information name,rollno,age,marks\n”); scanf(“%s %d %d %d”,p2->name,&p2->rollno,&p2->age,&p2- >marks);
printf(“enter the student 3 information name,rollno,age,marks\n”); scanf(“%s %d %d %d”,p3->name,&p3->rollno,&p3->age,&p3- >marks);
printf(“enter the student 4 information name,rollno,age,marks\n”); scanf(“%s %d %d %d”,p4->name,&p4->rollno,&p4->age,&p4- >marks);
printf(“enter the student 5 information name,rollno,age,marks\n”); scanf(“%s %d %d %d”,p5->name,&p5 >rollno,&p5->age,&p5- >marks);
//printing the five students information using structure pointers:
printf(“the first student information is\n name=%s-%p rollno=%d-%p age=%d-%p marks=%d-%p\n”,p1->name,p1->name,p1- >rollno,&p1->rollno,p1->age,&p1->age,p1->marks,&p1->marks);
printf(“the second student information is\n name=%s-%p rollno=%d-%p age=%d-%p marks=%d-%p\n”,p2->name,p2->name,p2- >rollno,&p2->rollno,p2->age,&p2->age,p2->marks,&p2->marks);
printf(“the third student information is\n name=%s-%prollno=%d-%p age=%d-%p marks=%d-%p\n”,p3->name,p3->name,p3->rollno,&p3->rollno,p3->age,&p3->age,p3->marks,&p3->marks);
printf(“the fourth student information is\n name=%s-%p rollno=%d-%p age=%d-%p marks=%d-%p\n”,p4->name,p4->name,p4- >rollno,&p4->rollno,p4->age,&p4->age,p4->marks,&p4->marks);
printf(“the fifth student information is\n name=%s-%p rollno=%d-%p age=%d-%p marks=%d-%p\n”,p5->name,p5->name,p5- >rollno,&p5->rollno,p5->age,&p5->age,p5->marks,&p5->marks);
}
Structure
Description:
1 Structures:
A structure is a user defined data type in c.A structure creates a data type that can be used to group items of possibly
different types into a single type.
Example:
To store the employee details
struct employee |struct=keyword employee=tag or group
name
{
|
char name[10];
|members or fields of structure
int id[5];
float salary;
};
1.1 Write a program using structures, to store one student information
and print it
#include<stdio.h>
struct student
{
char name[10];
int age;
int roll_no;
int marks;
char mail_id[25];
};
int main()
{
struct student a;//struct student is an data type and a is the
object that holds all information
printf(“enter the student name,age,rollnum,marks and mail_id
orderly\n”);
scanf(“%s %d %d %d
%s”,a.name,&a.age,&a.roll_no,&a.marks,a.mail_id);/*scanning the
student information */
printf(“the student information is”);
printf(“\n name=%s\n age=%d\n roll_no=%d\n marks=%d mail_id=
%s\n”,a.name,a.age,a.roll_no,a.marks
,a.mail_id);//printin
g the student information
}
1.2 Write a program using structures to store 5 student information
and print it without using array.
#include<stdio.h>
struct student
{
char name[10];
int age;
int marks;
int rollnum;
};
int main()
{
struct student a,b,c,d,e;//5 different variables for 5 different
students
printf(“enter student 1 information name,age,rollnum and marks\n”);
scanf(“%s %d %d %d”,a.name,&a.age,&a.marks,&a.rollnum);//scanning
student 1 information upto student 5 below
printf(“enter student 2 information name,age,rollnum and marks\n”);
scanf(“%s %d %d %d”,b.name,&b.age,&b.marks,&b.rollnum);
printf(“enter student 3 information name,age,rollnum and marks\n”);
scanf(“%s %d %d %d”,c.name,&c.age,&c.marks,&c.rollnum);
printf(“enter student 4 information name,age,rollnum and marks\n”);
scanf(“%s %d %d %d”,d.name,&d.age,&d.marks,&d.rollnum);
printf(“enter student 5 information name,age,rollnum and marks\n”); scanf(“%s %d %d %d”,e.name,&e.age,&e.marks,&e.rollnum);
printf(“student 1 information”);//printing the 5 students infromation
printf(“\n name=%s\n age=%d\n marks=%d\n rollnum=
%d\n”,a.name,a.age,a.marks,a.rollnum);
printf(“student 2 information”);
printf(“\n name=%s\n age=%d\n marks=%d\n rollnum=
%d\n”,b.name,b.age,b.marks,b.rollnum);
printf(“student 3 information”);
printf(“\n name=%s\n age=%d\n marks=%d\n rollnum=
%d\n”,c.name,c.age,c.marks,c.rollnum);
printf(“student 4 information”);
printf(“\n name=%s\n age=%d\n marks=%d\n rollnum=
%d\n”,d.name,d.age,d.marks,d.rollnum);
printf(“student 5 information”);
printf(“\n name=%s\n age=%d\n marks=%d\n rollnum=
%d\n”,e.name,e.age,e.marks,e.rollnum);
}
1.3 write a program using structures to store information of 5
students using array and print them.
#include<stdio.h>
struct student//structure definition
{
char name[10],mail_id[35];
int age,roll_num,marks;
};
int main()
{
struct student a[5];//declaring data type of array which stores
our structure members
int i;
for(i=0;i<5;i++)//user input of 5 student information using array
{
printf(“enter the %d student information
name,age,rollnum,emailid and marks in order\n”,i+1);
scanf(“%s %d %d %s
%d”,a[i].name,&a[i].age,&a[i].roll_num,a[i].mail_id,&a[i].marks);
}
for(i=0;i<5;i++)//printing of 5 students information using array
{
printf(“\nstudent %d details”,i+1);
printf(“\n name=%s\n age=%d\n rollno=%d\n mailid=%s\n marks=
%d\n”,a[i].name,a[i].age,a[i].roll_num,a[i].mail_id,a[i].marks);
}
}
Pointer
1. POINTERS:
pointers are the variable it stores the address of another variable, a pointer variable is given as *variable.
1.1 Declare int, char variable and 2 pointer variable for respected data types,assign the address. print the value of&a,aptr,*aptr,&aptr,&b, bptr,*bptr,&bptr.
#include<stdio.h>
int main()
{
int a;
char b;
int *aptr;//pointer variable for integer
char *bptr;//pointer variable for character
a=10;//declaration of integer a
b=’e’;//declaration of integer b
aptr=&a;//pointer variable stores the address of a variable
bptr=&b;
printf(“%ld\n”,sizeof(bptr));//to print the memory size
printf(“%d %p %p %p \n”,*aptr,&a,aptr,&aptr);//%p for printing
the address(hexadecimal)
printf(“%c %p %p %p \n”,*bptr,&b,bptr,&bptr);
}
1.2, Declare an array of size (5) assign its address to an integer pointer using the pointer, initialize array and pointer array.
#include<stdio.h>
int main()
{
int a[5]={1,2,3,4,5},*p,i;//array initialization along with
initialization of pointer variable
for(i=0;i<5;i++)//loop for storing address address of each
element of the array
{
p=&a[i];
printf(“%d %p\n”,*p,p);//for printing array values and the
address of each element of the array
p++;
}
}
1.3 Print your name using pointer. print the address of each element of your name.
#include<stdio.h>
void str(char a[],int size);
int main()
{
char a[8]={“priyanka”};//character array for storing name
str(a,8);//function call
}
void str(char a[],int size)
{
int i;
char *p;//character pointer variable
p=&a[0];//address of first element of the string
printf(“%s\n”,p);//by using address it prints whole string
printf(“%d\n”,sizeof(p));
for(i=0;i<8;i++)//loop for obtaining address of the each element
in the name
{
p=&a[i];
printf(“%c %p\n”,*p,p);
p++;//next location
}
}
1.4 Addition,subtraction,multiplication and division of two numbers using pointers.
#include<stdio.h>
int add(int a,int b);//prototypes of add,sub,mul,div
int sub(int a,int b);
int mull(int a,int b);
float divi(int a,int b);
int main()
{
int a,b,sum,diff,mul;
float div;
printf(“enter the two numbers:\n”);
scanf(“%d %d”,&a,&b);
sum=add(a,b);//function call for addition
printf(“sum=%d\n”,sum);
diff=sub(a,b);//function call for subtraction
printf(“difference=%d\n”,diff);
mul=mull(a,b);//function call for multiplication
printf(“xtion=%d\n”,mul);
1.5 Find the average of 5 given numbers using pointers
1.6 Swap the two given numbers using pointers
1.7 Write a program to take your name as input save into another array using the pointer
1.8 Find out how many vowels are there in name using pointers
DESCRIPTION:
1)Give any name as a input to character array as a string.
2)declare the character pointer variable and feed the string address to that variable.
3)compare the given string elements one by one with the five vowels(a,A,e,E,i,I,o,O,u,U),if the elements is equal to this letters then increment the counter variable(intialize it to zero first.
4)print the counter value as number of vowels found in this string of given name.
MEMORY MAPPING:Input character array(any input)=priyanka
ptr=&name[0](or we can use ptr=name ptr=address of first character of the given array
ptr++=(address of the next character,we keep on incrementing till end of the string(‘\0′)
*ptr=dereferencing value
the address of each character of given input is as follow
p=0xbfa36f2
r=0xbfa36f3
i=0xbfa36f4
y=0xbfa36f5
a=0xbfa36f6
n=0xbfa36f7
k=0xbfa36f8
a=0xbfa36f9
note:this address may be changing for each compilation and different pcs
#include<stdio.h>
int main()
{
char name[10],*ptr;//*ptr is a pointer variable
printf(“enter the name\n”);
scanf(“%s”,name);
ptr=&name[0];//for string we need not to add (&)address symbol
int vowel=0;//vowel count in the given name
int consonant=0;//number of consonant in the given name
while(*ptr!=’\0′)//we keep on comparing the each elements in the name till end of the string
{
if(*ptr==’A’||*ptr==’a’||*ptr==’E’||*ptr==’e’||*ptr==’I’||*ptr==’i’||*ptr==’O’||*ptr==’o’||*ptr==’U’||*ptr==’u’)
vowel++;//if condition to check the number of vowels in the given name
else
consonant++;
printf(“%c=%p\n”,*ptr,ptr);//to print the address of each letter
ptr++;//for next location
}
printf(“the number of vowels=%d\n the number of consonant=%d\n”,vowel,consonant);
}
1.9 Write a program to subtract two numbers using the function, pass one argument as integer pointer and another as integer,return the integer pointer to the main function.
#include<stdio.h>
int* sub(int *x,int y);//return type is
int main()
{
int a,b,*d;
printf(“enter the two numbers to find the difference\n”);
scanf(“%d %d”,&a,&b);
d=sub(&a,b);//function call
printf(“the difference value is=%d\n”,*d);//printing the difference value
}
int* sub(int *x,int y)
{
*x=*x-y;//subtraction process
return x;//returning address.
}
1.10 Pass two char array as the size of the same size into a function using a pointer, in the function write a program to compare both arrays and return 0 or 1.if they are the same return 1 and if they are not the same return 0.
#include<stdio.h>
int cmp(char *x,char *y);
int main()
{
char array1[10],array2[10];
int p;
printf(“enter the first character array\n”);
scanf(“%s”,array1);
printf(“enter the second character array\n”);
scanf(“%s”,array2);
p=cmp(&array1[0],&array2[0]);//function call where we pass the address of first element of both strings
if(p==1)
printf(“two strings are equal\n”);
else
printf(“two strings are not equal\n”);
}
int cmp(char *x,char *y)//receiving the adress of first elements as a pointers in function
{
while(*x==*y)//to compare 1st elements of the array initially, if it agrees then continue else return 0
{
if(*x==’\0′ || *y==’\0′)//if any one string becomes null then stop running of loop
break;
printf(“%c=%p\n”,*x,x);
x++;//next location address of first array
y++;//next location address of second array
}
if(*x==’\0′ && *y==’\0′)//if both the strings are become null at a time
return 1;
else
return 0;
}
1.11 Declare array of character pointer initialize them and print them.
#include<stdio.h>
int main()
{
char *name[10]=”priyanka”;//character array declaration
printf(“%s”,&name[0]);//or printf(“%s”,&name[0]);/*for
string only first address is required.*/
}
8051 embedded c program
8051 embedded c program
Write a program for make led ON when if switch is pressed, make it OFF when the switch is released (use push button ).
#include<reg51.h>
sbit c=P1^1; // assigning port p1 pin 1 for use switch
void main()
{
P1=0x0ff; // making p1 port is input
P2=0x00; // making p2 port is output
P1=0x00; // initially make it port 1 is OFF condition
while(1) // infinite loop
{
if(c==1) // when the input is HIGH (switch is pressed)
{
P2=0x0ff; // p2 will go to high(led ON)
}
else
{
P2=0x00; // p2 will go to low(led OFF)
}
}
Explanation:
Here we are going to make a lid on and off when the button is pressed. IN 8051 is having a 4 port, generally the 4 port inbuiled GPIO pins. This pins are using as an either input or output pin. We assign the port pin as an input pin for the switch. In main function, we should assign which is the input and output port. Here we are using the port 1 for input and the port 2 is for output. Then made one while loop for continuous checking. When the input goes to high the LED will ON otherwise it will be OFF.
Embedded C Compiler
Embedded C Compiler
A compiler translates the high level programming languages in which programs are written into the machine languages so that the microprocessors which are embedded in electronic products can actually understand.
Where the compilation is process of converting the high-level programming into the machine language.
A compiler executes four major steps:
- Scanning:
The scanner reads one character at a time from the source code and keeps track of which character is present in which line.
- Lexical Analysis:
the sequence of characters which appear in the source code are converted by compiler into a series of strings of characters which are also known as tokens, these are associated by a particular rule by a program which is called a lexical analyzer. The lexical analyzer uses symbol table to store the words in the source code that correspond to the token generated.
- Syntactic Analysis:
The third step includes performing the syntactic analysis,this step is performed to check whether the tokens which are generated during lexical analysis are in correct order as per their usage. The set of keywords which are in correct order, which leads to expected result is called as syntax. To ensure the syntactic accuracy the compiler has to check the source code.
- Semantic Analysis:
The fourth step is semantic analysis.this step includes many intermediate steps.
The first step is to check the structure of tokens along with their orders with respect to the given language grammar.the parser and analyzer interpret the meaning of the token structure to generate an intermediate code, which is called the object code. The object code contains the instructions which represents the processor action for a particular token when encountered in program. Lastly the full code is parsed and interpreted to check is their any optimizations are possible. Once the optimization process is performed, the correct modified tokens are inserted in the main object code to produce the final object code, which will be inside the file.
Some of the compiler available for embedded c are
GCC by GNU.
IAR C/C++ Compilers by IAR systems.
Intel C compiler by intel
Keil C/C++ Compilers by Keil company.
Ch by SoftIntegration, Inc.
XL c by IBM.
There are different compilers are available for embedded c.the selection of compiler for embedded depends on the architecture of embedded system.
The most often used compiler is “ported” gcc compiler and another popular compiler is keil.for a standard architecture/CPU supported by GCC, then GCC should be good.
The good software for ARM might be Keil or IAR C compilers. IAR also supports ot many other processors and microcontrollers.
About Us: Professional Training Institute is one of the top Embedded System training Institute in Bangalore, Our unique training practical based training approach makes every student as knowledgeable and experienced. Our student Practical hands on training in Embedded system makes student employable. And also till now are having 100% placement records in our Embedded system.
Embedded c to assembly converter
Embedded c to assembly converter
Here we will see how to convert c file to assembly file in keil software.
Step1:
Consider the below picture. This is the small example program and this is in the c file.
Now going to convert into assembly .
step 2:
Click the target. Will get the option for target. Select that option the one window will open. That i so in the below.
Step 3 :
Now to select the listing menu. You will get the below dialog box.
Step 4:
In the above window you can see the “assembly code” . to tick that box and select ok.
Step 5:
Move to the file location. You can refer the top of the window. You will get the path of this file where it saved.
The assembly file of the corresponding c file, you can get the assembly file from there.
About Us: Professional Training Institute is one of the top Embedded System training Institute in Bangalore, Our unique training practical based training approach makes every student as knowledgeable and experienced. Our student Practical hands on training in Embedded system makes student employable. And also till now are having 100% placement records in our Embedded system.