새발블로그

C Basics 본문

Computer Science/Data Structure

C Basics

EUG 2023. 8. 9. 16:41

High-level view of programming

  • Compile the source code
    - Compilation : converting the source code into machine code
  • Run or Execute the machine-language file

A simple C program

#include <stdio.h>
int main()
{
	printf("simple C program");
    return 0;
}
  • comments
    - /* *\/ & ignored by computer, describe program
  • #include <stdio.h> (standard input/output operation)
    preprocessor directive

Variable

variables

  • store/retrieve data
  • type of variable determines what can be placed
  • Assignment : placing a valude in a variable
  • variable must be declared before they are assigned
  • variable : flexible <-> constant : same value

naming variable

  • reflect the role of variable
  • begin with a letter(digitx), can contain digit, underscores

variable Declartation

must be declared before the first executable statment

main(){
	int a, b, c;
    float d;
}
  • var_type var_namel
    -int age;
    • float annual_salary;
    • double weigh, height / multiple vars ok/

variable assignment

declaration - > assignment
float a,b;
int c;
b=2.12;
c=200;

  • Using the "=" operator
    int age= 52; //joint declaration/assignment
    double salary;
    salary= 150000.23;
    age =53; //value may change at any time

C Data Types

char

  • single byte(8bits)
  • signed or unsigned
  • internally char is just a number
  • ASCII character set used in ANSI C

int

  • signed interger of 4 or 8 bytes(32 or 64 bits)
  • precise size is machine-dependent

float and Dobule

  • 32 bit / 64 bit real numbers
  • precise size depend on the architecture
    -int : integer of unspecified size
    -float : single-precision floating point (32)
    -double : Double-precision floating poing (64)

Additional...

  • short, long, signed, unsigned
  • machine-specific

Structure of a C program

/* description of program */
#include <stdio.h>
/*any other includes go here*/
int main(){
/*program body*/
return 0;
}

Statements

  • end with a semicolon
  • comma : separate multiple declaration
  • blank lines have no effect
  • extra spacese between tokens has no effect
  • comments are ignored by the compiler

Executable Statements

  • variable declaration/initialization
  • not a declaration
    -Assignment statements
    -Arithmetic statements
    -Print statements

Printf() examples

  • output to standard out -screen
  • printf("%s\n","Hello world");
    : %s-> as a string, \n ->newline
  • printf("%d\t%d\n",j,k);
    : j as an integer(%d), \t->tap
    k as an integer(%d), \n->new line
  • printf("%f : %f : %f\n,x,y,z);
    : floating point variable x, followed by a space, colon, space

Invisible character \n : newline \t : horizontal tab

scnaf function

  • in <stdio.h>
  • scanf(format-specifier, &var1, &var2, etx);
    int a; scanf("%d", &a);
    double x; scanf("%f", &x);

While loops

  • repeating a statement
  • while(expr)
    {
    statement1;
    statement2;
    ......
    }
    if expr == true, preform statement....
    otherwiser, skip to end of while block

Operator

Arithmetic

  1. + "plus" -> c= a + b
  2. - "minus" -> c = a - b
  3. * "times" -> c = a*b
  4. / "divided by" c = a / b
  5. % "modulus" c= a % b

Relational

  1. > "greater than"
  2. < "less than"
  3. >= "greater than or equal to"
  4. <= "less than or equal to"

Equality

  1. == "is equal to"
  2. != "is not equal to"

Logical

  1. || "logical or"
  2. && "logical and"

logical operator, cont
1. if((a==1)&&(b<3)||(c==1))

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

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