You need to remove the trailing ;
from each of your for
loops, e.g. change:
for (a = 1; a < 10; a ++);
to:
for (a = 1; a < 10; a ++)
You also need to move the line printf("the count is %d\n", count);
so that it's outside the loops, i.e. just before the return 0;
, otherwise it will print on every loop iteration - you just want it to print once, at the end.
The proper tools for C include the editor of your choice, and the cli tools that make up your toolchain, such as the compiler.
Your major choices for C compliers on Linux are GCC and CLang.
The book I started with was K&R's ”The C Programming Language". Most people consider it the most concise and easy to digest coverage of the language. It will not cover stuff like GTK, but there's plenty of material online. Check official documentation.
Sure. You can just run the program:
./program > log.txt
which redirects stdout to a file. Or you can replace your calls to printf with fprinf
Since you know about programming, I would recommend you to "The C Programming Language by Brian Kernighan ve Dennis Ritchie". It has a very good exercises in it.
You can search it online first if you want.
If you can, find a copy of "The C Programming Language", it is a very good reference for large parts of the language while still being fairly accessible. Aside from that, I haven't really liked many of the tutorials I've encountered. Your best bet is probably to learn how to make some data structures in C like linked lists and binary trees and whatnot.
Try this. (http://ideone.com/f58vV4)
#include<stdio.h>
int main() { int first, last, sum, num; printf ("Enter 4 digit number"); scanf ("%d",&num);
last=num%10; while(num/10) { first=num/10; num = num/10; }
sum=first+last; printf("sum of first and last digits = %d", sum); return 0; }
https://www.hackerrank.com/ is free and has most of the questions available in C. I have used it successfully during my last 2 job searches. Work through the “Cracking the Coding Interview” questions.
Buffer overflows occur when you write past the end of a malloced block. To know when a buffer overflow occurs, you just need to make sure you're not writing past the end. So, first of all, determine how big buffer is: you've got it malloced as SIZE + 1 chars, where SIZE = 10, right? So at most, you can store 10 characters plus the null terminator.
Let's look at the docs for fgets. See where it says "Parameters"? Specifically:
> n -- This is the maximum number of characters to be read (including the final null-character). Usually, the length of the array passed as str is used.
You are fine for buffer overflows in this case, although you may have issues elsewhere if a line is shorter than you think it is. You should really assert that the length of the string, after reading a line, is 10 (not including the null terminator).
> I actually found out that I was doing loop unrolling without realizing it!
You were not; if you unroll a loop, you will no longer have a loop, but you still have a for loop.
To be clear, I'm not telling you that your too many ifs are bad, I'm telling you they're incorrect. There is an error in your check permutations! What you're actually doing is checking every product is divisible by every number in your list. It's possible for your cnt to be larger than 6!
Just to give a couple more options:
Honestly, and this is my personal opinion so you should do you, but I wouldn't worry about it.
Most of the code I deal with looks correct on the face of it. Speeding through makes it look even more correct because you start making assumptions on what is happening. Defects are where those assumptions are wrong and until you start crawling through the code and thinking about the implications of each line you don't see them. Hell, sometimes even when you do, until you have a defect from test/field saying that that line is wrong you don't see it.
Pretty much the only plugin I consider required for me right now in VIM is Mark because even after 20+ years of school and professional, I still need to slow down and keep notes about what's happening where.
Speed in groking comes less from reading fast and more from keeping all of the interacting pieces in your head.
I for one advocate the approach of learning bits of assembly first and then jumping to C. I taught my students like this for the last 6 years and it worked wonders. Try this book to see how:
https://www.amazon.com/Low-Level-Programming-Assembly-Execution-Architecture/dp/1484224027
A quick googling showed that it's easy to find a PDF online ;)
Use this, aka "K&R C". I'm not sure if I can post a direct link, but if you google for "K&R C" you'll find a PDF so you don't have to spend £30 and find out you hate C.
It's a great and classic book, an completely relevant.
Also, if you want to make your own OS, skip C and try doing it in assembly and on a raspberry PI. You'll learn a lot, and learning assembly will help you out when you start to learn C. My programming education started with making an OS on ARM using assembly, so I can't see why yours can't :P