The Basic Elements Of C - C Programming Professional Made easy (2015)

C Programming Professional Made easys (2015)

Chapter 1 The Basic Elements Of C

The seemingly complicated C program is composed of the following basic elements:

Character Set

The alphabet in both upper and lower cases is used in C. The 0-9 digits are also used, including white spaces and some special characters. These are used in different combinations to form elements of a basic C program such as expressions, constants, variables, etc.

Special characters include the following:

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

White spaces include:

· Blank space

· Carriage return

· Horizontal tab

· Form feed

· New line

Identifiers

An identifier is a name given to the various elements of the C program, such as arrays, variables and functions. These contain digits and letters in various arrangements. However, identifiers should always start with a letter. The letters may be in upper case, lower case or both. However, these are not interchangeable. C programming is case sensitive, as each letter in different cases is regarded as separate from each other. Underscores are also permitted because it is considered by the program as a kind of letter.

Examples of valid identifiers include the following:

ab123

A

stud_name

average

velocity

TOTAL

Identifiers need to start with a letter and should not contain illegal characters. Examples of invalid identifiers include the following:

2nd - should always start with a letter

“Jamshedpur” - contains the illegal character (“)

stud name - contains a blank space, which is an illegal character

stud-name - contains an illegal character (-)

In C, a single identifier may be used to refer to a number of different entities within the same C program. For instance, an array and a variable can share one identifier. For example:

The variable isint difference, average, A[5]; // sum, average

The identifier isA[5].

In the same program, an array can be named A, too.

__func__

The__func__ is a predefined identifier that provides functions names and makes these accessible and ready for use anytime in the function. The complier would automatically declarethe__func__ immediately after placing the opening brace when declaring the function definitions. The compiler declares the predefined identifier this way:

static const char _ _func_ _[] = “Alex”;

“Alex” refers to a specific name of this particular function.

Take a look at this example:

#include <stdio.h>

void anna1(void) {

printf("%sn",__func__);

return;

}

int main() {

myfunc();

}

What will appear as an output will be anna1

Keywords

Reserved words in C that come with standard and predefined meanings are called keywords. The uses for these words are restricted to their predefined intended purpose. Keywords cannot be utilized as programmer-defined identifiers. In C, there are 32 keywords being used, which include the following:

auto

break

char

case

continue

const

do

default

double

float

else

extern

enum

goto

for

if

long

int

register

short

return

sizeof

signed

switch

typedef

struct

union

switch

void

unsigned

while

volatile

Data Types

There are different types of data values that are passed in C. Each of the types of data has different representations within the memory bank of the computer. These also have varying memory requirements. Data type modifiers/qualifiers are often used to augment the different types of data.

Supported data types in C include int, char, float, double, void, _Bool, _Complex, arrays, and constants.

int

Integer quantities are stored in this type of data. The data type int can store a collection of different values, starting from INT_MAX to INT_MIN. An in-header file, <limits h>, defines the range.

These int data types use type modifiers such as unsigned, signed, long, long long and short.

Short int means that they occupy memory space of only 2 bytes.

A long int uses 4 bytes of memory space.

Short unsigned int is a data type that uses 2 bytes of memory space and store positive values only, ranging from 0 to 65535.

Unsigned int requires memory space similar to that of short unsigned int. For regular and ordinary int, the bit at the leftmost portion is used for the integer’s sign.

Long unsigned int uses 4 bytes of space. It stores all positive integers ranging from 0 to 4294967295.

An int data is automatically considered as signed.

Long long int data type uses 64 bits memory. This type may either be unsigned or signed. Signed long long data type can store values ranging from ─9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Unsigned long long data type stores value range of 0 to 18,446,744,073,709,551,615.

char

Single characters such as those found in C program’s character set are stored by this type of data. The char data type uses 1 byte in the computer’s memory. Any value from C program’s character set can be stored as char. Modifiers that can be usedare eitherunsignedorsigned.

A char would always use 1 byte in the computer’s memory space, whether it is signed or unsigned. The difference is on the value range. Values that can be stored as unsigned char range from 0 to 255. Signed char stores values ranging from ─128 to +127. By default, a char data type is considered unsigned.

For each of the char types, there is a corresponding integer interpretation. This makes each char a special short integer.

float

A float is a data type used in storing real numbers that have single precision. That is, precision denoted as having 6 more digits after a decimal point. Float data type uses 4 bytes memory space.

The modifier for this data type islong, which uses the same memory space as that of double data type.

double

The double data type is used for storing real numbers that have double precision. Memory space used is 8 bytes. Double data type useslong as a type modifier. This uses up memory storage space of 10 bytes.

void

Void data type is used for specifying empty sets, which do not contain any value. Hence, void data type also occupies no space (0 bytes) in the memory storage.

_Bool

This is a Boolean type of data. It is an unsigned type of integer. It stores only 2 values, which is 0 and 1. When using _Bool, include <stdboolh>.

_Complex

This is used for storing complex numbers. In C, three types of _Complex are used. There is thefloat _Complex,double _Complex, andlong double _Complex. These are found in <complex h> file.

Arrays

This identifier is used in referring to the collection of data that share the same name and of the same type of data. For example, all integers or all characters that have the same name. Each of the data is represented by its own array element. The subscripts differentiate the arrays from each other.

Constants

Constants are identifiers used in C. The values of identifiers do not change anywhere within the program. Constants are declared this way:

const datatype varname = value

const is the keyword that denotes or declares the variable as the fixed value entity, i.e., the constant.

In C, there are 4 basic constants used. These include the integer constant, floating-point, character and string constants. Floating-point and integer types of constant do not contain any blank spaces or commas. Minus signs can be used, which denotes negative quantities.

Integer Constants

