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

Nur Gunawan Sastranegara, Ir., M.Eng. 1

This document contains examples of C code demonstrating various programming concepts like data types, operators, functions, arrays, pointers, and passing arguments by reference. The code snippets show how to declare and initialize variables, use conditional and looping statements, define and call functions, manipulate strings and arrays, and pass arguments and return values. Each code example is accompanied by its output to demonstrate the behavior.

Uploaded by

nuel_evans
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Nur Gunawan Sastranegara, Ir., M.Eng. 1

This document contains examples of C code demonstrating various programming concepts like data types, operators, functions, arrays, pointers, and passing arguments by reference. The code snippets show how to declare and initialize variables, use conditional and looping statements, define and call functions, manipulate strings and arrays, and pass arguments and return values. Each code example is accompanied by its output to demonstrate the behavior.

Uploaded by

nuel_evans
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Nur Gunawan Sastranegara, Ir., M.Eng.

Email: [email protected]
Page 1 of 6
Saturday, 23 April 2005
1
6-1.
main()
{
int x1, x2, x3 ;
x1 = sizeof(int) ;
x2 = sizeof(long) ;
x3 = sizeof(double) ;

printf(" int --- %d \n", x1) ;
printf(" long --- %d \n", x2) ;
printf(" double --- %d \n", x3) ;
}
int --- 2
long --- 4
double --- 8

6-2.
main()
{
int c, k;
for ( k = 0; k < 6; k++ ) {
c = ( k < 3 ) ? 0 : 1 ;
printf( " c - %d \n", c) ;
}
}
c - 0
c - 0
c - 0
c - 1
c - 1
c - 1

6-3.
#include "stdio.h"
main()
{
int c = ' 0 ' ;

while ( !( c == '9' ) )
putchar( c++ ) ;
}
012345678




6-4.
main()
{
int i = 30, j = 20 ;
float z ;

z = (float) i / (float) j ;
printf(" z = %6.3f \n", z ) ;
}
z = 1.500

6-5.
main()
{
int k, n ;
n = 6 ;

k = n ++ ;
printf(" k=%d n=%d\n", k, n ) ;
}
k=6 n=7

6-6.
main()
{
int k, n ;
n = 6 ;

k = ++ n ;
printf(" k=%d n=%d\n", k, n ) ;
}
k=7 n=7

7-1.
#include "stdio.h"
main()
{
fnc() ;
}

fnc()
{
putchar(' * ') ;
}
*



Nur Gunawan Sastranegara, Ir., M.Eng.
Email: [email protected]
Page 2 of 6
Saturday, 23 April 2005
2
7-2.
#include "stdio.h"
main()
{
int k ;
for( k=0; k < 10; k ++ )
fnc() ;
putchar(' \n ') ;
}

fnc()
{
putchar(' * ') ;
}
**********

7-3.
#include "stdio.h"
main()
{
char n ;
n = ' * ' ;
fnc( n ) ;
}

fnc( s )
char s ;
{
putchar( s ) ;
}
*

7-4.
#include "stdio.h"
main()
{
int c ;

c = getchar() ;
fnc( c ) ;
}

fnc( s )
char s ;
{
putchar( s ) ;
putchar(' \n ') ;
}
f
f

7-5.
main()
{
int a, b, c ;
a = 20 ; b = 10 ;

c = plus( a, b ) ;
printf( " c = %d \n", c ) ;
}

plus( x , y )
int x , y ;
{
int z ;
z = x + y ;
return ( z ) ;
}
c = 30

7-6.
main()
{
double fnc() ;
double x , y ;
x = 3.141592 ;

y = fnc(x) ;
printf ( " %9.5f \n ", y ) ;
}

double fnc(z)
double z ;
{
return( z * z ) ;
}
9.86960

7-7.
main()
{
int a , b ;
a = 123 ; b = 135 ;

printf( " mini=%d \n",min(a,b)) ;
}
Nur Gunawan Sastranegara, Ir., M.Eng.
Email: [email protected]
Page 3 of 6
Saturday, 23 April 2005
3
min(x , y)
int x , y ;
{
if ( x <= y )
return( x ) ;
else
return( y ) ;
}
mini = 123

8-1.
main()
{
int j , k[3] ;

k[0] = 123 ;
k[1] = 456 ;
k[2] = 789 ;

for ( j = 0; j < 3; j++ )
printf(" k[%d]=%d \n", j,k[ j ] ) ;
}
k[0] = 123
k[1] = 456
k[2] = 789

8-2.
#include "stdio.h"
main()
{
int k = 0 ;
char str[4] ;

str[0] = ' U ' ;
str[1] = ' N ' ;
str[2] = ' I ' ;
str[3] = ' X ' ;

while ( k < 4 ) {
putchar (str[ k ] ) ;
k++ ;
}
putchar( ' \n ' ) ;
}
U N I X


