Thursday, July 9, 2020
Understanding Structures and Padding in C
Understanding Structures and Padding in C Understanding Structures and Padding in C Back Home Categories Online Courses Mock Interviews Webinars NEW Community Write for Us Categories Artificial Intelligence AI vs Machine Learning vs Deep LearningMachine Learning AlgorithmsArtificial Intelligence TutorialWhat is Deep LearningDeep Learning TutorialInstall TensorFlowDeep Learning with PythonBackpropagationTensorFlow TutorialConvolutional Neural Network TutorialVIEW ALL BI and Visualization What is TableauTableau TutorialTableau Interview QuestionsWhat is InformaticaInformatica Interview QuestionsPower BI TutorialPower BI Interview QuestionsOLTP vs OLAPQlikView TutorialAdvanced Excel Formulas TutorialVIEW ALL Big Data What is HadoopHadoop ArchitectureHadoop TutorialHadoop Interview QuestionsHadoop EcosystemData Science vs Big Data vs Data AnalyticsWhat is Big DataMapReduce TutorialPig TutorialSpark TutorialSpark Interview QuestionsBig Data TutorialHive TutorialVIEW ALL Blockchain Blockchain TutorialWhat is BlockchainHyperledger FabricWhat Is EthereumEthereum TutorialB lockchain ApplicationsSolidity TutorialBlockchain ProgrammingHow Blockchain WorksVIEW ALL Cloud Computing What is AWSAWS TutorialAWS CertificationAzure Interview QuestionsAzure TutorialWhat Is Cloud ComputingWhat Is SalesforceIoT TutorialSalesforce TutorialSalesforce Interview QuestionsVIEW ALL Cyber Security Cloud SecurityWhat is CryptographyNmap TutorialSQL Injection AttacksHow To Install Kali LinuxHow to become an Ethical Hacker?Footprinting in Ethical HackingNetwork Scanning for Ethical HackingARP SpoofingApplication SecurityVIEW ALL Data Science Python Pandas TutorialWhat is Machine LearningMachine Learning TutorialMachine Learning ProjectsMachine Learning Interview QuestionsWhat Is Data ScienceSAS TutorialR TutorialData Science ProjectsHow to become a data scientistData Science Interview QuestionsData Scientist SalaryVIEW ALL Data Warehousing and ETL What is Data WarehouseDimension Table in Data WarehousingData Warehousing Interview QuestionsData warehouse architectureTalend T utorialTalend ETL ToolTalend Interview QuestionsFact Table and its TypesInformatica TransformationsInformatica TutorialVIEW ALL Databases What is MySQLMySQL Data TypesSQL JoinsSQL Data TypesWhat is MongoDBMongoDB Interview QuestionsMySQL TutorialSQL Interview QuestionsSQL CommandsMySQL Interview QuestionsVIEW ALL DevOps What is DevOpsDevOps vs AgileDevOps ToolsDevOps TutorialHow To Become A DevOps EngineerDevOps Interview QuestionsWhat Is DockerDocker TutorialDocker Interview QuestionsWhat Is ChefWhat Is KubernetesKubernetes TutorialVIEW ALL Front End Web Development What is JavaScript â" All You Need To Know About JavaScriptJavaScript TutorialJavaScript Interview QuestionsJavaScript FrameworksAngular TutorialAngular Interview QuestionsWhat is REST API?React TutorialReact vs AngularjQuery TutorialNode TutorialReact Interview QuestionsVIEW ALL Mobile Development Android TutorialAndroid Interview QuestionsAndroid ArchitectureAndroid SQLite DatabaseProgramming aria-current=page>Uncat egorizedUnderstanding Structures And P...Understanding Structures and Padding in C Last updated on May 06,2020 6.8K Views edureka BookmarkDefinition of Structures in C:Structure is a collection of multiple variables of different types. Structures are used to create user-defined data types as a collection of other data types. For example, the co-ordination of location on earth consists of latitude and longitude. Lets look at the syntax for this;Struct coord{ Float latitude; Char lat_direction; Float longitude; Float long_direction; };The above syntax declares a new user-defined data type struct coord which can be used to store the co-ordinates.Examples of Using Structures:#includestdio.h struct name { char fname[50]; char lname[50]; }; int main() { struct name student; printf(Enter your first name:); gets(student.fname); printf(Enter your last name:); gets(student.lname); printf(First Name:%s ,student.fname); printf(Last Name:%s , student.lname); system(PAUSE); Return 0; }Lets compile and run the program.Enter the first name and the last name. The output will be as shown below:Structure Padding in C:Many processors expect memory for variables to be aligned based on the size of the variable. A char of 1 byte can be allocated anywhere in memory like 0x5000 or 0x5001. And an int of 4 bytes, must start at a 4-byte boundary like 0x5004 or 0x5008. The structure padding is automatically done by the compiler to make sure all its members are byte aligned.The size of the below structure is 16 bytes due to structure padding:Struct dummy { Char ch; Int num; Double temp; }Here char is only 1 byte but after 3 byte padding, the number starts at 4 byte boundary. For int and double, it takes up 4 and 8 bytes respectively.The compiler used the 3 wasted bytes (marked in red) to pad the structure so that all the other members are byte aligned. Now, the size of the structure is 4 + 1 +3 = 8 bytes. The size of the entire structure is 8 bytes. On knowing the structured padding, it is easier to redesign or rewrite the structure.Lets look at another example where the syntax is slightly different;#includestdio.h Struct dummy { Int num; Double x; Float f; }; Struct dummy1 { Double x; Int num; Float f; }; Int main() { Printf(size:%d %d , sizeof(struct dummy ), sizeof(struct dummy1)); System(PAUSE); Return 0; }The output will be as shown below;Got a question for us? Please mention them in the comments section and we will get back to you.Related Posts:Pointers in CIntroduction to C Programming AlgorithmsRecommended blogs for you What is Debugging and Why is it important? Read Article Scrum Board: Everything You Need to Know Read Article Introduction to the World of Mobile Application Testing Read Article Vol. XIX â" Edureka Career Watch â" 24th Aug 2019 Read Article Elasticsearch Tutorial Power Up Your Searches Read Article 10 Hottest Tech Skills To Master In 2016 Read Article Implementing Hadoop R Analytic Skills in Banking Domain Read Article Vol. XII â" Edureka Career Watch â" 27th Apr. 2019 Read Article A One-Stop Guide to Learning from Home Read Article Top 10 Highest Paying Jobs Read Article Learning Is Freedom: Celebrating Independent Indians Read Article JMeter vs LoadRunner Battle of the Best Performance Testing Tool Read Article How To Implement Inline Function in C++? Re ad Article What is Static Member Function in C++? Read Article A Beginnerâs Guide To CRM Salesforce Read Article How to Get SAFe Certified? Read Article How to Implement Linear Search in C? Read Article How to convert Char to Int in Java? Read Article Why Edurekaâs Pedagogy results in a steep learning curve Read Article Understanding Structures and Padding in C Read Article Comments 0 Comments Trending Courses Python Certification Training for Data Scienc ...66k Enrolled LearnersWeekend/WeekdayLive Class Reviews 5 (26200)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.