This document provides information on variables, data types, constants, and operators in the C programming language. It defines variables as named storage locations that can store and modify data during program execution. Data types specify the range of values a variable can hold and the operations that can be performed. Constants are fixed values that cannot be altered. The document also describes integer, floating-point, character, and string literals used to represent constants, as well as arithmetic, relational, and logical operators used to perform operations.
2. 11/12/2015
Variable
This refers to the named area in memory that stores a value or string
assigned to that variable. It is named storage location capable of
containing a certain type of data that can be modified during program
execution.
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.
C Language
2
6
:
5
5
A
M
3. 11/12/2015
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.
Example:
number
_myAge
sum1
6
:
5
5
A
M
C Language
3
4. 11/12/2015
Data Types
This is a definition of a set of data that specifies the possible range of
values of the set, the operations that can be performed on the values,
and the way in which the values are stored in memory. Knowing the
type of data enables the computer to manipulate it appropriately.
Data types in C refer to an extensive system used for declaring variables or
functions of different types. The type of a variable determines how much
space it occupies in storage and how the bit pattern stored is interpreted.
6
:
5
5
A
M
C Language
4
5. 11/12/2015
Basic Data Types: Integer Type
The following table provides the details of standard integer types with
their storage sizes and value ranges.
6
:
5
5
A
M
C Language
5
6. 11/12/2015
Basic Data Types: Integer Type
Type Storage size Value range
char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
int 2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
long 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long 4 bytes 0 to 4,294,967,295
6
:
5
5
A
M
C Language
6
7. 11/12/2015
Basic Data Types: Floating-Point Type
The following table provide the details of standard floating-point
types with storage sizes and value ranges and their precision.
6
:
5
5
A
M
C Language
7
8. 11/12/2015
Basic Data Types: Floating-Point Type
Type Storage size Value range Precision
float 4 byte 1.2E-38 to 3.4E+38 6 decimal places
double 8 byte 2.3E-308 to 1.7E+308 15 decimal places
long double 10 byte 3.4E-4932 to 1.1E+4932 19 decimal places
6
:
5
5
A
M
C Language
8
9. 11/12/2015
Constants
Constants refer to fixed values that the program may not alter during
its execution. These fixed values 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.
Constants are treated just like regular variables except that their values
cannot be modified after their definition.
6
:
5
5
A
M
C Language
9
10. 11/12/2015
Constants: Integer Literals
An integer literal can be a decimal, octal, or hexadecimal constant. A
prefix specifies the base or radix: 0x or 0X for hexadecimal, 0 for octal,
and nothing for decimal.
An integer literal can also have a suffix that is a combination of U and L, for
unsigned and long, respectively. The suffix can be uppercase or lowercase and
can be in any order.
6
:
5
5
A
M
C Language
10
11. 11/12/2015
Constants: Integer Literals
Example:
212 /* Legal */
215u /* Legal */
0xFeeL /* Legal */
078 /* Illegal: 8 is not an octal digit */
032UU /* Illegal: cannot repeat a suffix */
85 /* decimal */
0213 /* octal */
0x4b /* hexadecimal */
30 /* int */
30u /* unsigned int */
30l /* long */
30ul /* unsigned long */
6
:
5
5
A
M
C Language
11
12. 11/12/2015
Constants: Floating-point Literals
A floating-point literal has an integer part, a decimal point, a
fractional part, and an exponent part. You can represent floating point
literals either in decimal form or exponential form.
While representing decimal form, you must include the decimal point, the
exponent, or both; and while representing exponential form, you must
include the integer part, the fractional part, or both. The signed exponent is
introduced by e or E.
6
:
5
5
A
M
C Language
12
13. 11/12/2015
Constants: Floating-point Literals
Example:
3.14159 /* Legal */
314159E-5L /* Legal */
510E /* Illegal: incomplete exponent */
210f /* Illegal: no decimal or exponent */
.e55 /* Illegal: missing integer or fraction */
6
:
5
5
A
M
C Language
13
14. 11/12/2015
Constants: Character Constants
Character literals are enclosed in single quotes, e.g., 'x' can be stored
in a simple variable of char type.
A character literal can be a plain character (e.g., 'x'), an escape
sequence (e.g., 't'), or a universal character (e.g., 'u02C0').
There are certain characters in C that represent special meaning
when preceded by a backslash for example, newline (n) or tab (t).
6
:
5
5
A
M
C Language
14
15. 11/12/2015
Constants: Character Constants
Escape sequence Meaning Escape sequence Meaning
character n Newline
' ' character r Carriage return
" " character t Horizontal tab
? ? character v Vertical tab
a Alert or bell ooo Octal number of one to three digits
b Backspace xhh . . . Hexadecimal number of one or more digits
f Form feed
6
:
5
5
A
M
C Language
15
16. 11/12/2015
Constants: String Literals
String literals or constants are enclosed in double quotes "". A string
contains characters that are similar to character literals: plain
characters, escape sequences, and universal characters.
You can break a long line into multiple lines using string literals and
separating them using white spaces.
6
:
5
5
A
M
C Language
16
18. 11/12/2015
Operators
An operator is a symbol that tells the compiler to perform specific
mathematical or logical functions. C language is rich in built-in
operators and provides the following types of operators.
Arithmetic Operators
Relational Operators
Logical Operators
6
:
5
5
A
M
C Language
18
19. 11/12/2015
Arithmetic Operators
The following table shows all the arithmetic operators supported by the C
language. Assume variable A holds 10 and variable B holds 20.
6
:
5
5
A
M
C Language
19
20. 11/12/2015
Arithmetic Operators
Operator Description Example
+ Adds two operands. A + B = 30
Subtracts second operand from the first. A B = 10
* Multiplies both operands. A * B = 200
/ Divides numerator by de-numerator. B / A = 2
% Modulus Operator and remainder of after an integer division. B % A = 0
++ Increment operator increases the integer value by one. A++ = 11
-- Decrement operator decreases the integer value by one. A-- = 9
6
:
5
5
A
M
C Language
20
21. 11/12/2015
Relational Operators
The following table shows all the relational operators supported by C. Assume
variable A holds 10 and variable B holds 20.
6
:
5
5
A
M
C Language
21
22. 11/12/2015
Relational Operators
Operator Description Example
== Checks if the values of two operands are equal or not. If yes, then the
condition becomes true.
(A == B) is not true.
!= Checks if the values of two operands are equal or not. If the values are
not equal, then the condition becomes true.
(A != B) is true.
> Checks if the value of left operand is greater than the value of right
operand. If yes, then the condition becomes true.
(A > B) is not true.
< Checks if the value of left operand is less than the value of right operand.
If yes, then the condition becomes true.
(A < B) is true.
>= Checks if the value of left operand is greater than or equal to the value of
right operand. If yes, then the condition becomes true.
(A >= B) is not true.
<= Checks if the value of left operand is less than or equal to the value of
right operand. If yes, then the condition becomes true.
(A <= B) is true.
6
:
5
5
A
M
C Language
22
23. 11/12/2015
Logical Operators
Following table shows all the logical operators supported by C language.
Assume variable A holds 1 and variable B holds 0.
6
:
5
5
A
M
C Language
23
24. 11/12/2015
Logical Operators
Operator Description Example
&& Called Logical AND operator. If both the operands are non-zero, then the
condition becomes true.
(A && B) is false.
|| Called Logical OR Operator. If any of the two operands is non-zero, then the
condition becomes true.
(A || B) is true.
! Called Logical NOT Operator. It is used to reverse the logical state of its
operand. If a condition is true, then Logical NOT operator will make it false.
!(A && B) is true.
6
:
5
5
A
M
C Language
24
25. 11/12/2015
C - Program Structure
A C program basically consists of the following parts:
Preprocessor Commands
Functions
Variables
Statements & Expressions
Comments
6
:
5
5
A
M
C Language
25
26. 11/12/2015
C - Program Structure
#include <stdio.h>
int main()
{
/* my first program in C */
printf("Hello, World! n");
return 0;
}
6
:
5
5
A
M
C Language
26
27. 11/12/2015
C - Program Structure
Let us take a look at the various parts of the above program
The first line of the program #include <stdio.h> is a preprocessor command,
which tells a C compiler to include stdio.h file before going to actual
compilation.
The next line /*...*/ will be ignored by the compiler and it has been put to add
additional comments in the program. So such lines are called comments in the
program.
The next line return 0; terminates the main() function and returns the value 0.
6
:
5
5
A
M
C Language
27
29. 11/12/2015
C - Input & Output
When we say Input, it means to feed some data into a program. An
input can be given in the form of a file or from the command line. C
programming provides a set of built-in functions to read the given
input and feed it to the program as per requirement.
When we say Output, it means to display some data on screen,
printer, or in any file. C programming provides a set of built-in
functions to output the data on the computer screen as well as to
save it in text or binary files.
6
:
5
5
A
M
C Language
29
30. 11/12/2015
The scanf() and printf() Functions
The int scanf(const char *format, ...) function reads the input from
the standard input stream stdin and scans that input according to
the formatprovided.
The int printf(const char *format, ...) function writes the output to
the standard output stream stdout and produces the output
according to the format provided.
The format can be a simple constant string, but you can specify %s,
%d, %c, %f, etc., to print or read strings, integer, character or float
respectively. There are many other formatting options available which
can be used based on requirements.
6
:
5
5
A
M
C Language
30
31. 11/12/2015
Example:
#include <stdio.h>
int main( )
{
char str[100];
int i;
printf( "Enter a value :");
scanf("%s %d", str, &i);
printf( "nYou entered: %s %d ", str, i);
return 0;
}
6
:
5
5
A
M
C Language
31