8-3.
#include "stdio.h"
main()
{
int k = 0 ;
char str[5] ;

str[0] = ' U ' ;
str[1] = ' N ' ;
str[2] = ' I ' ;
str[3] = ' X ' ;
str[4] = ' \0 ' ;

while ( str[ k ] ) {
putchar (str[ k ] ) ;
k++ ;
}
putchar( ' \n ' ) ;
}
U N I X

8-4.
#include "stdio.h"
main()
{
int k ;
static char str[ ] = " U N I X " ;

for ( k = 0 ; str[ k ] ; k++ )
putchar (str[ k ] ) ;
putchar( ' \n ' ) ;
}
U N I X

8-5.
#include "stdio.h"
#define SZ 100
main()
{
char str[ SZ ] ;
int c , k = 0 ;

while ((c = getchar( )) != EOF){
str[ k ] = c ;
k++ ;
}
str[ k ] = ' \0 ' ;
k = 0 ;

Nur Gunawan Sastranegara, Ir., M.Eng.
Email: [email protected]
Page 4 of 6
Saturday, 23 April 2005
4
while( str[ k ] ) {
putchar( str[ k ] ) ;
k++ ;
}
putchar(' \n ' ) ;
}
012345
abcdefghi
***123***
^Z
012345
abcdefghi
***123***

8-6.
main()
{
static char str[ 5 ] = " U N I X " ;

printf(" str = %s \n ", str ) ;
}
str = U N I X

9-1.
#include "stdio.h"
main()
{
char c , *p ;

p = &c ;
c = ' A ' ;

putchar( *p ) ;
}
A

9-2.
#include "stdio.h"
main()
{
char c = ' A ', *p ;
p = &c ;

(*p)++ ;
putchar( *p ) ;
}
B

9-3.
#include "stdio.h"
main()
{
static char str[ ] = "U N I X " ;
char *p ;

p = str ;

while( *p ) {
putchar( *p ) ;
p++ ;
}
putchar(' \n ') ;
}
U N I X

9-4.
main()
{
char *p ;
p = "U N I X " ;

printf(" %s \n", p ) ;
}
U N I X

9-5.
main()
{
char *p ;
p = "U N I X " ;

printf(" %c \n", *p ) ;
}
U

9-6.
main()
{
char *p ;
p = "U N I X " ;

p++ ;
printf(" %c \n", *p ) ;
}
Nur Gunawan Sastranegara, Ir., M.Eng.
Email: [email protected]
Page 5 of 6
Saturday, 23 April 2005
5
N

9-7.
#include "stdio.h"
main()
{
char *p ;
p = "U N I X " ;

while( *p )
putchar( *p++ ) ;
}
U N I X

9-8.
#include "stdio.h"
main()
{
char *p , *q ;
p = q = "U N I X " ;

while( *p )
p++ ;

while( p > q ) {
p -- ;
putchar( *p ) ;
}
putchar(' \n ') ;
}
X I N U

10-1.
main()
{
int a , b , c ;
a = 30; b = 20 ;

fnc( a, b, &c ) ;
printf(" c = %d \n", c) ;
}

fnc(x, y, z)
int x, y, *z ;
{
*z = x + y ;
}
C = 50
10-2.
main()
{
char a , b ;
a = 'A'; b = 'B' ;

swap( &a, &b ) ;
printf("a=%c b=%c \n", a, b) ;
}

swap(x, y)
char *x, *y ;
{
int z ;
z = *x ;
*x = *y ;
*y = z ;
}
a = B b = A

10-3.
main()
{
int a , b , c , d ;
a = 30; b = 20 ;

d = fnc( a, b, &c ) ;
printf(" c = %d \n", c) ;
printf(" d = %d\n", d) ;
}

fnc(x, y, z)
int x, y, *z ;
{
*z = x + y ;

return( x - y ) ;
}
C = 50
d = 10

10-4.
main()
{
static char str[ ] = "U N I X" ;

fnc(str) ;
}

Nur Gunawan Sastranegara, Ir., M.Eng.
Email: [email protected]
Page 6 of 6
Saturday, 23 April 2005
6
fnc(sr)
char sr[ ] ;
{
printf(" %s \n", sr) ;
}
U N I X

10-5.
#include "stdio.h"
main()
{
static char str[3][4] = {
{'0', '1', '2', '3' },
{'A', 'B', 'C', 'D' },
{'a', 'b', 'c', 'd' } } ;

fnc(str) ;
}

fnc(s)
char (*s)[4] ;
{
int i, j ;

for( i = 0; i < 3; i++ ) {
for( j = 0; j < 4; j++)
putchar( s[ i ][ j ] ) ;
putchar( '\n' ) ;
}
}
0123
ABCD
abcd

10-6.
#include "stdio.h"
main()
{
char *p = "U N I X" ;

fnc( p ) ;
}






fnc(s)
char *s ;
{
while (*s)
putchar( *s++ ) ;
putchar(' \n ') ;
}
U N I X

You might also like