C Intro
C Intro
What is a preprocessor in C?
Character set
Alphabets
Digits
Keywords
Identifiers
Rules of naming identifiers
Variables
Literals
Integers
Floating-point Literals
Characters
Escape Sequences
String Literals
Constants
Basic Data Types
int
float and double
Char
void
short & long
Signed and unsigned
Derived Data Types
Input
I/O Multiple values
Types of Comments
Operators
What is a preprocessor in C?
The Preprocessor is not part of the compiler, but is a separate step in the compilation process. In simple terms, a
C Preprocessor is just a text substitution tool and its instructs the compiler to do require pre-processing before the
actual compilation.
Character set
A character set is a set of alphabets, letters and some special characters that are valid in C language.
Alphabets
Uppercase: A B C ................................... X Y Z
Lowercase: a b c ...................................... x y z
Digits
0 1 2 3 4 5 6 7 8 9
Blank space, newline, horizontal tab, carriage return and form feed.
Keywords
These 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. E.g. int
Identifiers
These refer to name given to entities such as variables, functions, structures etc.
Identifiers must be unique. They are created to give a unique name to an entity to identify it during the execution
of the program. E.g. money
int money
1. A valid identifier can have letters (both uppercase and lowercase letters), digits and underscores.
2. The first character of an identifier should be either a letter or an underscore.
3. You cannot use keywords like int, while etc. as identifiers because these are keywords.
4. There is no rule on how long an identifier can be. However, you may run into problems in some compilers if
the identifier is longer than 31 characters.
You can choose any name as an identifier if you follow the above rule, however, give meaningful names to
identifiers that make sense.
Each variable should be given a unique name (identifier). Variable names are just the symbolic representation of a
memory location. For example:
Note: You should always try to give meaningful names to variables. E.g. firstName is better than fn
C is a strongly typed language. This means that the variable type cannot be changed once it is declared. E.g.
Here, the type of the age variable is an integer. You can't assign a floating-point value to this variable. Also you
can't define the data type of the variable to a double. BTW, to store decimal variables in C you can either use a
double or float.
Literals
These are data used for representing fixed values. They can be used directly in the code. E.g. 1, 2.5, 'c', etc.
The examples above are literals. Why? You cannot assign different values to these terms.
Integers
These are numeric literals without any fractional or exponential part. There are three types of integer literals in C.
Floating-point Literals
These are numeric literals that has either a fractional form or an exponent form. E.g. -2.0, 0.0000234, -0.22E-5
Characters
These are created by enclosing a single character inside single quotation marks. E.g. a, m, F, 2, }, etc.
Escape Sequences
There are times where it is necessary to use characters that cannot be types or has special meaning in C. E.g.
newline, tab, question mark, etc.
String Literals
These are literals that are a sequence of characters enclosed in double quotation marks. E.g.
Constants
These are variables whose value cannot be changed, you can use the const keyword.
const float PI = 3.1415265;
Data Types
Data types are declaration for variables. This determines the type and size of data associated with variables.
int age;
In the example above, the variable age is a int(integer) type. The size of int is 4 bytes.
These are whole numbers like 0, -5, 10. You can declare multiple variables at once in C. E.g.
The size of int is usually 4 bytes(32 bits). And, it can take 2^32 distinct states from -2147483648 to 2147483647.
These hold real numbers. In C, floating-point numbers can also be represented in exponential. E.g.
float salary;
double price;
The difference between them is that a float(single precision float data type) is 4 bytes. And the size of
double(double precision float data type) is 8 bytes.
CHAR
This is used for declaring character type variables. The size of the character variable is 1bytes.
VOID
void is an incomplete type. Meaning 'nothing' or 'no type'. You can think of void as absent. E.g. if a function is not
returning anything, its return type should be void.
If you need to use a large number, you can use a type specifier long.
long a;
In the example above, variable a and num can store long integer values and variable decimalNnumber can store a
long floating-point number.
short number;
TIP
You can check the size of the variable using the sizeof() operator.
signed and unsigned are type modifiers. You can alter the data storage of a data type by using them:
//valid codes
//invalid code
Data types that are derived from fundamental data types are derived types. E.g. arrays, pointers, functions types,
structures, etc.
Bool type
Enumerated type
Complex types
#include <stdio.h>
int main(){
printf("C Programming");
return 0;
printf() is a library function to send formatted output to the screen. To use this function, we need to include
stdio.h header file using the #include <stdio.h> statement.
The return 0 statement is the "Exit status" of the program. It's optional.
Input
In C programming, scanf() is one of the commonly used function to take input from the user. The scanf()
function reads formatted input from the standard input such as keyboards.
#include <stdio.h>
int main(){
int testInteger;
scanf("%d", &testInteger);
printf("Number = %d",testInteger);
return 0;
Here, we have used %d format specifier inside the scanf() function to take int input from the user. When the user
enters an integer, it is stored in the test Integer variable.
NOTE:
In the above code we used &testInteger. It's because &testInteger gets the address of testInteger, and
the value entered by the user is stored in that address.
#include <stdio.h>
int main()
char chr;
return 0;
When a character is entered by the user in the above program, the character itself is not stored. Instead, an integer
value (ASCII value) is stored.
And when we display that value using %c text format, the entered character is displayed. If we use %d to display the
character, it's ASCII value is printed.
#include <stdio.h>
int main(){
int a;
float b;
Comments
In programming, comments are hints that a programmer can add to make their code easier to read and
understand. For example,
#include <stdio.h>
int main() {
printf("Hello World");
return 0;
Types of Comments
Operators
An operator is a symbol that operates on a value or a variable. For example: + is an operator to perform addition.
Arithmetic Operators
| ------------ | ------------------------------------------ |
| * | Multiplication |
| / | Division |
C programming has two operators increment ++ and decrement -- to change the value of an operand (constant or
variable) by 1.
Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1. These two operators are
unary operators, meaning they only operate on a single operand.
If you use the ++ operator as a prefix like: ++var, the value of var is incremented by 1; then it returns the value.
If you use the ++ operator as a postfix like: var++, the original value of var is returned first; then var is
incremented by 1.
Assignment Operators
| = | a = b | a = b |
| += | a += b | a = a+b |
| -= | a -= b | a = a-b |
| *= | a *= b | a = a * b |
| /= | a /= b | a = a / b |
| %= | a %= b | a = a % b |
An assignment operator is used for assigning a value to a variable. The most common assignment operator is =
// Working of assignment operators
#include <stdio.h>
int main() {
int a = 5, c;
c = a; // c is 5
c += a; // c is 10
c -= a; // c is 5
c *= a; // c is 25
c /= a; // c is 5
c %= a; // c = 0
return 0;
Relational Operators
A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the
relation is false, it returns value 0.
| == | Equal to | 5 == 3 is evaluated to 0 |
#include <stdio.h>
int main()
int a = 5, b = 5, c = 10;
return 0;
Logical Operators
An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or
false. Logical operators are commonly used in decision making in C programming.
#include <stdio.h>
int main()
return 0;
Bitwise Operators
During computation, mathematical operations like: addition, subtraction, multiplication, division, etc are converted
to bit-level which makes processing faster and saves power.
| ------------ | ----------------------- |
| | | Bitwise OR |
| ^ | Bitwise exclusive OR |
| ~ | Bitwise complement |
Comma Operator
Comma operators are used to link related expressions together. For example:
int a, c = 5, d;
sizeof Operator
#include <stdio.h>
int main()
int a;
float b;
double c;
char d;
return 0;