Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Language Examples - Adding Two Integers

C Language Examples - Adding Two Integers

C Language Examples C Language Examples

Use scanf() to receive input, and printf() with %d to output integers.

Example

#include

<

stdio.h

int

main

(

)

{

int

firstNumber

,

secondNumber

,

sumOfTwoNumbers

;

printf

(

"

Enter two numbers (separated by a space):

"

)

;

// Receive two integers from the user via the scanf() function

scanf

(

"

%d %d

"

, &

firstNumber

, &

secondNumber

)

;

// Add the two numbers

sumOfTwoNumbers

=

firstNumber

secondNumber

;

// Output result

printf

(

"

%d + %d = %d

"

,

firstNumber

,

secondNumber

,

sumOfTwoNumbers

)

;

return

0

;

}

Running result:



输入两个数(以空格分割): 1 2

1 + 2 = 3

C Language Examples C Language Examples