CSE - 2 - C Programming
IDENTIFIERS
------------
1. Some compilers accepts max of 31 characters
2. Must begin with an alphabet (uppercase / lowercase), or underscore symbol ( _ ).
Subsequent characters can be another alphabet, digit, or underscore.
3. C language is Case-sensitive – Uppercase letters and Lowercase letters are
treated differently.
All of the below names are treated differently -
age AGE Age AGe AgE aGE agE
4. Keywords cannot be used as an Identifier.
VALID NAMES
IF
WHILE
SIGNED
Else
SWitch
5. No special characters (! @ # $ % ^ & * ...etc) are allowed in a name.
6. Blank spaces are not allowed in-between the names.
7. Name of identifier cannot be exactly same as of name of another identifier
within the scope of the function.
Example:
#include<stdio.h>
int main()
{
int ivar = 15;
float ivar = 20.5; // ERROR
printf("%d",ivar);
return 0;
}
8. Suppose we are making use of any header file in our program, there is one thing
we must pay attention to –Header files consist of pre-defined functions and
variables. We cannot make use of the same as our Identifier names.
Let us take example of the <math.h> Header file.
It consists of a pre-defined constant variable M_PI.
If we write our program as below – It will generate ERROR
Example:
#include<stdio.h>
#include<math.h>
int main()
{
int M_PI = 25;
printf("%d",M_PI);
return 0;
}
KEYWORDS
--------
• Also known as Reserved Words (or) Pre-defined Words
They convey a special meaning (or) purpose and can only be used in a specified
manner.
All of C language Keywords are in Lower-case
C language supports 32 keywords which are given below.
int long char float
double short signed unsigned
auto register extern static
if else for while
do switch case default
break continue goto sizeof
void return const typedef
struct union enum volatile
VARIABLES
----------
Containers - used to store data; temp
C is a Statically Typed Programming Language
Syntax:
DataType varName = value;
DataType varName;
DataType v1,v2,v3,....,vN;
Example:
int a;
float marks;
double salary;
char gender;
char name[50]; // Character Array - Strings
Example:
int a = 25;
float marks = 85.3;
double salary = 87523.254;
char gender = 'F';
char name[50] = “Anushya Srikanth”;
Programming Language --> 2 types
a. Statically Typed Programming Languages --> C, C++, Java, C#
Variables and Data Types are binded together
Syntax: DataType varName = value;
b. Dynamically Typed Programming Languages --> Python,
JavaScript, R,Ruby
Variables are created without mentioning the data types.
Based on the value given, interpreter decides the data types.
Syntax: varName = value;
Point 1: While declaring the type of variable we can also initialize it as shown
below –
int i=10, j=25 ;
float a=1.5, b=1.99 + 2.4 * 1.44 ;
Point 2: The order in which we define the variables is sometimes important
sometimes not.
For Example: (Not Important)
int i=10, j=25;
int j=25, i=10;
For Example: (Important)
float a=1.5, b=a+3.1;
float b=a+3.1, a=1.5; // ERROR
The following statements would work
int a, b, c, d ;
a = b = c = 10; // VALID
However, the following statement would not work
int a = b = c = d = 10; // ERROR
DATA TYPES
-----------
1. It tells the compiler the type of information we are going to store within the
variable.
2. Memory Allocation - how much bytes of memory needs to be allocated for a
variables is decided based on the data type.
Primitive / Basic / Fundamental Data Types
a. int --> whole numbers
b. float --> decimal (or) Fractional values; 6 decimal values
c. double --> decimal (or) Fractional values; 12 to 16 decimal
values
d. char --> Single character enclosed within single quotation
e. void --> means Nothing; Data Type of Function & Pointers
1. Statically Typed
* Compiler based languages
* Variables must be declared before using it within a
program.
* Mandatory to mention the Data Type of the variable
while declaration.
Syntax: datatype varName = value;
Languages: C, C++, Java, C#
2. Dynamically Typed
* Interpreter based languages
* Variable declaration not required
* Data Type need not be mentioned while creating variables. The interpreter
automatically assigns the data type according to the value given for the variable.
Syntax: varName = value;
Languages: Python, JavaScript, R, Ruby
Data Types
-----------
1. It gives me an idea about the type of information/data planning to be store
inside of the variable.
2. Memory Allocation - how much of memory is required to store the value is decided
based on the data type of that variable.
int a; // 4 bytes
char b; // 1 byte
double c; // 8 bytes
1. Primitive / Fundamental / Basic --> 5 types
int, float, double, char, void
4 4 8 1 1
2. Derived --> 3 types
Arrays, Functions, Pointers
3. User-Defined --> 3 types
Structure, Union, Enumerated
Modifiers --> signed, unsigned, short, long
signed: can accept both +ive and -ive numbers
unsigned: can accept only +ive numbers
changing the data type's functionality
short - 2 bytes (int)
long - 8 bytes (int, double)
long double - 16 bytes
Format Specifiers
------------------
Integers:
signed short int : %hi
unsigned short int : %hu
signed int : %d
unsigned int : %u
signed long int : %ld
unsigned long int : %lu
int : %i
Octal : %o
Hexadecimal : %x (or) %X
Real Numbers:
float : %f
double : %lf
long double : %Lf
Character:
char : %c
char Array (String) : %s
C Programming Fundamentals / Foundation
-----------------------------------------
1. Introduction to C Programming
2. Comments
3. Keywords
4. Identifiers
5. Data Types
6. Variables
7. Constants - Immutable values
8. Literals
9. Type Casting/Conversion
10. Input & Output Functions
11. Operators
12. Structure of a C Program
13. Control Statements
a. Decision Making
b. Looping
c. Jump
Computer --> Binary: 0 and 1
Conversion:
Program gets converted to Binary (Machine Language)
Software: Compilers, Interpreters (Translators)
Compilers --> C, C++
Interpreters --> Python, JavaScript, R
Compilers: It is a Software used for converting High-level language into Binary
Code.
Steps:
1. File - Code
2. Save the File --> FileName.c
3. Compilation (Syntax Checking, Binary Code)
4. Run/Execute --> Output
PRE-PROCESSOR DIRECTIVE
------------------------
Before the Program's Compilation -
# Pre-Processor
HEADER FILES
-------------
Pre-defined Functions are present inside the Header Files. If we want to make use
of the Function, we need to include that header file within the program.
Syntax: <HeaderFilename.h> --> pre-defined
Syntax: "HeaderFilename.h" --> user-defined
.h --> File extension
Common Header Files used
-------------------------
<stdio> --> Standard Input & Output
printf(), scanf()
<math> --> Mathematical Functions
sqrt(), pow()
<conio> --> Console Input & Output
clrscr(), getch()
<string> --> String Manipulation Functions
strlen(), strcat(), strcpy(), strcmp()
<stdlib> --> Standard Library
exit(),
DMA - Dynamic Memory Allocation (Pointers)
malloc(), calloc(), realloc(), free()
main() Function --> Entry point of a program's execution. Without main() program
will compile but will not execute(run).
main() -- lowercase
OS will execute the main() function.
return value from the main() function will be sent to the OS
Significance of the return value ZERO:
#include<stdio.h>
int main()
{
printf("Welcome to C Programming");
return 0;
}
; -> Statement terminator
Escape Sequences:
\n -> Newline
\t -> Horizontal Tabspace
\b -> Backspace
\r -> carriage return: Moves the cursor position to the starting line
#include<stdio.h>
int main()
{
printf("\nab");
printf("\bsi");
printf("\rha");
return 0;
}
Output:
hai