Thread: printing define value

  1. #1
    Registered User
    Join Date
    May 2021
    Posts
    66

    printing define value

    I have written three programs first two program are working as it supposed to do but i don't understand what's happening in last program

    Program 1

    Code:
     #include<stdio.h> 
    
    
    #define value   'A'
    
    
    int main()
    {
        printf(" %c \n", value );	
    	
    	return 0;
    }
    A

    Program 2
    Code:
     #include<stdio.h> 
    
    
    #define value   1
    
    
    int main()
    {
        printf(" %d \n", value );	
    	
    	return 0;
    }
    1


    Last program

    Code:
     #include<stdio.h> 
    
    #define value   "A"
    
    
    int main()
    {
        printf(" %c \n", value );	
    	
    	return 0;
    }
    d



    Why program print d instead of A? What does value store in a program A or d?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Maybe you can use %s instead of %c in the last case.

    Or learn to use your compiler.
    Code:
    $ gcc -Wall foo.c
    foo.c: In function ‘main’:
    foo.c:8:12: warning: format ‘%c’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat=]
         printf(" %c \n", value );
                ^
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2021
    Posts
    66
    Quote Originally Posted by Salem View Post
    Maybe you can use %s instead of %c in the last case.

    Or learn to use your compiler.
    Code:
    $ gcc -Wall foo.c
    foo.c: In function ‘main’:
    foo.c:8:12: warning: format ‘%c’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat=]
         printf(" %c \n", value );
                ^
    Really I made silly mistake I didn't notice this is happening because of the format specifier

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-23-2020, 12:03 PM
  2. Printing define variable
    By Jimmy589 in forum C Programming
    Replies: 4
    Last Post: 04-26-2017, 06:27 AM
  3. difference between #define and #define ()
    By bored_guy in forum C Programming
    Replies: 8
    Last Post: 07-20-2009, 06:58 PM
  4. Printing non-printing characters in ^ and M- notation
    By sbeard22 in forum C Programming
    Replies: 6
    Last Post: 10-03-2008, 11:12 PM
  5. #define
    By tikelele in forum C Programming
    Replies: 2
    Last Post: 12-01-2007, 11:43 PM

Tags for this Thread