Ex-1 #include <stdio.h> struct sample { int a=0; char b='A';

Slides:



Advertisements
Similar presentations
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
Advertisements

C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
Bit Field.
Union, bitfield, typedef, enum union nama_u{ }; union nama_u{ struct nama_s byte; }; enum{ }; Tipedef var BYTE.
Structure of a C program
UBC104 Embedded Systems Variables, Structures & Pointers.
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Functions, Pointers, Structures Keerthi Nelaturu.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
Programming in C Structs and Unions. Java vs C Suppose you were assigned a write an application about points and straight lines in a coordinate plane.
Enumerated Data Type. An enumeration consists of a set of named integer constants. An enumeration type declaration gives the name of the (optional) enumeration.
ECE 103 Engineering Programming Chapter 49 Structures Unions, Part 1 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
Struct Advanced Programming Language. Struct Struct allows us to create a new data type We can now declare “struct Point” as a new data type and can use.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
Welcome to Concepts Pointer Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
© Oxford University Press All rights reserved. CHAPTER 8 STRUCTURES.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
Structures and Union. Review bitwise operations –you need them for performance in terms of space and time –shifts are equivalent to arithmetics enumeration.
CSE 251 Dr. Charles B. Owen Programming in C1 structs Aggregating associated data into a single variable Box width length height Circle radius int main()
Programming in C Arrays, Structs and Strings. 7/28/092 Arrays An array is a collection of individual data elements that is:An array is a collection of.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
Computer science C programming language lesson 3.
Java Programming Language Lecture27- An Introduction.
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
Lecture 10 union, enumeration, bit
Numbers in ‘C’ Two general categories: Integers Floats
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Structures, Unions, Enumerations
CSE 220 – C Programming Pointers.
BILASPUR UNIVERSITY DEPARTMENT OF COMPUTER SCIENCE AND APPLICATION
LESSON 3 IO, Variables and Operators
C LANGUAGE MULTIPLE CHOICE QUESTION
C Fundamentals & Pointers
Module 2 Arrays and strings – example programs.
Introduction to C Programming Language
Introduction to C Programming
Visit for more Learning Resources
14th September IIT Kanpur
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Arrays, For loop While loop Do while loop
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Lecture 8.
Structures and Union.
פרטים נוספים בסילבוס של הקורס
פרטים נוספים בסילבוס של הקורס
Introduction to C Programming
Pointers.
Govt. Polytechnic,Dhangar
Unions A union is a custom data type used for storing several variables in the same memory space. Although you can access any of those variables at any.
C Storage classes.
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Structures and Union.
WEEK-2.
Programming Language C Language.
In C Programming Language
POINTER CONCEPT 4/15/2019.
Lecture 14: Problems with Lots of Similar Data
Structures and Union.
Course Outcomes of Programming In C (PIC) (17212, C203):
Lecture 8.
Pointer Arithmetic By Anand George.
POINTER CONCEPT 8/3/2019.
Extra C Material Based on material in: The C Programming Language, Second Edition by Brian W. Kernighan and Dennis M. Ritchie. Prentice Hall, Inc., 1988. 
Presentation transcript:

Ex-1 #include <stdio.h> struct sample { int a=0; char b='A';     float c=10.5; }; int main()     struct sample s;     printf("%d,%c,%f",s.a,s.b,s.c);     return 0; }

01 Error: Can not initialize members here We can only declare members inside the structure, initialization of member with declaration is not allowed in structure declaration.

Ex-2 #include <stdio.h> int main() { typedef struct tag{         char str[10];         int a;     }har;       har h1,h2={"IHelp",10};     h1=h2;     h1.str[1]='h';     printf("%s,%d",h1.str,h1.a);     return 0; }

02 Ihelp,10 It is possible to copy one structure variable into another like h1=h2. Hence value of h2.str is assigned to h1.str.

Example 4 #include<stdio.h> void main() { struct india {char c; float d;}; struct world {int a[3]; char b; struct india orissa; }; struct world st ={{1,2,3},'P','q',1.4}; clrscr(); printf("%d \t %c \t %c \t %f", st.a[1],st.b,st.orissa, st.orissa.d); }

Output 4 2 p q 1.400000

p5 #include <stdio.h> int main() { union test { int i; int j; };     {         int i;         int j;     };           union test var=10;     printf("%d,%d\n",var.i,var.j); }

05 Error: Invalid Initialization You cannot initialize an union variable like this.

P5 #include<stdio.h> int main() { union a { int i; char ch[2]; }; union a u; u.ch[0]=3; u.ch[1]=1; printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i); u.ch[0]=‘a’; u.ch[1]=‘b’; printf(“%d ,%d ,%c ,%c”,u.ch[0],u.ch[1],u.ch[0],u.ch[1]); return 0; }

05 3, 2, 259 97,98,a,b

05-reason Reason :- The system will allocate 2 bytes for the union. The statements u.ch[0]=3; u.ch[1]=2; store data in memory as given below. 1st byte [0]= 00000011 (3) 2nd byte [1]=00000001 (1) so it is 000000001 (256) 00000011 (3) . So 256+3=259

p6 int main() { struct value { int bit1:1; int bit3:4; int bit4:4; printf("%d\n", sizeof(bit)); return 0; }

06 2 Note :- Since C is a compiler dependent language, in Turbo C (DOS) the output will be 2, but in GCC (Linux) the output will be 4.

p7 #include<stdio.h> int main() { struct bits { int i:40; }bit; printf("%d\n", sizeof(bit)); return 0; }

07 Error :bit field too large

p8 struct marks{ int p:3; int c:3; int m:2; }; void main(){   struct marks s={2,-6,5};   printf("%d %d %d",s.p,s.c,s.m);  }

08 2 2 1 ----------- Explanation: Binary value of 2: 00000010 (Select three two bit) Binary value of 6: 00000110 Binary value of -6: 11111001+1=11111010 (Select last three bit) Binary value of 5: 00000101 (Select last two bit)

p9 #include <stdio.h> struct student { }; void main() struct student s[2]; printf("%d", sizeof(s)); }

09

p10 #include <stdio.h> struct point { int x; int y; }; struct notpoint void foo(struct point); int main() struct notpoint p1 = {1, 2}; foo(p1); } void foo(struct point p) printf("%d\n", p.x);

010 Compile time error

p11 #include <stdio.h> typedef struct p { int x, y; }k; int main() struct p p = {1, 2}; k k1 = p; printf("%d\n", k1.x); }

011 1

p12 #include <stdio.h> struct p { char x : 2; int y : 2; }; int main() struct p p; p.x = 2; p.y = 1; p.x = p.x & p.y; printf("%d\n", p.x); }

012

p13 #include <stdio.h> union u { struct unsigned char x : 2; unsigned int y : 2; }p; int x; }; int main() union u u; u.p.x = 2; printf("%d\n", u.p.x); }

013 2

p14 #include <stdio.h> union u { struct unsigned char x : 2; unsigned int y : 2; }p; int x; }; int main() union u u.p.x = 2; printf("%d\n", u.p.x); }

014 Compile time error

p15 #include <stdio.h> struct p { unsigned int x : 1; unsigned int y : 1; }; int main() struct p p; p.x = 1; p.y = 2; printf("%d\n", p.y); }

015