Thread: A beginner level problem with char datatype

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    9

    A beginner level problem with char datatype

    Hi all,
    I am a new user of c language and I have written a program which just first stores the char values in memory then it reads and prints. Following is the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    int main()
    {
    	
    	int n1,i;
    	int buff_size = 26;
    	char * y;
    
    	y = (char *)malloc(sizeof(char)*buff_size);
    	if (y == NULL) {
    	fprintf(stderr, "Malloc returned void pointer\n");
    	exit(EXIT_FAILURE);
        }
    	for (i=0; i<buff_size; i++){
    		y[i] = (char)(i);
    		printf('value stored  is : %c \n' ,  y[i]);
    	}	
    
        return 0;
    }
    This code is not compiling giving the following error message:
    Code:
    error C2015: too many characters in constant  (at the following  line) 
    
    printf('value stored  is : %c \n' ,  y[i]);
    what could be the problem??

    Thanks

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    		printf('value stored  is : %c \n' ,  y[i]);
    Those should be "double quotes", not 'single quotes'.

    You may also want to replace %c with %d since 0-26 are not printable characters and may screw up your console -- but try it both ways and see.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Code:
    printf("value stored  is : %c \n" ,  y[i]);
    Devoted my life to programming...

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    y = (char *)malloc(sizeof(char)*buff_size);
    When using C, avoid typecasting the return value of the malloc call. Also, sizeof(char) is guaranteed to always be 1, thus the above could be safely reduced to:
    Code:
    y = malloc(buff_size);
    You should also free the memory you've allocated to y at the end of your program (before the return statement):
    Code:
    free(y);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The UNIX System Interface
    By rrc55 in forum C Programming
    Replies: 1
    Last Post: 10-20-2009, 05:56 PM
  2. Char Help! "Packing " bits to a signle unsigned char
    By xxrexdartxx in forum C Programming
    Replies: 7
    Last Post: 10-11-2009, 04:45 AM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  4. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 02:06 PM
  5. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM

Tags for this Thread