0

I had made this code that iterate through the alphabets using their ascii code

#include <stdio.h>

int main() {
    for ( int alphabet = (int) char A = 'A'; alphabet <= (int) char Z = 'Z'; alphabet++) {
        printf("The number of the Alphabet %c is %d\n ",(char) alphabet , alphabet  );
    }
}

but upon compiling it just say that it is expected to have an expression before char A or char B which I don't really understand what does that mean so any help will be appreciated xD

Alex Lop.
  • 6,810
  • 1
  • 26
  • 45
Susano
  • 230
  • 2
  • 9
  • 1
    Your syntax is incorrect. Perhaps you meant `for ( int alphabet = 'A'; alphabet <= 'Z'; alphabet++)` – Retired Ninja Jul 15 '20 at 06:34
  • 1
    You cannot assign two declarations + you cannot declare a variable inside the `for` loop condition block. This is the proper way: `for (int alphabet = 'A'; alphabet <= 'Z'; alphabet++) {` – Alex Lop. Jul 15 '20 at 06:34
  • But I thought that I need to typecast the 'A' character before assigning it to the alphabet variable... How it can just treat it as integer if it's not ? – Susano Jul 15 '20 at 06:38
  • @MahmoudSalah, google for *c type casting* - https://www.guru99.com/c-type-casting.html – Alex Lop. Jul 15 '20 at 06:40
  • 2
    A character literal like `'A'` is an int. https://stackoverflow.com/questions/433895/why-are-c-character-literals-ints-instead-of-chars Even if it wasn't you wouldn't need the cast. https://en.cppreference.com/w/c/language/conversion – Retired Ninja Jul 15 '20 at 06:41
  • @RetiredNinja so to insure that my understanding is right.... just the c language that treats the char as int numbers ? so if i would to make the same program in c++ i would need to actually typecasting it before making that assignment to the variable alphabet right ? – Susano Jul 15 '20 at 06:46

1 Answers1

2

You don't need to define identifiers like (int) char A = 'A' to represent characters.

Just simply do:

for (int alphabet = 'A'; alphabet <= 'Z'; alphabet++)
    printf("The number of the Alphabet %c is %d\n", alphabet, alphabet);

Notice that you don't need to use (char) in the printf(), it automatically converts into the character from the integer. Also, you don't need to use curly-braces for single syntax in loops & conditions, it's recommended to do to avoid confusions their ending scopes.

Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
  • so to insure that my understanding is right.... just the c language that treats the char as int numbers ? so if i would to make the same program in c++ i would need to actually typecasting it before making that assignment to the variable alphabet right ? – Susano Jul 15 '20 at 06:50
  • 1
    @MahmoudSalah the chars are actually integers in ASCII type. Suppose, the char 'A', it's equal to integer 65 in ASCII. You still don't need to typecast explicitly in C++ too. – Rohan Bari Jul 15 '20 at 06:51
  • "Also, you don't need to use curly-braces for single syntax in loops & conditions." - but it is recommended and in most coding conventions it is even required. – Alex Lop. Jul 15 '20 at 06:56
  • 1
    @MahmoudSalah a `char` is a type of integer, just like `int`, `long`, or the fixed-width types `uint8_t`, `int16_t` etc. It is however a little bit special, in that the signedness is implementation defined, which means that it might be signed on one platform but unsigned on another. – Felix G Jul 15 '20 at 07:47
  • @RohanBari: In C, characters are encoded as integers, but not necessarily ASCII. Many C implementations use ASCII, but you should not teach people to assume that. – Eric Postpischil Jul 15 '20 at 10:14