How to print variables in C?
- a01337106
- 11 feb 2015
- 1 Min. de lectura
For this explanation we will need a little program
#include <stdio.h>
int main(){
int a=5;
int b=9;
int sum=a+b;
printf("La suma es : %d \n", sum);
}
We can see that as we have said before, we started the code and opened the main.
Inside we declare three variables: a,b and sum;
The numbers inside a and b will be summed and stored in sum.
Now, for printing we put the printf();
Inside we open start and statement: “La suma es : %d \n”
The text will print as it is written, the term %d refers to a variable, finally de \n indicates to the computer to go to the next line.
After this statement there is a comma and the variable that will be printed.
Note: All the variables are represented with the statement: %d. If you want to call several variables in a statement, you just have to write several times “%d”. The variables assigned to the statement %d will depend on the order in which you call them after the comma.
For example:
int A=1;
int B=2;
int C=3;
print(“Esta es una prueba, variable A: %d, variable B: %d, variable C: %d”,A,B,C);
The printed statement will be:
Esta es una prueba, variable A:1, variable B:2, variable C:3
Comments