새발블로그

C programming 본문

Computer Science/Data Structure

C programming

EUG 2023. 8. 9. 16:42

C

  • assembly language
  1. operating systems of major languages
  2. language compilers
  3. assemblers
  4. computer network drivers
  5. databases
  6. language interpreters
  7. text editors
  8. print spoolers
  9. utilities

writing and running programs

  1. write text of program(source code)
  2. use compiler to convert program
    3~N. errors and warnings// fix, re-compile
    N. run it

C : fundamentals

  • hardware independent
  • porable to most computers
  • case-sensitive ( int a; int A; are different)
    1. Editing -> Preprocessing or libraries -> compiling -> linking

C : Simple Variables

int i =5;

  • integer value
  • i with memory location
  • store the value 5 at this location

int i=5
-> single value

Operator ‘&’
main( ) {
int i = 5 ;
printf ( "\nAddress of i = %u", &i ) ; 
printf ( "\nValue of i = %d", i ) ; 
}

==> 85525, 5

Operator ‘*’
main( )
{
int i = 5 ;
printf ( "\nAddress of i = %u", &i) ; 
printf ( "\nValue of i = %d", i ) ;
printf ( "\nValue of of i = %u", *(&i)) ;
}

=> 85525, 5, 5

* : value at
& : address of

C : Pointer Variables

int *i =5;
address, containing the value

#include <stdio.h>
int main( ) {
int i = 5 ; 
int *j ;
j = & i;
printf ( "\nAddress of i = %u", &i ) ; 
printf ( "\nAddress of i = %u", j ) ; 
printf ( "\nAddress of j = %u", &j ) ; 
printf ( "\nValue of j = %u", j ) ; 
printf ( "\nValue of i = %d", i ) ; 
printf ( "\nValue of i = %d", *( &i ) ) ; 
printf ( "\nValue of i = %d", *j ) ;

}

Address of i = 85525
Address of i = 85525
Address of j = 85527
Value of j = 85525
Value of i = 5
Value of i = 5
Value of i = 5

Pointers to a pointers

#include <stdio.h>
int main( ) {
int i = 3, *j, **k ; 
j = &i ;
k = &j ;
printf ( "\nAddress of i = %u", *k ) ; 
printf ( "\nAddress of j = %u", &j ) ; 
printf ( "\nAddress of j = %u", k ) ; 
printf ( "\nAddress of k = %u", &k ) ; 
printf ( "\nValue of j = %u", j ) ; 
printf ( "\nValue of k = %u", k ) ; 
printf ( "\nValue of i = %d", i ) ; 
printf ( "\nValue of i = %d", * ( &i ) ) ; 
printf ( "\nValue of i = %d", *j ) ;
printf ( "\nValue of i = %d", **k ) ;
}

Address of i = 85525
Address of j = 85528
Address of j = 85528
Address of k = 85531
Value of j = 85525
Value of k = 85528
Value of i = 3
Value of i = 3
Value of i = 3
Value of i = 3

Pointer Arithmetic

  1. Addition of a number to a pointer.
int i=4; *j; *k;
j=&i; //i의 주소값이 j의 value가 됨
j=j+1;   //주소값이 가리키는 값에 증가 ex)i=값이 4, 주소값이 410이라면 j=411
j=j+9;   //마찬가지로 j=420
k=j+3;	//k=423이 됨
  1. Subtraction of a number from a pointer
int i = 4, *j, *k ; 
j = &i ;
j=j-2;
j=j-5; 
k = j - 6;

3.Subtraction of one pointer from another

main( ) {
int arr[ ] = { 10, 20, 30, 45, 67, 56, 74 } ; int *i, *j ;
i =&arr[1];
j = &arr[5];
printf ( "%d %d", j - i, *j - *i ) ;
}
  1. Comparison of two pointer variables
main( ) {
int arr[ ] = { 10, 20, 36, 72, 45, 36 } ; int *j, *k ;
j = &arr [ 4 ] ; k = ( arr + 4 ) ;
if ( j == k )
printf ( "The two pointers point to the same location" ) ;
else
printf ( "The two pointers do not point to the same location" ) ;
}

Do not attempt following operations on pointers
• Addition of two pointers
• Multiplication of a pointer with a constant
• Division of a pointer with a constant

The "While" Loop //Repeart

float pow(float x, int exp) {
int i=0;
float result=1.0; while (i < exp)
{
result = result * x; i++; }
return result; }
int main() {
float p;
p = pow(10.0, 5);
printf(“p = %f\n”, p); return 0;
}

The "for" Loop

Functions and arrays

 

 

 

 

Functions: Parameter Passing

Value (of the arguments)

main( ) {
int p = 40, q =80;
swap ( p, q ) ;
printf ( "\np = %d q = %d", p, q ) ;
}
wap ( int x, int y ) {
int temp;
temp = x ;
x=y;
y = temp ;
printf ( "\nx = %d y = %d", x, y ) ; }

Out put
x = 80 y = 40
p = 40 q = 80

Reference (addresses of the arguments)

main( ) {
int p = 40, q =80;
swap ( &p, &q ) ;
printf ( "\np = %d q = %d", p, q ) ;
}
swap ( int *x, int *y ) {
int temp;
temp = *x;
*x = *y ;
*y = temp ;
printf ( "\nx = %d y = %d", x, y ) ; }

Out put
x = 1830466124 y = 1830466120
x,y는 주소, 주소는 변하지 않음(왜? *x, *y로 할당된 값이 바뀐거지 주소는 그대로)
p = 80 q = 40

Arrays

  • A collection of similar elements stored in consecutive memory locations
  • An arrary is known as a 'supscripted variable;
  • int Value[5];
  • Functions manipulating arrays-
    - An arrary element can be passed as an argument/parameter to a function (as a primitive data type)
    - An array can also be returned from a function
    - An arrary of pointers


Structures

meaningful, related data to identify an entity

main() {
struct student{
int roll_no[15]; Char[] name; char *authors; double marks;
}
//declarations
struct student student1, student2; }
main() {
printf(“Student1 roll-number is %d”, student1.roll_no); printf(“Student1 marks is %f”, student1.marks);
}

Arrary of Structures

 

'Computer Science > Data Structure' 카테고리의 다른 글

Queue  (0) 2023.08.09
Stacks  (0) 2023.08.09
Complexity Analysis  (0) 2023.08.09
C Basics  (0) 2023.08.09
Data Structure Concepts  (0) 2023.08.09