Thread: I/O Program not printing variable name

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171

    Question I/O Program not printing variable name

    Hi! I am new to the C programming language and have only programmed in C++ and Java before.

    And I don't really know how to fix this error that probably is really simple to fix. The problem is that when I input my name and age the program will only print the age and not my name.

    Code:
    #include <stdio.h>
    
    
    int main() {
    	
    	char name[] = "";
    	printf("State your name: "); 
    	scanf("%s", name); 
    
    
    	short age = 0;
    	printf("Age: ");
    	scanf("%d", &age); 
    	
    	printf("Hello %s you are %d years old", name, age);
    	
    	return 0;
    }

  2. #2
    Banned
    Join Date
    Aug 2017
    Posts
    861
    it's a short not an int
    Code:
     scanf("%hd", &age);
    output
    Code:
    userx@slackwhere:~/bin
    $ ./print_vrs
    State your name: bob
    Age: 99
    Hello bob you are 99 years old

  3. #3
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    Oh, okay. I though %d was for "short" also. That makes sense why it didn't work. Thank you!

    What does %hd stand for by the way?

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    basically its a short h, int d

    short int hd

    Format Codes for printf

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,526
    > char name[] = "";
    You also need to allocate some space for the name as well.
    Say
    char name[50] = "";
    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.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,234
    If you do allocate 50 characters for name, it means that the length of the string would be at most 49 characters because of the terminating null character, so you should amend your scanf call to include this, otherwise it is vulnerable to buffer overflow:
    Code:
    scanf("%49s", name);
    When you have these working, another thing you can look into is checking the return value of scanf, i.e., to check that input was correctly read.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    Oh, okay. I though %d was for "short" also.
    It may be that you thought this because you had seen printf("%d", age) be used for data types such as short or unsigned char. This is okay providing that you don't have values which lie outside the range of int. You are only telling it how to format printed text. When you use scanf however you are telling it how to save data, so you must match the format to the data type.

    It might also be better practice to use both format specifiers and corrisponding variables in your printf's. Your use of printf doesn't exactly expose security vulnerabilities if you write a string literal right into the printf, but this kind of thinking may at some point lead you to write code that does. I debated on whether to mention this since it isn't really harming anything, but if you are aware of Uncontrolled Format String vulnerabilities, that's cool. In other words printf("%s", "text") is better than printf("text"). Also consider puts("text").
    Last edited by jack jordan; 10-28-2017 at 01:04 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program not printing variable max in the console
    By High Voltage in forum C Programming
    Replies: 5
    Last Post: 03-27-2016, 12:26 PM
  2. Program printing a random number instead of variable
    By tlxxxsracer in forum C Programming
    Replies: 7
    Last Post: 03-30-2015, 07:02 AM
  3. Replies: 8
    Last Post: 03-21-2012, 04:51 PM
  4. Printing all the variable names in a program.
    By Fatima Rizwan in forum C++ Programming
    Replies: 3
    Last Post: 09-29-2010, 09:59 AM
  5. Printing a variable's name.
    By BrokenShots in forum C Programming
    Replies: 9
    Last Post: 03-12-2008, 03:13 AM

Tags for this Thread