Integer constants are integer valued numbers consisting of sequence of digits. These can be written using 3 different number systems, namely, decimal, octal and hexadecimal.

Decimal system (base 10)

An integer constant written in the decimal system contains combinations of numbers ranging from 0 to 9. Decimal constants should start with any number other except 0. For example, a decimal constant is written in C as:

const int size =76

Octal (base 8)

Octal constants are any number combinations from 0 to 7. To identify octal constants, the first number should be 0. For example:

const int a= 043; const int b=0;

An octal constant is denoted in the binary form. Take the octal 0347. Each digit is represented as:

0347 = 011 100 111 = 3 * 82 + 4 * 81 + 7 * 80 = 231

--- --- ---
3 4 7

Hexadecimal constant (base 16)

This type consists of any of the possible combinations of digits ranging from 0 to 9. This type also includes letters a to f, written in either lowercase or uppercase. To identify hexadecimal constants, these should start with oX or 0X. For example:

const int c= 0x7FF;

For example, the hexadecimal number 0x2A5 is internally represented in bit patterns within C as:

0x2A5 = 0010 1010 0101 = 2 * 162 + 10 * 161 + 5 * 160 = 677
---- ---- ----
2 A 5

Wherein, 677 is the decimal equivalent of the hexadecimal number 0x2.

Prefixes for integer constants can either be long or unsigned. A long integer constant (long int) ends with alofL, such as 67354Lor67354l. The last portion of an unsigned long integer constant should either beul or UL, such as672893ULor 672893ul. For an unsigned long long integer constant,ULorul should be at the last portion. An unsigned constant should end withUoru, such as673400095uor673400095U. Normal integer constants are written without any suffix, such as a simple67458.

Floating Point Constant

This type of constant has a base 10 or base 16 and contains an exponent, a decimal point or both. For a floating point constant with a base 10 and a decimal point, the base is replaced by anEore. For example, the constant 1.8 *10̂-3 is written as 1.8e-3 or 1.8E-3.

For hexadecimal character constants and the exponent is in the binary form, the exponent is replaced byPorp. Take a look at this example:

This type of constant is often precision quantities. These occupy around 8 bytes of memory. Different add-ons are allowed in some C program versions, such asFfor a single precision floating constant orL for a long floating point type of constant.

Character Constant

A sequence of characters, whether single or multiple ones, enclosed by apostrophes or single quotation marks is called a character constant. The character set in the computer determines the integer value equivalent to each character constant. Escape sequences may also be found within the sequence of a character constant.

Single character constants enclosed by apostrophes is internally considered as integers. For example, ‘A’ is a single character constant that has an integer value of 65. The corresponding integer value is also called the ASCII value. Because of the corresponding numerical value, single character constants can be used in calculations just like how integers are used. Also, these constants can also be used when comparing other types of character constants.

Prefixes used in character constants such asL,Uoru are used for character literals. These are considered as wide types of character constants. Character literals with the prefixL are considered under the typewchar_t, which are defined as <stddef.h> under the header file. Character constants that use the prefixUoruare considered as typechar16_torchar32_t. These are considered as unsigned types of characters and are defined under the header file as<uchar.h>.

Those that do not have the prefixL are considered a narrow or ordinary character constant. Those that have escape sequences or are composed of at least 2 characters are considered as multicharacter constants.

Escape sequences are a type of character constant used in expressing non-printing characters like carriage return or tab. This sequence always begins with a backward slash, followed by special characters. These sequences represent a single character in the C language even if they are composed of more than 1 character. Examples of some of the most common escape sequences, and their integer (ASCII) value, used in C include the following:

Character Escape Sequence ASCII Value

Backspace \b 008

Bell \a 007

Newline \n 010

Null \0 000

Carriage \r 013

Horizontal tab \t 009

Vertical tab \v 011

Form feed \f 012

String Literals

Multibyte characters that form a sequence are called string literals. Multibyte characters have bit representations that fit into 1 or more bytes. String literals are enclosed within double quotation marks, for example,“A” and “Anna”. There are 2 types of string literals, namely, UTF-8 string literals and wide string literals. Prefixes used for wide string literals includeu,UorL. Prefix for UTF-8 string literals isu8.

Additional characters or extended character sets included in string literals are recognized and supported by the compiler. These additional characters can be used meaningfully to further enhance character constants and string literals.

Symbolic constants

Symbolic constants are substitute names for numeric, string or character constants within a program. The compiler would replace the symbolic constants with its actual value once the program is run.

At the beginning of the program, the symbolic constant is defined with a # define feature. This feature is called the preprocessor directive.

The definition of a symbolic constant does not end with a semi colon, like other C statements. Take a look at this example:

#define PI 3.1415

(//PI is the constant that will represent value 3.1415)

#define True 1

#define name "Alice"


For all numeric constants such as floating point and integer, non-numeric characters and blank spaces are not included. These constants are also limited by minimum and maximum bounds, which are usually dependent on the computer.

Variables

Memory locations where data is stored are called variables. These are indicated by a unique identifier. Names for variables are symbolic representations that refer to a particular memory location. Examples are count, car_no and sum.

Rules when writing the variable names

Writing variable names follow certain rules in order to make sure that data is stored properly and retrieved efficiently.

· Letters (in both lowercase and uppercase), underscore (‘_’) and digits are the only characters that can be used for variable names.

· Variables should begin either with an underscore or a letter. Starting with an underscore is acceptable, but is not highly recommended. Underscores at the beginning of variables can come in conflict with system names and the compiler may protest.

· There is no limit on the length of variables. The compiler can distinguish the first 31 characters of a variable. This means that individual variables should have different sequences for the 1st 31 characters.

Variables should also be declared at the beginning of a program before it can be used.