Computer Science Notes

Notes From CS Undergrad Courses FSU

This project is maintained by awa03

Some C++ Only Features

The c style implementation of libraries look like # include <stdio.h> There are 4 diffrent styles of casting (static, dynamic, const, and reinterpreted casts)

Declerations

int size =100; int list[size]; - Would NOT work int list[100]; - Would work

Unlike c++, C requires variable declarations at the top of the functions This means that, ALL variables should be declared at the beginning of the function

for (int i=0; i< 10; i++){} // wrong

int i; for(i = 0; i < 10; i++){} // correct

Standard I/O streams stdin:: input stream stdout:: output stream stderr:: error stream

Streams are sequences of characters flowing from one place to another

stdio.h - contains basic I/O functions

Formatied I/O - refers to the conversion of data to and from a stream of characters for printing

scanf: - Reads from standard input (stdin) printf: - writes to standard input (stdout)

Output with printf printf (format_string, list_of_expressions);

Specifier Purpose
%d int (signed decimal int)
%u unsigned decimal int
%f floating point (fixed notation)
%e floating point (exponential notation)
%s string
%c char
%x Hexidecimal number

printf(%10d) - for 10 spaces

Using scanf basics

scanf (format_string, list_of_variable_addresses)

int month, day; printf("Please enter birth month, followed by birthday"); scanf(%d %d, &month, &day);

Conversion Specifiers

C Strings char word1[20]; scanf("%s", word1);`

char greeting[] = "Hello"; printf("%s", greeting); // Prints the word Output: "Hello"

C-Strings dont need the address because the name acts as a pointer to the array

Structs and enums

We can define a new type in C using typedef

typedef struct student { char name[20]; double gpa; } Student; // now the new type name struct student s1; // C declaration using "tag" Student s2; // C declaration using new type name

Dynamic Memory Allocation

- C does not come with the `new` and `delete` operators. We need to use `malloc` and `free`. - These functions are in `` - The syntax for an array of ints is: `line * arr = (line*) malloc (size_of_array * sizeof(int));`

For an arrau of the line strict, which has been typedef'ed is: line * arr = (line*) malloc (size_of_array * sizeof(line));

[[Computer Science Notes/CDA 3100/Index|Index]]