C.Module 01
C.Module 01
C.Module 01
1. Header Section
Includes the preprocessor directives.
Preprocessor directives: are the commands (#include, #define, etc.) that instruct the
preprocessor to perform specific tasks before compilation.
• Standard Header Files: These are provided by the C (stdio.h, string.h, math.h, stdlib.h).
#include <stdio.h>
2. Global Declarations
Declare variables, functions, and structures outside of any function.
6. Functions
Functions are blocks of code that perform a specific task. They can be user-defined or built-in.
7. Return Statement
The return statement in the main function indicates that the program has ended successfully
(Optional in C99 or later).
8. Comments
Comments are non-executable statements used to explain the code. They are ignored by the
compiler.
Key Points:
return 0;
}
return 0;
}
A variable is a named storage location in the computer's memory that holds a value that can be
changed during the program's execution. Variables are used to store data.
1. Local Variables: Declared inside a function or a block and accessible only within that
function or block.
void myFunction() {
2. Global Variables: Declared outside of all functions, usually at the top of the program.
These variables are accessible from any function within the program.
void myFunction() {
3. Static Variables: Retain their value between function calls. A static variable is initialized
only once and exists for the lifetime of the program.
void myFunction() {
printf("%d\n", count);
4. Extern Variables: Declared using the extern keyword, indicating that the variable is defined
elsewhere, typically in another file or module.
Summary
Data types define the type of data that a variable can hold. They determine the amount of
memory allocated for a variable.
C provides type modifiers to alter the size and range of basic data types:
Derived data types are derived from the fundamental data types. They include:
• Arrays: A collection of elements of the same data type stored in contiguous memory
locations.
o Example: int numbers[5] = {1, 2, 3, 4, 5};
• Pointers: Variables that store the address of another variable.
o Example: int *ptr; (a pointer to an integer)
• Structures (struct): A user-defined data type that groups different types of variables under
a single name.
struct Person {
char name[50];
int age;
float salary;
};
• Unions (union): A user-defined data type similar to structures but stores different types
of data in the same memory location.
union Data {
int i;
float f;
char str[20];
};
User-defined data types are types defined by the user using one or more of the derived data
types. These include:
Format
Data Type Description
Specifier
%d int Prints or reads a signed decimal integer
%i int Prints or reads a signed decimal integer (same as %d)
%u unsigned int Prints or reads an unsigned decimal integer
%f float Prints or reads a floating-point number (single precision)
%lf double Prints or reads a double-precision floating-point number
%c char Prints or reads a single character
%s char* (string) Prints or reads a string of characters (a character array)
%x unsigned int Prints an unsigned integer in hexadecimal (lowercase)
%X unsigned int Prints an unsigned integer in hexadecimal (uppercase)
%o unsigned int Prints an unsigned integer in octal format
%p void* (pointer) Prints a pointer address in hexadecimal format
Prints a floating-point number in scientific notation
%e or %E float or double
(lowercase or uppercase)
Prints a floating-point number in the shorter of %f or %e
%g or %G float or double
format
%ld long int Prints or reads a long signed integer
%lu unsigned long int Prints or reads an unsigned long integer
%lld long long int Prints or reads a long long signed integer
unsigned long long
%llu
int
Prints or reads an unsigned long long integer
%hhd signed char Prints or reads a signed char as a decimal integer
%hhu unsigned char Prints or reads an unsigned char as a decimal integer
%zd size_t Prints or reads a size_t (used for sizes of objects)
%% None Prints a literal % character
int main() {
int age = 25;
float height = 5.9;
double pi = 3.14159265359;
char initial = 'A';
char name[] = "John Doe";
return 0;
}
int main() {
int age;
float height;
char initial;
return 0;
}
int main() {
int num = 123;
float price = 123.456;
// Outputs: " 123"
printf("Integer with width 5: %5d\n", num);
// Outputs: "123.46"
printf("Float with precision 2: %.2f\n", price);
return 0;
}
int main() {
int age = 30; // Integer data type
float height = 5.9; // Float data type
char initial = 'J'; // Char data type
double pi = 3.14159265359; // Double data type
unsigned int count = 100; // Unsigned integer
return 0;
}
Some facts
- 10% 20%
- This takes input like this.
- 10a 20b
- This takes input like this.
• 1 bit – 0 or 1
• 2 bits – 2n
• 232 – 1/109 – integer capacity
• 264 – 1/1018 – long long integer capacity
• int does not take input more than 10 digits
• double –> long float
• %lf – long float