What is a diference between ((int) a) and (int(a))?
文章推薦指數: 80 %
c++ - int a = 0 and int a(0) differences - Stack Overflow
Resultsfromthe2022DeveloperSurveyarenowavailable
Home
Public
Questions
Tags
Users
Companies
Collectives
ExploreCollectives
Teams
StackOverflowforTeams
–Startcollaboratingandsharingorganizationalknowledge.
CreateafreeTeam
WhyTeams?
Teams
CreatefreeTeam
Collectives™onStackOverflow
Findcentralized,trustedcontentandcollaboratearoundthetechnologiesyouusemost.
Learnmore
Teams
Q&Aforwork
Connectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch.
Learnmore
Whatisadiferencebetween((int)a)and(int(a))?
AskQuestion
Asked
2years,5monthsago
Modified
3monthsago
Viewed
1ktimes
7
1
Whatisadifferencebetween((int)a)and(int(a))?
Isthesecondexpressionvalidinpure"С"(not"C"under"C++")?
c++ctype-conversion
Share
Improvethisquestion
Follow
editedMar7at22:01
NoSkill
askedJan19,2020at9:44
NoSkillNoSkill
57633silverbadges1313bronzebadges
12
Seehere:en.cppreference.com/w/cpp/language/explicit_cast
– BasinhetVeld
Jan19,2020at9:48
Related:stackoverflow.com/questions/5048921/…
– Evg
Jan19,2020at9:49
2
Agoodstartwhencheckingifsomethingisvalidin"pureC"istocompilewith-std=c11-pedantic-errors(oryourcompiler'sequivalent).Itwillgiveyouacorrectanswer99%ofthetime.
– StoryTeller-UnslanderMonica
Jan19,2020at9:50
4
@Evg-Notoffthetopofmyhead.Butcompilersdostillhaveconformancebugsattimes,hencemy1%errormargin.
– StoryTeller-UnslanderMonica
Jan19,2020at9:52
1
by"pureC"youmean"C"?
– M.M
Jan19,2020at10:15
|
Show7morecomments
2Answers
2
Sortedby:
Resettodefault
Highestscore(default)
Trending(recentvotescountmore)
Datemodified(newestfirst)
Datecreated(oldestfirst)
7
There'snodifferencebetweentheminC++.However,Csupportsonlythefirstcastoperation.
Seethisexamplefromtutorial:
doublex=10.3;
inty;
y=(int)x;//c-likecastnotation
y=int(x);//functionalnotation
Share
Improvethisanswer
Follow
editedJan19,2020at10:13
alk
68.4k1010goldbadges9393silverbadges238238bronzebadges
answeredJan19,2020at9:48
MohammedDeifallahMohammedDeifallah
1,27511goldbadge1010silverbadges2222bronzebadges
Addacomment
|
3
(type_name)identifier(ormorespecifically(type_name)cast_expression(6.5.4))isaC-stylecast.(int(a))issyntacticallyinvalidinCunlessaisatype.Thenitcouldbepartofacasttoafunctiontakingtypeaandreturningint,whichwouldbeasyntacticallyvalidbutsemanticallyinvalidcast,souselesstoo.int(a);inCwouldbeadeclarationequivalenttointa;.
C++doessupporttheint(a)syntaxforcasts(thetypenamemustbeasingleword;itdoesn'tworkwithe.g.,unsignedlong(a))onthegroundsthatint(thetypename)thenbecomeskindoflikeatypewithaparametrizedconstructor(althougheventhisisinC++groupedtogetherwithC-stylecastsasakindofadeprecatedwayofcasting,andthemorefine-grained/visiblestatic_cast/reinterpret_cast/const_castcastsarepreferred).
TheC++syntaxthenappearstobequiteinterestingbecausethisworks(C++):
typedefinttype_name;
type_name(a);//adeclaration
a=0;
printf("%d\n",type_name(a));//type_name(a)isacastexprhere
Share
Improvethisanswer
Follow
editedJan19,2020at16:08
answeredJan19,2020at10:23
PSkocikPSkocik
55.2k66goldbadges8888silverbadges132132bronzebadges
2
2
wouldn'tint(a)beaananonymousintobjectwithaasparameterfortheconstructor,exactlythesamewaystring("abc")isananonymousstringconstructedwithchar*"abc"argument)?
– Christophe
Jan19,2020at15:24
@ChristopheYes,thatdoesmakesabitmoresenseoverall.
– PSkocik
Jan19,2020at16:09
Addacomment
|
YourAnswer
ThanksforcontributingananswertoStackOverflow!Pleasebesuretoanswerthequestion.Providedetailsandshareyourresearch!Butavoid…Askingforhelp,clarification,orrespondingtootheranswers.Makingstatementsbasedonopinion;backthemupwithreferencesorpersonalexperience.Tolearnmore,seeourtipsonwritinggreatanswers.
Draftsaved
Draftdiscarded
Signuporlogin
SignupusingGoogle
SignupusingFacebook
SignupusingEmailandPassword
Submit
Postasaguest
Name
Email
Required,butnevershown
PostYourAnswer
Discard
Byclicking“PostYourAnswer”,youagreetoourtermsofservice,privacypolicyandcookiepolicy
Nottheansweryou'relookingfor?Browseotherquestionstaggedc++ctype-conversionoraskyourownquestion.
TheOverflowBlog
Askedandanswered:theresultsforthe2022Developersurveyarehere!
LivingontheEdgewithNetlify(Ep.456)
FeaturedonMeta
Testingnewtrafficmanagementtool
Upcomingcleanupofduplicatevotes
AskWizardTestResultsandNextSteps
Trending:Anewanswersortingoption
Updatedbuttonstylingforvotearrows:currentlyinA/Btesting
Linked
36
C++castsyntaxstyles
12
DifferentmeaningsofparenthesesinC++?
Related
2855
Whatisthedifferencebetween#include
延伸文章資訊
- 1C語言之int *f()、int(*f)()、int *a[]、int (*a)[] 區別小記 - IT人
int *a[] 是一個指標陣列,表示陣列中的每一個元素都是一個指標,可以指向一個int型別的數值。 int (*a)[] 這是一個指向多維陣列的指標變數。 例如: int ( ...
- 2C語言int a是什麼意思? 20 - 多學網
C語言inta是什麼意思,C語言int a是什麼意思? 20,1樓天空沒蜻int a指的是定義一個指抄向int型別數襲據的指標a,int a指的是定bai義一個整數變數dua ...
- 3What is a diference between ((int) a) and (int(a))?
c++ - int a = 0 and int a(0) differences - Stack Overflow
- 4int *a和(int *)a的区别_liebecl的博客
(int *)a将指针变量a强制转换为整型指针,说明a一般不是一个整型指针,也可以是个整型指针。int *a定义一个整型指针变量a。
- 5C語言int a 是什麼意思 - 迪克知識網
c語言inta 是什麼意思,C語言int a 是什麼意思,1樓非常可愛int a的時候,編譯器不會給a分配記憶體空間,因為它只是另一個變數的標記a是存放a的記憶體 ...