C Flashcards
Tips
Global variable
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
memory allocation
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)
C functions syntax template
function( ){
return 0;
}
SCANF( )
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.
Command Line Arguement
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
Functions
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.
Process of writing a function
- 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
- 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 = ab;
//return value of our calculations/operation or output of the blackbox; //
return product;
}
While Loop vs If statement
IF = check condition once
while = continuous
Array
A reserved /segmentchunk of memory.
First index: 0
Last Index: n-1
Array Syntax
type name[size]
Array example
int num[10] ; –> make room for 10 integers
bool menu[5]; –> make room for 5 booleans
char name[24]; –> reserve room for 24characters
Array declaration
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)
Array segmentation error
Specifying an index that is out of the range of a declared array
Array Elements declaration
// method 1:
string cities[3];
cities[0] = kaduna;
cities[1] = lagos;
cities[2] = sokoto;
// method 2
string cities[3] = {kaduna, lagos, sokoto};