C Tutorial

C Output

printf sends text to the console. Format specifiers print numbers and strings. This is how every first program talks back.

printf writes to the console

printf is the standard way to print in C. You pass a format string, then any values that string refers to. Text goes in double quotes. After the program runs, you see those characters in the console — on StudyGrid, that is the output pane under the editor.

Example

#include <stdio.h>

int main(void) {
  printf("Hello from printf\n");
  return 0;
}

Open examples with Try it in C. That is /c/try: a gcc compile-and-run editor.

Format specifiers

Placeholders in the format string tell printf how to print each argument. %d is a signed int. %s is a string. %f is a floating-point value (use it fordouble as well). The arguments after the string must match those placeholders, in order.

Example

#include <stdio.h>

int main(void) {
  int score = 12;
  double price = 2.5;
  printf("Score: %d points\n", score);
  printf("%d plus %d is %d\n", 3, 4, 3 + 4);
  printf("price %f\n", price);
  printf("%s\n", "done");
  return 0;
}
SpecifierTypical type
%dint
%fdouble (or float)
%sstring (a char array)
%cchar

Newlines with \n

\n is the newline character inside a string. Without it, the next printf continues on the same line. Two messages then glue together: HelloWorld instead of two words.

Example

#include <stdio.h>

int main(void) {
  printf("first line\n");
  printf("second line\n");
  printf("third line\nfourth line\n");
  return 0;
}

\n is two characters in the source and one newline in the output. Do not put a real line break inside the quotes unless you want the source to span lines.

Spaces are your job

C does not insert spaces between printed values. If you need a gap, put it in the format string. A missing space is a missing character, not a compiler error.

CodeWhat you see
printf("%d%d", 2, 5);25
printf("%d %d", 2, 5);2 5
printf("n=%d", 3);n=3

You need stdio.h

printf lives in #include <stdio.h>. Include that header at the top. Forgetting it is a common first error: gcc may warn, or refuse to compile, depending on flags.

Example

#include <stdio.h>

int main(void) {
  char grade = 'A';
  printf("%d\n", 7);
  printf("%c\n", grade);
  printf("%s\n", "same header, several specifiers");
  return 0;
}

Next: comments, so you can leave notes next to the lines that print.

Worked examples

The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.

Open them in the C editor at /c/try. Change one measurement and check whether the result still has the right unit.

Physics

Cells in series

Identical cells in series add their emfs. Four 1.5 V cells give 6.0 V, ignoring internal resistance. The count of cells is an int. The voltage is not.

%d prints the count. %.1f prints the voltage to one decimal place, which matches how those cells are labelled on the packet.

V_total = n × V_cell

Real batteries sag under load. This is the open-circuit sum, not what a motor would actually see.

Example

#include <stdio.h>

int main(void) {
  int cells = 4;
  double volts = 1.5 * cells;
  printf("%d cells -> %.1f V\n", cells, volts);
  return 0;
}

Astronomy

A Mars year

Mars takes about 687 Earth days to go once around the Sun. That is a sidereal year, measured against the stars, not a calendar date on Mars.

printf can mix a name and an integer on one line. Mission logs look like this: an object, then a number, then a unit implied by the sentence.

Example

#include <stdio.h>

int main(void) {
  printf("Mars year ~ %d Earth days\n", 687);
  return 0;
}

FAQ: C Output

Common questions about this page.

What is the StudyGrid C tutorial?

The StudyGrid C tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, pointers, structs, files, and the standard library. Each chapter has copy-and-run examples.

Should I run c printf examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c printf in this C C lesson (C Output).

Is the C editor the same as Try Python, Try HTML, or Try C++?

No. Try C compiles with gcc at /c/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. Try C++ stays at /cpp/try. C lessons never open those editors.

Do I need to install a compiler to learn C?

No. Open a chapter, click Try it in C, and compile in the browser. You can also download a .c file and compile locally with gcc.

Where should I start the C tutorial?

Start at C Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After pointers, open C Examples, then files and bitwise. Use Next at the bottom of each chapter.

Is the C tutorial free?

Yes. The C workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.