C Programming
C Programming
C Programming
Uses of C
*Most parts of Windows, Linux, and other operating systems are written in C.
*C is used to write driver programs for devices like Tablets, Printers, (etc.).
Comments
Comments is some text that does not get executed while the program runs and used
for the explanation or description of the code. The compiler ignores the comments
when it runs the code.
There are two types of comments: Single-line and Multi-line Comments
// This is /* This is
// Single-Line Multi-line
// Comment Comment */
Escape Sequence
Variables
Variables are the small amount of memory whose value may change while the
program runs. We create a variable in two steps in C: Declaration and Initialization.
*Notes:
It is the data type that stores either ‘true’ or ‘false’ value. It uses 1 Byte of memory
and works in binary; 1 for ‘true’, and 0 for ‘false’.
Ex. bool e = true;
printf(“%d”, e); // output will be 1.
*Notes:
-We must include <stdbool.h> to use this data type.
-The format specifier for it is %d.
Constants
Constants are the fixed values which can not be altered by the program during its
execution.
Ex. const double PI = 3.141592654;
*Notes:
-Convention says that constant name is written in uppercase, and the words are
separated by underscore (_). // MY_FAV_NUM
Arithmetic Operators
Arithmetic Operators are one of the elements in C that perform certain mathematical
calculation.
+ = for addition
- = for subtraction
* = for multiplication
/ = for division
% = for modular division
++ = for increment by
-- = for decrement by 1
*Notes:
Math Functions
If ( statement ) {
1st condition;
} else if ( statement ) {
2nd condition;
} else {
Last condition;
}
*Notes:
Switch Statements
Switch statement helps us to check a value for equality against many conditions. It is
more efficient alternative to using many ‘else if’ statements.
Syntax of it goes like this:
switch ( value ) {
case value1:
statement(s);
break; // break; is mandatory
….
…
default:
statement(s);
}
AND Logical Operator
AND logical operator returns true value if all given two or more conditions are true.
‘&&’ represents AND operator in C.
It can be used as:
OR Logical Operator
OR logical operator checks if at least one condition among all the given conditions is
true. ‘||’ represents OR operator in C.
It can be used as:
int math = 70
int science = 92
if ( math > 90 || science > 85 ) {
printf(“You can enroll my class.”);
} else {
printf(“Better luck next time:)”);
} // output will be “You can enroll my class”
NOT logical operator reverses the state of a condition. This means it returns false
value if the condition is true and vice-versa. ‘!’ represents NOT operator in C.
It can be used as:
bool sunny = false;
if ( !sunny ) {
printf(“It’s cloudy outside.”);
} else {
printf(“Wow! It’s sunny outside”);
} // output will be “It’s cloudy outside.”
Ternary Operator
Ternary operator is the shortcut to if/else statement while assigning or returning the
value. It can be used in functions while returning value.
Syntax of it goes like this:
Ex.
String Functions are pre-defined functions included in <string.h> header file, that help
us to perform certain operation on a string.
For Loops
In programming, a loop is the repetition of a block of code until the certain condition
is satisfied. For loop is one of the looping statements in C.
Syntax of it goes like this:
} While Loops
While loop repeats a block of code until the given condition remains satisfied. In
some cases, while loop might not execute at all.
Syntax of it goes like this:
while ( condition ) {
// code to be executed.
}
Do While Loop
Do While loop always executes a block of code first, THEN checks for the condition.
Syntax of it goes like this.
do {
// code to be executed.
} while ( condition ) ;
Nested Loop
Break is used to exit out of the entire loop / switch. Continue is used to skip the
current iteration of the loop and moves to the next iteration of the loop.
They can be used as:
Array
An array is the data structure that can hold many values of same data type in a single
variable.
Ex. int houseNumber [ ] = { 245 , 111 , 109 , 094 } ;
*Notes:
-To access the elements in an array, we need to use their indexes.
-The index of first element is 0, that of second is 1, an d so on.
-Ex. “ printf(“%d”, houseNumber [ 2 ] ); ” will print the value 109.
Loop + Array
struct biodata {
char name [25];
int age;
double gpa;
}; // ; is mandatory
int main() {
struct biodata firstPerson;
strcpy( firstPerson.name, “Unik” );
}
Array of Structs
#include <stdio.h>
struct employeeName {
char name[25];
int age;
};
int main() {