0.4 – C Language Fundamentals 1

by subbu on July 21, 2013

Classical way of learning any foreign language starts with learning letters, numbers and symbols of that language, and then learning words, sentences and finally speaking meaningfully using grammar. Let us follow the same steps in learning C language instead straightaway writing programs. Learning C language is much easier than any foreign language because C is comparatively small with only 32 keywords. Let us start our journey by learning characters.

c-learning-steps

C character set:

The basic step in learning any language is learning of characters. Set of Characters used in writing any C program is called C character set. It includes English (Latin) alphabets, digits (western Arabic numerals) and some of the symbols (ASCII). In C language both the small and capital letters are treated as separate characters; say “g” is different from “G”. So C is called case sensitive language.

This known alphabets, digits and symbols made the language easy to learn and use (user friendly).

C character set

Tokens (Words in C language)

The second step in learning any language is learning words by combining characters. English language has thousands of words but classified as 9 different types according to their usage called parts-of-speech.

In C language combining characters forms a word called token. According to the usage, these tokens are classified as

  1. Keywords
  2. Constants (literals)
  3. Identifiers
  4. String literals
  5. Operators
  6. Separators

Keywords:

These are the reserved words, the meanings of which were known to the compiler. Every keyword has its purpose and grammar in using. These must be written in small case otherwise the meaning of which would be changed. ANSI C has 32 keywords, listed here for reference. We are going to learn these keywords along this course.

The current ANSI standard C11 has 43 keywords that is addition of 11 keywords to  previous versions.

 C11 keywords

Note:

Some implementations also reserve fortran, asm, entry and far as keywords.

Keywords const, signed, volatile, enum and void were not in original C implementation, added to ANSI C

 Example:

keywords

In the above example int, goto are two keywords used on different purposes. Here the keyword int is used to select a piece of memory in RAM to store an integer value; goto is used to send the control to other portion of the program by skipping the statements in between from execution.

Variable:

A variable is a name for a piece of memory in the RAM, which can be used to store and access the data. The value stored in the allocated (selected) memory may change as the program execution progresses so called variable.

Declaration of variable:

A declaration statement announces the property of a variable to the compiler.

Variables are allocated (selected) for different data types using different keywords. For example int is used to allocate (select) memory to store an integer value, float is used to allocate memory to store a floating point value and char is used to allocate memory to store a single character.

In the example the declaration statement int qty; selects a piece of memory with the name qty called variable which is of integer type.

variable

In the above example qty is a variable, changes its value as the execution of program progresses.

Line 1: The keyword int selected a piece of memory with a name qty to store an integer value
Line 3: An integer value 45 is assigned (stored) to the variable qty using assigning operator (=)
Line 4: The value of qty is incremented by 10 and assigned to the same variable, which will be overwriting on the previous value.

Constants:

These are the values stored in the allocation memory of storage called variables. There are different kinds of constants identified by the C language like integer constants, floating constants (real), single character constants, string constants and enumeration constants. These are also called data types. Now we will discourse an example to gain fundamental knowledge on constants.

constants

In the above example qty, price, grade and cname are different locations (variables) in the storage (memory). These are allocated using different keywords int, float and char.

1. The keyword int allocated the memory with the name qty to store an integer value
2. The keyword float allocated the memory with the name price to store a real constant,
3, 4. The keyword char allocated the memory with the name grade to store a single character constant and with the name cname to store the text up to 20 characters length.
Operator = is called assigning operator, used to store a constant in a specified memory location.
6. An integer constant 45 is assigned (stored) to the location qty
7. A floating point constant 12.75 is assigned to the location price
8. A single character constant ‘A’ is assigned to the location grade
9. A string constant “India” is assigned to the location cname.

In C language a single character constant must be enclosed with in left side single inverted commas. Here `A’ is invalid and ‘A’ is valid, a word or a line of text called a string must be enclosed within double inverted commas.

A string constant can’t be assigned using = as any other constant, rather need to use a function strcpy() (Will be covered later)

Putting all together:

So far we got fundamental knowledge in keywords, variables and constants.

  • A keyword is a registered word used on a purpose
  • A Variable is a named location in the memory
  • A constant is a value stored in the variable

keywords-constants-variables

Previous post:

Next post: