C Tutorial

C Strings

A C string is a char array that ends with a null byte. Include string.h to measure and copy text.

A char array, not a string type

C has no string class like C++. Text is an array of char. A string literal in double quotes fills that array and adds a null terminator. Include #include <stdio.h> to print with%s.

Example

#include <stdio.h>

int main(void) {
  char first[] = "Ada";
  printf("%s\n", first);
  return 0;
}

Try this at /c/try. The C editor compiles with gcc.

The null terminator

Every C string ends with a byte of value 0, written '\0'. That is how printf andstrlen know where the text stops. "Hi" occupies three bytes: 'H','i', and '\0'.

If you declare a fixed buffer, leave room for that extra byte. char name[8] can hold at most seven visible characters plus '\0'.

strlen from string.h

Include #include <string.h> for string functions. strlen counts characters before the null. It does not count '\0' itself. Print that length with %zu.

Example

#include <stdio.h>
#include <string.h>

int main(void) {
  char word[] = "C";
  printf("%s\n", word);
  printf("%zu\n", strlen(word));
  printf("%c\n", word[0]);
  return 0;
}

Do not read word[0] when the first byte is already '\0' (an empty string) unless you mean to. Check strlen first, or only index strings you just filled with text.

scanf reads one word

scanf("%s", name) reads one word into a char array. It skips leading whitespace, then stops at the next space or newline. You do not pass &name: the array name is already an address.

Example

#include <stdio.h>

int main(void) {
  char word[32];
  printf("Type one word: ");
  scanf("%31s", word);
  printf("Got: %s\n", word);
  return 0;
}

%31s leaves room for the null terminator in a 32-byte array. A full name with a space would store only the first word. Put that word in the stdin box on /c/try.

Copy with strcpy

strcpy(dest, src) copies characters including the null. The destination array must be large enough. This is not C++ concatenation with +. C does not add two arrays with +.

Example

#include <stdio.h>
#include <string.h>

int main(void) {
  char src[] = "StudyGrid";
  char dest[16];
  strcpy(dest, src);
  printf("%s\n", dest);
  printf("length %zu\n", strlen(dest));
  return 0;
}

Single quotes versus double quotes

'A' is a char. "A" is a string literal: two bytes, 'A' and'\0'. %c prints one character. %s prints until the null. Next: math functions in #include <math.h>.

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.

Biology

A short DNA motif

DNA is a sequence of bases: A, T, C, G. A pairs with T, C with G. The string "ATGCCAT" is not a gene. It is a motif you might search for in a longer strand.

In C the characters live in a char array that ends with a hidden 0. dna[0] is the first base. Printing with %s walks until that 0.

Example

#include <stdio.h>

int main(void) {
  char dna[] = "ATGCCAT";
  printf("strand: %s\n", dna);
  printf("first base: %c\n", dna[0]);
  return 0;
}

Chemistry

A formula as text

H2SO4 is sulphuric acid. Subscripts are written as ordinary digits in a char array because C has no chemical typesetter. The letters are not variables named H and S.

%s prints the whole formula. Later chapters copy and compare such names with string.h.

Example

#include <stdio.h>

int main(void) {
  char formula[] = "H2SO4";
  printf("sulphuric acid: %s\n", formula);
  return 0;
}

FAQ: C Strings

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 strings 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 strings in this C C lesson (C Strings).

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.