-6

I'm just starting out learning C and wanted to ask some very elementary questions. I don't have any programming background at all. I'm just at the very start of a tutorial (http://www.cprogramming.com/tutorial/c-tutorial.html) and there's some things it doesn't explain. Like, for example, in this following introductory simple program…

Question: why does he use "%d" – why d and not another letter? I understand the % is the modulo operand, meaning remainder, but why d? I understand that stdio.h imports a library/glossary of terms to use, such as printf and so on… is %d the same as that? :

#include <stdio.h>

int main()
{
    int this_is_a_number;

    printf( "Please enter a number: " );
    scanf( "%d", &this_is_a_number );
    printf( "You entered %d", this_is_a_number );
    getchar();
    return 0;
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Abdul Montaqim
  • 41
  • 1
  • 1
  • 2
  • 6
    What happened to reading the documentation for the functions you're calling? – Mat Dec 22 '12 at 15:17
  • 5
    d in %d stands for decimal. You should read about printf format strings: http://en.wikipedia.org/wiki/Printf_format_string OR in short RTFM! – Bharat Dec 22 '12 at 15:18
  • 9
    I recommend you revise the way you learn. It's no good coming running to StackOverflow for every minute detail of the language fundamentals. You won't get the best learning experience, and you won't get the best out of StackOverflow. Instead, pick up a good C textbook (we have recommendations in the FAQ) and learn to use language references (such as `man`). Then work yourself through as many problems as you can, and make simple test cases to gain confidence and understanding of each individual component. – Kerrek SB Dec 22 '12 at 15:20
  • 1
    @RBK: "Read the Formatted-IO Manual"? – Kerrek SB Dec 22 '12 at 15:21
  • 2
    "This question does not show _any_ research effort" - to explain the slew of downvotes. – John Dvorak Dec 22 '12 at 15:23
  • @KerrekSB, well, not really.. I intended(& took it for granted) that for a beginner the K&R book is the manual(or Bible). They are the C Gods you see :) – Bharat Dec 22 '12 at 15:24
  • 2
    possible duplicate: [Why does %d stand for Integer?](http://stackoverflow.com/q/13409014/187543) – cpx Dec 22 '12 at 15:25
  • -10 votes! you dont need to show your ass to new users like this, I am sure he will not be back again. be more constructive! – Spring Dec 22 '12 at 15:30

2 Answers2

5

Actually % is not the modulo operand or modulo operator here because it's included inside a string. It's just a character chosen to separate literal text from parameters. And d is used because it performs input and output in decimal (base 10).

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • actually, `%` _is_ the modulo operator at times (didn't downvote). – John Dvorak Dec 22 '12 at 15:20
  • @JanDvorak: He didn't say "operator", he said "operand". Although that sounds Kafkaesque, it's not wrong :-S – Kerrek SB Dec 22 '12 at 15:22
  • Thanks everyone for your answers. Mat: the tutorial is very basic/brief so it omits a lot. I've got the C book by Denis Ritchie, which I'll tackle next. @RBK: Thanks. Helpful. Kerrek SB: I have been trying to get my head around programming for almost a year, reading a range of material, but I just can't pick it up. I'm not a genius, but I'm not stupid either. I tried to avoid coming to Stack Overflow with elementary questions. I'm from a journalistic background and my attitude is to explain things in a very basic way. I do not see that in computer programming books/tutorials at all. Thanks. – Abdul Montaqim Dec 22 '12 at 15:40
  • I meant Dennis M. Ritchie. And Brian W. Kernighan. I'm nervous. – Abdul Montaqim Dec 22 '12 at 15:46
1

In this case is not a modulo operator but a sign meaning you want to display the sub-sequent information in a certain format. The letter is called a specifier.

%d means you're going to display it as a decimal integer. %f would display it as a float number etc.

More in the man : http://linux.die.net/man/3/printf

Vincent L.
  • 122
  • 3