-1

Below is the code where I am trying to print default values/values of char variable & pointer. But Can not able to see it on console.Is it has default values or just not able to read ASCII range.?

#include <stdio.h>
int main()
{
    char c, *cp;

    printf("\nValue of char c:%c\n", c);
    printf("\nValue of char ptr:%c\n", *cp);

    return 0;
}
L. F.
  • 19,445
  • 8
  • 48
  • 82
  • 2
    Since they are not initialized they can be anything. – NiVeR Aug 02 '18 at 07:21
  • 1
    The values are *"indeterminate"*. (variables with *automatic* storage type have indeterminate value if not initialized and in the alternative until a value is assigned) – David C. Rankin Aug 02 '18 at 07:29

5 Answers5

2

Surprise!! there is no "default" value here, you're venturing into undefined behavior.

To elaborate, if a variable is local scoped and automatic storage duration, unless initialized explicitly, the stored value is indeterminate. Using that further, causes undefined behavior.

Quoting C11, chapter §6.7.9

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.[....]

  • For the first case, where you have a char type, if in your case, which can have a trap representation, it'll lead to UB, otherwise, it'll be a random value.

  • For the second case, where you have a char * type, the pointer holds indeterminate value, which is invalid in context of your program, so attempt to dereference the pointer surely invokes undefined behavior.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

All automatic variables in C/C++ have undefined value if unintialized!

You are actually wandering into the dangerous region of 'Undefined Behavior' by using such uninitialized variables (all the more, by de-referencing the pointer).

Jay
  • 24,173
  • 25
  • 93
  • 141
1

First of all C and C++ are very different languages with often very different semantics.

However, this is one case where both are quite similar (even though there are differences between the specifications): Uninitialized local ("automatic") non-static variables will have an indeterminate value.

In C++ using them in any way except to initialize them leads to undefined behavior. Dereferencing an uninitialized pointer almost doubly so.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

The default value would be the value assigned by default initialization, which is 0 for an integer type (like char) and a nullpointer for any pointer type. So, your code would already be wrong here:

printf("\nValue of char ptr:%c\n", *cp);

because the nullpointer can't be dereferenced, it points explicitly nowhere. Dereferencing it leads to undefined behavior.

But, as you define these variables with automatic storage duration (the default in function scope) -- they don't get initialized at all unless you do it yourself. So, their initial value is just indeterminate. This means your first line

printf("\nValue of char c:%c\n", c);

prints some indeterminate value. Whether this is undefined behavior as well depends on whether your char is by default signed or unsigned. A signed char is allowed to have trap representations, so you could attempt to print something here that isn't a valid representation of a char -> undefined behavior.

Dereferencing some "random" pointer is undefined behavior as well (and in practice very likely to crash your program).

0

First of all, they are not initialised! assign a value to your variables.

For example:

char c = 's', *cp = %c;

printf("\nValue of char c:%c\n", c); 
printf("\nValue of char ptr:%c\n", *cp);
Alan
  • 589
  • 5
  • 29