Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Basic structure of C syntex of C programming with ...

The document provides a comprehensive overview of the basic structure and concepts of C programming, including sections like preprocessor directives, functions, operators, and control statements. It covers essential topics such as recursion, pointers, arrays, strings, structures, and the typedef keyword. Additionally, it emphasizes the importance of practice and utilizing resources for further learning.

Uploaded by

garvitmalik63
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Basic structure of C syntex of C programming with ...

The document provides a comprehensive overview of the basic structure and concepts of C programming, including sections like preprocessor directives, functions, operators, and control statements. It covers essential topics such as recursion, pointers, arrays, strings, structures, and the typedef keyword. Additionally, it emphasizes the importance of practice and utilizing resources for further learning.

Uploaded by

garvitmalik63
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

Basic Structure of a C Program


A C program typically consists of the following sections:
● Preprocessor Directives: These lines start with # and include header files that provide
access to standard library functions (e.g., #include <stdio.h> for input/output operations).
● Global Declarations: Variables declared outside any function have global scope,
meaning they can be accessed from anywhere in the program.
● Functions: The main function (main()) is the entry point of execution. Other functions
contain reusable blocks of code.
● Statements: Instructions that perform actions, such as calculations, input/output, or
control flow.
Hello World Example:
#include <stdio.h>

int main() {
printf("Hello, world!\n");
return 0;
}

2. Operators in C
Operators are symbols that perform operations on data. Here are the main types:
● Arithmetic Operators: +, -, *, /, % (modulo)
● Relational Operators: == (equals), != (not equals), >, <, >=, <=
● Logical Operators: && (and), || (or), ! (not)
● Assignment Operators: =, +=, -=, *=, /=, %=
● Bitwise Operators: & (and), | (or), ^ (xor), ~ (complement), << (left shift), >> (right shift)
● Increment/Decrement Operators: ++, --
● Conditional Operator (Ternary Operator): condition ? expression1 : expression2
3. Comments in C
Comments are used to explain code and are ignored by the compiler.
● Single-line comments: // This is a single-line comment
● Multi-line comments: /* This is a multi-line comment */
4. Conditional Statements
● if statement: Executes a block of code if a condition is true.
if (condition) {
// Code to execute if the condition is true
}

● if-else statement: Executes one block of code if a condition is true and another block if
it's false.
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}

● nested if and if-else statements: Allows you to embed if or if-else statements within
other if or if-else statements for more complex logic.
if (condition1) {
if (condition2) {
// Code to execute if both conditions are true
} else {
// Code to execute if condition1 is true but condition2 is
false
}
} else {
// Code to execute if condition1 is false
}

5. Switch Statement
The switch statement provides a way to execute different code blocks based on the value of an
expression.
switch (expression) {
case value1:
// Code to execute if expression == value1
break;
case value2:
// Code to execute if expression == value2
break;
default:
// Code to execute if expression doesn't match any case
}

6. Loop Control
● for loop: Repeats a block of code a specific number of times.
for (initialization; condition; update) {
// Code to execute repeatedly
}

● while loop: Repeats a block of code as long as a condition is true.


while (condition) {
// Code to execute repeatedly
}

● do-while loop: Similar to a while loop, but the code block is executed at least once
before the condition is checked.
do {
// Code to execute repeatedly
} while (condition);

● Nested loops: You can place loops inside other loops to create more complex iterations.
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 3; j++) {
// Code to execute in the inner loop
}
}
7. Functions
Functions are self-contained blocks of code that perform specific tasks.
● Definition:
return_type function_name(parameter_list) {
// Function body (code to execute)
return value; // (if the return type is not void)
}

● Example:
int add(int a, int b) {
return a + b;
}

● Function Properties:
○ Modularity: Break down complex problems into smaller, manageable parts.
○ Reusability: Use the same function multiple times in a program.
○ Maintainability: Easier to debug and update code.
● Function Types:
○ Library functions: Predefined functions (e.g., printf, scanf)
○ User-defined functions: Functions created by the programmer.
● Argument vs. Parameter:
○ Parameter: A variable in the function definition.
○ Argument: The actual value passed to the function when it's called.
8. Recursion
Recursion is a technique where a function calls itself.
● Definition:
void recursive_function(parameters) {
if (base_case) {
// Stop the recursion
} else {
recursive_function(modified_parameters);
}
}

