Click here to Skip to main content
15,896,426 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[])
{
int counter;
char car[3]={'a','b','c',};
counter = 0; 
 while(counter < 3)  
{

   printf("%d\n",car[counter]); 
   counter++; 
    } 
 
 

	
	return 0;

What I have tried:

i have tried changing the values and trying different codes but nothing comes up it just gives me ramdon numbers on the output which i find annoying cuz the values are ABC
Posted
Updated 30-Nov-17 8:05am

Change from
Quote:
printf("%d\n",car[counter]);
to
printf("%c\n",car[counter]);
Have a look at printf documentation[^].

By the way, the numbers you get are not random ones. They are the ASCII codes[^] corresponding to the characters 'a', 'b', 'c'.
 
Share this answer
 
v2
Because you are using the "%d" format specifier - which says "print me an integer number". The number it is printing is the ASCII representation of each character.
Try this:
C++
printf("%c\n",car[counter]);
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900