0% found this document useful (0 votes)
343 views

C Language

The document provides an introduction to the C programming language. It discusses some key features of C including that it is a general purpose, structured, and portable language. It also presents the basic structure of a C program, noting it must include standard header files, contain a main function defined between curly brackets, and end statements with semicolons. The document then outlines some basic data types in C like integers, floating point numbers, and characters.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
343 views

C Language

The document provides an introduction to the C programming language. It discusses some key features of C including that it is a general purpose, structured, and portable language. It also presents the basic structure of a C program, noting it must include standard header files, contain a main function defined between curly brackets, and end statements with semicolons. The document then outlines some basic data types in C like integers, floating point numbers, and characters.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 70

C

* - : C C -:
- : .

: Structured Language C . .

) ( - : Bit manipulation . Bit Bit


. byte

: Portable C IBM . Apple

- - : .

- : C

ANSI

The Basic Structure of c Program


C> # include < stdio .h
) ( main
{
; ) " printf ( " hello egypt
}

> # include < h . include

- ) ( main { } .

. - ) ; (

/* */

} {


* -:

Data Type

) ( integer ) ( . float

* byte -:

) ( char

) ( int

) ( long

) ( float

e- 38 e + 38

e -308 e+308

) ( double
- :
- : .

- : ) ( .

) : (Long

: 5 .33 - : .

* - : -:
-

HP hp

.
* - :
C Variables C ++

; int a
; float

* operators

- arithmetic operators
+

addition

Subtraction

multiplication

division

- -:Relational operators :

>

10>8

less than

<

10<8

equal to

==

==

not equal to

=!

!=

less than or equal to

=<

< = 8

greater than or equal to

=>

10 > = 8

greater than

- Logical operator

AND

&&

10 > 8 && 9 > 7

OR

||

1 0 < 8 || 7 < 8

NOT

) ! (10 = = 8

- Assignment Operators

A = a +5
A=a5
A=a+5
A=a/3

A+=5
A-=5
A*=5
A/=3

11
1
30
2

+ = addition assignment operators


Subtraition assignment opertors
Multipication assibnment operators
Division assignment operators

- Decrement &increment

A=a+1

A ++

A=a1

A --

- %
)

a = 5 C = a % 2 1 = 5 / 2


* )(* printf
:
# include )( printf stdio.h #

> include < stdio.h

) ( printf ) ( String , char , float , int .

) " printf ( " welcome with compuscience


" "

; ) " printf ( " \n welcome \n with \n compuscience

: new line

welcome
with
compusciene

) (printf

\n

new line

) printf ( " \n

\t

) ( Tap

) print f ( " \t

\b

Backspace

) " print f ( " \b

\xdd

a printf ( x41 " ) :

hexadecimal
\ddd

octol
) ( each d represents a digit
double quate

\a

) (

a printf ( " \101 ) :


) ""\ " ( printf
) printf ( " \a

* :
/ * program name 1 * /
> # include < stdio.h
) ( main
{
; ) printf ( " \n this text display in new line
; ) printf ( " \n word1 \t lab1 \t tab2
; ) printf ( " \n bell \a bell \ b
; ) \ printf ( " \n this line display quotations
; ) printf ( " \n
}
* :
)( print f

; ) printf ( " % d , a
; ) printf ( " % f , b
% . .

%d

)printf ( " % d , - 10

( Signed decimal integer ) int


%f

) printf ( % p , 507

( floating point ) float


%c

) (

) printf ( % c , a

char Single character


%s

%u

(
) (unsigned decimal integer
hex

%o

) printf ( % s , is
String

%x

Qctal

) printf ( % u , 1
) printf ( % x , af
) printf ( % o , 67

*
/ * program name 2 * /
> # include < stdio.h
) ( main
{
; int a , b , c
; float f
; long t
; char ch = y
; " char name [10] = " aly
;a=5
; b = 10
;c=a+b
; ) printf ( \n c = % d , c
; ) printf ( \n f = % f , f
; ) printf ( \n name = % s , name
; )printf ( \n ch = % c , ch
; ) printf ( \n t = % ld , t
}

* *

> # include < stdio.h stdio.h


) ( printf

) ( main {

7 , 6 , 5

ch ) ( char y
name
Aly
A , B C
{

: : % 3F 534.6735

534.674

* ) ( *Scanf

) ( Print f
:

; ) scanf ( " % f " , &r

> # include < stdio.h


) ( main
{
; int a , b , c
; float r , s , t
;]char name [10
; ) " printf ( " \n \n enter your name :
; ) scanf ( " % s " , name
; ) " = printf ( " a
; ) scanf ( " % d " , & a
; ) " = printf ( b
; ) scanf ( % d " , & b
; ) " = printf ( " r
; ) " =printf ( " s
; ) scanf ( " % f " & s
; ) printf ( " \n welcome % s " , name
; ) printf ( " \n \n c = a + b = % d " , a + b
; ) printf ( " \n \n t = r + s = % d " , r + s
}