● Example (factorial):
int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}

● Properties of Recursion:
○ Must have a base case to stop the recursion.
○ Each recursive call should move towards the base case.
○ Can be less efficient than iterative solutions (loops) due to function call overhead.
9. Pointers
A pointer is a variable that stores the memory address of another variable.
● Definition:
data_type *pointer_name;

● Example:
int num = 10;
int *ptr = &num; // ptr stores the address of num

● Pointer to Pointer: A pointer that stores the address of another pointer.


int num = 10;
int *ptr1 = &num;
int **ptr2 = &ptr1; // ptr2 stores the address of ptr1

● Pointers in Functions:
○ Call by value: A copy of the argument is passed to the function. Changes made
inside the function don't affect the original variable.
○ Call by reference: The address of the argument is passed to the function.
Changes made inside the function affect the original variable.
void swapByValue(int x, int y) {
int temp = x;
x = y;
y = temp;
}

void swapByReference(int *x, int *y) {


int temp = *x;
*x = *y;
*y = temp;
}

10. Arrays
An array is a collection of elements of the same data type stored in contiguous memory
locations.
● Definition:
data_type array_name[size];

● Example:
int numbers[5] = {1, 2, 3, 4, 5};

● Initialization: Can be initialized during declaration or element by element later.


● Pointer Arithmetic: You can perform arithmetic operations (addition, subtraction) on
pointers to move them through an array.
● Array is a Pointer: The array name itself acts as a pointer to the first element of the
array.
● Traversing an array: Use loops (e.g., for loop) to access each element of the array.
● Arrays as Function Arguments: Arrays are passed to functions by reference (the array
name, which is a pointer, is passed).
● Multidimensional Arrays: Arrays with more than one dimension (e.g., 2D arrays like
matrices).
int matrix[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };

11. Strings
A string in C is an array of characters terminated by a null character (\0).
● Definition:
char string_name[size];

● Example:
char message[] = "Hello";

● Initialization: Can be initialized with a string literal or character by character.


● String Format Specifier: %s is used in printf and scanf to work with strings.
● String Important Points: Always ensure there is enough space in the array to store the
string and the null terminator.
● String Functions: C provides standard library functions for string manipulation (e.g.,
strcpy, strcat, strlen in the string.h header file).
● Strings Using Pointers: You can use pointers to manipulate strings.
● String Library Functions:
○ strlen(str): Returns the length of the string.
○ strcpy(dest, src): Copies the source string to the destination string.
○ strcat(dest, src): Concatenates the source string to the end of the destination string.
○ strcmp(str1, str2): Compares two strings lexicographically.
12. Structures
A structure is a user-defined data type that groups together variables of different data types
under a single name.
● Definition:
struct structure_name {
data_type member1;
data_type member2;
// ... more members
};

● Example:
struct Student {
char name[50];
int roll_no;
float marks;
};

● Structure in Memory: Members of a structure are stored in contiguous memory


locations.
● Array of Structures: You can create arrays where each element is a structure.
struct Student students[100];

● Initialization of Structures:
struct Student s1 = {"John Doe", 123, 85.5};
● Pointer to Structures: You can use pointers to access and manipulate structures.
● Arrow Operator (->): Used to access members of a structure using a pointer to the
structure.
struct Student s1;
struct Student *ptr = &s1;
ptr->roll_no = 123; // Accessing roll_no using the arrow operator

● Passing Structure to Function: Structures can be passed to functions by value or by


reference.
13. typedef Keyword
The typedef keyword is used to create aliases for existing data types.
typedef unsigned int uint; // Now you can use uint instead of unsigned
int

14. Taking Input as Address


You can use the & operator with scanf to take input directly into a memory address (usually for
pointers).
int num;
printf("Enter a number: ");
scanf("%d", &num); // Store the input at the address of num

Important Notes:
● This is a comprehensive overview of the basic concepts of C programming.
● There are many more advanced topics and libraries in C that you can explore as you
learn more.
● Practice writing code regularly to solidify your understanding.
● Use online resources, tutorials, and books to deepen your knowledge.
● Don't hesitate to ask for help if you get stuck!
● https://github.com/SwadhinPanda/code.iblogger
● https://github.com/astajyoti1/Python-Training
● https://history-computer.com/how-to-use-switch-case-in-python/
● https://kuchtoseekho.com/javascript-2/

You might also like