C Programming Basic Concepts VV
C Programming Basic Concepts VV
INTRODUCTION TO C PROGRAMMING
OVERVIEW OF C
C language was developed by Dennis Ritchie in 1972 at
Bell Laboratories.
In 1989, the C language was standardized, where C
language features were defined, also known as the 1989
ANSI standard for C, and that is the reason, you will see C
language also called C89, because of the 1989 ANSI
standard.
The current latest version of the C language is C99, as some
new features were added to the C language in 1999.
You must be thinking, 1999 was more than 20 years ago, but
the C language is still very relevant and widely used although
there are many other new languages like C++, Java, Python, C#,
etc. in the market.
Features of C
C language is a robust language whose rich set of
built-in functions & operators.
C compiler combines the capabilities of an assembly
language with the features of high-level language.
Well suited for writing both system-software and
business packages.
PROGRAMMING IN C –NEP SCHEME VVFGC MYSORE
Linkage section
Definition section
Global declaration section
Main( )
{
Declaration part;
Executable part;
}
Sub-program section
Documentation Section
The documentation section is the part of the program where
the programmer gives the details associated with the
program.
Example
/**
* File Name: Helloworld.c
* Author: Manthan Naik
* date: 09/08/2019
* description: a program to display hello world
* no input needed
*/
Link Section
This part of the code is used to declare all the header files that
will be used in the program. This leads to the compiler being told
to link the header files to the system libraries.
PROGRAMMING IN C –NEP SCHEME VVFGC MYSORE
Example
#include<stdio.h>
Definition Section
In this section, we define different constants. The keyword define is used in
this part.
#define PI=3.14
int a=7;
int main(void)
int a=10;
return 0;
return a+b;
Sample C program
* Author: vidya
* date: 09/08/2019
**/
#include<stdio.h>//link section
float r;
scanf("%f",&r);
return 0;
float area(float r)
}
PROGRAMMING IN C –NEP SCHEME VVFGC MYSORE
return a
Click on the Start button
PROGRAMMING IN C –NEP SCHEME VVFGC MYSORE
Select Run
Type cmd and press Enter
Type cd c:\TC\bin in the command prompt and press Enter
Type TC press Enter
Click on File -> New in C Editor window
Type the program
Save it as FileName.c (Use shortcut key F2 to save)
otherwise generates object code in a file with name Sample.obj and submit it to
the linker. The linker combines the code from specified header file into an object
file and generates executable file as Sample.exe. With this compilation process
completes.
press Ctrl + F9. When we press Ctrl + F9 the executable file is submitted to the
CPU. Then CPU performs the task according to the instructions written in
that program and place the result into UserScreen.
Then we press Alt + F5 to open UserScreen and check the result of the program.
Important Points
C program file (Source file) must save with .c extension.
Input to the compiler is .c file and output from the compiler is .exe file,
The compiler converts the file only if there are no errors in the source code.
Overall Process
Type the program in C editor and save with .c extension (Press F2 to save).
Press Alt + F9 to compile the program.
If there are errors, correct the errors and recompile the program.
If there are no errors, then press Ctrl + F9 to execute/run the program.
Press Alt + F5 to open User Screen and check the result.
C Character Set
As every language contains a set of characters used to construct words,
statements, etc., C language also has a set of characters which
include alphabets, digits, and special symbols. C language supports a
total of 256 characters.
1. Alphabets
2. Digits
3. Special Symbols
PROGRAMMING IN C –NEP SCHEME VVFGC MYSORE
Alphabets
C language supports all the alphabets from the English language. Lower and
Digits
C language supports 10 digits which are used to construct numerical values
in C language.
Digits - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Special Symbols
C language supports a rich set of special symbols that include symbols to
Special Symbols - ~ @ # $ % ^ & * ( ) _ - + = { } [ ] ; : ' " / ? . > , < \ | tab newline
return 0;
}
Tokens in C
Tokens in C is the most important element to be used in creating a program
in C. We can define the token as the smallest individual element in C. For
`example, we cannot create a sentence without using words; similarly, we
cannot create a program in C without using tokens in C. Therefore, we can
say that tokens in C is the building block or the basic component for
creating a program in C language.
Classification of tokens in C
o Keywords in C
o Identifiers in C
o Strings in C
o Operators in C
o Constant in C
o Special Characters in C
Keywords in C
PROGRAMMING IN C –NEP SCHEME VVFGC MYSORE
words having its own importance, and each keyword has its own
compiler, so they cannot be used as the variable names. If the keywords are
Do If static while
Identifiers in C
Variables in C
A variable is a name of the memory location. It is used to store data. Its
value can be changed, and it can be reused many times.
1. type variable_list;
1. int a;
2. float b;
3. char c;
Here, a, b, c are variables. The int, float, char are the data types.
We can also provide values while declaring the variables as given below:
1. int a=10,b=20;//declaring 2 variable of integer type
2. float f=20.8;
3. char c='A';
1. int a;
2. int _ab;
3. int a30;
1. int 2;
2. int a b;
3. int long;
Types of Variables in C
There are many types of variables in c:
1. local variable
2. global variable
3. static variable
4. automatic variable
5. external variable
1. Local Variable
A variable that is declared inside the function or block is called a local variable.
void function1( )
{
int x=10;//local variable
}
Global Variable
A variable that is declared outside the function or block is called a global
variable. Any function can change the value of the global variable. It is
available to all the functions.
int value=20;//global variable
void function1( )
{
int x=10;//local variable
}
Static Variable
A variable that is declared with the static keyword is called static variable.
void function1( )
{
int x=10;//local variable
static int y=10;//static variable
x=x+1;
y=y+1;
printf("%d,%d",x,y);
}
If you call this function many times, the local variable will print the
same value for each function call, e.g, 11,11,11 and so on. But the static
variable will print the incremented value in each function call, e.g. 11,
12, 13 and so on.
Automatic Variable
All variables in C that are declared inside the block, are automatic variables
by default. We can explicitly declare an automatic variable using auto
keyword.
void main( )
{
PROGRAMMING IN C –NEP SCHEME VVFGC MYSORE
int x=10;//local variable (also automatic)
auto int y=20;//automatic variable
}
External Variable
We can share a variable in multiple C source files by using an external
variable. To declare an external variable, you need to use extern keyword.
myfile.h
extern int x=10;//external variable (also global)
program1.c
#include "myfile.h"
#include <stdio.h>
void printValue( )
{
printf("Global variable: %d", global_variable);
}
DATA TYPES
Data used in c program is classified into different types based on its
properties. In the C programming language, a data type can be defined as a
set of values with similar characteristics. All the values in a data type have
the same properties.
Data types in the c programming language are used to specify what kind
of value can be stored in a variable.
The memory size and type of the value of a variable are determined by the
variable data type.
In a c program, each variable or constant or array must have a data type
and this data type specifies how much memory is to be allocated and what
type of values are to be stored in that variable or constant or array. The
formal definition of a data type is as follows...
PROGRAMMING IN C –NEP SCHEME VVFGC MYSORE
types. All the primary data types are already defined in the system. Primary
The integer data type is a set of whole numbers. Every integer value does not
have the decimal value. We use the keyword "int" to represent integer data
type in c. We use the keyword int to declare the variables and to specify the
return type of a function. The integer data type is used with different type
modifiers like short, long, signed and unsigned. The following table provides
Floating-point data types are a set of numbers with the decimal value. Every
float
double
"double" to represent double data type in c. Both float and double are
similar but they differ in the number of decimal places. The float value
data types.
The following table provides complete details about the character data type.
PROGRAMMING IN C –NEP SCHEME VVFGC MYSORE
The following table provides complete information about all the data types in
c programming language...
specify a function which does not return any value. We also use the void
constants and each integer constant is given a name. The keyword "enum"
programming language, the derived data types are created using the
following concepts...
Arrays
Structures
Unions
Enumeration
Constants in C
A constant is similar to the variable but the constant hold only one value
during the program execution. That means, once a value is assigned to the
constant, that value can't be changed during the program execution. Once
the value is assigned to the constant, it is fixed throughout the program. A
constant can be defined as follows...
A constant is a named memory location which holds only one value throughout the
program execution.
upper-case names.
Syntax:
Integer constants
An integer constant can be a decimal integer or octal integer or hexadecimal
integer. A decimal integer value is specified as direct integer value whereas
octal integer value is prefixed with 'o' and hexadecimal value is prefixed with
'OX'.
An integer constant can also be unsigned type of integer constant or long
type of integer constant. Unsigned integer constant value is suffixed with 'u'
and long integer constant value is suffixed with 'l' whereas unsigned long
integer constant value is suffixed with 'ul'.
Example
125 ----->Decimal Integer Constant
O76----->Octal Integer Constant
OX3A -----> Hexa Decimal Integer Constant
50u -----> Unsigned Integer Constant
30l -----> Long Integer Constant
100ul -----> Unsigned Long Integer Constant
Example
The floating-point value 3.14 is represented as 3E-14 in exponent form.
PROGRAMMING IN C –NEP SCHEME VVFGC MYSORE
Character Constants
A character constant is a symbol enclosed in single quotation. A character
constant has a maximum length of one character.
‘8’ is not as same as 8. Character constants have a specific set of integer
values known as ASCII (American standard code for information
interchange).
Creating constants in C
In a c programming language, constants can be created using
two concepts...
Example
const int x = 10 ;
Here, 'x' is a integer constant with fixed value 10.
#include<stdio.h>
#include<conio.h>
void main()
{
int i = 9 ;
const int x = 10 ;
i = 15 ;
x = 100 ; // creates an error
printf("i = %d\nx = %d", i, x ) ;
}
void main( )
{
int r, area ;
printf("Please enter the radius of circle : ") ;
scanf("%d", &r) ;
area = PI * (r * r) ;
printf("Area of the circle = %d", area) ;
}
Initialization of variable
C variables declared can be initialized with the help of
assignment operator ‘=’;
Syntax
Datatype variable-name = const/literal/expression
Ex:
int a=10,b=20;
PROGRAMMING IN C –NEP SCHEME VVFGC MYSORE
int c=a+b;
#include<stdio.h>
void main( )
{
int age;
age=20;
printf(“%d”,age);
}
Points to remember:
Variables should be declared in the c program before
use.
Memory space is not allocated for a variable while
declaration. It happens only on the variable definition.
Variable initialization means assigning a value to the
variable.
Symbolic constants
Symbolic constant is the name that subsitutes for a
sequence of characters or a numeric constant, a character
constant or a string constant.
Syntax :
#define name text
Ex:
#define MAX 50
#define TRUE 1
#define FALSE 0
#define SIZE 15
A pre-processor is a system program, which comes into
action prior to compiler and it replaces the
replacement of text by the actual text
a string
getchar, putchar.
printf() function
The printf() function is used to print string or data values or a combination
of string and data values on the output screen (User screen). The printf()
function is built-in function defined in a header file called "stdio.h".The
printf (print formatted) standard library function is used to print the values
of expressions on standard output (i. e., display) in a specified format. A
typical call to the printf function takes the form of a C statement as
printf ( format _string, expr1, expr2, … ) ;
where exprl, expr2, … are expressions whose values are to be printed
and format_string specifies how they should be printed. The ellipsis (… )
indicates that the printf function call may contain variable number of
expressions. These expressions are optional and may be omitted.
Syntax:
printf(format_string,expr1,expr2…….expr-n);
scanf() function
The scanf() function is used to read multiple data values of different data
types from the keyboard. The scanf( ) function is built-in function defined in
a header file called "stdio.h
PROGRAMMING IN C –NEP SCHEME VVFGC MYSORE
The scanf() function is use to read data from the standard input
stream stdin into the locations given by each entry in argument-list.
Arguments must be pointers to variables whose types correspond to
those in format-string. Input fields are interpreted by a format-string,
which consists of a multibyte character string that begins and ends in
a shift state.
The scanf( ) function has the following syntax...
Syntax:
scanf("format strings",&variableNames);
Control Strings
Escape sequences
putchar() function
The putchar() function is used to display a single character on the
output screen. The putchar() functions prints the character which is
passed as a parameter to it and returns the same character as a
return value.
Ex:
#include<stdio.h>
#include<conio.h>
void main()
{
char ch = 'A';
putchar(ch);
}
PROGRAMMING IN C –NEP SCHEME VVFGC MYSORE
puts() function
The puts() function is used to display a string on the output screen. The
puts() functions prints a string or sequence of characters till the newline.
Ex:
#include<stdio.h>
#include<conio.h>
void main()
{
char name[30];
printf("\nEnter your favourite website: ");
gets(name);
puts(name);
}
getchar() function
The getchar() function is used to read a character from the keyboard
and return it to the program. This function is used to read a single
character.
Ex:
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
printf("\nEnter any character : ");
ch = getchar();
printf("\nYou have entered : %c\n",ch);
}
PROGRAMMING IN C –NEP SCHEME VVFGC MYSORE
getch()
The getch() function is similar to getchar function. The getch() function is
used to read a character from the keyboard and return it to the program.
This function is used to read a single character.
Ex:
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
printf("\nEnter any character : ");
ch = getch();
printf("\nYou have entered : %c",ch);
}
gets() function
The gets() function is used to read a line of string and stores it into a
character array. The gets() function reads a line of string or sequence
of characters till a newline symbol enters.
Ex:
#include<stdio.h>
#include<conio.h>
void main( )
{
char name[30];
printf("\nEnter your favourite website: ");
gets(name);
printf("%s",name);
}
PROGRAMMING IN C –NEP SCHEME VVFGC MYSORE
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Increment & Decrement Operators
5. Assignment Operators
6. Bitwise Operators
7. Conditional Operator
8. Special Operators
The arithmetic operators are the symbols that are used to perform basic
mathematical operations like addition, subtraction, multiplication, division
and percentage modulo. The following table provides information about
arithmetic operators.
+ Addition 10 + 5 = 15
- Subtraction 10 - 5 = 5
* Multiplication 10 * 5 = 50
/ Division 10 / 5 = 2
PROGRAMMING IN C –NEP SCHEME VVFGC MYSORE
⇒ The addition operator can be used with numerical data types and
character data type. When it is used with numerical values, it performs
mathematical addition and when it is used with character data type values,
⇒ The remainder of the division operator is used with integer data type only.it
performs concatenation in strings (appending).
Operato
r Meaning Example
Operato
r Meaning Example
The logical operators are the symbols that are used to combine multiple
conditions into one condition. The following table provides information
about logical operators.
Operato
r Meaning Example
&& Logical AND - Returns TRUE if all conditions are TRUE 10 < 5 && 12 > 10 is
otherwise returns FALSE FALSE
|| Logical OR - Returns FALSE if all conditions are FALSE 10 < 5 || 12 > 10 is
otherwise returns TRUE TRUE
! Logical NOT - Returns TRUE if condition is FLASE and !(10 < 5 && 12 > 10)
returns FALSE if it is TRUE is TRUE
⇒ Logical AND - Returns TRUE only if all conditions are TRUE, if any of the
conditions is FALSE then complete condition becomes FALSE.
⇒ Logical OR - Returns FALSE only if all conditions are FALSE, if any of the
conditions is TRUE then complete condition becomes TRUE.
PROGRAMMING IN C –NEP SCHEME VVFGC MYSORE
The increment and decrement operators are called unary operators because
The increment operators adds one to the existing value of the operand and
the decrement operator subtracts one from the existing value of the operand.
operators.
Operator Meaning
The increment and decrement operators are used infront of the operand (++a) or
after the operand (a++). If it is used infront of the operand, we call it as pre-
increment or pre-decrement and if it is used after the operand, we call it as post-
increment or post-decrement.
Pre-Increment or Pre-Decrement
In the case of pre-increment, the value of the variable is increased by
one before the expression evaluation.
In the case of pre-decrement, the value of the variable is decreased by
one before the expression evaluation.
PROGRAMMING IN C –NEP SCHEME VVFGC MYSORE
Ex:
#include<stdio.h>
#include<conio.h>
void main( )
{
int i = 5,j;
j = ++i; // Pre-Increment
Post-Increment or Post-Decrement
when we use post-increment or post-decrement, first the expression is
evaluated with existing value, then the value of the variable is incremented
or decremented by one.
Ex:
#include<stdio.h>
#include<conio.h>
void main( )
{
int i = 5,j;
j = i++; // Post-Increment
printf("i = %d, j = %d",i,j);
}
The assignment operators are used to assign right-hand side value (Rvalue)
to the left-hand side variable (Lvalue). The assignment operator is used in
different variants along with arithmetic operators. The following table
describes all the assignment operators in the C programming language.
+= Add both left and right-hand side values and store the result into left- A += 10
hand side variable ⇒A=
A+10
-= Subtract right-hand side value from left-hand side variable value and A -= B
store the result ⇒ A = A-B
into left-hand side variable
*= Multiply right-hand side value with left-hand side variable value and A *= B
store the result ⇒ A = A*B
into left-hand side variable
/= Divide left-hand side variable value with right-hand side variable A /= B
value and store the result ⇒ A = A/B
into the left-hand side variable
%= Divide left-hand side variable value with right-hand side variable A %= B
value and store the remainder ⇒A=A
into the left-hand side variable %B
Example
A = (10<15)?100:200; ⇒ A value is 100
sizeof operator
This operator is used to find the size of the memory (in bytes) allocated for a
variable. This operator is used with the following syntax .
sizeof(variableName);
Example:
Operator Precedence
Operator Associativity
Operator associativity is used to determine the order of operators with equal
precedence evaluated in an expression. In the c programming language,
when an expression contains multiple operators with equal precedence, we
use associativity to determine the order of evaluation of those operators.
PROGRAMMING IN C –NEP SCHEME VVFGC MYSORE
PROGRAMMING IN C –NEP SCHEME VVFGC MYSORE
C Expression Evaluation
In the C programming language, an expression is evaluated based on the
associativity. The operator with higher precedence is evaluated first and the
17
Type conversion
In a c programming language, the data conversion is performed in two
different methods as follows...
1. Type Conversion
2. Type Casting
Type Conversion
PROGRAMMING IN C –NEP SCHEME VVFGC MYSORE
The type conversion is the process of converting a data value from one
data type to another data type automatically by the compiler.
Sometimes type conversion is also called implicit type conversion.
The implicit type conversion is automatically performed by the
compiler.
If the operands are of two different datatypes then an operand having
lower datatype is automatically converted into higher datatype
Ex:
#include<stdio.h>
#include<conio.h>
void main( )
{
int i = 95 ;
float x = 90.99 ;
char ch = 'A' ;
i=x;
printf("i value is %d\n",i);
x=i;
printf("x value is %f\n",x);
i = ch ;
printf("i value is %d\n",i);
}
Typecasting
Typecasting is also called an explicit type conversion. Compiler
converts data from one data type to another data type implicitly.
When compiler converts implicitly, there may be a data loss. In such a
case, we convert the data from one data type to another data type
using explicit type conversion. Forcefull type conversion and done by
the programmer. To perform this we use the unary cast operator.
PROGRAMMING IN C –NEP SCHEME VVFGC MYSORE
To convert data from one type to another type we specify the target
data type in parenthesis as a prefix to the data value that has to be
converted. The general syntax of typecasting is as follows.
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c ;
float avg ;
printf("Enter any three integer values : ");
scanf(“%d%d%d”,&a,&b,&c);
avg = (a + b + c) / 3 ;
printf( "avg before casting = %f”,avg);
avg = (float)(a + b + c) / 3 ;
printf( "avg after casting = %f”,avg);
return 0;
}