Consider the following C code. What will it print when run?
#include <stdio.h> int main() { printf("Hello, World!\n"); return 0; }
Look carefully at the exact string inside printf including capitalization and punctuation.
The program prints exactly "Hello, World!" followed by a newline. The printf function outputs the string as is.
What value does x hold after the program finishes?
#include <stdio.h> int main() { int x = 5; x = x + 10; return 0; }
Remember that x = x + 10; adds 10 to the current value of x.
The variable x starts at 5, then 10 is added, so x becomes 15.
What will this program print if run with the command ./program 3 7?
#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { if (argc < 3) { printf("Not enough arguments\n"); return 1; } int a = atoi(argv[1]); int b = atoi(argv[2]); printf("Sum: %d\n", a + b); return 0; }
Check how argc and argv are used to get command line arguments and convert them to integers.
The program converts the first two arguments "3" and "7" to integers and prints their sum, which is 10.
What error will the compiler show for this program?
#include <stdio.h> void main() { printf("Hello\n"); }
Check the return type of the main function in standard C.
The standard C requires main to return int. Using void main() causes a compiler warning.
In a typical C program, how many times is the main function called during the program's lifetime?
Think about the role of main as the program's entry point.
The main function is called exactly once by the operating system when the program starts. It is not called again unless explicitly invoked, which is uncommon and not standard practice.