Menu Close

Articles

Introduction to C

C is a low-level imperative and procedural language. It is a compiled language into machine instructions, and require minimal run-time support. It is also a structure oriented and strongly typed language. It is not object oriented nor functional programming language. 

Punctuation

All source code is contained in subroutines. Each subroutine has a name and is enclosed in a block of code. C language uses curly brackets to start and end a block of code. One block of code can contain numerous statements. Each statement is ending with semi-column: “;”.

C is a free form language. That means spaces and tabs are ignored. However many times one space is required as separator. A good practice is to use code indentation at equal number of spaces for nested blocks of code.

C is case sensitive language. There is a small, fixed number of keywords used to represent actions or declarations. These are reserved keywords you can not use as identifiers.

Keywords:

auto break case char const continue default do double else enum extern float for goto if inline
int long register restrict return short signed sizeof static struct switch typedef union unsigned
void volatile while

Note: Not all C keywords are English words. For example: enum, extern, typedef and goto are invented words. Therefore sometimes a text editor that do not know C will wrongly underline these words with a red line as incorrect. You can ignore this if you are positive you have spelled correctly the keyword.

Comments

To document your code or have some code commented out you can use block comments using delimiters:  “/* … */” and the line comments using delimiter “//”. A line comment can be used at the beginning of a new line or after statement, before the end of line.

Example:

Execution:

  • The #include <stdio.h> is a pre-processor command. This command tells compiler to include the contents of stdio.h (standard input and output) file in the program. The stdio.h file contains functions such as scanf() and printf() to take input and display output respectively.
  • The execution of a C program starts from the main() function.
  • The printf() is a library function to send formatted output to the screen. In this program, the printf() displays Hello, World! text on the screen.
  • The return 0; statement is the “Exit status” of the program. In simple terms, program ends with this statement.

Declaration

To make a C program you must declare names or identifiers for things that can be constants, variables, functions or data types. C has no “define” keyword: instead, a statement beginning with the name of a type. There is no “function” keyword: instead, a function is indicated by the parentheses following the function name. In parentheses you can enumerate formal parameters.

Data types:

To understand C code you must grasp idea of data type.  You can not declare anything without using a data type. However you can define your own types using keyword: typedef. C has very few basic data types:

Type NameDescriptionSizeprintf() placeholder
charcharacter1 byte%c
intdefault integer4 bytes%d
short intshort integer2 bytes%hd
long intlong integer8 bytes%li
unsigned intunsigned integer8 bytes%u
floatsingle precision floating point4 bytes%f
doubledouble precision floating point8 bytes%lf

To detect the real size of the data type you can use function: sizeof()

Example:

Note: You can use following modifiers for integer type:

  • short
  • long 
  • signed
  • unsigned

Derived types:

You can create new data types from basic types using a derived data types. There are different methods of derivation. One method is to group multiple elements into data collection or structure. These are called improperly user defined data types.

  • enumerations
  • arrays
  • pointers
  • function types
  • structures 

I will present derived types in future articles.

Variables

Next, you will learn about variables and rules for naming a variable.  A variable is like a labeled container or storage area in computer memory used to hold data for a short period of time. Variable names are symbolic representation of a memory location. Content of this location can be changed.

Example:

Notes:

  1. A variable name can have only Latin letters, digits and underscore,
  2. The first letter of a variable should be either a letter or an underscore,
  3. Identifier name in C should not be longer than 31 characters.

Constants

You can define variables that are immutable. These area actually called constants. 

Example:

Literals

Now is time to understand something more. C source code can contain numbers and characters after the symbol “=”, for example: 3.14.  These elements of the language are also constants, but are different kind of constants named Literals. The literals can be changed only if you change the source code.

Literal examples:

  • Character: ‘A’, ‘b’, ‘1’
  • Decimal: 0, -9, 22
  • Octal: 021, 077, 033
  • Hexadecimal: 0x7f, 0x2a, 0x521
  • Floating point: -2.0
  • Scientific notation -0.25E-5 
  • String: “abc”

Escape sequence:

Some characters are not printable. For representing these characters in string literals you can use escape notation. Use symbol “\” follow by a letter. This will be replaced by the compiler with the proper code point.

\bBackspace
\fForm feed
\nNewline
\rReturn
\tHorizontal tab
\vVertical tab
\\Backslash
\’Single quotation mark
\”Double quotation mark
\?Question mark
\0Null character

Example:

  • “Onle line \nSecond line.” 

Output

You must already get it from example. The main function capable to print output to the console is printf(). This function accept a string template as first parameter and optional a second parameter representing data to be inserted into the template.  Character “%” is the placeholder for data element that is different for each data type.

Example:

Input

Data input can be done using scanf() a function similar to printf(). For reading data, first you print a message using printf() then accept the user input using scanf().

Example:

Exit status

C do not have direct support for error handling like try or trial statement that is available in other languages. The only thing we can do is to return an error code from a function, then you can analyze this code. Main function is usually returning “0” = zero if the program finished correctly and “-1”, NULL or a different code that indicate an error. We will have an article about error handling at the end of this tutorial. Until then this is all you need to know to understand the examples and practice with the main function.

Static Variables

Example:

In previous example we have used keyword: “static” to define a special kind of variable. This variable is defined only once, and it exists during program execution. When the function is called this variable will not be re-defined. Therefore will not have value 0 second time it is called but a different value.

Next article: Decision statement