First Assignment (Click Here to Download)
Variable: A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in C has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.
The name of a variable can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore. Upper and lowercase letters are distinct because C is case-sensitive.
Rules for creating variable name:
1. All variable names must begin with letter of the alphabet or an underscore (_).
2. After the first initial letter, variable names can also contain letters and numbers. No spaces of special characters, however, are allowed.
3. Variable names are case sensitive.
4. No spaces or special characters are allowed.
Constant: Constant refer to fixed values that the program may not alter during its execution. These fixed valued are also called literals.
Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal. There are enumeration constants as well.
Constants are treated just like regular variables except that their valued cannot be modified after their definition.
Keyword: Keywords are predefined, reserved words used in programming that have special meanings to the compiler. Keywords are part of the syntax and they cannot be used as an identifier. For Example, int money;
Here, int is a keyword that indicates ‘money’ is a variable of type integer.
As C is a case sensitive language, all keywords must be written in lowercase. There are 32 keywords in C programming.
Token: C tokens are the basic buildings blocks in C language which are constructed together to write a C program.
Each and every smallest individual units in a C program are known as C tokens.
C tokens are of six types. They are,
1. Keywords (eg: int, while)
2. Identifiers (eg: main, total)
3. Constants (eg: 20, 23)
4. Strings (eg: “total”, “hello”)
5. Special symbols (eg: (), {})
6. Operators (eg: +, /, -, *)
Tommy’s Note
Identifier: Identifiers are the names that are given to various program elements such as variables, symbolic constants and functions. Variable or function identifier that is called a symbolic constant name.
Identifier can be freely named, the following restrictions:
Alphanumeric characters (a ~ z, A~Z, 0~9) a half underscore (_) can only be used.
The first character of the first contain letters (a ~ z, A ~ Z) or half underscore (_) can only be used.
Case is distinguishable. That is, king and KING is recognized as a separate identifier.
Reserved words are not allowed. However, part of an identifier reserved words can be included.
Function: A number of statements grouped into a single logical unit are called a function. The use of function make programming easier since repeated statements can be grouped into functions. Splitting the program into separate function make the program more readable and maintainable.
It is necessary to have a single function ‘main’ en every C program, along with other functions used/defined by the programmer.
A function definition has two principal components: the function header and body of the function. The function header is the data type of return value followed by function name and (optionally) a set of arguments separated enclosed in parenthesis.
Associated type to which function accepts precedes each argument. In general terms function header statement can be written as
return_type, function_name (type1 arg1, type2 arg2, ... , typen argn) Where return type represents the data type of the item that is returned by the function, function name represents the name of the function, and type1, type2,…,typen represents the data type of the arguments arg1,arg2,..,argn. int add(int p, int q) { Return p+q; //Body of the function }
Algorithm: In programming, algorithm are the set of well-defined instruction in sequence to solve a program. An algorithm should always have a clear stopping point.
Inputs and outputs should be defined precisely.
Each steps in algorithm should be clear and unambiguous.
Algorithm should be most effective among many different ways to solve a problem.
An algorithm shouldn't have computer code. Instead, the algorithm should be written in such a way that, it can be used in similar programming languages.
Tommy’s Note
Example of Algorithms in Programming. Step 1: Start Step 2: Declare variables num1, num2 and sum. Step 3: Read values num1 and num2. Step 4: Add num1 and num2 and assign the result to sum. sum←num1+num2 Step 5: Display sum Step 6: Stop
Data types: Data type’s simple refers to the type and size of data associated with variables and functions.
Data types in C are –
1. Fundamental Data Types
a. Integer types
b. Floating type
c. Character type
2. Derived Data Type
a. Arrays
b. Pointers
c. Structures
d. Enumeration
Declaration Variable and Initialization:
Declaration of Variable in C –
Data_type variable_name;
Or
Data_type variable1, variable2, ..... , variablen;
Where data_type is any valid C data type and variable_name is any valid identifier.
For example,
int a;
float variable;
float a, b;
Initialization of Variable in C –
data_type variable_name = constant/literal/expression;
Tommy’s Note
Or
variable_name = constant/literal/expression;
Example,
int a = 10;
int a = b+c;
a = 10;
a = b+c;