ملخص لغة البرمجة c

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

‫ ملخص لغة البرمجة‬c

‫منظمة‬ ‫رموز‬
C language is a structured language it uses some notations
#include <stdio.h> is a directive C preprocessor.
int main() : main is a program building block called function. Everything
‫تنفيذه‬
is C begins executing at function main.
• The entire line is called a statement. Semi colon at the end of the
statement is called terminator. Every statement has this
terminator.

Characters \n is not printed. \ is called escape character.


Varibles is something we to use to storage ( characters – number – etc )
Characters varibles :
Char …………[]=”……”;
And when we want to add it to a statement it will be like this

#include<stdio.h>
int main(){

char where[]="here";
printf("I want to stay %s", where);
int gimmyage= 33;
printf("\ngimmy is now %d",gimmyage);
return 0;
}
Data type in C language:
1) Numpers
a- Integers : countable numbers ( 1/28/2222/10000000/65656 )
int age = 45
b- Decimal number ( 2.5/3.66/10.5)
double gpa = 3.5
float price= 221.2
2) Character
a) we use ( char ……… = “ A” to store only one character in
storage
b) we use ( char……..[]=”ajckaj”; to store a phrase (bunch of
characters) in storage
if we but unsigned before any of them we can storage only positive
numbers and double value
also we have long int and short int these can maximized or minimized
the value which int can hold
prinntf specialize
to print symbols we cant print normally we have to put \ before th
symbol
example :
printf(“fuck u “ brad”); it should be like this printf(“fuck u \“ brad”);
%s to deified a phrase like > printf(“I love %s”, “football”);
%f to deified a Decimal or float number like > printf(“I love %f”, 500.32);
%d to deified a normal number like > printf(“I love %d”, 56);
%c to deified a character like > printf(“I love %c”,”a” );

Printf(“%f”,5.0 + 4.1); any math >> if we use %f one of the numbers


Must be decimal (000.00)
any math >> if we use %d both of the
numbers must be normal (0-1-2-3-4-5-
6-7-8-9 …………..)
Printf(“%d”, 4*5);

Some functions we can use


printf(“%d”, pow(3,2)); it will give us 3^2
printf(“%d”, sqrt(36)); it will give us 36 = 6
printf(“%d”, ceil(54.222)); it will give us 55 biggest neares number
printf(“%d”, floor(55.864)); it will give us 55 smallest nearest number

to add comment inside the code ( it will be ignored by system)


add /* blab la bla */
we can add const before any variable and that will make unchangeable
like
const int fav_num = 55
printf(“%d”, (fav_num)), usually programmer make const variable in
capital characters like FAV_NUM

scanf >>>> scanf(“%...., &…..);


we use scan f to get specific types of input from the user and we storage
them in variable .
examble
int much; double gpa ;
printf("how old are you?"); printf("enter your GPA ");
scanf("%d", &much); scanf("%lf", &gpa);
printf("your age is: %d", much ); printf("your GPA is: %lf", gpa );

char grade ; char name[3];


printf("enter your grade "); printf("enter your name: ");
scanf("%c", &grade); scanf("%s", name);
printf("your GPA is: %c", grade ); printf("your name is: %s", name);
• Most C programs preform arithmetic calculations.
(+)Add (-)subtract (*)Multiply / divide % modulus ‫باقي‬
‫القسمة‬
• In C if we write ab it is interpreted as two letter name or identifier.
• a*b is the multiplication.
• Arithmetic operators are binary operators
• i.e. 4+5 contains binary operator + and operands 4,5
• Integer division yields integer result.
19/4=4 19%4=3
• Parentheses are used in C expressions:
a*(b+c)

int nam, definition


int nam=2, deceleration
we use % to take the last digit and then we can spreart the digits
if we enterd 2 spearted in last examble scanf will not be able to deal
with so we can use another fuction wich is : fgets
fgets understand numers and characters etc.. without %....

char name[20];
printf("enter your name: ");
fgets(name , 20,stdin);
printf("your name is: %s", name);

Coding a calculater :
#include <stdio.h>

int main()
{
int num1;
int num2;
printf("enter the first number: ");
scanf("%d",&num1);
printf("enter the seconed number: ");
scanf("%d",&num2);
printf("the total is : %d",num1 + num2);
return 0;
}
Defining a function :
Defining a function in C language means creating a block of code that
performs a specific task and can be reused in the program

Steps to Define a Function in C:


1. Function Declaration: Declare the function before using it. This
tells the compiler about the function name, return type, and
parameters (if any).
2. Function Definition: Write the actual code for what the function
should do.
3. Function Call: Use the function in your program by calling it.
Structure of a Function:
A function typically has:
1. Return type: Specifies the type of value the function will return
(e.g., int, float, or void if it doesn’t return anything).
Void= nothing
#include <stdio.h> Declaration before
// Function declaration intmain()
int add(int a, int b);

int main() {
int result;
// Function call
result = add(5, 3);

// Print result
printf("The sum is: %d\n", result);
return 0;
}
// Function definition
Definition after
int add(int a, int b) { intmain()
return a + b; // Returns the sum of a and b
}
İf statment :
İn if statment && means and , || means or

== mens equal to != not equal to

• The solution to any computer program requires a series of action


in an order.
• A procedure has aciton to be executed This is called
algorithm.
has order to be executed
#include <stdio.h> ‫يعني أن‬

Unsigned counter

‫يمكنه فقط تخزين القيم غير السالبة‬


int main(void) {
‫ مما يعبر عن عداد إدخال‬... ,2 ,1 ,0 ‫مثل‬
unsigned int counter; ‫الدرجات بشكل أكثر دقة‬.

‫ نستخدم معه‬%u
int grade;
int total;
double average;
total= 0;
counter= 0;
printf("%s","enter your grade,-1 to end:");
scanf("%d",&grade);
while (grade!= -1){
total=total+grade;
counter=counter+1 ;
printf("%s","enter your grade,-1 to end:");
scanf("%d",&grade);}
if ( counter!= 0){
average = (float)total/counter;
printf("class average is %.2f\n", average);
}
else{
puts("no grades enter");
}
}
Examitation results:
#include <stdio.h>

int main(void) {
unsigned int student=1;
unsigned int passes=0;
unsigned int failurs=0;
int result;
while (student<=10){
puts("enter your grade(1=pass 0=fail)");
scanf("%d",&result);
if (result==1){
passes=passes+1;
}
else{
failurs=failurs+1;
}
student=student+1;
printf("passed%u\n",passes);
printf("faild%u\n",failurs);
if(passes>8){
puts("bounus to akdeja") ;
}
}
}
variable)(operator)(expression); c=c+3
Where operator is binary operator of (+)(-)(*)(/) or (%) can be written in
the form:
Variable operator = expression; c+=3
C+=3 c=c+3 adds 3 to c
d+=4 d=d+4
e*=5 e=e*5

It will print a+1


It will print a,
then will add 1

For is a loop give us the same work as while but less steps
#include <stdio.h>
#include <math.h>
int main()
{
int n, i, isPrime = 1;
printf("n=");
scanf_s("%d", &n);
if ((n % 2) == 0 && n > 2)
{
isPrime = 0;
}
else
{
for (i = 3; i < (n / 2); i = i + 2)
{
if ((n % i) == 0)
{
isPrime = 0;
break;
}
}
}
if (isPrime == 1)
{
printf("The number is Prime"); }
else
{
printf("The number is Not Prime");
}
return 0;
}

You might also like