Giter Club home page Giter Club logo

c-program-tutorial's Introduction

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

c-program-tutorial's People

Contributors

mengsreang-chhoeung avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.