ToothlessSnake ToothlessSnake - 9 days ago 6
C Question

The %d for printf() is outputting large integers.

I have been learning C programming, and have seemed to stumble upon %d giving me large integer values like 11146096.

#include<stdio.h>

void main() {
const int age = 23;
int brotherage;
brotherage = age*2;
printf("I am %d years old, and my uncle is %d years old.");
}

Answer

Your printf statment is wrong. You need to specify the variables for the corresponding %d's in your printf function :

printf("I am %d years old, and my uncle is %d years old.", age, brotherage);

A large number is printed in your case because, the printf function will assume that you have supplied the arguments for the %d's and go looking for it on the stack. It will pick up whatever is there, and hence, can return garbage values.