C.Module 01

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

Here’s a typical outline:

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.

• #include - Used to include a header file.


• #define - Used to define macros or constants.
• #ifdef, #ifndef, #endif - Used for conditional compilation.
Header Files: are the files being included when using the #include preprocessor directive.
They contain the actual declarations or definitions that the program needs. (stdio.h, string.h,
math.h, stdlib.h)

• Standard Header Files: These are provided by the C (stdio.h, string.h, math.h, stdlib.h).
#include <stdio.h>

• User-defined Header Files: These are created by the programmer.


#include "myheader.h"

2. Global Declarations
Declare variables, functions, and structures outside of any function.

3. Main Function (Entry Point)


o Has the signature int main().
o Contains the program's logic.
int main() {
// Code execution starts here
return 0;
}
4. Variable Declaration
Declarations can be done at the beginning of a block or anywhere in C99 and later standards.

5. Body of the Program


This section contains the logic of the program, including expressions, statements, and control
structures.

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:

• C programs are case-sensitive.


• Semicolons are used to terminate statements.
• Comments are used to explain code (single-line comments start with //, multi-line
comments are enclosed in /* and */).
• Indentation is used to improve readability.
• The main function is the starting point of execution.
First C Program
#include <stdio.h>
int main()
{
printf("\aHello! I am Muhammad Manzoor Murshed.\nI am
learning C Programming at
Phitron.\a\n");

return 0;
}

- Printf() is a built-in or library function.


- A library function is a pre-defined or pre-written function that is provided as part of a
programming language's standard library.

Program to print % and \


#include <stdio.h>
int main()
{
printf("\\, %%, Tab\tTab\n\n");

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.

Key Characteristics of Variables

1. Name (Identifier): Each variable has a unique name (or identifier).


2. Data Type: Every variable has a specific data type that determines the kind of data it can
store.
3. Value: A variable holds a value, which can be changed throughout the program's
execution.
4. Memory Location: Variables are stored in specific locations in the computer's memory,
and their addresses can be accessed using pointers.
Types of Variables in C

1. Local Variables: Declared inside a function or a block and accessible only within that
function or block.

void myFunction() {

int localVar = 10; // Local variable

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.

int globalVar = 20; // Global variable

void myFunction() {

globalVar = 30; // Modifies the global variable

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() {

static int count = 0; // Static variable


count++;

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.

extern int num; // Declaration (defined elsewhere)


Naming Rules for Variables

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


• Subsequent characters can be letters, digits, or underscores.
• Cannot contain spaces or special characters like !, @, #, etc.
• Case-sensitive (age and Age are different variables).
• Should not be a reserved keyword in C (like int, return, void).

Summary

• Variable: A named memory location that holds a value.


• Data Type: Defines what kind of data the variable can hold.
• Declaration: Specifies the data type and name of the variable.
• Initialization: Assigns an initial value to the variable.

Data types define the type of data that a variable can hold. They determine the amount of
memory allocated for a variable.

Categories of Data Types in C

1. Basic (Primitive) Data Types


2. Derived Data Types
3. User-Defined Data Types

1. Basic (Primitive) Data Types

These are the fundamental data types provided by the C language.

• Int - Used to store whole numbers (both positive and negative).


o Size: Typically 2 or 4 bytes (depends on the platform and compiler).
• Float - Used to store single-precision floating-point numbers
o Size: Typically 4 bytes.
• Double - Used to store double-precision floating-point numbers
o Size: Typically 8 bytes.
• char - Used to store a single character.
o Size: 1 byte.
• Void - Represents the absence of any data type. Used primarily for functions that do not
return a value.
o Example: void myFunction() { /* function body */ }Modifiers
for Basic Data Types

C provides type modifiers to alter the size and range of basic data types:

• short: Reduces the size of an integer.


• long: Increases the size of an integer.
• signed: Specifies that a data type can hold both positive and negative values.
• unsigned: Specifies that a data type can hold only non-negative (positive) values,
effectively doubling the range of positive numbers.

Examples of Modified Data Types:

• short int or short: Smaller integer (typically 2 bytes).


• long int or long: Larger integer (typically 4 or 8 bytes).
• unsigned int: Non-negative integer.
• long double: Double-precision floating-point number with extended precision.

2. Derived 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];

};

• Enumerations (enum): A user-defined data type consisting of integral constants.

enum Color { RED, GREEN, BLUE };

3. User-Defined Data Types

User-defined data types are types defined by the user using one or more of the derived data
types. These include:

• Structures (struct): Groups different data types into a single unit.


• Unions (union): Allows different types of data to share the same memory location.
• Enumerations (enum): Allows the creation of named integer constants.
• Typedef: Creates new names (aliases) for existing data types to make the code more
readable.

typedef unsigned long int uli;


// Creates a new name 'uli' for 'unsigned long int'

Sizes and Ranges of Common Data Types

Data Type Size (bytes) Range


char 1 -128 to 127 or 0 to 255
unsigned char 1 0 to 255
int 4 -2,147,483,648 to 2,147,483,647 (4 bytes)
unsigned int 4 0 to 4,294,967,295 (4 bytes)
short int 2 -32,768 to 32,767
unsigned short int 2 0 to 65,535
long int 4 -2,147,483,648 to 2,147,483,647
unsigned long int 4 0 to 4,294,967,295
float 4 1.2E-38 to 3.4E+38
double 8 2.3E-308 to 1.7E+308
long double 10 or 16 3.4E-4932 to 1.1E+4932
Format Specifiers
Format specifiers in C are special placeholders used in formatted input/output functions like
printf() and scanf(). They indicate the type of data being printed or read, ensuring that
the correct format is used for different types of data.

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

Using Format Specifiers with printf()


#include <stdio.h>

int main() {
int age = 25;
float height = 5.9;
double pi = 3.14159265359;
char initial = 'A';
char name[] = "John Doe";

printf("Age: %d\n", age);


printf("Height: %.1f\n", height);
printf("Value of Pi: %.5lf\n", pi);
printf("Initial: %c\n", initial);
printf("Name: %s\n", name);
printf("Hexadecimal of age: %x\n", age);

return 0;
}

Using Format Specifiers with scanf()


#include <stdio.h>

int main() {
int age;
float height;
char initial;

printf("Enter your age: ");


scanf("%d", &age);

printf("Enter your height: ");


scanf("%f", &height);

printf("Enter your initial: ");


scanf(" %c", &initial);

printf("You entered age: %d, height: %.1f, and initial:


%c\n", age, height, initial);

return 0;
}

Width and Precision in Format Specifiers


#include <stdio.h>

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);

// Outputs: " 123.456"


printf("Float with width 8 and precision 3: %8.3f\n", price);

return 0;
}

Program for Varaibles and Data Types


#include <stdio.h>

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

printf("Age: %d\n", age);


printf("Height: %.1f\n", height);
printf("Initial: %c\n", initial);
printf("Value of Pi: %.11f\n", pi);
printf("Count: %u\n", count);

return 0;
}
Some facts

Scanf(“%d%% %d%%”, &a, &b);

- 10% 20%
- This takes input like this.

Scanf(“%da %db”, &a, &b);

- 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

You might also like