C programming notess

Cards (51)

  • C Programming
    Combination of low level (assembly) and high-level programming languages, can be used to write applications and interact with low-level system memory and hardware, can be written on practically any operating system and even works in most handheld devices
  • C
    General-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972
  • C is a very popular language, despite being old
  • Reason for C's popularity
    It is a fundamental language in the field of computer science
  • C is strongly associated with UNIX, as it was developed to write the UNIX operating system
  • Why learn C
    • One of the most popular programming languages in the world
    • If you know C, you will have no problem learning other popular programming languages such as Java, Python, C++, C#, etc, as the syntax is similar
    • C is very fast, compared to other programming languages, like Java and Python
    • C is very versatile; it can be used in both applications and technologies
  • Difference between C and C++
    • C++ was developed as an extension of C, and both languages have almost the same syntax
    • The main difference between C and C++ is that C++ support classes and objects, while C does not
  • C syntax
    1. #include <stdio.h>
    2. int main() {
    3. printf("Hello World!");
    4. return 0;
    5. }
  • #include <stdio.h>
    Header file library that lets us work with input and output functions, such as printf() (used in line 4). Header files add functionality to C programs.
  • C ignores white space
  • main()
    A function. Any code inside its curly brackets {} will be executed.
  • printf()
    A function used to output/print text to the screen
  • return 0 ends the main() function
  • Computer program
    A list of "instructions" to be "executed" by a computer
  • Statements
    Programming instructions in a programming language
  • It is important that you end the statement with a semicolon ;
  • C Output (Print Text)
    1. #include <stdio.h>
    2. int main() {
    3. printf("Hello World!");
    4. return 0;
    5. }
  • Double Quotes
    When you are working with text, it must be wrapped inside double quotations marks ""
  • New Lines
    1. #include <stdio.h>
    2. int main() {
    3. printf("Hello World!\n");
    4. printf("I am learning C.");
    5. return 0;
    6. }
  • Newline character (\n)
    Forces the cursor to change its position to the beginning of the next line on the screen, resulting in a new line
  • Other valid escape sequences
    • \t - Creates a horizontal tab
    • \\ - Inserts a backslash character (\)
    • \" - Inserts a double quote character
  • Comments in C
    Can be used to explain code, and to make it more readable. Can also be used to prevent execution when testing alternative code
  • Single-line Comments
    Start with two forward slashes (//) and any text between // and the end of the line is ignored by the compiler
  • Multi-line Comments
    Start with /* and ends with */. Any text between /* and */ will be ignored by the compiler
  • Variables
    Containers for storing data values, like numbers and characters
  • Types of variables in C
    • int - stores integers (whole numbers), without decimals
    • float - stores floating point numbers, with decimals
  • char
    Stores single characters, such as 'a' or 'B'. Characters are surrounded by single quotes
  • Declaring (Creating) Variables
    type variableName = value;
  • Where type is one of C types (such as int), and variableName is the name of the variable (such as x or myName). The equal sign is used to assign a value to the variable.
  • Output Variables
    To output variables in C, you must get familiar with something called "format specifiers", which you will learn about in the next chapter.
  • Format Specifiers
    Format specifiers are used together with the printf() function to tell the compiler what type of data the variable is storing. It is basically a placeholder for the variable value.
  • Outputting variables
    • int myNum = 15; printf("%d", myNum); // Outputs 15
    • int myNum = 15; float myFloatNum = 5.99; char myLetter = 'D'; printf("%d\n", myNum); printf("%f\n", myFloatNum); printf("%c\n", myLetter);
  • Outputting variables
    • int myNum = 15; char myLetter = 'D'; printf("My number is %d and my letter is %c", myNum, myLetter);
  • Changing Variable Values
    Assign new value to overwrite previous value
  • Changing Variable Values
    • int myNum = 15; myNum = 10; // Now myNum is 10
    • int myNum = 15; int myOtherNum = 23; myNum = myOtherNum; // myNum is now 23, instead of 15
  • Adding Variables Together
    int x = 5; int y = 6; int sum = x + y;
  • Declaring Multiple Variables
    • int x = 5, y = 6, z = 50; printf("%d", x + y + z);
    • int x, y, z; x = y = z = 50; printf("%d", x + y + z);
  • C Variable Names (Identifiers)
    All C variables must be identified with unique names. These unique names are called identifiers.
  • It is recommended to use descriptive names in order to create understandable and maintainable code
  • Rules for naming variables
    • Names can contain letters, digits and underscores
    • Names must begin with a letter or an underscore (_)
    • Names are case-sensitive (myVar and myvar are different variables)
    • Names cannot contain whitespaces or special characters like !, #, %, etc.
    • Reserved words (such as int) cannot be used as names