What's the Difference Between C and C++

What's the difference between &C::c and &(C::c)?

C++ distinguishes two forms of operands to the & operator, lvalues in general and (qualified) identifiers specifically. In &C::c the operand of & is a qualified identifier (i.e. just a name) whereas in &(C::c) the operand is a general expression (because ( cannot be part of a name).

The qualified identifier form has a special case: If it refers to a non-static member of a class (like your C::c), & returns a special value known as a "pointer to member of C". See here for more information about member pointers.

In &(C::c) there is no special case. C::c is resolved normally and fails because there is no object to get a c member of. At least that's what happens in main; in methods of C (like your foo) there is an implicit this object, so C::c actually means this->c there.

As for why the output is different for printf vs. cout: When you try to print a member pointer with <<, it is implicitly converted to a bool, yielding false if it's a null pointer and true otherwise. false is printed as 0; true is printed as 1. Your member pointer is not null, so you get 1. This is different from normal pointers, which are implicitly converted to void * and printed as addresses, but member pointers cannot be converted to void * so the only applicable overload of operator<< is the one for bool. See https://en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt#Notes.

Note that technically your printf calls have undefined behavior. %p takes a void * and you're passing it pointers of different types. In normal function calls the automatic conversion from T * to void * would kick in, but printf is a variable-arguments function that provides no type context to its argument list, so you need a manual conversion:

printf("%p\n", static_cast<void *>(&(C::c)));

The relevant part of the standard is [expr.unary.op], saying:

The result of the unary & operator is a pointer to its operand.
The operand shall be an lvalue or a qualified-id. If the operand is a qualified-id naming a non-static or variant member m of some class C with type T, the result has type “pointer to member of class C of type T” and is a prvalue designating C​::​m.
Otherwise, if the type of the expression is T, the result has type “pointer to T” [...]

What are the major differences between C and C++ and when would you choose one over the other?

While C is a pure procedural language, C++ is a multi-paradigm language. It supports

  • Generic programming: Allowing to write code once, and use it with different data-structures.
  • Meta programming: Allowing to utilize templates to generate efficient code at compile time.
  • Inspection: Allows to inspect certain properties at compile time: What type does an expression have? How many parameters does a function have? What type does each one have?
  • Object oriented programming: Allowing the programmer to program object oriented, with sophisticated features such as multiple inheritance and private inheritance.
  • Procedural programming: Allows the programmer to put functions free of any classes. Combined with advanced features such as ADL allows writing clean code decoupled from specifics of certain classes.

Apart from those, C++ has largely kept compatibility with C code, but there are some differences. Those can be read about in Annex D of the C++ Standard, together with reasons and possible fixed to make C code valid C++ code.

What's the difference between %c and %C in printf?

From MSDN:

%c

type: int or wint_t 

When used with printf functions,
specifies a single-byte character;
when used with wprintf functions,
specifies a wide character.

%C

type: int or wint_t

When used with printf
functions, specifies a wide character;
when used with wprintf functions,
specifies a single-byte character.

more about format specifiers here

What is the difference between %c and %s in C

Passing wrong arguments to format specifiers is undefined behavior. Therefore you obtain such a weird output.

  1. "%s" expects a pointer to a null-terminated string (char*).

  2. "%c" expects a character (int). Surprised? Read this.

To print the nth character of name, use

printf(" %c \n", name[n]);

What's the difference between C and C++

Check out Stroustrup's FAQ here, specifically:

What is the difference between C and C++?

C++ is a direct descendant of C that
retains almost all of C as a subset.
C++ provides stronger type checking
than C and directly supports a wider
range of programming styles than C.
C++ is "a better C" in the sense that
it supports the styles of programming
done using C with better type checking
and more notational support (without
loss of efficiency). In the same
sense, ANSI C is a better C than K&R
C. In addition, C++ supports data
abstraction, object-oriented
programming, and generic programming
(see The C++ Programming Language (3rd
Edition)"; Appendix B discussing
compatibility issues is available for
downloading).

Difference between char in C and C++?

is char in C++ is integral type or strict char type ?

Character types, such as char, are integral types in C++.

The type of narrow character constant in C is int, while the type of narrow character literal in C++ is char.



Related Topics



Leave a reply



Submit