C program error (need help!)

This is a discussion on C program error (need help!) within the C Programming forums, part of the General Programming Boards category; I have started learning C a couple of weeks ago, but I have the following question. Any 'char' variables can ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    20

    Exclamation C program error (need help!)

    I have started learning C a couple of weeks ago, but I have the following question. Any 'char' variables can hold string values and numeric valued from -128 to 127. If it is unsigned it can hold numeric values from 0 to 255. So I want to make a program were you type a character, and the program returns the character but in ASCII number. Here is the code from that program, but once I compile it it gives me an error (compiler error):

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

    void main()
    {
    unsigned char c; //character
    int n; //number

    printf("Input a character:");
    scanf("%c",c);
    n=atoi(c);
    printf("Its ASCII code is: %i\n",n);
    }


    Can anyone help me out and tell me where the error in the program is. Thanks.
    Last edited by James00; 04-03-2003 at 05:39 PM.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    firstly main always returns an int. anything else is undefined.
    Other than that remove the atoi statement and add...

    n = c;
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    20
    Thanks!!! Anyway, why don't I need the 'atoi' statement? I have a 'char' and I want to convert it to a 'int'. Could you please explain why I don't need the 'atoi' in this program. Thanks.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    a char is different to a string. atoi is cstrings to ints. a char can be promoted to an int so assignment is valid.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    18
    I wrote a code to try to understand char/string/int:

    #include<stdio.h>

    main()
    {

    /* int c_A,c_a,c_return; */
    char c_A, c_a, c_return; /* characters - each 1 byte */

    int i;
    char c[10]; /* character array - each element has 1 byte*/
    /* or can we call it a string? */

    c_A='A';
    c_a='a';
    c_return='\n';

    c[0]='0'; c[1]='1'; c[2]='2';
    c[3]='3'; c[4]='4'; c[5]='5';
    c[6]='6'; c[7]='7'; c[8]='8';
    c[9]='9';

    for(i=0; i<10; i++) /* first c[i] is promoted to int */
    printf("int: %d char: %c \n", c[i], c[i]);

    printf("c= %s \n", c); /* also print c as a string */
    printf("\n \n");

    printf("c_A= %d \n", c_A); /* char promoted to int */
    printf("c_a= %d \n", c_a);
    printf("c_return= %d \n", c_return);
    printf("\n");
    printf("c_A repr: %c \n", c_A); /* char as char */
    printf("c_a repr: %c \n", c_a);
    printf("c_return repr: %c \n", c_return);

    return 0;
    }

    result:

    int: 48 char: 0
    int: 49 char: 1
    int: 50 char: 2
    int: 51 char: 3
    int: 52 char: 4
    int: 53 char: 5
    int: 54 char: 6
    int: 55 char: 7
    int: 56 char: 8
    int: 57 char: 9
    c= 0123456789


    c_A= 65
    c_a= 97
    c_return= 10

    c_A repr: A
    c_a repr: a
    c_return repr:


    Then I use instead:

    int i;
    char c[]="0123456789";
    ...

    to assign values to c and got the same output. Thus,
    is an array of characters such as c[10] defined above also a string? If it's true, then a string is an array holding characters (one char per element), and the value of c[i], i=0,9 can be promoted to be an integer?

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    ylzhang: Edit your post and add code tags
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    18
    Just to test how to use code tags:

    Code:
    #include<stdio.h>
    main()
    {
       int i;
       int j;
       i=0; j=1;
             printf("%d %d \n",
                         i,j);
       return 0;
    }
    How does the "quota tags" work? I saw people using that -- but don't know how it is done.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    18
    Code reposted:

    Code:
    #include<stdio.h>
    
    main()
    { 
    
    /* int c_A,c_a,c_return; */
    char c_A, c_a, c_return; /* characters - each 1 byte */
    
    int i;
    char c[10]; /* character array - each element has 1 byte*/
    /* or can we call it a string? */
    
    c_A='A';
    c_a='a';
    c_return='\n';
    
    c[0]='0'; c[1]='1'; c[2]='2';
    c[3]='3'; c[4]='4'; c[5]='5';
    c[6]='6'; c[7]='7'; c[8]='8';
    c[9]='9'; 
    
    for(i=0; i<10; i++) /* first c[i] is promoted to int */
    printf("int: %d char: %c \n", c[i], c[i]);
    
    printf("c= %s \n", c); /* also print c as a string */
    printf("\n \n");
    
    printf("c_A= %d \n", c_A); /* char promoted to int */
    printf("c_a= %d \n", c_a);
    printf("c_return= %d \n", c_return); 
    printf("\n");
    printf("c_A repr: %c \n", c_A); /* char as char */
    printf("c_a repr: %c \n", c_a);
    printf("c_return repr: %c \n", c_return);
    
    return 0;
    }

    result:

    int: 48 char: 0
    int: 49 char: 1
    int: 50 char: 2
    int: 51 char: 3
    int: 52 char: 4
    int: 53 char: 5
    int: 54 char: 6
    int: 55 char: 7
    int: 56 char: 8
    int: 57 char: 9
    c= 0123456789


    c_A= 65
    c_a= 97
    c_return= 10

    c_A repr: A
    c_a repr: a
    c_return repr:


    Then I use instead:

    int i;
    char c[]="0123456789";
    ...

    to assign values to c and got the same output. Thus,
    is an array of characters such as c[10] defined above also a string? If it's true, then a string is an array holding characters (one char per element), and the value of c[i], i=0,9 can be promoted to be an integer?

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    18
    Sorry... the tab key accidetally submits the last post....

    Code:
    #include<stdio.h>
    
    main()
    { 
    
           /* int c_A,c_a,c_return; */
            char c_A, c_a, c_return;        /* characters - each 1 byte */
    
            int i;
            char c[10];     /* character array - each element has 1 byte*/
                                      /* or can we call it a string? */
    
            c_A='A';
            c_a='a';
            c_return='\n';
    
             c[0]='0'; c[1]='1'; c[2]='2';
             c[3]='3'; c[4]='4'; c[5]='5';
             c[6]='6'; c[7]='7'; c[8]='8';
             c[9]='9'; 
    
                   for(i=0; i<10; i++)        /* first c[i] is promoted to int */
                          printf("int: %d char: %c \n", c[i], c[i]);
    
              printf("c= %s \n", c);           /* also print c as a string */
              printf("\n \n");
    
             printf("c_A= %d \n", c_A);      /* char promoted to int */
             printf("c_a= %d \n", c_a);
             printf("c_return= %d \n", c_return); 
    
             printf("\n");
             printf("c_A repr: %c \n", c_A); /* char as char */
             printf("c_a repr: %c \n", c_a);
             printf("c_return repr: %c \n", c_return);
    
              return 0;
    }

    result:

    int: 48 char: 0
    int: 49 char: 1
    int: 50 char: 2
    int: 51 char: 3
    int: 52 char: 4
    int: 53 char: 5
    int: 54 char: 6
    int: 55 char: 7
    int: 56 char: 8
    int: 57 char: 9
    c= 0123456789


    c_A= 65
    c_a= 97
    c_return= 10

    c_A repr: A
    c_a repr: a
    c_return repr:


    Then I use instead:

    int i;
    char c[]="0123456789";
    ...

    [quota]
    to assign values to c and got the same output. Thus,
    is an array of characters such as c[10] defined above also a string? If it's true, then a string is an array holding characters (one char per element), and the value of c[i], i=0,9 can be promoted to be an integer?
    [/quota]

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>How does the "quota tags" work?
    That's quote, not quota.

    >>the tab key accidetally submits the last post.
    As a registered member, you can "edit" your own posts. Just click the edit button on the particular post.

    >>character array - each element has 1 byte
    >>or can we call it a string?
    It's only a string if it is terminated with a \0. In your case, it isn't, you have 10 bytes holding 0-9, which makes this statement incorrect:
    >>printf("c= %s \n", c);


    To learn more about obtaining numbers from the user, read this
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21