C For Biginer
C For Biginer
BEGINNER SLIDEBOOK
AUTHOR
• Author Name: Mahmoud said ()محمود سعيد
• Exist: Egypt, Alex
• Works: instructor in Computer science
• Full stack developer
• Logic design and algorithms
• Contact Author: https://www.facebook.com/mahmoud.said.NST
• The Following content is provide New System Technology Training Organization
• https://www.facebook.com/NSTechnologyO
INTRODUCTION
• Easy to learn
• It is portable
• Fast, powerful and efficient
• solving problems for software or hardware
• C is the native language of Linux/Unix OS
• Easy connecting with system devices
C, HOW TO COMPILE
STRUCTURE OF PROGRAM IN C
• This program print hello tech.
• #include <stdio.h>
int main()
{
printf(“hello Tech”);
return 0;
}
1. #include : this is a preprocess to tell compiler link this program with library and including all of
codes to run it and stdio.h file it’s file have lot of codes called shared library.
2. Int is a return data type it’s short name for integer number.
3. Main() : this main function to executes program c programs cloud consist of one or more functions,
but only function called main.
4. Printf() : it is function to print on user screen, return 0; return value 0 to operating system to
terminate program, ( ; ) semicolon write to end any statement.
CASE SENSITIVE LANGUAGE
• C is a case sensitive language, so the names of the function must be typed in lower
case if it actually lower.
• We can use white spaces, taps and new line characters to make our code easy to
read.
VARIABLES
• Variables is a visual space allocate memory items that you can access memory to write and read
actually data in short memory.
• All of variables pointer of address in memory to keep data and access at run time.
• Variables has more of types called Data Type to give programmer flexibility to interact with end-
user data
• Variables name has some rule as :-
• Can’t use any character like(!,@,#,%,^,&) but you can use _ or $
• can’t use numbers in first like (1hello,8wow) but you can use number within word like hello1,go2o
• An operator is a symbol that tells the compiler to perform specific mathematical or logical
functions. C language is rich in built-in operators.
• Assignment Operators (=, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=)
• Misc Operators ( sizeof(), &, *, ? : )
USING OPERATORS
• Asthmatic : int a = 5 + 3;
• Relational : int A = 3 , B = 3; (A == B) is true.
• Logical : (A && B) is true.
• Assignment : C = A + B will assign the value of A + B to C
• C += A is equivalent to C = C + A
• Misc : sizeof(a), where a is integer, will return 4.
INPUT/OUTPUT
• int main(){
char name[20];
printf("please Enter your name : ");
scanf("%s",name);
printf("Welcome MR.%s", name);
return 0;
}
CONTROL FLOW
• If condition
• If else condition
• Nested if
• Switch
• While
• Do while
• For loop
• Nested for loop
• For each loop
CONTROL FLOW – IF CONDITION
• The ability to control the flow of your program, letting it make decisions on what code to execute
• Without a conditional statement such as the if statement, programs would run almost the exact same way
every time, always following the same sequence of function calls.
• Example :
if ( 55 < 100 )
printf( “55 is now less than 100" );
• If condition check is true then code will execute if false not excite and jump to an other code
• Syntax has 2 deferent way
• if ( statement is TRUE ) Execute this line of code
• if ( TRUE ) {
/* between the braces is the body of the if statement */
Execute all statements inside the body
}
CONTROL FLOW – IF ELSE CONDITION
• when the condition in an if statement evaluates to false, it would be nice to execute some code
instead of the code executed when the statement evaluates to true
if(Boolean expression) {
/* statement will execute if the Boolean expression is true */
}
else {
/* statement will execute if the Boolean expression is false */
}
CONTROL FLOW – NESTED IF
• If we want make some rule or condition for some operation so we
want other solution to make data flow or make multi choices as
If-else if like this Example :
int a = 100;
if( a == 10 ) { printf("Value of a is 10\n" );
} else if( a == 20 ) { printf("Value of a is 20\n" );
} else if( a == 30 ) { printf("Value of a is 30\n" );
} else { printf("None of the values is matching\n" );
}
printf("Exact value of a is: %d\n", a );
CONTROL FLOW – SWITCH
• A switch statement allows a variable to be tested for equality against a list of values. Each value is called
a case, and the variable being switched on is checked for each switch case. Like Example:
char grade = 'B';
switch(grade) { case 'A' : printf("Excellent!\n" ); break;
case 'B' : case 'C' :
printf("Well done\n" ); break;
case 'D' :
printf("You passed\n" );
break;
default :
printf("Invalid grade\n" );
}
CONTROL FLOW – WHILE LOOP
• when a block of code needs to be executed several number of times.
• while loop in C repeatedly executes a target statement as long as a given condition is true.
• The syntax of while is:
while(condition) {
statement
}
• Example:
int a = 10;
while( a < 20 ) {
printf("value of a: %d\n", a);
a++;
}
CONTROL FLOW – DO-WHILE
• if the fact that it is guaranteed to execute at least one time then condition is true can re-execute
• The syntax of do…while()
do { statement } while( condition );
• Example:
int a = 1;
do {
printf("value of a: %d\n", a);
a = a + 1;
}while( a <= 10 );
CONTROL FLOW – FOR LOOP
• for loop is a repetition control structure that allows you to efficiently write a loop that
needs to execute a specific number of times.
• Example of for loop:
int a;
for( a = 10; a < 20; a ++ ){
printf("value of a: %d\n", a);
}
CONTROL FLOW – NESTED FOR LOOP
• Nested allow make loop inside loop with a deferent statement loop type.
• You can show For loop nested by an other for loop or while nested by do...while or can make For
loop inside while loop inside tow level of For.
• Example:
int i, j;
for(i = 1; i<6; i++) {
for(j = 1; j <= i; j++){
printf("*");
}
printf("\n");
}
FUNCTIONS
• Simple function
• Function prototype
• Overload function
• Passing parameter
• Inline function
FUNCTION –SIMPLE FUNCTION
• Functions like main or printf and scanf function it has many types to definition or declaration
• Any simple function contain return type, function name, parameter and function body.
• function may return a value like integer or any data type, but some function does return value
which declare it by void
• This is the actual name of the function. If you want call it you can use name to called as printf.
• parameter is like a placeholder. When a function is invoked, you pass a value to the parameter.
This value is referred to as actual parameter or argument.
• The function body contains a collection of statements
FUNCTION – PROTOTYPE
• Some time you want to make function make the same functionality with same name ,
but with other values or data type how do you make that?
• Using overloading function you can show other information in this link:
• https://stackoverflow.com/questions/479207/function-overloading-in-c
• Because it’s very complicated to explain for beginner .
FUNCTION – PASSING PARAMETER
• This example explain how to pass parameter by value:
void swap(int x, int y){
int temp;
temp = x;
x = y;
y = temp; }
int main () {
int a = 100, int b = 200;
printf("Before swap, value of a : %d\n", a );
printf("Before swap, value of b : %d\n", b );
swap(a, b);
printf("After swap, value of a : %d\n", a );
printf("After swap, value of b : %d\n", b );
return 0;
}
FUNCTION –INLINE FUNCTIO
• There are various ways to define inline functions; any given kind of definition might definitely emit
stand-alone object code, definitely not emit stand-alone object code, or only emit stand-alone
object code if it is known to be needed. Sometimes this can lead to duplication of object code, which
is a potential problem for following reasons:
• It wastes space.
• It can cause pointers to what is apparently the same function to compare not equal to one another.
• It might reduce the effectiveness of the instruction cache.
• Example:
inline int max(int a, int b) {
return a > b ? a : b;
}
FILE I/O
• You can make file and write and read and delete using only standard library stdio.h
• the standard input and output devices handled by C programming language. This
chapter cover how C programmers can create, open, close text or binary files for
their data storage.
WHAT ID CRWD?
• Example
FILE *fp = fopen("textFile.txt" ,"a");
• You can use the fopen( ) function to create a new file or to open an existing file. This
call will initialize an object of the type FILE
• filename is a string literal, which you will use to name your file, and access mode
MODE OF FILES
• r
• Opens an existing text file for reading purpose.
• w
• Opens a text file for writing. If it does not exist, then a new file is created. Here your program will start
writing content from the beginning of the file.
• a
• Opens a text file for writing in appending mode. If it does not exist, then a new file is created. Here your
program will start appending content in the existing file content.
• r+
• Opens a text file for both reading and writing.
• w+
• Opens a text file for both reading and writing. It first truncates the file to zero length if it exists, otherwise
creates a file if it does not exist.
• a+
• Opens a text file for both reading and writing. It creates the file if it does not exist. The reading will start
from the beginning but writing can only be appended.
READ FILE
fp = fopen("/tmp/test.txt", "w+");
fprintf(fp, "This is testing for fprintf...\n");
fclose(fp);
• You should be close file after write to don’t have any exception and to free memory.
DELETE FILES
• The C library function int remove(const char *filename) deletes the given filename so that it is no longer accessible.
• Example:
int result;
FILE *fp;
char filename[] = "file.txt";
fp = fopen(filename, "w");
fprintf(fp, "%s", "This is tutorialspoint.com");
fclose(fp);
result = remove(filename);
if(result == 0) {
printf("File deleted successfully");
}else {
printf("Error: unable to delete the file");
}
SIZE OF FILE
• Using group of functions as (fseek(),ftell())
• fseek() take 3 parameter return nothing and ftell() take pointer of FILE object return decimal number
FILE *fp;
char filename[80];
long length;
printf("input file name:");
gets(filename); // or scanf(“%s”,filename)
fp=fopen(filename,"r");
if(fp==NULL) {
printf("file not found!\n");
}else {fseek(fp,OL,SEEK_END);
length=ftell(fp);
printf("the file's length is %1dB\n",length);
fclose(fp);
}
return 0;
OTHER FAMOUS WORKS