c Programming Notes-merged
c Programming Notes-merged
Why Learn C?
Common Applications:
Fundamentals of C Programming
The character set in C consists of the basic building blocks used to write C programs.
Categories:
Identifiers
Identifiers are the names given to variables, functions, arrays, or other user-defined
elements in a C program.
Keywords
Keywords are reserved words in C that have a special meaning and cannot be used as
identifiers.
Examples of Keywords in C:
auto, break, case, char, const, continue, default, do, double, else, enum, extern, float,
for, goto, if, int, long, register, return, short, signed, sizeof, static, struct, switch, typedef,
union, unsigned, void, volatile, while
Constants are fixed values that do not change during the execution of a program.
1.5 Variables
Variables are named storage locations in memory that hold data, which can be modified
during program execution.
data_type variable_name;
Example:
int age;
float salary;
char grade;
2. Operators in C
4. Library Functions
5. Preprocessor
Eg- #include
C provides various functions for input and output operations. These functions are
included in the standard library and defined in <stdio.h>.
● scanf(): Reads formatted input (numbers, characters, strings, etc.) from standard
input.
● printf(): Outputs formatted data to the standard output.
Branching allows the program to make decisions and execute different code blocks based on
conditions.
1.1 If Statement
if (condition)
Executes one block of code if the condition is true and another block if it is false.
if (condition)
else
if (condition1)
else if (condition2)
else
2. Looping in C
Loops are used to execute a block of code repeatedly until a condition is satisfied.
2. 1 While Loop
while (condition)
// Code to execute
do
// Code to execute
} while (condition);
2.3 For Loop
The most commonly used loop; initializes, tests, and increments in a single line.
// Code to execute
switch (expression)
case value1:
break;
case value2:
break;
default:
}
3.2 Continue Statement
Skips the current iteration of a loop and moves to the next iteration.
printf("%d\n", i);
goto label;
label:
// Code to execute
1. Functions in C
Functions are blocks of code designed to perform specific tasks, which can be reused
multiple times in a program.
return_type function_name(parameters)
{
// Function body
return value; // If required
}
example-
#include <stdio.h>
int add(int a, int b)
{
return a + b;
}
A copy of the argument's value is passed to the function. Changes made inside the
function do not affect the original value.
The actual memory address of the argument is passed, allowing changes to affect the
original variable.
1.4 Recursion
An array is a collection of variables of the same data type stored in contiguous memory
locations.
Concepts of Arrays
2 Strings in C
#include <stdio.h>
int main()
{
char names[3][10] = {"Alice", "Bob", "Eve"};
for (int i = 0; i < 3; i++)
{
printf("Name: %s \n", names[i]);
}
return 0;
}
3 Structures in C
Structures (also called structs) are a way to group several related variables into one
place. Each variable in the structure is known as a member of the structure.
Unlike an array, a structure can contain many different data types (int, float, char, etc.).
You can create a structure by using the struct keyword and declare each of its members
inside curly braces:
struct MyStructure
{ // Structure declaration
int myNum; // Member (int variable)
char myLetter; // Member (char variable)
}; // End the structure with a semicolon
Use the struct keyword inside the main() method, followed by the name of the structure
and then the name of the structure variable:
Create a struct variable with the name "s1":
struct myStructure
int myNum;
char myLetter;
};
int main()
return 0;
}
1. Pointers in C
Pointers are variables that store the memory address of another variable.
1.2 Address Operator (&)- is used to get the memory address of a variable.
int a = 10;
data_type *pointer_name;
int a = 10;
Access the value stored at the memory address pointed to by the pointer.
int a = 10;
Files are used in C programming to store data permanently on a disk. File handling allows you
to create, open, read, write, and close files.
1. File Operations in C
2. Opening a File
FILE *fopen(filename,mode);
3. Creating a File
Example:
if (file != NULL)
fclose(file);