a , b , c , r , s , t , name
)( printf enter your name

) ( scan f name

) ( scan f ) scan f ( ) ( % d " , &a


a

# & #
: &a a a

&
* -:

enter your name : ahmed


a=5
b = 1
r = 2
s = 3

welcome ahmed
c = a + b = 15
t = r + s = 50
:

/ * function heading */
/* variable declarations */
/* output statement */
/* input statement */
/* assignment statement */
/* output statement */
* *

> # include < stdio .h


/ * program to calculate erea of circle * /
) ( main
{
; float redius, area
; ) ? = printf ( redius
; ) scanf ( % f , redius
; area = 3.14159 * redius * redius
; ) printf ( area = % f , area
}

) ( getchar ( ) , getche ( ) , getch

) ( ) - : getchar ( stdio .h

enter

; char a
; ) ( a = getchar
; ) printf ( % c , a
- ) ( ) - : getche ( conio .h


) ( getchar enter

; char a
; ) ( a = getche
; ) printf ( % c , a
) ( ) : getch ( conio.h


enter

; char a
; ) ( a = getch
; ) printf ( % c , a
* ) ( ) : putchar ( stdio.h
-

)' putchar ( ' a a * ) ( * gets stdio.h

) ( gets string

;]char name[20
; )gets (name
) ( gets Name
* ) ( * puts stdio.h

- string

char name[10] = ahmed


; )puts (name
; ) puts ( mohammed

ahmed
mohammed
: ] char name[10 name

*

) abs ( i

include
stdlib.h

) log ( d

math.h

) pow ( d1 , d2

d1 d1

math.h

) labs ( e l
) sin ( d
d

math.h
math.h

) sqrt ( d

math.h

) strcpy ( s1,s2

s2 s1

string.h

main .

- :

) (

Loop
- : for
:

; For ( initial value ; condition ; increment ) ; statement

: initial value
: condition

: increment

:
> # include < stdio.h
) ( main
{
; int i
) for ( i = 0 ; i < 10 ; i ++
; ) printf ( \ n i = % d " , i
}
* : For {

} .

> # include < stdio.h


/ * calculate the average of n numbers * /
) ( main
{
; int n , count
; float x , average , sum = 0
/ * initialize and read in a value for n * /

; ) " ? printf ( " how many numbers


; ) scanf ( " % d " , & n
/ * read in the numbers * /
) for ( count = 1 ; count < = n ; count ++
{
; ) = printf ( x
; ) scanf ( % f " ; & x
; sum + = x
}
/ * calculate the average and write out the answer * /
; average = sum / n
; ) printf ( " \ n the average is % f \n " , average
}
* For
-

loop

Loop .

> # include < stdio.h


) ( main
{
; int i , j
) for ( i = 1 ; i < = 12 ; i ++
{
; )" printf ( in
) for ( j = i ; j < = 12 ; j ++
{
; ) printf ( " i * j = % d " , i * j
}
}
}
: For
CTRL + C ) ;; ( For

:while while

:
) While ( condition
{
; statement 1
; statement 2
}

) While ( condition
; Statement

} { ) (
False . .

False

: While
> # include < stdio.h
) ( main
{
; int n , count = 1
; float x , average , sum = 0
; ) " ? printf ( how many number
; ) scanf ( % d , & n
; ) while ( count < = n
{
; )" = printf ( x
; ) scanf ( % f " , & x
; sum + = x
;count ++
}
; average = sum / n
; ) printf ( " \ n the average is % f " ; average
}

* For * While

- For

while while

- : Do while do while
. Do

) ( True
False

> # include < stdio.h


) ( main
{
; int count = 0
; int total = 0
do
{
; total + = count

; ) " ) printf ( count = % d , total = % d \ n , count ++ , total


}
; ) while ( count < = 10
}
: : . .

