The first day of C + + classes, preface: Structure variable declaration, instantiation and output

Source: Internet
Author: User

Today, the first day of the class, will not regularly update the basic content of C + +, I hope you can enjoy a lot, if you have good suggestions, please leave me a message.

Today's topic: struct variable declaration, instantiation, and output

The code is as follows:

1#include <stdio.h>2#include <string.h>3#include <stdlib.h>4 5 structpeople{6     Charname[Ten];7     intAge ;8};//defines the structure body that contains the name and age fields People9 Ten intMain () { One     structPeople mypeople = {"Luo Shuai", -};//Create a People instance A      -printf"%s\n","information for instance variable mypeople:"); -      the     Charmynameintro[ the] ="Name property value:"; -strcat (Mynameintro,mypeople.name);//Combining Strings -printf"%s\n", Mynameintro);//Output Name field information -      +     Charmyageintro[ the] ="Age Property Value:"; -     Charmyagestring[5] =""; +Itoa (Mypeople.age,myagestring,Ten);//converting an integral type to a string Astrcat (myageintro,myagestring);//Combining Strings atprintf"%s\n", Myageintro);//Output Age Field information -  -     return 0; -}

Here we need to explain the syntax of several functions:

The printf () function syntax used to format the output (reference stdio.h): int __cdecl printf (const char * __restrict__ _format,...);

The parameter format indicates the specific format of the output to be formatted, followed by ... Represents the content parameter of the output.

In the process of output, the use of the integer to string and string splicing technical points, but also to tell you how to use:

string concatenation to use the strcat () function syntax (reference string.h): char * __cdecl strcat (char * __restrict__ _dest,const char * __restrict__ _source);

For example, there are strings A and B, we need to append B to a, just call it like this:

Strcat (A, b);

So B is appended to the A, when we output a can get the desired value.

The itoa () function syntax to use for integer-to-string (reference stdlib.h): char *__cdecl itoa (int _val,char *_dstbuf,int _radix);

The parameter Val represents an integer variable to be converted to a string, the parameter dstbuf represents a string variable that stores the result of the transformation, and the parameter radix represents the conversion's binary.

Hope to be helpful to the novice, oneself is also the first time to start learning. If there is an omission of the place I hope you can give me a message, thank you attention!

The first day of C + + classes, preface: Structure variable declaration, instantiation and output

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.