+1 vote
in Programming Languages by (56.8k points)
I want to print integer, floating point integer, character, and string variables. How should I display them in C?

1 Answer

+2 votes
by (74.2k points)
selected by
 
Best answer

To print any variable in C, you need to provide the format specifier with the printf() function. Without format specifiers, the printf() function will not display anything.

A format specifier tells the C compiler the type of data the variable is storing and it starts with a percentage sign (%), followed by a character.

Format specifiers for integer, float, character, and string are as follows:

%c: a single character

%s: a string

%d: a decimal integer

%f: a floating point number

Here is an example to show the use of format specifiers:

#include <stdio.h>

int main() {

  int n = 125;              // Integer 

  float f = 15.24;          // Floating point number

  char c = 'A';             // Character

  char name[] = "Airplane"; // String

  

  // Print variables

  printf("%d\n", n);

  printf("%f\n", f);

  printf("%c\n", c);

  printf("%s\n", name);

  return 0;

}

Related questions

+1 vote
1 answer
+1 vote
1 answer
+2 votes
1 answer
+1 vote
1 answer

...