Using modulus operator on a char in C [duplicate] - c

This question already has answers here:
Simple Caesar shift in C
(2 answers)
Closed 6 years ago.
I am trying to mod a char in c by using the following:
int shift = 1;
c = (c + shift ) % 26;
printf("c= %c \n",c);
The variable c is a lowercase english character between a-z. When I try to print out the char c using the above code, I get the following:
c=
Could someone please tell me what I am doing wrong?

Shift it by 'a' or 'A'. Then you'll always get a character that's part of the alphabet:
int main(){
unsigned i;
for(i=0;;i++)
printf("i=%c\n", 'a'+i%26);
}
prints:
i=a
i=b
i=c
i=d
i=e
i=f
i=g
i=h
i=i
i=j
...

Related

Need help implementing total random characters in C [duplicate]

This question already has answers here:
Why do I always get the same sequence of random numbers with rand()?
(12 answers)
Generating a random bit - lack of randomness in C rand()
(4 answers)
Closed 3 years ago.
I am creating a program that prints out 1000 random occurrences of the letters "H" and "T" and my code implements rand()%2 but as i can see from running this program (code below) that its not completely random (the letters are random but always the same with every execution). I want to establish a more effective way by implementing RANDMAX to make every case of the program running completely random, how would I be able to do this?
#include <stdio.h>
#include<stdlib.h>
int main()
{
int i=1,n;
char ch ;
for( i = 1; i <= 1000 ; i ++ )
{
n = rand()%2;
if(n==0)
ch = 'T';
else
ch = 'H';
putchar(ch);
}
return 0;
}
Just add
srand(time(NULL));
after
int main()
{
That will initialize the random generate with a new seed every time you run the program. In this way, you'll get a new random sequence every time.

printing the char variable value using %d [duplicate]

This question already has answers here:
C/C++ unsigned integer overflow
(4 answers)
Closed 4 years ago.
I was trying to get into c programming, but in a question a get stuck, please explain this.
int main()
{
char c = 255;
c=c+10;
printf("%d",c);
return 0;
}
the output it gave is
> 9
kindly explain this to me.
The maximum value of a char is 255.
By adding 10 to that number you get 265.
Because that value is not a suitable value for a char it will do 265 % 256 resulting 9
That's why your result is 9

What happens when we convert string literal to integer in C? [duplicate]

This question already has answers here:
Multiple characters in a character constant
(3 answers)
Closed 6 years ago.
Consider a C program:
#include <stdio.h>
int main (void)
{
int x = 'a';
printf("%d", x);
}
Here the output is 97 as per the ASCII value table.
But in the example below:
#include <stdio.h>
int main(void)
{
int x ='aa';
printf("%d", x);
}
The output is 24929.
Can anyone please explain how the literal has been converted to this integer value?
int x ='aa';
This is valid but value of x is implementation defined. And btw, this is not a string literal. String literal would be this "aa".
You assigned a value to the int using octets : 'a' is 0x61.
So writing int x = 'aa' is like writing int x = 0x6161.
Edit: but do not write that. Just write int x = 0x6161 or int x = 24929.

Trying to understand this printf formatting output [duplicate]

This question already has answers here:
Why does printf print wrong values?
(7 answers)
Closed 7 years ago.
I don't understand why this code produces the below output:
#include <stdio.h>
#define LOWER 0
#define UPPER 300
#define STEP 20
float c_to_f(float c);
int main(void) {
for (int c = LOWER; c <= UPPER; c += STEP) {
printf("%3.0f %6.1f\n", c, c_to_f(c));
}
return 0;
}
float c_to_f(float c) {
return c * (9.0/5.0) + 32;
}
with output:
0 5144477247317086170901765440027035767837163293591161256351693248184965237877467107389389528872273154691913581744607058050215827488351921876414407003384176234234181468372580859505320314312544948225387164490993094256968227227818959640206687395851530141696.0
0 5144477248223936133773425621301895611375254354002848388186656757998856300277376171732587490599187934010444965285645525890922748188992798901374640654313592657351174354641522290319226294263523595109393871854132336451448805097328901373303486131449817464832.0
0 5144477248704033172940775129035644940307184915397270987393402145547386862724386852855456999748731052473196874219136479453649941501096792620471234940099754293118994117960373636044235342472865231695044481634617817613232640440010635232001791344413616635904.0
0 5144477249104114038913566385480436047750460383225956486732356635171162331430229087124514924040016984525490131663712274089255935927850120719718396844921555656258843920726083090815076215980649928849753323118355718581385836558912080114250379021883449278464.0
0 5144477249344162558497241139347310712216425663923167786335729328945427612653734427685949678614788543756866086130457750870619532583902117579266693987814636474142753802385508763677580740085320747142578628008598459162277754230252947043599531628365348864000.0
0 5144477249584211078080915893214185376682390944620379085939102022719692893877239768247384433189560102988242040597203227651983129239954114438814991130707717292026663684044934436540085264189991565435403932898841199743169671901593813972948684234847248449536.0
I do understand that c should be a float, just not the output. Any help would be greatly appreciated.
The problem here, as I see it is
printf("%3.0f %6.1f\n", c, c_to_f(c));
c is an int and you're trying to print it's value using %f, which is undefined behavior.
Using an inappropriate type of argument for a format specifier is undefined behavior.
When using printf and floats you have the right token %f. But your using it wrong. To printf out 2 decimal places you would use %.2f, 3 would be %.3f and so on. I think the console is bugging out because you are using %3.0f and %6.1f

Program doesn't show expected output [duplicate]

This question already has answers here:
Printing array elements
(6 answers)
Closed 9 years ago.
The expected output of the following C program is to print the elements in the array. But when actually run, it doesn't do so.
#include<stdio.h>
#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {23,34,12,17,204,99,16};
int main()
{
int d;
for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
printf("%d\n",array[d+1]);//printing the array
return 0;
}//looks simple but no result
What's going wrong? Why am I not getting any output?
In the comparison
d <= (TOTAL_ELEMENTS-2)
TOTAL_ELEMENTS has type size_t so d is converted to unsigned. For, say, sizeof(size_t)==4, this makes the test
0xffffffff < 5
which fails, causing the loop to exit.
If you really want to start your loop counter from -1
d <= (int)(TOTAL_ELEMENTS-2)
would work

Resources