Variables Operators and Input Output

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Variables, Operators and Input/ Output

Category Lecture notes

Created @February 17, 2024 1:05 AM

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.

This PDF is simply to save your time.

Insta @signaloninternet

If you find this helpful, don't forget to


Reminder follow us on insta

Status Open

https://www.youtube.com/watch?
URL
v=rQoqCP7LX60&list=PLxgZQoSe9cg1drBnejUaDD9GEJBGQ5hMt&index=1

Updated @February 17, 2024 4:25 AM

The first program in C


#include<stdio.h>

int main()
{
printf(“hello world”);
return 0;
}

1. #include <stdio.h> : This is a preprocessor directive in C programming. It tells the compiler to

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.

Variables, Operators and Input/ Output 1


All escape sequences in C
Escape sequences are combinations of characters that represent special characters or
actions in strings within programming languages. They are typically preceded by a
backslash ( \ ).

1. \a (Alert): Used to produce an audible alert or bell sound.

2. \b (Backspace): Used to move the cursor one position to the left, typically used to remove

the last character printed.

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.

8. \\ (Backslash): Used to insert a backslash character itself into the string.

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.

13. \xhh (Hexadecimal Character Representation): Used to insert a character represented in


hexadecimal format (e.g., \x41 represents the character 'A').

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.

2. break : Used to terminate the execution of a loop or switch statement.

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.

Variables, Operators and Input/ Output 2


5. const : Specifies that a variable's value cannot be modified once it's initialized.

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.

9. double : Used to declare double-precision floating-point data types.

10. else : Used in conjunction with if statements to specify an alternative code block to execute

if the condition is false.

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.

13. float : Used to declare single-precision floating-point data types.

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.

16. if : Used to execute a block of code based on a specified condition.

17. int : Used to declare integer data types.

18. long : Used to declare long integer data types.

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.

21. short : Used to declare short integer data types.

22. signed : Specifies that a data type can represent both positive and negative numbers.

23. sizeof : Returns the size of a data type or variable in bytes.

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

same memory location.

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.

Variables, Operators and Input/ Output 3


31. volatile : Specifies that a variable's value can be changed unexpectedly, such as by
hardware or another thread.

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. char: Represents individual characters. It occupies 1 byte of memory.

c. float: Represents single-precision floating-point numbers. It typically occupies 4 bytes.

d. double: Represents double-precision floating-point numbers. It typically occupies 8


bytes.

2. Derived Data Types:


a. Array: Represents a collection of elements of the same data type arranged in contiguous
memory locations.

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:

a. const: Specifies that a variable's value cannot be modified after initialization.

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.

6. User-defined Data Types:

a. Typedef: Allows the programmer to create their own data type names by assigning an
alias to an existing data type.

Type Qualifiers & Modifiers


1. Type Qualifiers:

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:

Variables, Operators and Input/ Output 4


c const int x = 5; x = 10; // Error: Attempting to modify a const variable

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:

c volatile int sensor_reading;

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:

c unsigned int distance;

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:

c short int num;

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:

c long int big_num;

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:

c long long int very_big_num;

_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:

Variables, Operators and Input/ Output 5


printf("%o", 10); // Output: 12 (octal representation of 10)

2. %d: Used for formatting signed decimal integers with printf() and for reading signed
decimal integers with scanf() . For example:

int num = 42;


printf("%d", num); // Output: 42

3. %x: Used for formatting unsigned hexadecimal integers (lowercase) with printf() . For
example:

printf("%x", 255); // Output: ff (hexadecimal representation of 255)

4. %X: Used for formatting unsigned hexadecimal integers (uppercase) with printf() . For
example:

printf("%X", 255); // Output: FF (uppercase hexadecimal representatio


n of 255)

5. %c: Used for formatting characters with printf() and for reading characters with scanf() .
For example:

printf("%c", 'A'); // Output: A

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:

printf("%i", 42); // Output: 42 (decimal)


printf("%i", 052); // Output: 42 (octal)
printf("%i", 0x2a); // Output: 42 (hexadecimal)

7. %u: Used for formatting unsigned decimal integers with printf() and for reading unsigned
decimal integers with scanf() . For example:

printf("%u", 42); // Output: 42

8. %ld: Used for formatting long integers with printf() and for reading long integers with
scanf() . For example:

printf("%ld", 10000000000L); // Output: 10000000000

9. %hd: Used for formatting short integers with printf() and for reading short integers with
scanf() . For example:

Variables, Operators and Input/ Output 6


printf("%hd", (short)42); // Output: 42

10. %lu: Used for formatting unsigned long integers with printf() and for reading unsigned long
integers with scanf() . For example:

printf("%lu", 4294967295UL); // Output: 4294967295

11. %hu: Used for formatting unsigned short integers with printf() and for reading unsigned
short integers with scanf() . For example:

printf("%hu", (unsigned short)65535); // Output: 65535

12. %f: Used for formatting floating-point numbers (float) with printf() . For example:

printf("%f", 3.14); // Output: 3.140000

13. %e: Used for formatting floating-point numbers in exponential notation (lowercase 'e') with
printf() . For example:

printf("%e", 1000000.0); // Output: 1.000000e+06

14. %E: Used for formatting floating-point numbers in exponential notation (uppercase 'E') with
printf() . For example:

printf("%E", 1000000.0); // Output: 1.000000E+06

15. %g: Used for formatting floating-point numbers in either decimal or exponential notation,
depending on the value, with printf() . For example:

printf("%g", 1000000.0); // Output: 1e+06

16. %G: Similar to %g , but uses uppercase 'E' for exponential notation if required. For example:

printf("%G", 1000000.0); // Output: 1E+06

17. %lf: Used for formatting double-precision floating-point numbers with printf() . For
example:

printf("%lf", 3.141592653589793); // Output: 3.141593

18. %s: Used for formatting strings with printf() . For example:

printf("%s", "Hello, World!"); // Output: Hello, World!

19. %p: Used for formatting pointers with printf() to display memory addresses. For example:

Variables, Operators and Input/ Output 7


int x = 42;
printf("%p", (void *)&x); // Output: 0x7fffcd4c5a3c (memory address o
f variable x)

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:

Variable declaration typically follows the syntax: datatype variable_name;

3. Definition:

Definition is the process of both declaring a variable and allocating memory for it.

It reserves storage space in the computer's memory for the variable.

A variable can be declared and defined at the same time.

If a variable is defined but not initialized, it will contain garbage or unpredictable values.

Variable definition typically follows the syntax: datatype variable_name = initial_value;

4. Initialization:

Initialization is the process of assigning an initial value to a variable at the time of


declaration or later in the program.

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.

Variables, Operators and Input/ Output 8


Local variables are created when the block or function containing them is entered and
destroyed when the block or function exits.

7. Naming Conventions:

Variable names in C must follow certain rules and conventions:

Must start with a letter (uppercase or lowercase) or an underscore ( _ ).

Can contain letters, digits, and underscores.

Cannot be a keyword or a reserved word in C.

Case-sensitive (e.g., count and Count are different variables).

Commas, blanks, or any special characters are not allowed

Variables, Operators and Input/ Output 9

You might also like