Fundamentals of C

Tweet
This entry is part 2 of 10 in the series Fundamentals of C

Fundamentals of C

The basic elements used to construct a simple C program are: the C character set, identifiers and keywords, data types, constants, arrays, declarations , expressions and statements. Let us see how these elements can be combined to form more comprehensive program components-

The C Character Set:

C uses letters A to Z in lowercase and uppercase, the digits 0 to 9, certain special characters, and white spaces to form basic program elements (e.g variables , constants, expressions etc.)

The special characters are:

+  –  * /  = % & # ! ? ^ ” ‘ /  | < > ( ) [  ]  {  } : ; . , ~ @ !

The white spaces used in C programs are: blank space, horizontal tab, carriage return, new line and form feed.

Identifiers and Keywords:

Identifiers are names given to various program elements such as variables, functions, and arrays. Identifiers consist of letters and digits, in any order, except that the first character must be a letter. Both uppercase and lowercase letters are permitted and the underscore may also be used, as it is also regarded as a letter. Uppercase and lowercase letters are not equivalent, thus not interchangeable. This is why it is said that C is case sensitive. An identifier can be arbitrarily long.

The same identifier may denote different entities in the same program, for example, a variable and an array may be denoted by the same identifier, example below.

int sum, average, A[10]; // sum, average and the array name A are all identifiers.

The _ _func_ _ predefined identifier:-

The predefined identifier __func__ makes a function name available for use within the function. Immediately following the opening brace of each function definition, _ _func_ _ is implicitly declared by the compiler in the following way:

static const char _ _func_ _[] = "function-name";

where function-name is the name of the function.

consider the following example

#include <stdio.h>

void func1(void)    {
         printf("%sn",__func__);
         return;
}  

int main() {
     myfunc();
}

The output would be

func1

Keywords

Keywords are reserved words that have standard predefined meanings. These keywords can only be used for their intended purpose; they cannot be used as programmer defined identifiers. Examples of some keywords are: int, main, void, if.

Data Types

Data values passed in a program may be of different types. Each of these data types are represented differently within the computer’s memory and have different memory requirements. These data types can be augmented by the use of data type qualifiers/modifiers.

The data  types supported in C are described below:

int:

It is used to store an integer quantity. An ordinary int can store a range of values from INT_MIN to INT_MAX as defined by in header file <limits.h>. The type modifiers for the int data type are: signed, unsigned, short, long  and long long.

  • A short int occupies 2 bytes of space and a long int occupies 4 bytes.
  • A short unsigned int occupies 2 bytes of space but it can store only positive values in the range of 0 to 65535.
  • An unsigned int has the same memory requirements as a short unsigned int. However, in case of an ordinary int, the leftmost bit is reserved for the sign.
  • A long unsigned int occupies 4 bytes of memory and stores positive integers in the range of 0 to 4294967295.
  • By default the int data type is signed.
  • A long long int occupies 64 bits of memory. It may be signed or unsigned. The signed long long int stores values from −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 and the unsigned long long ranges from 0 to 18,446,744,073,709,551,615.

char:

It stores a single character of data belonging to the C character set. It occupies 1 byte of memory, and stores any value from the C character set. The type modifiers for char are signed and unsigned.

Both signed and unsigned char occupy 1 byte of memory but the range of values differ. An unsigned char can store values from 0 to 255 and a signed char can store values from -128 to +127. Each char type has an equivalent integer interpretation, so that a char is really a special kind of short integer. By default, char is unsigned.

float:

It is used to store real numbers with single precision i.e. a precision of 6 digits after decimal point. It occupies 4 bytes of memory. The type modifier for float is long. It has the same memory requirements as double.

double:

It is used to store real numbers with double precision. It occupies 8 bytes of memory. The type modifier for double is long. A long double occupies 10 bytes of memory.

void:
It is used to specify an empty set containing no values. Hence, it occupies 0 bytes of memory.

_Bool:
A boolean data type, which is an unsigned integer type, that can store only two values, 0 and 1. Include the file <stdbool.h> when using _Bool.

_Complex:

It is used to store complex numbers. There are three complex types: float _Complex, double _Complex, and long double _ComplexIt is found in the <complex.h> file.

Arrays:

An array is an identifier that refers to a collection of data items of that have the same name. They must also have the same data type (i.e. all characters, all integers etc.). Each data item is represented by its array element. The individual array elements are distinguished from one another by their subscripts.

Syntax for array declaration:

Suppose arr is a 5 element integer array which stores the numbers 5, 4, 2, 7, 3 in that order. The first element is referred to as arr[0]which stores the data value 5, the second element is arr[1] which stores the value 4 and so on. The last element is arr[4] which stores the value 3. The subscript assosciated with each element is shown in square braces. For an n-element array the subscripts will range from 0 to n-1.

In case of a character array of size n, the array will be able to store only n-1 elements as a null character is automatically stored at the end of the string to terminate it. Therefore, a character array letter that stores 5 elements  must be declared of size 6. The fifth element would be stored at letter[4] and letter[5] will store the null character.

Constants:

A constant is an identifier whose value remains unchanged throughout the program. To declare any constant the syntax is:
const datatype varname = value; where const is a keyword that declares the variable to be a fixed value entity.

There are four basic types of constants inC. They are integer constants, floating point constants, character constants and string constants. Integer and floating point constants cannot contain commas or blank spaces; but they can be prefixed by a minus sign to indicate a negative quantity.

Integer Constants:

An integer constant is an integer valued number. It consists of a sequence of digits. Integer constants can be written in the following three number systems:

Decimal(base 10): A decimal constant can consist of any combination of digits from 0 to 9. If it contains two or more digits, the first digit must be something other than 0, for example: const int size =50;

Octal(base 8): An octal constant can consist of any combination of digits from 0 to 7. The first digit must be a 0 to identify the constant as an octal number, for example: const int a= 074; const int b= 0;

Hexadecimal constant(base 16): A hexadecimal constant can consist of any combination of digits from 0 to 9 and a to f (either uppercase or lowercase). It must begin with 0x or oX to identify the constant as a hexadecimal number, for example: const int c= 0x7FF;

Integer constants can also be prefixed by the type modifiers unsigned and long. Unsigned constants must end with u or U, long integer constants must end with lor L and unsigned long integer constants must end with ul or UL. Long long integer constants end with LL or ll. Unsigned long long end with UL or ul

Floating Point Constant:

Its a base 10 or a base 16 number that contains a decimal point or an exponent or both. In case of a decimal floating point constant the exponent the base 10 is replaced by e or E. Thus, 1.4 *10^-3 would be written as 1.4E-3 or 1.4e-3.

In case of a hexadecimal character constant, the exponent is in binary and is replaced by p or P. For example:

const float a= 5000. ;

const float b= .1212e12;

const float c= 827.54;

Floating point constants are generally double precision quantities that occupy 8 bytes. In some versions of C, the constant is appended by Fto indicate single precision and by L to indicate a long floating point constant.

Character Constants:

A character constant is a sequence of one or more characters enclosed in apostrophes. Each character constant has an equivalent integer value that is determined by the computer’s character set. It may also contain escape sequences. A character literal may be prefixed with the letter L, u or U, for example L'c'.

A character literal without the L prefix is an ordinary character constant or a narrow character constant. A character literal with the L prefix is a wide character constant. The type of a narrow character constant and a multicharacter constant is int. The type of a wide character constant with prefix L is wchar_t defined in the header file <stddef.h> .A wide character constant with prefix u or U is of type char16_t or char32_t. These are unsigned character types defined in <uchar.h>.

An ordinary character literal that contains more than one character or escape sequence is a multicharacter constant, for example: const char p= 'A';

Escape Sequences are also character constants that are used to express certain non printing characters such as the tab or the carriage return.An escape sequence always begins with a backward slash and is followed by one or more special characters. for eg. b will represent the bell, n will represent the line feed.

String Literals:

A string literal consists of a sequence of multibyte characters enclosed in double quotation marks. They are of two types, wide string literal and UTF-8 string literal. A UTF-8 string literal is prefixed by u8 and a wide string literal by L, u or U.

The compiler recognizes and supports the additional characters (the extended character set) which you can meaningfully use in string literals and character constants. The support for extended characters includes the multibyte character sets. A multibyte character is a character whose bit representation fits into one or more bytes.

Symbolic Constants:

A symbolic constant is a name that substitutes for a numeric constant, a character constant or a string constant throughout the program. When the program is compiled each occurrence of a symbolic constant is replaced by its actual value.

A symbolic constant is defined at the beginning of a program using the # define feature. The # define feature is  called a preprocessor directive, more about the C preprocessor in a later article.

A symbolic constant definition never ends with a semi colon as it is not a C statement rather it is a directive, for example:

#define  PI  3.1415    //PI is the constant that will represent value 3.1415

#define  True 1

#define  name  "Alice"

These were the some of the basic elements of C. Elements like scope of an identifier, declarations, statements, expressions, etc., will be explained in future articles with the help of practical examples. The next article will discuss the operators provided in the C language.

Fundamentals of C

<< An Introduction to COperators in C >>

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • Arnold

    I would remove the ‘return’ statement from func1 and put it in main with a return value unless you declare main as ‘void’.

  • Albert

    How do you run this code, I’d love to follow the tutorial as I’ve never programmed in C and would like to follow along but don’t know where and what complier to use? Suggestions please.

    • http://www.myblogonunix.blogspot.com Surabhi Saxena

      Hi Albert,
      Sorry for answering so late.
      The answer depends on the operating system that you are using:-
      1. For windows the compiler is cygwin which you can download from http://cygwin.com.Also you will need to install the GCC compiler which you can download from http://gcc.gnu.org/install/index.html
      To compile a c program on windows write the program on a text editor and save it with .c extension.For e.g .Take any program from my previous article introduction to c and write it out on a text editor. Give it a name say ‘area’ and save it as area.c
      After that you can compile the program . At http://www3.ntu.edu.sg/home/ehchua/programming/howto/cygwin_Howto.html you would find a very useful guide to installing cygwin and then compiling a c program on it.

      2. If you are using Linux or any other open source based operating system you will need the GCC compiler. After that you can compile the program.At http://www.techlila.com/c-programming-linux/ you will get a detailed description of compiling and executing a c program.

      3. It is also possible to compile your program online at http://codepad.org. Here you can simply copy paste your program and then compile it online. you would not need to download anything. However , it is always better to have a compiler installed on your system.

      I hope all this helps . If you still face any problems do let me know.
      Surabhi