C Programming Basics
By Kayalvizhi
@kayaviz (2)
Madurai, India
December 10, 2014 8:37am CST
The ‘C’ language is one of the most popular languages among programmers. C was developed in 1972 by Dennis Ritchie at Bell Telephone Laboratories. C is a Middle-level language. Before programming in C, you should know some basics of C.The sequence of instructions that are given to execute a specific task is known as a Program.
C BASICS:
Character set
Tokens
Identifiers
Keywords
Constants
Data types
Variables
Operators
CHARACTER SET:
The characters are used to form words, numbers and expressions on which the program is run. The characters in C are grouped as:
Letters
Digits
Special characters
White spaces
Letters consist of upper case letters A….Z and lowercase letters a…z. C is case sensitive. Consider the following example, HELLO, Hello, hello (Each represents the different entity.)
Digits consist of all decimal digits (numbers) 0…9
List of Special characters:
~ tilde
! exclamation mark
# hash
$ dollar
( ) left and right parentheses
_ underscore
- minus sign (hyphen)
= equals
+ plus
{ } left and right braces
[ ] left and right brackets
| vertical bar
% per cent
^ caret
& ampersand
* asterisk
\ backslash
: colon
; semi-colon
" double quotation
' single quotation (apostrophe)
closing angle bracket (greater than sign)
, comma
. period
? question mark
/ slash
White spaces consist of blank space, new line, form feed, horizontal tab, and carriage return. White spaces may be used to separate words, but are prohibited to use between the characters of keywords and identifiers.
TOKENS:
Token is the fundamental unit in c language. The smallest individual units in a C program are known as tokens.
IDENTIFIERS:
The identifier may be the name of a variable or a constant or a function. The identifiers may be of alphanumeric but it should start with an alphabet (or underscore) and followed by alphabets or numbers or alphanumeric characters. In turbo c, the maximum length of identifier is 31, but in Unix OS, there is no such limit. No special characters and empty spaces are allowed in defining an identifier except underscore. Underscores can be used in identifiers for better readability and for better understanding.
Examples for Valid identifiers: a, a1, area, area1, hello_world
Examples for Invalid identifiers: 1area, *star, hello*
KEYWORDS:
All keywords have fixed meanings and these meanings cannot be changed. Keywords should not be used as identifiers. The list of keywords are
auto
double
int
struct
break
else
long
switch
case
enum
register
typedef
char
extern
return
union
const
float
short
unsigned
continue
for
signed
void
default
goto
sizeof
volatile
do
if
static
while
CONSTANTS:
The fixed values which do not vary during the execution of the program are called constants.
Numeric constants
Integer constants (whole numbers)
Real or floating point Constants (decimal numbers)
Character constants
Single character constants (a single character within single quotations)
String constants(set of characters within double quotations)
DATA TYPES:
Char – a single character can be defined as a char data type. A single character occupies 8 bits (1 byte) in memory.
int – whole numbers are represented as int (integer) data type. An integer occupies 16 bits (2 bytes) in memory.
float – decimal numbers are represented as float data type. Floating point numbers uses 32 bits (4 bytes) for storage.
double – decimal numbers can be defined as double data type. A double data type number uses 64 bits (8 bytes) for storage.
The difference between float and double data type is storage.
VARIABLES:
A variable is used to reserve the memory space in a computer and also to store the values in a program. Variable declaration: data_type variable_list;
variable_list refers to the list of variables separated by comma. Declaration statements must end with semicolon. Some examples are
int area;
char name,s1;
OPERATORS:
An operator is a symbol that is used to perform specific mathematical and logical operations. Operators are classified into many categories a
Assignment operators
Arithmetic operators
Relational operators
Logical operators
Increment and Decrement operators
Conditional operators
Bitwise operators
Special operators
Assignment operators are used to assign the values on right hand side to the variables on the left hand side. Some examples for assignment operations are
a=15;
a+=15; (or) a=a+15;
Arithmetic operators are used to perform arithmetic operations such as addition, subtraction, multiplication, division.
+ addition
- subtraction
* multiplication
/ multiplication
% modulo division
Modulo division operator gives the remainder of the operator.
For example 5%3 gives 2(remainder)
Relational operators are used to compare 2 values.
greater than
= greater than or equal to
== equal to
!= not equal to
Logical operators
&& logical AND
|| logical OR
! logical NOT
Increment operator is used to increment (add 1) the value. Increment operator is ++. Decrement operator is used to decrement (subtract 1) the value. Decrement operator is --.
Prefix operator first increments/decrements the operand and then the result is assigned to the variable on the left.
Postfix operator assigns the value to variable on the left and then increments/decrements the operand.
Consider the following examples,
a=5;
b=a++;
The above expressions result the value of b is5.
a=5;
b=++a;
The above expressions result the value of b is 6.
Conditional operator is “?:”
(expr1) ? (expr2) : (expr3)
Expr1 is evaluated first, if it is true expr2 is evaluated and it becomes the value of the expression. If expr1 is false expr3 is evaluated by skipping expr2 and it becomes the value of the expression.
Consider the following example,
a=10;
b=5;
max=(ab)?a:b;
Bitwise operators are used for manipulating data at bit level. The term bitwise refers to the testing, setting or shifting of the actual bits in a byte or word. The bitwise operators are
& bitwise AND
| bitwise OR
^ bitwise Exclusive OR
~ bitwise Complement
Shift right
Special operators are comma, sizeof operator, pointer operator (& and *) and member selection operators (. and -).
1 response