Skip to content

C is a general-purpose programming language, developed in 1972, and still quite popular. C is very powerful; it has been used to develop operating systems, databases, applications, etc.

Notifications You must be signed in to change notification settings

Mengsreang-Chhoeung/c-program-tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

15 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

C Program Tutorial

C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972.

It is a very popular language, despite being old.

C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Why Learn C?

  • It is one of the most popular programming language in the world

  • If you know C, you will have no problem learning other popular programming languages such as Java, Python, C++, C#, etc, as the syntax is similar

  • C is very fast, compared to other programming languages, like Java and Python

  • C is very versatile; it can be used in both applications and technologies

Difference between C and C++

  • C++ was developed as an extension of C, and both languages have almost the same syntax

  • The main difference between C and C++ is that C++ support classes and objects, while C does not

Get Started With C

To start using C, you need two things:

  • A text editor, like Notepad, to write C code and in this tutorial we will be using Visual Studio Code

  • A compiler, like GCC, to translate the C code into a language that the computer will understand

C Quickstart

Let's create our first C file.

#include <stdio.h>

int main() {
  printf("Hello World!");
  return 0;
}

After build and run...

Hello World!
Process returned 0 (0x0) execution time : 0.011 s
Press any key to continue.

C Syntax

You have already seen the following code a couple of times in the first chapters. Let's break it down to understand it better:

#include <stdio.h>

int main() {
  printf("Hello World!");
  return 0;
}

Example explained

  • Line 1: #include <stdio.h> is a header file library that lets us work with input and output functions, such as printf() (used in line 4). Header files add functionality to C programs.

    Don't worry if you don't understand how #include <stdio.h> works. Just think of it as something that (almost) always appears in your program.

  • Line 2: A blank line. C ignores white space. But we use it to make the code more readable.

  • Line 3: Another thing that always appear in a C program, is main(). This is called a function. Any code inside its curly brackets {} will be executed.

  • Line 4: printf() is a function used to output/print text to the screen. In our example it will output "Hello World".

    Note that: Every C statement ends with a semicolon ;

    Note: The body of int main() could also been written as: int main(){printf("Hello World!");return 0;}

    Remember: The compiler ignores white spaces. However, multiple lines makes the code more readable

  • Line 5: return 0 ends the main() function.

  • Line 6: Do not forget to add the closing curly bracket } to actually end the main function.

C Output (Print Text)

To output values or print text in C, you can use the printf() function:

#include <stdio.h>

int main() {
  printf("Hello World!");
  return 0;
}

You can use as many printf() functions as you want. However, note that it does not insert a new line at the end of the output:

#include <stdio.h>

int main() {
  printf("Hello World!");
  printf("I am learning C.");
  return 0;
}

Let's see the output...

Hello World!I am learning C.
Process returned 0 (0x0) execution time : 0.011 s
Press any key to continue.

C New Lines

To insert a new line, you can use the \n character:

#include <stdio.h>

int main() {
  printf("Hello World!\n");
  printf("I am learning C.");
  return 0;
}

Output:

Hello World!
I am learning C.
Process returned 0 (0x0) execution time : 0.011 s
Press any key to continue.

Tip: Two \n characters after each other will create a blank line:

#include <stdio.h>

int main() {
  printf("Hello World!\n\n");
  printf("I am learning C.");
  return 0;
}

Output:

Hello World!

I am learning C.
Process returned 0 (0x0) execution time : 0.011 s
Press any key to continue.

What is \n exactly?

The newline character (\n) is called an escape sequence, and it forces the cursor to change its position to the beginning of the next line on the screen. This results in a new line.

Other valid escape sequences are:

Escape Sequence Description
\t Creates a horizontal tab
\\ Inserts a backslash character (\)
" Inserts a double quote character

πŸ“œ References

🀝 Contributors

About

C is a general-purpose programming language, developed in 1972, and still quite popular. C is very powerful; it has been used to develop operating systems, databases, applications, etc.

Topics

Resources

Stars

Watchers

Forks

Languages