Variables Operators and Input Output
Variables Operators and Input Output
This PDF is no way associated with PW, or any course teacher or professor. It is
compiled for free, by a volunteer on association with other volunteers who is
Disclaimer taking course and wished to spread the resource to all.
Insta @signaloninternet
Status Open
https://www.youtube.com/watch?
URL
v=rQoqCP7LX60&list=PLxgZQoSe9cg1drBnejUaDD9GEJBGQ5hMt&index=1
int main()
{
printf(“hello world”);
return 0;
}
include the standard input-output library ( stdio.h ). This library contains functions such as
printf() and scanf() which are used for input and output operations.
2. int main() { } : This is the main function of the C program. All C programs must have a
main() function as it serves as the entry point of the program. The int before main indicates
that the function returns an integer value. The curly braces { } mark the beginning and end
of the function body.
3. printf("hello world"); : This is a function call to printf() . It's a standard library function used
to print formatted output to the console. In this case, it prints the text "hello world" to the
console.
4. return 0; : This statement is used to return a value (0 in this case) from the main() function to
the operating system. It indicates that the program executed successfully. In C, returning 0
from main() is conventionally used to signify successful termination of the program.
2. \b (Backspace): Used to move the cursor one position to the left, typically used to remove
3. \f (Form Feed): Used to advance the output to the next logical page or clear the screen,
depending on the context.
4. \n (Newline): Used to move the cursor to the beginning of the next line.
5. \r (Carriage Return): Used to move the cursor to the beginning of the current line.
6. \t (Horizontal Tab): Used to insert a tab character, typically used for indentation or
alignment.
7. \v (Vertical Tab): Used to move the cursor to the next vertical tab stop, although its use is
less common.
9. \' (Single Quote): Used to insert a single quote character into the string, especially when
the string is enclosed in single quotes.
10. \" (Double Quote): Used to insert a double quote character into the string, especially when
the string is enclosed in double quotes.
11. \? (Question Mark): Used to insert a question mark character into the string.
12. \0 (Null Character): Used to represent the null character, often used to terminate strings in
C.
14. \ooo (Octal Character Representation): Used to insert a character represented in octal
format (e.g., \101 represents the character 'A').
Keywords in C
Keywords in C are reserved words that have predefined meanings and cannot be used for other
purposes such as naming variables or functions.
1. auto : Specifies automatic storage duration for a variable, meaning it is created and
destroyed automatically within a block.
3. case : Used within a switch statement to specify different code blocks to be executed based
on the value of an expression.
4. char : Used to declare character data types that represent individual characters or small
integers.
6. continue : Skips the rest of the current iteration of a loop and proceeds to the next iteration.
7. default : Specifies the default case in a switch statement, executed when none of the other
cases match.
8. do : Begins a do-while loop, which executes a block of code repeatedly until a specified
condition becomes false.
10. else : Used in conjunction with if statements to specify an alternative code block to execute
11. enum : Declares an enumeration type, a user-defined data type consisting of named integer
constants.
12. extern : Indicates that a variable or function is defined elsewhere in the program.
14. for : Begins a for loop, which iterates a specified number of times based on initialization,
condition, and increment/decrement expressions.
15. goto : Transfers control to a labeled statement within the same function.
19. register : Suggests to the compiler that a variable be stored in a CPU register for faster
access.
20. return : Used to exit a function and return a value to the calling function.
22. signed : Specifies that a data type can represent both positive and negative numbers.
24. static : Specifies that a variable or function retains its value or scope throughout the
program's execution.
25. struct : Defines a user-defined data type that groups together variables of different data
types under one name.
26. switch : Begins a switch statement, which allows for multi-way branching based on the value
of an expression.
27. typedef : Creates a new data type alias using an existing data type.
28. : Defines a user-defined data type that can hold values of different data types in the
union
29. unsigned : Specifies that a data type can represent only non-negative numbers.
30. void: Specifies that a function does not return a value or indicates the absence of a data
type.
32. while : Begins a while loop, which executes a block of code repeatedly as long as a
specified condition is true.
Data Types
1. Basic Data Types:
a. int: Represents integer values. It typically occupies 2 or 4 bytes depending on the system
architecture.
b. Pointer: Stores the memory address of another variable. Pointers allow dynamic memory
allocation and manipulation of memory addresses.
c. Structure (struct): Represents a collection of variables (of possibly different data types)
grouped together under a single name.
d. Union: Similar to a structure, but all members share the same memory location. Only one
member can hold a value at a time.
3. Qualifiers:
b. volatile: Indicates that the value of a variable can be changed unexpectedly, such as by
hardware or another thread.
4. Enumeration (enum): Defines a user-defined data type consisting of a set of named integer
constants.
5. Void: Represents an empty data type. It is used to specify that a function does not return
any value or that a pointer does not point to any specific data type.
a. Typedef: Allows the programmer to create their own data type names by assigning an
alias to an existing data type.
A type qualifier neither affects the range of values nor the arithmetic properties of the
declared object. They are used to indicate the special properties of data within an objec
const: The const qualifier specifies that a variable's value cannot be changed after
initialization. It makes the variable read-only. For example:
volatile: The volatile qualifier indicates that the value of a variable can be changed
unexpectedly, such as by hardware or another thread. It prevents the compiler from
optimizing accesses to the variable. For example:
2. Type Modifiers:
A type modifier modifies the base type to yield a new type. It modifies the range and the
arithmetic properties of the base type.
signed: The signed modifier is used with integer data types ( char , int , long ) to indicate
that the variable can hold both positive and negative values. It is optional, as integer
types are signed by default.
unsigned: The unsigned modifier is used with integer data types to indicate that the
variable can hold only non-negative values (zero or positive). It restricts the range of
values to be from 0 to the maximum positive value. For example:
short: The short modifier is used with integer data types to specify that the variable
should be smaller in size, typically occupying fewer bytes of memory. For example:
long: The long modifier is used with integer data types to specify that the variable
should be larger in size, typically occupying more bytes of memory. For example:
long long: The long long modifier is used with integer data types to specify an integer
type that is larger than long . It is typically used when long is not large enough to hold
the required range of values. For example:
_Bool: The _Bool modifier is used to specify a Boolean data type that can hold either
true or false values. It is equivalent to unsigned char and typically occupies 1 byte of
memory.
_Complex: The _Complex modifier is used to specify a complex data type that represents
complex numbers. It is typically used with float or double data types to represent the
real and imaginary parts of a complex number.
Formatting specifier
Formatting specifiers in C are used with input and output functions like printf() and scanf() to
specify the format in which data should be displayed or read.
1. %o: Used for formatting unsigned octal integers with printf() . For example:
2. %d: Used for formatting signed decimal integers with printf() and for reading signed
decimal integers with scanf() . For example:
3. %x: Used for formatting unsigned hexadecimal integers (lowercase) with printf() . For
example:
4. %X: Used for formatting unsigned hexadecimal integers (uppercase) with printf() . For
example:
5. %c: Used for formatting characters with printf() and for reading characters with scanf() .
For example:
6. %i: Used for formatting signed integers with printf() and for reading signed integers with
scanf() . It's typically used as a more flexible specifier that can interpret decimal, octal, or
hexadecimal integers based on the input prefix (none for decimal, '0' for octal, '0x' or '0X'
for hexadecimal). For example:
7. %u: Used for formatting unsigned decimal integers with printf() and for reading unsigned
decimal integers with scanf() . For example:
8. %ld: Used for formatting long integers with printf() and for reading long integers with
scanf() . For example:
9. %hd: Used for formatting short integers with printf() and for reading short integers with
scanf() . For example:
10. %lu: Used for formatting unsigned long integers with printf() and for reading unsigned long
integers with scanf() . For example:
11. %hu: Used for formatting unsigned short integers with printf() and for reading unsigned
short integers with scanf() . For example:
12. %f: Used for formatting floating-point numbers (float) with printf() . For example:
13. %e: Used for formatting floating-point numbers in exponential notation (lowercase 'e') with
printf() . For example:
14. %E: Used for formatting floating-point numbers in exponential notation (uppercase 'E') with
printf() . For example:
15. %g: Used for formatting floating-point numbers in either decimal or exponential notation,
depending on the value, with printf() . For example:
16. %G: Similar to %g , but uses uppercase 'E' for exponential notation if required. For example:
17. %lf: Used for formatting double-precision floating-point numbers with printf() . For
example:
18. %s: Used for formatting strings with printf() . For example:
19. %p: Used for formatting pointers with printf() to display memory addresses. For example:
Variables
Variables are fundamental components used to store and manipulate data.
1. Definition:
A variable is a named storage location in the computer's memory where data can be
stored and manipulated during program execution.
Variables have a data type that specifies the type of data they can hold, such as
integers, characters, floating-point numbers, etc.
Each variable has a unique identifier (name) that is used to access its value during
program execution.
2. Declaration:
3. Definition:
Definition is the process of both declaring a variable and allocating memory for it.
If a variable is defined but not initialized, it will contain garbage or unpredictable values.
4. Initialization:
Variables can be initialized at the time of declaration using the assignment operator ( = ),
like int x = 10; , or later in the program using assignment statements.
5. Scope:
The scope of a variable refers to the region of the program where the variable is
accessible.
Global variables: Defined outside of any function and are accessible throughout the
entire program.
Local variables: Defined within a block or function and are accessible only within
that block or function.
6. Lifetime:
The lifetime of a variable refers to the duration during which the variable exists in
memory.
Global variables exist throughout the entire execution of the program, from the time the
program starts until it terminates.
7. Naming Conventions: