Introduction
C is a general-purpose, procedural, high-level
programming language used in the development of
computer software and applications, games, and
more.
C language was developed by Dennis M. Ritchie at
the Bell Telephone Laboratories in 1972.
Introduction
It is a powerful and flexible language which was first developed for the
programming of the UNIX operating System.
C is one of the most widely used programming languages.
C programming language is known for its simplicity and efficiency. It is
the best choice to start with programming as it gives you a foundational
understanding of programming.
Features
Simplicity and Efficiency: The simple syntax and structured approach
make the C language easy to learn.
Fast Speed: C is one of the fastest programming language. C is also a
compiler-based which is the reason for faster code compilation and
execution.
Features
Portable: C programs can be compiled and run on a wide range of
platforms and operating systems.
Pointers: C comes with pointers. Through pointers, we can directly
access or interact with the memory. We can initialize a pointer as an
array, variables, etc.
Features
Memory Management: C provides lower level memory management
using pointers and functions like realloc(), free(), etc.
Structured Language: C provides the features of structural programming
that allows you to code into different parts using functions which can be
stored as libraries for reusability.
Structure
Program:
#include <stdio.h>
int main() {
printf(“Welcome to Raaya Infotech Solutions!!”);
return 0;
}
Output:
Welcome to Raaya Infotech Solutions!!
C Variables
In C, variable is a name given to the memory location that helps us to
store some form of data and retrieves it when required.
It allows us to refer to memory location without having to memorize the
memory address.
Syntax:
data_type variable_name; // defining single variable
data_type variable_name1, variable_name2; // defining multiple variable
C Variables
There are 2 aspects of defining a variable:
1.Variable Declaration (Definition)
2.Variable Initialization
C Variables
1.Variable Declaration (Definition)
Before using a variable, it must be declared. Declaration specifies the
type of the variable and optionally initializes it with a value.
The syntax for declaring a variable is:
type variable_name;
C Variables
2.Variable Initialization
Variables can be initialized when they are declared. Initialization assigns
an initial value to the variable.
The syntax for initializing a variable is:
type variable_name = value;
C Variables
Rules for Naming Variables in C
1. A variable name must only contain alphabets, digits, and underscore.
2. A variable name must start with an alphabet or an underscore only. It
cannot start with a digit.
3. No white space is allowed within the variable name.
4. A variable name must not be any reserved word or keyword.
C Comments
The comments in C are human-readable explanations or notes in the
source code of a C program.
A comment makes the program easier to read and understand.
These are the statements that are not executed by the compiler or an
interpreter.
C Comments
Types of comments in C
In C there are two types of comments in C language:
• Single-line comment: A single-line comment in C starts with double
forward slash( // ) .
• Multi-line comment: The Multi-line comment in C starts with a
forward slash and asterisk ( /* ) and ends with an asterisk and forward
slash ( */ ).
Data Types
Each variable has an associated data type. It specifies the type of data
that the variable can store like integer, character, floating, double, etc.
Integer Variables: Used to store integer values.
Examples: int, short, long, long long.
Floating-point Variables: Used to store real numbers.
Examples: float, double, long double.
Character Variables: Used to store single characters.
Example: char.
Data Types
Data Types
Integer Data Type:
The integer datatype in C is used to store the integer numbers (any
number including positive, negative and zero without decimal part).
Range: -2,147,483,648 to 2,147,483,647
Size: 4 bytes
Format Specifier: %d
Data Types
Float Data Type:
In C programming, float data type is used to store single precision
floating-point values. These values are decimal and exponential numbers.
Range: 1.2E-38 to 3.4E+38
Size: 4 bytes
Format Specifier: %f
Data Types
Character Data Type:
Character data type allows its variable to store only a single character.
The size of the character is 1 byte. It is the most basic data type in C.
Size: 1 byte
Format Specifier: %c
Syntax of char: char var_name; (The char keyword is used to declare
the variable of character type)
Data Types
Double Data Type:
The double data type in C is used to store decimal numbers (numbers
with floating point values) with double precision. It can easily
accommodate about 16 to 17 digits after or before a decimal point.
Range: 1.7E-308 to 1.7E+308.
Size: 8 bytes.
Format Specifier: %lf.
Data Types
Void Data Type:
The void data type in C is used to specify that no value is present.
It does not provide a result value to its caller. It has no values and no
operations. It is used to represent nothing.
Void is used in multiple ways as function return type, function arguments
as void, and pointers to void.
Data Types
Void Data Type
Syntax:
// function return type void
void exit(int check);
// Function without any parameter can accept void.
int print(void);
// memory allocation function which
// returns a pointer to void.
void *malloc (size_t size);
sizeof Operator
The memory size refers to how much space a type occupies in the
computer's memory.
To actually get the size (in bytes) of a data type or variable, use the sizeof
operator,
Input and Output
C provides a standard set of functions to handle input from the user and
output to the screen or to files.
These functions are part of the standard input/output library <stdio.h>.
The most commonly used functions for Input/Output are scanf() and
printf() respectively.
Input and Output
Standard Output Function – printf():
The printf() function is used to print formatted output to the standard
output stdout (which is generally the console screen).
Syntax:
printf(“formatted_string”, variables/values);
Input and Output
Standard Input Function – scanf():
scanf() is used to read user input from the console. It takes the format
string and the addresses of the variables where the input will be stored.
Syntax:
scanf (“formatted_string”, address_of_variables/);
Format Specifiers
The format specifier in C is used to tell the compiler about the type of
data to be printed or scanned in input and output operations.
They always start with a % symbol and are used in the formatted string
in functions like printf(), scanf(), etc.
Format Specifiers
Keywords in C
Keywords are predefined or reserved words that have special meanings
to the compiler.
A list of keywords in C or reserved words in the C programming
language are mentioned below:
Identifiers in C
In C programming, an identifier is a name given to entities such as
variables, functions, structures, etc.
These names are used to uniquely identify and reference these entities
within the program. Identifiers follow certain rules and conventions:
Rules for naming identifiers:
1.They can consist of letters (both uppercase and lowercase), digits, and the
underscore character _.
2.The first character must be a letter or an underscore.
Identifiers in C
Rules for naming identifiers:
3.Identifiers are case-sensitive. For example, variable, Variable, and VARIABLE are
considered different identifiers.
4.They cannot be a keyword or reserved word in C. For example, int, for, if, etc., are
reserved for specific purposes and cannot be used as identifiers.
5.They should be up to 31 characters long as only the first 31 characters are
significant.
Constants in C
The constants refer to the variables with fixed values.
If you don't want others (or yourself) to change existing variable values, you can use
the const keyword.
Constants may belong to any of the data types.
Constants in C
The constants refer to the variables with fixed values.
If you don't want others (or yourself) to change existing variable values, you can use
the const keyword.
Constants may belong to any of the data types.