Linux/C + + differences: type declaration, struct

Source: Internet
Author: User

C has arrays, structs, pointers, functions, macros

C + + has namespaces, references, default parameters, templates, function overloads, custom operators, inline, construct/destructor, private/protected Members, friends, exceptions.

I. Declaration of data types

1. C + + allows data declaration to appear anywhere in the program

C Code (Exception)

1#include <stdlib.h>2#include <stdio.h>3 4 intMainintargcChar*argv[])5 {6          for(intI=0; i<5; i++)7printf"Hello%d\n", i);8 9         return 0;Ten}

C + + code (normal)

1#include <iostream>2#include <cstdio>3 using namespacestd;4 5 intMainintargcChar*argv[])6 {7          for(intI=0; i<5; i++)8printf"Hello%d\n", i);9 Ten         return 0; One}

2. C + + allows entities to be defined using struct names

C Code (Exception)

1#include <stdlib.h>2#include <stdio.h>3#include <string.h>4 5 //You cannot define entities directly using struct names in C language6 struct Person7 {8         Charname[ -];9         intAge ;Ten }; One  A intMainintargcChar*argv[]) - { -         //struct person person; the person person ; -  -strcpy (Person.name,"Tom"); -Person.age =5; +  -printf"%s is%d\n", Person.name, person.age); +  A         return 0; at}

C + + code (normal)

1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 6 //You can define entities directly using struct names in C + +7 struct Person8 {       9         Charname[ -];Ten         intAge ; One }; A  - intMainintargcChar*argv[]) - {        the         // - person person ; -          -strcpy (Person.name,"Tom"); +Person.age =5; -          +printf"%s is%d\n", Person.name, person.age); A          at         return 0; -}

Second, struct

1. C + + allows functions to manipulate data members within a struct as a struct member declaration.

C Code (Exception)

1#include <stdlib.h>2#include <stdio.h>3#include <string.h>4 5 //a function in C that does not allow manipulation of data members within a struct, as a struct member declaration6 struct Person7 {8         Charname[ -];9         intAge ;Ten  One         // A         voidOutput () {printf ("%s is%d\n", name, age); } - }; -  the intMainintargcChar*argv[]) - { -         structperson person ; -  +strcpy (Person.name,"Tom"); -Person.age =5; +  A person.output (); at  -         return 0; -}

C + + code (normal)

1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 6 //a function in C + + that allows manipulation of data members within a struct as a struct member declaration7 struct Person8 {9         Charname[ -];Ten         intAge ; One  A         // -         voidOutput () {printf ("%s is%d\n", name, age); } - }; the  - intMainintargcChar*argv[]) - { -         // + person person ; -  +strcpy (Person.name,"Tom"); APerson.age =5; at  - person.output (); -  -         return 0; -}

Linux/C + + differences: type declaration, struct

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.