Skip to content
This repository has been archived by the owner on Jan 12, 2022. It is now read-only.

L14 [Intro to C]

Richard Luo edited this page Nov 2, 2021 · 1 revision

L14 [Intro to C]

Problem-Oriented Language

A language whose statements resemble terminology of the user application-oriented language rather than machine language

  • Gives symbolic names to values
  • Provide expressiveness
  • Abstract the underlying hardware
  • Enhance code readability
  • Provide safeguards against bugs

Data Types

Unsigned vs signed

  • char
  • short
  • int
  • long
  • long long

Floating Point Types

  • float
  • double
  • long double

Aggregate types

  • array
  • struct
  • union

Use sizeof, a compile-time constant

sizeof(char)

Booleans

C does not have a boolean data type: any integer that is 0 evaluates as false, anything else is true

Strings

Just arrays of characters

char mystr[] = "Hello";
char mystr[] = {'H', 'i'};
char mystr[2] = {'H', 'i'};

There are a number of library functions for dealing with strings, including strlen(), strcpy(), and strdup()

Never use sizeof(s) when you want the string length! That will return the size of the array (size of the pointer), not the actual length of the string. Use strlen(s) instead by #include <string.h> first

The C Preprocessor

File inclusion (#include) and Macro expansion (#define)

Conventionally, we only include files that end in .h

  • If you use double quotes, the preprocessor looks in current directory and then system directory
  • If you use angle brackets, it looks only in system directories

Macro processing is just text substitution using some very specific rules

  • By convention, this is how we do constants and they are often ALL_CAPS

Use gcc -E to show you the pre-processed input file with all the includes and macros expanded

Macros with arguments

#define Product(a, b) ((a) * (b))
PRODUCT(x + 3, y + 4) //Expands to ((x + 3) * (y + 4))

Structs

Structs in C are just aggregate data

struct car {
    char mfg[30];
    char model[30];
    int year;
}

The struct tag declares the type. For example, in the above example, the type is 'struct car', not just 'car'

Names following the struct tag defines instances of the struct

struct car mikes_car, joes_car

You can declare a struct and define an instance at the same time

struct car {
    char mfg[30];
    char model[30];
    int year;
} mikes_car;

Referencing Structure Members

Use the .operator format

printf("%s\n", mikes_car.model);

Remember that you can't assign a string literal to an array! You have to use strcpy() and you must make sure your destination has room to hold the source string

Pointers

int n;              /* an int variable */
int *p;             /* a pointer to an int */

p = &n;             /* p now points to n */

Using * will dereference

int n;              /* an int variable */
int *p;             /* a pointer to an int */

p = &n;             /* p now points to n */

*p = 2;             /* sets n to 2 */
printf("%d\n", *p); /* prints 2 */
printf("%d\n", p);  /* also prints 2 */

*p = *p + *p;       /* sets n to 4 */

The correct way to print the pointer is to use

printf("%p\n", (void *) *p);

The (void *) is required to cast the pointer, though on some machines that don't have different representations for different pointer types, this may not be necessary

There are a few operators (--, ++, and .) that take order over the *

(*p)++;             /* increment the value pointed to by p */
*p++;               /* WARNING: increments p itself */

Yale explains pointers pretty well

Pointers and Structs

C supports the operator ->

  • Left operand is a poitner to struct and right operand is struct member
  • Short hand for * and .
struct myStruct {
    int a;
    int b;
} *p; //p is a pointer to struct myStruct

(*p).a = (*p).b;
p -> a = p -> b;

Remember to declare functions before you use them. Forward declaration means you write the function prototypes before you implement them. Main should just be either at the top or bottom.

Command Line Arguments

int main(int argc, char *argv[]) {
    return 0;
}

argc is just arg count and argv is just an array of strings