C Flashcards

Tips

1
Q

Global variable

A

Scope: can be shared by every and any function/block of code within the program.

It should not be put within curley braces.

eg. a constant

care should be taken against another variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

memory allocation

A

int: 4 bytes
float: 4 bytes
char: 1 byte
double: 8 bytes
long: 4 or 8 bytes (depends on the system, 4 bytes on 32-bit systems, and 8 bytes on 64-bit systems)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

C functions syntax template

A

function( ){

return 0;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

SCANF( )

A

scanf is a function in C that is used to read input from the standard input stream (usually the keyboard) and store it into variables. It works with different data types based on the format specifier used in the function call. Here’s a basic overview of how scanf works with different data types:

Integer (%d, %i): If you want to read an integer, you use %d or %i as the format specifier in scanf. For example:
int num;
scanf(“%d”, &num);

Floating-point (%f, %lf): For floating-point numbers, you use %f for float and %lf for double. For example:
float num;
scanf(“%f”, &num);

Character (%c): To read a single character, you use %c. For example:
char ch;
scanf(“%c”, &ch);

String (%s): To read a string (sequence of characters), you use %s. For example:
char str[100];
scanf(“%s”, str);

Other Data Types: There are other format specifiers for different data types (%u for unsigned int, %ld for long, etc.) that you can use with scanf depending on the data you want to read.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Command Line Arguement

A

Command line arguments are values provided to a program when it is executed. In C, you can access these arguments through the argc and argv parameters of the main function.

argc (argument count) is an integer that specifies the number of arguments passed to the program, including the name of the program itself.
argv (argument vector) is an array of strings where each element is one of the arguments passed to the program. argv[0] is the name of the program, and argv[1] through argv[argc-1] are the actual arguments.

Enables C to detect user input from Command Line, which is usable within c.

Argc - Takes in Command Line Arguement count
Argv[] - Takes in Command line Array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Functions

A

A function in programming is a blackbox, a reusable piece of code that performs a specific task. It typically takes inputs (arguments), processes them, and returns a result. Functions help organize code, improve readability, and avoid repetition by allowing you to call the same code multiple times with different inputs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Process of writing a function

A
  1. Declaration: telling the compiler ahead of time. this is typically written before the main function.
    It is cosed with a semi-colon ;

return Type name (Arguemnts-list);

eg,
int add_num (int x, int y);

float mult_decimals(float a, float b);

double mult_decimals(float a, float b);

***note that double can store floating point numbers

  1. Function definition: the process of writing the code into the blackbox. No semi-colon.

//a function that multiply two floating point numbers//
**float mult_decimals(float a, float b)
{
//the operation//
product = a
b;

//return value of our calculations/operation or output of the blackbox; //
return product;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

While Loop vs If statement

A

IF = check condition once

while = continuous

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Array

A

A reserved /segmentchunk of memory.
First index: 0
Last Index: n-1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Array Syntax

A

type name[size]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Array example

A

int num[10] ; –> make room for 10 integers
bool menu[5]; –> make room for 5 booleans
char name[24]; –> reserve room for 24characters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Array declaration

A

declare the array as above:
syntax type name[size];***

1- char names[12];
2- names[2];

// line 1 is the declaration
// line 2 is the index specification (the third element)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Array segmentation error

A

Specifying an index that is out of the range of a declared array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Array Elements declaration

A

// method 1:
string cities[3];
cities[0] = kaduna;
cities[1] = lagos;
cities[2] = sokoto;

// method 2
string cities[3] = {kaduna, lagos, sokoto};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Array Tip

A

Always consider using a Loop to iterate the elements in an array

17
Q

unsized array/ unbounded array

A

In C, you can declare an array without specifying the index size if you initialize it with values. This is known as an “unsized array” or “unbounded array.” The compiler will automatically determine the size based on the number of elements you provide. However, you cannot leave the array declaration completely empty without initializing it.

int numbers[] = {1, 2, 3, 4, 5};
// Compiler will determine the size (5) based on the number of elements

18
Q

Array notes

A

Array grid
// eg for a 10x10 grid array
int numbers[10] [10];
in memory this is simply 100 elements.

//Array as variables
Each individual element in an array can be treated as a variable

//re-assigning arrays
this is directly impossibles unlike in variables.

eg.
int numbers[] = {1, 2, 3, 4, 5};
int age = int numbers //this is not possible

//inC, the only way is to copy the content of numbers into age using a loop

int numbers[] = {1, 2, 3, 4, 5};
for(i = 0; i<5;i++)
{
age = numbers[i];
}

19
Q

Command Line inputs

A

This is used to get user inputs before runtime to minimize start and stop.

//Syntax
int main(argc, argv[])

Argc - Argument count. this counts the number of arguments on the command line, starting with the program name as the first element 0, then proceeds to count the other sets of elements found on the command line.

Argv[] - stores the actual text into the index captured by th argc.
the first element of argv is always found at index[0]
while the last element of argv is found at index Argv[Argc - 1]
//everything stored in argv is in form of a string

20
Q

Array size change

A

once deeclared, an Array size cannot be changed

21
Q
A

If you would not use a number or set of numbers for any math,atical operation, store as array

22
Q

add a DOT (.) to a file to make it hidden

A
23
Q

Compare strings vs Int

A

strcmp function

this is used to compare the phone number strings since you cannot directly compare strings using the == operator in C.
#include<string.h></string.h>

24
Q

strcmp

A

compares the ASCII values of the characthers provided.

while == operator returns a boolean value.

25
Q
A

typedef struct
{
name;
phone_number;
}

NameYourDataType;

26
Q
A