-8

I was trying to make a program which was to much big for a noob like. it was approximatly 100 line(for noob like me,its big).But every time i run its loops aren't ending.So dicided to compile and check small parts of the code and I get bingo in fist part.here is that part

#include <stdio.h>
int main(){//B main
    int cha,z; 
    printf("enter the alphabte in capital until you want the alphabats :-  ");
    scanf("%c",&z);
    for(;z>=65;z--)
        for(cha=65;cha<=z;cha++)
        {  
            if(cha!=z)
                printf("%c ",cha);
            else
                printf("%c \n",cha);
        }
}//B main

.I have modified it it and if i enter c it should give an output like this:-

A B C
A B
A

But the loop does not ends and i keep getting something like this on terminal

              ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~  � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �        

thus try this part

 #include <stdio.h>
 int main(){
     int a;
     scanf("%d",&a);
     printf("%d",a);
 }

If I enter A then it should give me 65 as an output as A's ASCII value is 65 but it gives me following output

32766

but my this program give me correct ASCII values

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

  int main(){
      int b,c;
      b=0;
      printf("\n these are ASCII values of all letters \n\n\n ");
      while(b<=255)
      {
           printf("%c %d \n",b,b);
           b=b+1;
      }
 }

How can I use characters like a A G j as integers with there ASCII values?

How can I do arithmetic operations with their ASCII values?

3 Answers3

1

First of all, your second code should be:

#include <stdio.h>

int main(){
       char a;
       scanf("%c", &a); // read in single character value
       printf("%d",a); 
 }

Some other notes:

  • You can use literal chars instead of the values in your code:
    if (char == 'A'){ printf("char is 'A'");}
  • You can add and subtract like normal values:
    printf("%d", 'C' - 'A'); prints 2
  • I just ran your first code snippet. It works fine for me.

This is the output:

$ ./a.exe
enter the alphabte in capital until you want the alphabats :-  C
A B C
A B
A
Baldrickk
  • 4,291
  • 1
  • 15
  • 27
Rafael Coelho
  • 166
  • 3
  • 10
0

In your example, change it to use %c:

#include <stdio.h>
int main()
{
  char a;
  scanf("%c",&a);
  printf("%d",a);
}

Put in 'A' and it prints 65 as expected.

Now just expand that for your code. Oh and check the return value of scanf. I didn't here because I wanted to keep the example simple, but always do it in actual code.

Baldrickk
  • 4,291
  • 1
  • 15
  • 27
  • His question is: "how can i use characters like `A` `G` and use them as integers with their ASCII values ?". – Tony Tannous Jun 19 '17 at 12:32
  • the integer value will be stored in `a`. If 'A' is provided, `a` will have the value `65` and it can be used as if it were an int in sums. – Baldrickk Jun 19 '17 at 12:34
  • 2
    @AjayBrahmakshatriya *there is no promotion since `printf` doesn't have a type for the second argument* No. See **6.5.2.2 Function calls** of [the C Standard](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf): "The ellipsis notation in a function prototype declarator causes argument type conversion to stop after the last declared parameter. **The default argument promotions are performed on trailing arguments**." – Andrew Henle Jun 19 '17 at 12:42
  • @AjayBrahmakshatriya so it gets promoted. where is the problem? – Baldrickk Jun 19 '17 at 12:51
  • @AndrewHenle you are correct. They will be promoted. I will remove my comments. – Ajay Brahmakshatriya Jun 19 '17 at 13:02
  • @Baldrickk please my question again.I had edited it.and why my ASCII finder program is working?please.i am waiting for your answer.and thanks for answering.I really appreciate that. –  Jun 19 '17 at 13:05
0

Provide your own set of valid characters.
Use strchr to see if the input is in the set of valid characters. If not, try again for correct input.
Once correct input is obtained, use strchr again to make sure the incremented and decremented values remain valid.

#include <stdio.h>
#include <string.h>

int main( void) {
    char valid[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char z = valid[0];
    char cha = valid[0];
    char *ok = NULL;
    char *chaok = NULL;

    printf("enter the alphabte in capital until you want the alphabats :-  ");
    do {
        if ( 1 != scanf(" %c",&z)) {
            fprintf ( stderr, "problem with input\n");
            return 0;
        }
        ok = strchr ( valid, z);
        if ( !ok) {
            printf ( "enter an uppercase letter. try again\n");
        }
    } while ( !ok);

    while ( ( ok = strchr ( valid, z)) && ok >= valid) {
        cha = valid[0];
        while ( ( chaok = strchr ( valid, cha)) && chaok <= ok) {
            if( chaok != ok)
                printf("%c ",cha);
            else
                printf("%c \n",cha);
            cha = *(++chaok);
        }
        z = 0x1;
        if ( ok > valid) {
            z = *(ok - 1);
        }
    }
}
xing
  • 2,125
  • 2
  • 14
  • 10