2

I am quite new to C and i am trying to use a variable twice in one line:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int myAge = 10;
    char PL = "C";
    printf("I am Rydex (not my real name) and i am %d years old. This was made using %s", myAge, PL);
}

But, when i run it, I get:

I am Rydex (not my real name) and i am 10 years old. This was made using (null)

Instead of the value in the variable "PL" i get "(null)". Can someone please help me?

6
  • 4
    Use a string instead of char Commented Aug 25, 2021 at 13:35
  • 1
    char PL might have to be const char * PL Commented Aug 25, 2021 at 13:35
  • 5
    Do you not get a compiler warning on char PL = "C"? Turn up the diagnostics! Commented Aug 25, 2021 at 13:38
  • 1
    You're not using a variable twice here... The mention of a variable being used twice does not make sense in your question. Commented Aug 25, 2021 at 13:40
  • 1
    Get in the habit now of paying attention to all the warnings that your compiler gives you. Whatever tool chain you are using to build your code, learn how to make it give you those warnings. The code you show should not compile cleanly; if you aren't seeing warnings, you need to investigate your tool chain to discover why. Commented Aug 25, 2021 at 13:43

5 Answers 5

5

PL is a string, not a char. You need to change the declaration to:

char* PL = "C";

The double-quoted delimiter is for a string constant, which has a span of characters terminated with a null character \0. A single quote delimiter is to define a single character constant. Here's a good write-up

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. It worked. I can't correct this comment yet because of the cooldown lol
2

You should use

char * PL = "C";

To declare PL as a pointer. You probably also want \n at the end of your format string.

I'm surprised your compiler didn't complain that you weren't declaring a pointer. Mine did. It didn't like the string "C" (which is 2 bytes) being declared as a single character (which is equivalent to a single byte integer)

test.c: In function ‘main’:
test.c:7:15: warning: initialization of ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversio ]
     char PL = "C";

Comments

0

Also, when you try to use %s in printf function, it expects type char* – a pointer to char. Try using %c instead with type char.

2 Comments

Using type char would also require changing "C" to 'C'.
Indeed it would, and for the benefit of the OP, that would set PL to a single character (1 byte), instead of a null terminated string, and the %c is the correct format code for printing a single byte, vs. %s which expects a pointer to a null-terminated string
0

There are two ways to fix this:

  1. Change the declaration of PL to
    char *PL = "C";
    or
    char PL[] = "C";
    The %s format specifier expects its corresponding to have type char * - in other words, it expects PL to be the address of the first character of a string. The string "C" is actually an array of char containing the sequence {'C', 0}. Under most circumstances, an expression of array type is converted, or "decays", to a pointer type and the value of the expression is the address of the first element of the array.
  2. Change the declaration of PL to
    int PL = 'C'; // single quotes, not double
    and use %c instead of %s in the printf call:
    printf("I am Rydex (not my real name) and i am %d years old. This was made using %c", myAge, PL);
    'C' is a character constant, not a string. The %c format specifier expects its corresponding argument to have type int. In this case, PL stores the character code for 'C' (67 in ASCII and UTF-8).

3 Comments

int PL = 'C'; why not char PL = 'C'; ?
@4386427: Character constants in C have type int, and the %c format specifier actually expects an int.
printf handles that for you... you don't need int to store a single character
0

Use char *PL = "C" instead of char PL = "C". Notice the asterisk. That is called a pointer, but that's a topic for another time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.