the correct syntax for ''char'' type

This is a discussion on the correct syntax for ''char'' type within the C Programming forums, part of the General Programming Boards category; hi have to make a simple calculator for my prog course but the teacher didn't give the explanation about the ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    1

    Question the correct syntax for ''char'' type

    hi have to make a simple calculator for my prog course but the teacher didn't give the explanation about the char type

    so my prog will look like this
    Code:
     
    int main (void) 
    
    {
        
        float a,b,c;
        char operation[1];
       
        printf("Calculatrice de base\n entre les operation sous la forme a () b");
        printf("\n***************************");
        printf("\n + (addition)             *");
        printf("\n - (soustraction)         *");
        printf("\n * (multiplication)       *");
        printf("\n / (division)             *");
        printf("\n t (terminer le programme)*");
        printf("\n***************************\n");
           
        scanf("%f",&a);
        scanf("%s",operation);
        scanf("%s",b);
        
        if (operation=='+')
            printf("\n %f + %f = %f",a,b,(a+b));
        
        if (operation=='-')
           printf("\n %f - %f = %f",a,b,(a-b));
        
        if (operation=='*')
           printf("\n %f * %f = %f",a,b,(a*b));
        
    
        if (operation=='/')
           {if (b==0)
            printf("division pas 0 impossible !!!");
            else printf("\n %f / %f = %f",a,b,(a/b));
           }
        if (operation=='t')
            return 0;
        
            
        
        getch(); 
        return 0; 
    }

    EROR MESSAGE:
    [Warning] comparison between pointer and integer

    at each ''if''



    i don't want you to programm it but can you tell me the correct way to write the char declaration and if i can do a comparison with char like i did if yes, how to do it correctly

  2. #2
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    The char data type is a single byte, capable of holding one character in the local character set. Your implementation, however, represents a character array. Arrays are a series of elements (variables) of the same type placed consecutively in memory that can be individually referenced by adding an index to a unique name.

    That means that, for example, we can store 5 values of type char without having to declare 5 different variables each with a different identifier. Instead, using an array we can store 5 different values of the same type, char for example, with a unique identifier.
    Code:
    char item[5];
    
    Index Position
    item[0]  First
    item[1]  Second
    item[2]  Third
    item[3]  Fourth
    item[4]  Fifth
    These are numbered from 0 to 4 since in arrays the first index is always 0, independently of its length. Thus, your implementation should become clear why your compiler returns a warning:
    Code:
    if (operation=='+')
    Your character array operation is being checked against a single byte. Rather, you should keep the comparisions within the same boundaries:
    Code:
    char operation;
    scanf("%c", &operation);
    If you want to use an array, you can do the following while subscripting your arrays, [0], [1], etc...


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using c++ in c code
    By hannibar in forum C Programming
    Replies: 17
    Last Post: 10-28-2005, 09:09 PM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Cant find the error
    By Coder87C in forum C Programming
    Replies: 8
    Last Post: 06-19-2005, 01:57 AM

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