Hello,
I am writing a program where I have to take a string and add an input number(key) to each element of the string thus creating a sort of cipher. I have the line that does the addition to each element as
Code:
for(int i = 0; i < strlen(input); i++)
{
printf("%c", input[i] + (int)((key + 26) % 26));
}
This works as long as I have (int) type cast as shown above. But the function that implements the above code accepts the key as a long to accommodate for bigger integers. so if I just do not typecast to int and just do
Code:
for(int i = 0; i < strlen(input); i++)
{
printf("%c", input[i] + ((key + 26) % 26));
}
the compiler throws an error saying
error: format specifies type 'int' but the argument has type 'long' [-Werror,-Wformat]
[I] printf("%c", input + ((key + 26) % 26));
I do not follow:
1) Why I have to force the int typecast while a long is just an int with higher range.
2) Why does the error say that my format specifies int while I am clearly specifying %c and not %d?