Explain this line of code!? : sprintf(buffer, "%d", argv[1][strlen(argv[1])-1]);

Posted on 2006-05-24
Last Modified: 2010-05-18
Hi experts,

I am pulling apart some old c code at work and have come accross a line I have litle idea about, hopefully it will be completely obvious to you guys.

The line is this:

sprintf(buffer, "%d", argv[1][strlen(argv[1])-1]);

The value of argv[1] is UK12398
Buffer evaluates to 56

I can't see how buffer evaluates to 56, can someone explain to me exactly the process that it goes through to get that number from UK12398

Many Thanks in advance
Question by:purejamie
    LVL 16

    Accepted Solution

    argv[1] is a pointer to the string "UK12398"

    strlen(argv[1]) is the length of the string "UK12398": 7.

    So argv[1][strlen(argv[1])-1] will resolve to argv[1][6] which is the character 8.

    A character is represented by the number of its ASCII encoding, which for '8' is decimal 56.

    So sprintf "prints" the decimal value in buffer.

    Author Comment

    Capital! Many Thanks.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now