7

What is a difference between ((int) a) and (int(a))?

Is the second expression valid in pure "С" (not "C" under "C++")?

NoSkill
  • 718
  • 5
  • 15
  • See here: https://en.cppreference.com/w/cpp/language/explicit_cast – Bas in het Veld Jan 19 '20 at 09:48
  • Related: https://stackoverflow.com/questions/5048921/different-meanings-of-parentheses-in-c – Evg Jan 19 '20 at 09:49
  • 2
    A good start when checking if something is valid in "pure C" is to compile with `-std=c11 -pedantic-errors` (or your compiler's equivalent). It will give you a correct answer 99% of the time. – StoryTeller - Unslander Monica Jan 19 '20 at 09:50
  • @StoryTeller-UnslanderMonica, could you give an example of 1%, please? – Evg Jan 19 '20 at 09:52
  • 4
    @Evg - Not off the top of my head. But compilers do still have conformance bugs at times, hence my 1% error margin. – StoryTeller - Unslander Monica Jan 19 '20 at 09:52
  • 1
    by "pure C" you mean "C" ? – M.M Jan 19 '20 at 10:15
  • @M.M The word is used to contrast it with "polluted C", AKA C++. – Petr Skocik Jan 19 '20 at 10:31
  • @M.M I mean "C"-program compiled with "C"-compiler and not "C" under "CPP"-compiler. ;) – NoSkill Jan 19 '20 at 10:31
  • If you're using C++, consider using modern c++ conversions: https://en.cppreference.com/w/cpp/language/expressions static_cast converts one type to another related type dynamic_cast converts within inheritance hierarchies const_cast adds or removes cv qualifiers reinterpret_cast converts type to unrelated type – Eyal D Jan 19 '20 at 11:46
  • Rather than add a comment to each of the two correct answers, I'll just mention here that `int(a)` is a **function-style cast**. It was added to C++ in order to be able to invoke constructors to create temporary objects. When a class type has a constructor that takes multiple arguments you can create a temporary object with `my_type(arg1, arg2)` or whatever. The function-style cast is just a simpler context for the same thing. – Pete Becker Jan 19 '20 at 17:06
  • there's no such thing as `"C" under "C++"`. C and C++ are very different languages and when compiling C code even a C++ compiler must follow C rules – phuclv Mar 08 '22 at 00:50
  • Does this answer your question? [C++ cast syntax styles](https://stackoverflow.com/questions/32168/c-cast-syntax-styles) – phuclv Mar 08 '22 at 00:51

2 Answers2

7

There's no difference between them in C++. However, C supports only the first cast operation.

See this example from tutorial:

double x = 10.3;
int y;
y = (int) x;    // c-like cast notation 
y = int (x);    // functional notation
alk
  • 69,737
  • 10
  • 105
  • 255
Mohammed Deifallah
  • 1,290
  • 1
  • 10
  • 25
3

(type_name)identifier (or more specifically (type_name)cast_expression (6.5.4)) is a C-style cast. (int(a)) is syntactically invalid in C unless a is a type. Then it could be part of a cast to a function taking type a and returning int, which would be a syntactically valid but semantically invalid cast, so useless too. int(a); in C would be a declaration equivalent to int a;.

C++ does support the int(a) syntax for casts (the type name must be a single word; it doesn't work with e.g., unsigned long(a)) on the grounds that int (the type name) then becomes kind of like a type with a parametrized constructor (although even this is in C++ grouped together with C-style casts as a kind of a deprecated way of casting, and the more fine-grained/visible static_cast/reinterpret_cast/const_cast casts are preferred).

The C++ syntax then appears to be quite interesting because this works (C++):

typedef int type_name;
type_name (a); //a declaration
a=0;
printf("%d\n", type_name(a)); //type_name(a) is a cast expr here
Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
  • 2
    wouldn't int(a) be a an anonymous int object with a as parameter for the constructor, exactly the same way string("abc") is an anonymous string constructed with char* "abc" argument) ? – Christophe Jan 19 '20 at 15:24
  • @Christophe Yes, that does makes a bit more sense overall. – Petr Skocik Jan 19 '20 at 16:09