Language C
Language C
Introduction to C
C is a programming language developed at AT & T’s Bell Laboratories of USA in 1972 which is developed by Dennis Ritchie.
It is a high level, general purpose programming language which was initially used for system development work. It was
accepted as a system development language because it generates the code that runs almost as fast as the code written in
assembly language. Also called as ELL (English like language).
C is a:
Procedural language
Structured Language
High level Language
Easy to learn
It can be compiled on a variety of computer platforms.
Basics
Syntax basically refers to the protocols to be followed while writing a program. It is very necessary to follow
proper syntax while coding to get the desired set of output.
The C basic syntax consists of header files, main function, and program code. This is the most fundamental
structure in the C program.
#include<stdio.h>
int main() // main function with integer return type
{
printf ("Welcome to programming!\n"); // print statement to display output
return 0; // Indicates that the main function returns null value
}
SEMICOLONS
The semicolon is a statement terminator. That is, each individual statement must be ended with a semicolon. It
indicates the end of one logical entity. Given below are two different statements −
printf(“Hello, World! \n”);
return 0;
Internal Use - Confidential
COMMENTS
C comments are helping texts in your C program and are ignored by the compiler. They start with /* and
terminate with the characters */.
/* my first program in C */
You cannot have comments within comments and they do not occur within a string or character literals.
IDENTIFIERS
C identifier is a name used to identify a variable, function, or any other user-defined item. An identifier starts
with a letter A to Z, a to z, or an underscore '_' followed by zero or more letters, underscores, and digits (0 to
9). C does not allow punctuation characters such as @, $, and % within identifiers.
C is a case-sensitive programming language. So, Manpower and manpower are two different identifiers.
Examples of acceptable identifiers −
mohd zara abc move_name
a_123
myname50 _temp j a23b9
retVal
Internal Use - Confidential
KEYWORDS
The following list shows the reserved words in C. These reserved words may not be used as constants or variables or
any other identifier names.
auto else long switch
WHITESPACES in C
A line containing only whitespace, possibly with a comment, is known as a blank line, and a C compiler totally ignores it.
Whitespace is the term used in C to describe blanks, tabs, newline characters and comments. Whitespace separates
one part of a statement from another and enables the compiler to identify where one element in a statement, such as
int, ends and the next element begins.
int age;
Internal Use - Confidential
Language C
Datatypes
Basic Types, Enumerated Types, The Type Void, Derived Types, Integer Types
Floating - Point Types, The Void Type
A datatype determines the type and the operations that can be performed on the data. C provides various data
types and each data type is represented differently within the computer’s memory.
Type-name Type Range
int Numeric – Integer -32 768 to 32 767
short Numeric – Integer -32 768 to 32 767
long Numeric – Integer -2 147 483 648 to 2 147 483 647
float Numeric – Real 1.2 X 10-38 to 3.4 X 1038
double Numeric – Real 2.2 X 10-308 to 1.8 X 10308
char Character All ASCII characters
Constants – The difference between variables and constants is that variables can change their value at any time but
constants can never change their value. (The constants value is locked for the duration of the program). Constants can be
very useful, Pi for instance is a good example to declare as a constant.
Language C
Storage Classes
Register Storage Class, The Static Storage Class, The extern Storage Class
auto: This is the default storage class for all the variables declared inside a function or a block. Hence, the keyword auto is rarely used while
writing programs in C language. Auto variables can be only accessed within the block/function they have been declared and not outside
them (which defines their scope). Of course, these can be accessed within nested blocks within the parent block/function in which the auto
variable was declared. However, they can be accessed outside their scope as well using the concept of pointers given here by pointing to
the very exact memory location where the variables resides. They are assigned a garbage value by default whenever they are declared.
Internal Use - Confidential
extern: Extern storage class simply tells us that the variable is defined elsewhere and not within the same block where it is used. Basically,
the value is assigned to it in a different block and this can be overwritten/changed in a different block as well. So an extern variable is
nothing but a global variable initialized with a legal value where it is declared in order to be used elsewhere. It can be accessed within any
function/block. Also, a normal global variable can be made extern as well by placing the ‘extern’ keyword before its declaration/definition
in any function/block. It basically signifies that we are not initializing a new variable but instead we are using/accessing the global variable
only. Main purpose of using extern variables is that it can be accessed between two different files which are part of a large program.
static: This storage class is used to declare static variables which are popularly used while writing programs in C language. Static variables
have a property of preserving their value even after they are out of their scope! Hence, static variables preserve the value of their last use
in their scope. So we can say that they are initialized only once and exist till the termination of the program. Thus, no new memory is
allocated because they are not re-declared. Their scope is local to the function to which they were defined. Global static variables can be
accessed anywhere in the program. By default, they are assigned the value 0 by the compiler.
register: This storage class declares register variables which have the same functionality as that of the auto variables. The only difference is
that the compiler tries to store these variables in the register of the microprocessor if a free register is available. This makes the use of
register variables to be much faster than that of the variables stored in the memory during the runtime of the program. If a free register is
not available, these are then stored in the memory only. Usually few variables which are to be accessed very frequently in a program are
declared with the register keyword which improves the running time of the program. An important and interesting point to be noted here
is that we cannot obtain the address of a register variable using pointers.
To specify the storage class for a variable, the following syntax is to be followed:
Operators
Arithmetic Operators, Relational Operators, Logical Operators, Bitwise Operators, Assignment
Operators, Miscellaneous Operators, Operators precedence in C
Decision Making in C
1. If statement
2. Switch statement
3. Conditional operator statement (? : operator)
4. GOTO statement
1 c
Character
2 d or i
Signed decimal integer
3 e
Scientific notation (mantissa/exponent) using e character
4 E
Scientific notation (mantissa/exponent) using E character
5 f
Decimal floating point
6 g
Uses the shorter of %e or %f
7 G
Uses the shorter of %E or %f
8 o
Signed octal
9 s
String of characters
10 u
Unsigned decimal integer
11 x
Unsigned hexadecimal integer
12 X
Unsigned hexadecimal integer (capital letters)
13 p
Pointer address
14 n
Nothing printed
15 %
Character
Loops in C
Functions in C
A function may or may not accept any argument. It may or may not return any value. Based on these facts, There are four different aspects of
function calls.
Return Value
C function may or may not return a value from the function. If you don't have to return any value from the function, use void for the return
type.
Example without return value: void hello()
Arrays in C
Syntax: int a[ ];
There are various ways in which we can declare an array. It can be done by specifying its type and size, by initializing it or both.