C Programming Language
Sources: book C programming for absolute beginners
29 flashcards · Programming Languages · by lakesare
What makes main() function unique?
Its return values are returned directly to the operating system.
How should you initialize variables?
/* Declare variables */ int x; char firstInitial; /* Initialize variables */ x = 0; firstInitial = '\0'; It is never safe to assume that the newly assigned variable location is empty. It’s possible that the memory location contains previously used data (or garbage). So always assign values to the new variables.
How to print an integer?
C needs to know how to printf a variable, printf("\nThe value of integer variable x is %d", x); Format specifiers: %d Displays integer value %f Displays floating-point numbers %c Displays character
How to create and display a few charasters?
char a, b, c; a = 'A'; b = 'B'; c = 'C'; printf("My Initials are %c.%c.%c.", a, b, c);
scanf("%d", &iHello);
"%d" - how to interpret inputted variable &iHello - to what variable to assign it, an address operator (&), followed by the name of the variable.
What's the output? int x = 0; int y = 0; printf("%d\n", x++); printf("%d\n", ++y);
=> 0, 1 x++ - first return x, then do whatever (increment) ++x - first increment, then return
How to create a function in C?
int getLargest(int a, int b){} We must either: declare getLargest() before the main(), OR declare a function prototype int getLargest(int, int) before the main().
Name 3 ways to seed an array with values.
Declare an array and loop through every element of array assigning a value to it. int aa[3] = {0}; Will make every value 0. int aa[3] = {1, 2, 3};
How to find the length of an array?
sizeof(aa) BUT if you passed this array as an argument to some function, it decays to a pointer. And sizeof(aa) => size of pointer. You have to pass the size of an array as a second argument then.
How to assign value to a constant?
const float PI = 3.14; Constant data values must be assigned when the constant is first created.
What's a pointer? Variable VS pointer?
Variable is an identifier + value. We use them because we don't know upfront at what memory address value will be at. Pointer is not a variable! In fact, grammatically speaking, there is no such thing as a “pointer variable”: all variables are the same. There are, however, variables with different types, and “pointer variable” really means “variable of a pointer type”. The pointer to foo is the contents of foo_ptr. You could put a different pointer in the foo_ptr box, and the box would still be…
What's this: int* hi;?
It's a pointer to int. So this pointer will point to a memory location containing an integer. (int *hi is also okay, no difference)
int number = 100; How to get an address in memory at which number is stored?
int* addressOfNumber = &number Address of => Ampersand (if we want to find an address - use &) Stored => Star (if we want to find what's stored - use *)
How to get data if you have the memory address it's stored at (a pointer to it)? int* address = 32432432; int number = ?;
int number = *address;
How to create a string in C?
C doesn't have a primitive type 'string'. But strings can be created as an array of characters: char myString[] = "Hello";
What's the memory address of an array? Prove it.
It's equal to the address of array's first element. int aa[] = { 1, 2, 3 }; printf("%p, %p", &aa, &aa[0]); Will output 2 equal hex values.
Why do arrays decay to pointers when passed as an argument to some function?
If we have a large array, it would take long to deep clone it every time we just call a function. Array is always fixed-size. If we want this function to accept variable-sized arrays, we clearly can't use an array type. By using addresses instead of the actual data, we can allow a function to access and modify some location in memory without having to copy any data. http://www.buildyourownlisp.com/chapter9_s_expressions#pointers
What does #define AGE 10 do?
It isn’t a programming statement. This is a pragma which causes a text search-and-replace by preprocessor on compilation. Links: const vs #define
int number = 12; Find pointerToNumber, and then numberStoredInThatPointer.
int number = 12; int* pointerToNumber = &number; int numberStoredInThatPointer = *pointerToNumber;
Give an example of using typedef.
typedef lets you create an alternative name for a data type. typedef int* int_pointer; int_pointer ip; typedef char character; character hi = 'A'; printf("%c\n", hi); This can be a great help in improving code readability, since deeply nested type declarations can be difficult to decipher.
How to dereference a pointer?
Dereferencing means accessing a value stored at location pointer holds. int a = 10; int* pointer = &a; printf("%d", *pointer); // => 10
What does C formatting directive %.2x mean?
Integer should be printed in hexadecimal (x) with at least two digits (.2).
What would sizeof(int) show?
Amount of bytes used to store integers on this system.
int x = 2; int* p_x = &x; Make x == 4 without touching x.
Use dereferencing (access value stored at location pointer holds) *p_x = 4;
C has bitwise and logical operators. Name logical counterparts of these bitwise operators: ~ & |
! && ||
a = 0x69; b = 0x55; printf("%x", a && b); // - ? printf("%x", a & b); // - ?
0x69 && 0x55 = 1 (every integral number is true, except for 0) 0x69 & 0x55 = ...8*3 zeroes... 0110 10012 & ...8*3 zeroes... 0101 01012 = ...8*3 zeroes... 0100 00012 = 0x41
What is the difference between .c and .h extensions in C?
These are useful conventions, typically: .c - C source file .h - header file (to be included with a preprocessor #include directive). Contains stuff that is normally deemed to be shared with other parts of your code, like function prototypes, #define'd stuff, global variables and the like. So you create different C files, each one containing particular functions. That's all nice and clean. Then suddenly you realize that a declaration you have into a given C file should exist also in another C f…
What is the difference between including header files like: #include <hi.h>, or #include "hi.h"
#include <hi.h> - if you want to include one of the standard header files, such as stdio.h. #include "hi.h" - if this is your own .h file. In that case, the quotation marks tell the preprocessor to look for the file in the same directory as the .c file. <> tell the preprocessor to look in the directory where all the standard header files are stored. On a Linux system this is usually /usr/include .
What's usually contained in the header files?
The only files you should include in your .c files are header files that describe an interface type definitions function prototype declarations macro definitions external declarations Header files should not include an implementation. .c files include function implementations, not just declarations. While that may seem innocuous, if you included them, it can be extremely difficult to find if you accidentally included an implementation twice. Including a declaration twice is fine, as long as the…