(
* : IF IF ) (

) if ( condition
; statement
) ( condition

if {
} :

) if ( condition
{
; statement 1
; statement 2
}
* if : if :
) if ( condition
) if ( condition
) if ( condition
.
* : if else :
) if ( condition
{
statement 1
}
else
{
statement 2
}

) ( condition true ) ( statement 1

) ( statement 2 if .else
.

* : if . else if

: IF - :
;i=5
; ) if ( i < 5
; ) " printf ( i less than 5
) if ( i = 5
; ) " printf ( " i equal to 5
) if ( i > 5
; ) printf ( i greater than 5


) if ( condition
; statement 1
) else if ( condition
; statement 2
) else if ( condition
; statement 3

:
;I=5
) if ( i < 5
; ) " printf ( i less than 5
) else if ( i = 5
; " printf ( i equal to 5
) else if ( i > 5
; ) " printf ( " i greater than 5
: -:
> # include < stdio.h
) ( main
{
; float num1 , num2
; char op
)while (1
{
; ) " printf ( type number , operator , number \ n

scanf ( % f % c % f ) , & num 1 , & op , & num 2 ) ;


if ( op = = ' + ' )
printf ( = % f " , num1 + num2 ;
else if ( op = = ' ' )
printf ( = % f , num1 num2 ;
else if ( op = = ' * ' )
printf ( " % f " , num1 * num2 ;
else if ( op = = ' / ' )
printf ( = % f " , num1 / num2 ;
printf ( " \ n \ n " ) ;
}
}

Switch . case -

Switch

Switch

Switch .. case :
#include < stdio.h>
main ( )
{
float num1,num2;
char ch,op;
do
{
printf( \in type num1 op num2: ) ;
scanf( %f %c %f , &num1,&op,&num2);
switch (op)
{
case +;
printf ( "sum = %f ", num1+num2);
break;
case -;
printf ( "sub = %f ", num1-num2);
break;

/* void main(void) */

;* case
;)printf ( "mul = %f ", num1*num2
;break
;case /
;)printf ( "div = %f ", num1/num2
;break
default:
;)printf\n unknowen operator .
}
;) printf(\n Again (y/n):
}
while (( ch=getch())== y ):
}
: switch .. case
) ( menu

break switch

( goto

>#include <stdio.h
) (main
{
;char ss
ss:
;)printf(\t ALLAH
;goto ss
}
- : goto
- CTRL+C

** **
) ( ) ( : clrscr conio.h

) (clrscr

) ( ) ( : gotoxy conio

x y -

) (gotoxy

: 30 )gotoxy(30,10
) ( ) ( : textcolor conio.h
-

)textcolor(color no

)textcolor(color name
.

BLACK

BLUE

GRCEN

CYAN

RED

MAGENTA

BROWN

LIGHTGRAY

DARKGRAY

LIGHBLUE

LIGHTGREEN

LIGHTRED

LIGHTRED

LIGHTMAGENTA

YELLOW

WHITG

:
-
: $ %
f=p(1+i)n
I p n
:
** **
#include <stdio.h>
#include <math.h>
main()
{
float p,r,n,i,f;
/* read input data */
printf( please enter a value for the princepal (p) : );
scanf(%f k &p);
printf(please enter a value for the interest rate( r) : );
scanf(%f, &r);
printf(please enter a value for the number or year ( n) : );
scanf(%f, &n);
/*calculate i , then f */
i=r/100
f=p*pow((1+i),n);
/* write output */

;)printf( \n the final value (f) is : %2f \n,f


}

-
ax2 + bx +c = 0

4 ac

=X

2 a
** **
># include <stdio.h
># include < math.h
/* real roots of a quadratic equation */
) (main
{
;float a,b,c,d,x1,x2
/* read input data */
;) =printf(a
;)scanf(%f,&a
;) =printf(b
;)scanf(%f,&b
;) =printf(c
;)scanf(%f,&c
/* carry out the calculations */
;)d=sqrt(b*b 4*a*c

;)x2=%e ,x1,x2
:

;)x1=(-b+d)/(2*a
;)x2=(-b-d)/(2*a
/* write output */
printf(x1= ,%e
}

: %e

x 1
1 x 1 2 1 x 1 3 1 x 1 4 1 x 1 5
( )+
( ) +
( ) +
( ) +
)
x
2 x
3 x
4 x
5 x
* -:

** **

x 1
)
x

(=y

(= u

u 2
u 3
u 4
u 5
+
+
+
2
3
4
5

y = +

># include<stdio.h
># include<math.h
) (main
{
;float u,x,y
/* read input data */
;) = printf ( x
;) scanf( %f ,&x
/* carry out the calculations */
;u= (x 1)/x
;y=u+pow(u,2.)/2+pow(u,3.)/3+poe(u,4.)/4+pow(u,5.)/5

;) y =%f , x,y

printf(x= ,%f
}

># include<stdio.h
># include<conio.h
) (main
{
;]char pass[10
do
{
;) printf(\n enter password:
;)scanf(%s,pass
}
;)while(strcmp(pass,dahe)!=0
}

) ( : strcmp string

-:
) (
># include<stdio.h
># include<conio.h

main( )
{
chat ch;
char pass[10];
do
{
textcolor(WHITE);
textbackground(BLUE);
cprintf(\n enter password: );
textbackgrounf(WHITE);
cscanf(%s,pass);
}
while(strcmp(pass,dahe)!=0);
}

:

/* count characters and word in a phrase typed in */
# include <stdio.h>
main( )
{
int charcnt=0;
int wordcnt=0;
char ch;
printf( type in a phrase : \n );
/* read characters and quit loop on [ return ] */
while((ch=getche())!=\r);
{
charcnt ++
/* count character */
if(ch== );
/* space ? */
wordcnt++;
}
printf(\n character count is %d ,charcnt );
printf(\n word count is %d , wordcnt );
}



printf( )

) (puts

) (scanf

) (gets

)(getch

) (getche

) (clrscr

) (gotoxy

) (textcolor

) (textbackground
cprintf( ) , cputs

cscanf( ), cgets

FUNCTION
C -:
) ( Built in Function ) ( printf ) (scanf

) ( -:User Function
-

: .


- :

-
:
># include < filename
;function declarations
) (main
{

function1
function2
}
function1
{
----------}
function2
{
----------}
-:
) (

) (

:
>#include <stdio.h
>#include<conio.h
;)void line2(void
) (main
{
) (clrscr
) (line2
;) printf( ** Allah the god of all world ** \n
) (line2
/* end of main( ) function */
}
)void line2(void
{
;int j
;)for(j=0;j<=40;j++
;) * (printf
;) printf(\n
}
) ( line2 ) ( line2 -:

: function declaration
) ( main

: ) ( main
function coling ) ( line2

: ) ( .main
function definition .

**

** Function Type

int function

float function

string function


structure

void function
struct function

># include <stdion.h


) int sum(int a, int b
) (main
{
;int z , x = 10 , y = 40
;)z = sum(x,y
;) printf(\n\n z = %d , z
}
*/ */
) int sum(int a , int b
{

;int s
;s=a+b
;return s
}

** **
) ( sum int void .

x ,y ) (

- -:

a x .
b y .

a b s
s return

) z = sum(x,y . z = s

:
.

int int

float float

) (return void

** **
-

- ) void line2(void

* *
># include < stdio.h
># include <conio.h
)void line3(int no
) (main
{
) (clrscr
;)line3(30
;) printf( ** Allah the god of all world ** \n
;)line3(70
}

*/ */
)void line3(int no
{
;int j , no
) for(j = 0 ; j <= no ; j++
;) * (printf
;)printf(\n
}
** : .no
.

** **
) (

*
># include <stdio.h
/* determine the largest of three integer quantities */
) (main
{
;int a, b, c, d
/* read the integer quantities */
;) = printf(\n a
;) scanf( % d , &a
;) = printf(\n b
;) scanf( % d , &b
;) = printf(\n c
;) scanf( % d , &c
/* calculate and display the maximum value */
;) d = maximum( a, b
;)) printf(\n \n maximum = % d , maximum(c ,d
}
/* determine the larger of two integer quatities */
) maximum(x ,y

;int x ,y
{
;int z
;z = (x >= y ) ? x | y
;)return(z
}
: ) |? ( -:
(x > = y ) -

test

if | - Else

) (x >= y x

**

># include<stdio.h
/* calculates difference between two times */
) (main
{
;int mins1 , mins2
;) printf( type first time ( from 3:22 ):
;)(mins1=getmins
;) printf( type second(later) time
;)(mins2=getmins
;)printf( Difference is %d minutes. , mins2-mins1
}
/* getmins function */
/* gets time in hours : minutes formal */
/* return time in minutes */
) (getmins
{
;int hours, minuts
;)scanf( %d : %d , &hours , &minutes
;)return(hours * 60 + minutes
}
* int -:

># include < stdio.h


int sqr(int a):
int qup(int q):
) (main
{
;int s , qu , no =10
;)s = aqr(no
;)qu = qup(no
printf(\n squer of no = %d , s ):
printf(\n qupic of no = %d , qu ):
}
)int sqr(int a
{
;int v1
;v1= a*a
;return v1
}
) int qup(int q
{
;int v2
;v2=q*q*q
;return v2
}
squar of on = 100
qupic of on = 1000

float
.
float float
># include <stdio.h
># include < conio.h
) float add( float x , float y
) (main
{
;float no1, no2
;) printf(\n enter no1 , no2 :
scanf( %f ,%f , &no` ,&no2 ):

printf(\n addition of number is %f, add(no1,no2) );


}
float add(float x , float y );
{
float yt;
yt= x + y ;
return yt;
}
-:
Enter no1 ,no2 : 3,2 , 4,3
Addition of squares is 6.5

**

# include < stdio.h>
/* tests use of external variables */
int keynumb;
main( )
/* external variables */
{
printf( type keynumb : );
scanf(%d , &keynumb );
addeven( );
negative( );
}
/* checks if keynumb is odd or even */
oddeven( )
{
if (keynumb % 2 )
printf( keynumb is odd. \n);
else
printf( keynumb is even. \n);
}

/* checks if keynumb is nagative */


) (ngative
{
) if (keynumb < 0
;)printf( keynumb is negative . \n
else
;)printf( keynumb is positive . \n
}

MACROS
function

)
(

: C
-:
- #define

directive preprocessor *

# define macro line


5

#define

a 5

># include<stdio.h
# define sum(a,b) a+b
# define mul(x,y) x*y
) ( main
{

;int v1=5 , v2 = 10
;)printf(\n\n sum(v1,v2) = % d,sum(v1,v2
;)printf(\n\n mul(v1,v2) = % d,mul(v1,v2
}

-:
2 define sum a,b a+b

3 x,y x*y

: a,b .

) ..
(
* -: -:

: source code .C

: compilation
.obj

: object
linking .exe

-:

- .

- compilation object
linking

-
) 7 .

15 ) sum(v1,v2

( 15
-:
-

-
- .
:

># include<stdio.h
# define pi 3.14159
) ( main
{
;)float area(float
;float radius
;) printf(enter radius sphers :
scanf(%f, &radius):
;))printf(area of sphere is 2f,area(radius
}
/* returns ares of sphere */
)float area(rad
;float rad
{
;)return(4*pi*rad*rad
}

ARRAYS
*
char int float

) ( int , float , char .

) ( Type dived data

.arrays

** **
array .
int

. float .
* * .
) ( :
) (

) (

: 120

. float
:

]float salary[120

** -:

A[ 2 5 9 12 15 ] :
A

** **
- ]

: 75 3 student
:

student[3] = 75

38 salary :

]a = salary[37

;)]printf( The higest mark is %d , student[45


** -:
># include <stdio.h
# define max 10
)void main(void
{
;]float salary[max
;float average
;int count
;average = 0.0
)for(coint=0; count<max ; count++

{
;)printf( please enter salary for employee %d, count+1
;)]scanf(%f, &salary[count
}
;)printf(\n\n salary average is : %f, average/max
}
-:
10 .

- ]float salary[max

10 ]float salary[10

average .

for 10 )( scanf ]scanf("%f ", &salary[count

average ]average += salary[count


: count for
Scanf(%f, salary[couny]) .

count

** **

} int marks[5] = { 12 4 7 9 11

int

* **
.
-

int marks[ ] = { 5 9 4 10 7 } :

{ .

** **
# include <stdio.h>
/* calculate the average of a numbers , the compute deviation of each number
about the average */
main( )
{
int n , count ;
fliat avg, d , sum=0 ;
/* read in a value for n */
printf(\n how many numbers will be average? ;
scanf(%d, &n);
printf(\n);
/* read in the numbers and calculate ther sum */
for(count = 0 ; count < n count++ )
{
printf(i = %d x= , count+1);
scanf(%f, &list[count]);
sum+= list[count];
}
/* calcuate and write out the average */
avg = sum/n;
printf(\n the average is %5.2f\n\n , avg);
/* calculate and write out the deviations about the average */
for(count = 0 ; count < n ; count++)
{
d=list[count] avg ;

printf(i = %d x%5.2f d=%5.2f \n , count+1, list[count] , d);


}
}
-:
The average is 4.18
i = 1 x = 03.00 d = -1.18
i = 2 x = -2.00
d = -6.18
i = 3 x = 12.00 d = 7.82
i = 4 x = 4.40
d = 0.22
i = 5 x = 3.50
d = -0.68

** **
# include<stdio.h>
# define size 100
/* reorder a one-dimensinal, integer array from smallest to largest */
main( )
{
int i , n , x[size];
void reorder(int n , int x[ ]);
/* read in the a value for n */
printf(\n how many number will be entered ? );
scanf(%d, &n );
printf(\n);
/* read in the list of number */
for(i = 0 ; i<n ; i++)
{
printf(i = %d x = , i+1);
scanf(%d, &x[ i ]);
}
/* reprder all array elements */
reorder(n , x )
/* display the reordered list of numbers */
print(\n\reordered list of number :\n\n );
for(i=0;i<n; i++)
printf(i=%d x=%d\n, i+1 , x[ i ] );
}
int i, item ,temp;

) for(item = 0;item<n-1 ; item++


)]if(x[ i ] < x[item
{
/* interchange twwo elements */
;]temp = x[item
;] x[item] = x[ i
; x[ i ] = temp
}
;return
}

* *:
- ]int a[5] [10

5 10

: for

* :
= ]int a[3][4
{
{4,5,7,8 },
{3,2,4,5},
}{7,8,9,6
;}
: 0,0 4 0,1 5
- }

} {
:
;}int values[3][4] = {1,2,3,4,5,6,7,8,9,,10,11,12
0,0 1 ] values[0][0 0,1 2
]values[0][1
:
># include <stdio.h
# define stud 3

# define marks 10
void main (void)
{
int student[stud][marks];
int c1 ,c1 ;
for(c1 = 0 ; c1 < stud ; c1++)
for ( c2 = 0 ; c2 < marks ; c2++)
{
printf ( enter marks: %2d for student %2d : , c2+1 , c1+1 );
scanf ( %d , &student[c1][c2]);
}
for(c1 = 0 ; c1 < stud ; c1++ )
for(c2= 0 ; c2 < marks ; c2++)
printf( student [%2d] , marks[%2d] = %d\n , c1+1, c2+1 , student[c1][c2]);
}

student :
int student[stud][marks];
. for . .
.

: *

c[i][j] = a[i][j] + b[i][j]

# include <stdio.h>
# define mrow 20
# define mcol 30
/* caluculates the sum of the elements in two tables of integers */
void main (void)
{
int mrow , mcol;
/* array definitions */
int a[mrow][mcol] , b[mcol][mcol] , c[mcol][mcol];
/* function protyotypes */
void readinput(int a[ ][mcol],int nrow , int ncol);
void computesum ( int a[ ][mcol], int b[ ][mcol] , int c[ ][mcol], int nrow ,
int ncol);

void writeoutput ( int c[ ][mcol], int nrow , int ncol );


printf ( How many rows ? );
scanf ( %d , nrow);
printf ( How many columns ? );
scanf ( %d , ncol);
printf ( \n\n first table : \n );
readinput( a, nrow , ncol );
printf ( \n\n second table : \n );
readinput( b, nrow , ncol );
computsum(a, b, c, nrow, ncol);
printf ( \n\n sums of the elements : \n \n );
writeoutput(c , nrow , ncol);
}
void readinput( int a[ ][mcol] , int m , int n )
/* read in a table of integers */
{
int row , col ;
for(row = 0 ; row < m ; row ++ )
{
printf\n enter data for row no. %2d \n , row+1 );
for( col = 0 ; col < n ; col++)
scanf( %d , &a[row[col]):
}
return;
}
void computsum(int a[ ][mcol] , int b[ ][mcol] , int c[ ][mcol] , int m ,int n )
/* add the elements of two integer tables */
{
int row, col ;
for(row = 0 ; row < m ; row++)
for(col = 0 ; col < n ; col++)
c[row][col] = a[row][col] + b[row][col];
return;
}
void writeoutput(int a[ ] [mcol] , int m , int n )
/* write out a table of integers */
{

; int roe , col


)for (row = 0 ; row <m ; row ++
{
)for(col = 0 ; col < n ; col ++
printf(%4d , a[row][col]):
;printf(\n
}
;return
}

:
a ,b ,c row - .

ncol row -

col -

* -:

13


11
12

10

17

16

15

14

12

11

10

21

20

19

18


2
3

-:
how many rows? 3
how many columns? 4
first table :
enter data for row no. 1
1
2
3
4
enter data for row no. 2
5
6
7
8
enter data for rpw no. 3
9
10
11
12
second tables :
enter data for row no. 1
10
11
12
13
enter data for row no. 2
14
15
16
17
enter data for row no. 3

20

21

19

18

suns if the elements :


11
13
15
17
19
21
23
25
16
19
31
33

* * ) ( array of string
string
* *
** char :

]char name[80

name ) c )(null

**

"char name[ ] = "ali ahmed

** -:
- .

printf gets .

** -:
?# include<stdio.h

)void main(void
{
]char namr[80
;) printf(place enter your name :
;)gets(name
;)printf(welcome , %s , name
}
* :
name gets

'name[0] = 'a

;]ss = name[15

** **
) ( null
* *
># include <stdio.h
)void main(void
{
;char strin[ ] = hello
;))printf(character array = %s , its length = %d \n, strin , sizeof(string
}
* :
string hello char string[ ] = "hello - printf

sizeof character array = hello its length = 6


6 5 hello-

:
4
5
6
l
o
\0
** -:

3
l

2
e

1
h

># include<stdio.h
)void main(void
{
;int count
char string[ ] = hello
)for(count = 0 ; count < sizeof(string); count ++
;)]printf( string[%d] = =%d %c \n , count, string[count], string[count
}
string for

.-

)Count < sizeof(string

string


-:
String[ 1 ] = 101 e
String[ 3 ] = 108 l
String[ 5 ] = 0
=

h
l
o

String[ 0 ] = 104
String[ 2 ] = 108
String[ 4 ] = 111

** **

) ( -:

strlen

) " len = strlen("count this string

) ( -:
- c .

strcpy strcpy ( string1 , string2 ) : string2 string1

- strcpy .

;)" strcpy( string , " hello my string

string

strcpy .

) ( -:
strcat strcat(string1 , string2) : srcat string2 string1

string1 string2

># include < stdio.h


)void main(void
{
; char string1[20] = learning c
; char string2[ ] = is easy
;) strcat(string1 , string2
;)printf(string1
}
learning c is easy

) ( :
-

strcmp :;) result = strcmp(string1 , string2


strcmp result ) ( result

result -: strcmp
*
># include < stdio.h
># include <string.h
)void main(void
{
;]char string1[80
;]char string2[80
;int result
;) printf( enter the first string
;)gets(string1
;) printf( enter the second string :
;)gets(string2
;)result = strcmp(string1,string2
) if(result == 0
;) printf(the two string are identical :
else
;) printf(the two string are different :

}
:
strcmp

strcmp
result = strcmp(string1,string2);

* *

# include < stdio.h>
# include < string.h>
void main(void)
{
char string1[80];
char string2[80];
int result , choice;
printf( enter the first string : );
gets(strig1);
printf(enter the second string : );
gets(strung2);
printf(\n enter the function you want: \n
1- strcpy \n
2- strcat \n
3- strcmp \n);
printf(enter you choice : );
scanf(%d , &choice);
switch(choice)
{
case 1:
printf(\n copy second string to first \n);
strcpy(string1 , string2 );
printf(string1);
break;
case 2:
printf(\n merging the two string \n);
strcat(string1 , string2 );

;)printf(string1
;break
case 3:
;) printf(\n comparing the two strings \n
;)result = strcmp(string1,string2
) if(result == 0
;) printf( the two strimg are identical
else
;) printf( the two strimg are different
;break
default :
;) printf(plase enter a number 1-3
}
}
** **

;) ] [ void fn(int num

fn int

;)fn(num
* -:
-:

># include < stdio.h
># include <ctype.h
># include <string.h
;) ] [void changetoupper(char string

)void main(void
{
;]char string[80
do
{
;)printf( enter a string to change to upper case \n enter \ quit \ to exit
;)gets(string
;)changetoupper(string
}
;))while(strcmp(string , quit
}

) ] [void changetoupper(char string


{
;unsigned int count
)for(counr = 0 ; count < strlen(string); count++
;))]printf(%c , toupper(string[count
;)printf(\n\n
}
:
-

do.while quit

changetoupper .

toupper printf ;))]printf(%c , toupper(string[count


string functions
**

(strcat
)
) ( ) (
) (strchr

) (strcmp

) (

) (strcpy

) (strlen

) (strupr


**

atof

Atoi

-:

Atol

Strtod

_itoa

_ltoa

) ( Structure
: structure .

) ( . :

) ( char

) ( int

) ( float
structure .


* -:
- -:


struct C

struct
{
;--------------
;--------------
;--------------
;}

-:
struct employee
{
;]char name[40

;]char address[40
;int age
;float salary
- employee ) (

;}

:
;struct employee emp1
emp1 ) ( emp1 86 byte -:

* ) * ( 40 byte ) * ( 40 byte ) * ( 2 byte )( 4 byte


* -:
:

emp1.age

30 age :;emp1.age=30
- a.emp1.salary; :

gets(emp1.name); :

gets

name emp1
* -:
># include <stdio.h
)void main(void
{
struct employee
{
;]char name[40
;]char address[40
;int age
;float salary
}

;struct employee emp1


;)printf( enter name :
;)gets(emp1.name
;) printf(enter address:
;)gets(emp1.address
;) printf( enter age :
;)scanf(%d, &emp1.age

;) pruntf(enter salary :
;)scanf(%d,&emp1.salary
; printf(\n\n you entered: \n
;)printf( employee name :\t%s\n , emp1.name
;)printf( employee address :\t%s\n , emp1.address
;)printf( employee age :\t%d\n , emp1.age
;)printf( employee salary :\t%f\n , emp1.salary
}

* *
100
-

struct employee
{
;]char namr[40
;]char address[40
;int age
;float salary
;}
]struct employee emp[100
employee emp 100

- .

;emp[4].age=35
-

emp[count].salary = 123.5 count



*
-:
>#inclide<stdio.h
>#include<string.h
>#include<stdlib.h
#define max 10
//
)void main(void

{
struct booktag
{
char title[40];
char author[40];
int pages;
int year;
char edition;
};
struct bootag book[max];
//
int c ,c1 , result;
char temp[10];
//
for(c = 0 ; c < max ; c++)
{
printf( enter data for record no[%d]\n m c+1);
printf( or enter \ quit \ to end \n\n;
printfenter title : );
gets(boo[c].title);
/* )(
result = strcmp (book[c].title,quit);
if (result = = 0 )
break;
printf(enter author : );
gets(book[c].author);
printf(enter number of pages : );
gets(temp);
book[c].pages = atoi(temp);
printf(enter year of publication : );
gets(temp);
book[c].year = atoi(temp);
printf(enter edition : );
gets(temp);
book[c].edition = atoi(temp);
}
//
printf(record no. %d\n:, c1++);
printf(book title : \t\%\n , book[c1].title);
printf( book author : \t%d\n, book[c1].author);
printf(no of pages :\t%d \n , book[c1].pages);
printf(date of pub. \t%d\n,book[c1].year);
printf(edition : \t%d\n\n ,book[c1].edition);
}

** **
- book

booktag -:*

- 10

]struct booktag boo[max

for 10 quit

quit

break
;)result = strcmp(book[c].title , quit
if(result = = 0
;break
- strcmp quit

strcmp result fot


** **
. -:
>#include<stdio.h
>#include<string.h
>#include<stdlib.h
struct studenttag
{
;]char name[40
;int age
;int grade
}
;)void print(struct stdenttag student
)void main(void
{
;struct studenttag student
;]char temp[10
;) printf(enter student namr :
)gets(student.name
;) printf(enter student age :
;)gets(temp
;)student.age=atoi(temp

;) printf(enter student grade :


;)gets(temp
;)student.grade = atoi(temp
;)printf(\n\n you entered: \n\n
//
;)printf(student
}
//
)void print(struct studenttag student
{
;)printf(student name : \t%s\n , student.name
;)printf(student age : \t%d \n , student.age
;)printf(student grade :\t %d\n , student.grade
}
* *
print

studenttag . - * -:

print )void print(struct studenttag student


;)print(student
-

** ) (
.
.

** **

//
>#include<stdio.h
>#include<string.h
>#include<stdlib.h
struct sudenttag
{
;]char name[40
;int age
;int grade
;}

void print(struct studenttag student);


struct studenttag detinfo(void)
void main(void)
{
struct studenttag student;
student = getinfo( );
print(student);
}

//
void print(sturct studenttag student)
{
printf(\n\n you entedred : \n\n );
printf(student name: \t%s\n,student.name);
printf(sudent age: \t%d\n,student.age);
printf(student grade : \t%d\n,student.grade);
}
//
struct studenttag getinfo(void)
{
sturct studenttag student;
char temp[10];
printf(enter student name : );
gets(student.name);
printf(enter student age : );
gets(temp);
student.age = atoi)temp);
printf(enter student grade : );
gets(temp);
student.grade = atoi(temp);
return(student);
}

:
getinfo main

)struct studenttag detinfo(void


getinfo void . studenttag

getinfo main student = getinfo ;)return(student

- getinfo

main student main

print
** **
. . employee

- employee

- -:

- emp dept

struct employee
{
;]char name[40
;]char address[40
;int age
;float salary
}
struct dept
{
;int deno
;int product
;sturct employee emp
}

dept ;struct dept dept1 dept1.emp.age = 25 :

dept1

emp

age

** **
#include<stdio.h>
#include<stdlib.h>
struct employee
{
char name[40];
char address[40];
int age;
float salary;
};
struct department

{
int depnum;
char product[40];
struct employee emp;
};
void main(void)
{
struct deparment dept;
char temp[10];
printf(enter dept no : );
gets(temp);
dept.deptnum = atoi(temp);
printf(enter dep. Product : );
gets(dept.product);
prints(enter employee name : );
gets(dept.emp.name);
printf(enter employee address );
gets(dept.emp.address);
printf(enter employee age : );
gets(temp);
dept.emp.age = atoi(temp);
printf(enter employee salary : );
gets(temp);
dept.emp.salary = atof(temp)
printf(\n\n you entered \n\n );
printf(dept no : \t\t%d\n ,dept.deptnum);
printf(dept product : \t\t%s\n ,dept.product);
printf(employee address : \t\t%d\n, dept.emp.address);

;)printf(employee age : \t\t%d\n,dept.emp.age


;)printf(employee salary: \t\t%f,dept.emp.salary
}

-:
- employee

- department employee

gets name

emp :

;)gets(dept.emp.name

printf .

** **
* -:
- : -:

- : -:

struct enployee
{
;]char name[40
;]char address[40
;int age
;float salary
;}

;struct employee emp


* -: .
-:

struct employee
{
;]char name[40
;]char address[40
;int age
;float salary
;} emp
emp }

;struct employee emp

struct employee
{

;]char name[40
;]char address[40
;int age
;float salary
;} emp1 , emp2 , emp3
struct
{
;]char name[40
;]char address[40
;int age
;float salary
;} emp
-:struct
{
;]char name[40
;]char address[40
;int age
;float salary
{ = } emp
ali ahmed
26 st. cairo
39
135.6
;}

pointer
* - : pointer
. ) ( .) (

- .

; int d = 5 ram 2 byte

d 5

- -:

:
.

:
** **
- )

-:

;* p

int

)*(

- p

byte p

;* degree

float

- degress

float

. :struct student
{
;]char name[40
;int mark
}
. :
;struct student *st
** **

&

unary :

;)scanf(%d,&var

;int num
;int *ptr
;num = 5
;ptr = &num
num ptr num

- num 5

ptr = &num & num ptr

num ) ( .
ptr

** ** -:
: .
- .

- :

;)addten(ptr

addten ptr num :

) * (

; *ptr = 10

- num 10

;a=*ptr* :
>#include<stdio.h
)void main(void
{
;int u = 3
;int v
;int *pu
/* pointer to an integer */
int *pv
/* pointer to an integer */
/* assing address of u to pu */
/* assign values of u to v */
/* assing address of v to pv */

;pu = &u
;v = pu
;pv =&v

;)printf(\n u=%d &u=%x pu=%x *pu=%d , u , &u , pu , *pu


;)printf(\n v=%d &v=%x pv=%x *pv=%d , v , &v , pv , *pv
}
:
- pu u ,pv .v u pv v

u 3 u ) FF8E (

- . pu pu ) ( FF8E

)pu ( FF8E 3

v 3 *pv v** :
>#include<stdio.h

) (main
{
;int u1 ,u2
;int v =3
int pv
/ pv pointer to v */
u1 = 2*(v + 5 ); /* ordinary expression */
; pv = &v
;) u2 = 2*(*pv + 5
printf(\n u1 = %d
;) us2 = %d ,u1 , u2
}

:
- ) 2*(v + 5 .

) 2*(*pv + 5 v , *pv

- :

u2 = 16
pv = 3
)u2 = 2*(*pv + 5
)u2=2*(3+5
u2=16

v=3
u1 = 15
) u1=2*(v + 5
)u1=2*(3+5
u1= 16

** **
C . ) " " ( -:
)void main(void
{
;int num = 5
;)change(num
;
}
5

) void change( int num


{
; num = 7

;
}
:

main num 5 change .

num 5 change change num main

num main change local .

) : (

* . -:
- :
.

:
-

;) void change ( int * ptr

change :

change(ptr) : ptr

. :
>#include<stdio.d
;) void change( int *ptr
)void main(void
{
;int num = 5
;int *ptr
; ptr = &num
;)printf( berfore change , num =%d \n , num
;)change(ptr
;) printf(after change, num = %d \n , num
}
) void change( int * ptr

{
;*ptr = 7

}
;) void change( int *ptr

- change

) * ( . main num 5 ptr num :

ptr = &num


- ptr

peintf num change change ptr

change ;*ptr = 7 .

ptr 7

ptr num num -:before change , num = 5


after change , num = 7
** **
: return return

- -:

>#include<stdio.h
;)void fn( int *var1 , int *var2 , int *var3
)void main(void
{
;int var1 ,var2, var3
)fn(&var1 , &var2 , &var3
;)printf(var1 = %d , var2 = %d , var3 = %d, var1 , var2 , var3
}
)void fn( int *p1 , int *p2 , int *p3
{
;*p1=10

;*p2=20
;*p3=30
}

:
var1,var2,var3 - fn

fn p1,p2,p3 - fn

main printf var1,var2,var3


fn -:var1 = 10, var2 = 20 , var3 = 30

** ** ) (
) (

)
overdue ( delinquent
.

.
* -:
- 10%
overdue

delinquent
* -:
- ) ( .

- -:
2-street
3- city
5- previous balance
6- current balance

1- name
4- account number

7- payment date

-: -

:
1- name
4- city
7- new balance

-
2- account number
3- street
5- old balance
6- current balance
8- account status

-: *
#include<stdio.h>
struct date
{
int month;
int day;
int year;
};
struct account
{
char name[50];
char street[45];
char city[60];
int acc_no;
int acct_type;
/* c = current , o = overdue , d = delinquent */
float oldbalance;
float newbalance;
float paymeny;
struct date lastpayment;
}
main( )
{
int i ,n ;
void readinput(int i );
void writeoutput(int i (;
printf(customer billing system \n\n);
printf(how many customer are there ? );
scanf(%d,&n);
for(i=0,i<n,i++)
{

readinput(i);
if(customer[i].payment > 0 )
customer[i].acct_type =
(customer[i].oldbalance < 0.1*customer[i].oldbalabce) ? o | c;
else
customer[i].acct_type =
(customer[i].oldbalance > 0 ) ? d | c ;
};
for (i = 0 ; i < n ; i ++)
writeoutput(i);
}
void readinput(int i )
{
print(\n customer no. %d\n, i+1);
printf( name : );
sccanf( %[ ] ,customer[ i ].name);
printf( street : ) ;
scanf(% [ ] ,customer[ i ] .street);
printf( city : );
scanf( %[ ] ,customer[ i ] .city );
printf( account number : );
scanf(%d , &customer[ i ] . oldbalance);
printf( current payment : );
scanf(%f, &customer[ i ] .payment);
printf( payment date (mm/dd/yyyy) : );
scanf(%d / %d / %d , &customer[ i ] .lastpayment.month,
&customer[ i ] .lastpayment.day
&customer[ i ] .lastpayment.year
return;
}
void writeoutput(int i)
{
printf( name : %s, customer[i].name);
printf( account number : %d\n, customer[i].acct_no);
printf(street : %s\n , customer[i].street);
printf(city : %s\n\n , customer[i].city);
printf(old balance %7.2f, customer[i].oldbalance);
printf( current payment : %7.2f , customer[i].payment);
printf( new balance : %7.2f\n\ ,customer[i].newblance);
printf( account status : );
switch ( customer[i].acct_type)
{
case c
printf( current\n\n);
break;

case o
;)printf( overdue\n\n
;break
case d
;)printf( delinqunt\n\n
;break
;default
)printf( error \n\n
}
;return
}

You might also like